Simple controller

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
User avatar
AaronKelly
Posts: 7
Joined: Sun Jun 19, 2016 7:44 am

Simple controller

Post by AaronKelly » Thu Sep 29, 2016 11:10 pm

Hello, I'm trying to create a simple repeater server with sockets as a starting point to create a wifi controller. I want to sent an arbitrary string to the ESP8266 and have it parse and do things. I am just looking to connect locally through a phone/laptop wifi and send values directly to it. I've been looking at documentation and other peoples code to start out by connecting to the device, the closest I've got is the following;

Code: Select all

import usocket as socket

def server_init():
    s = socket.socket()
    s.bind( ('127.0.0.1', 83) )
    s.listen(1)
    c, a = s.accept()
    print "Client connected"
    return c
 
c = server_init()
please use code blocks to paste your code :) - platforma

However, it just hangs and it actively refuses a connection when trying to connect to the device.

Has anyone tried this before?

User avatar
timotet
Posts: 6
Joined: Sun Dec 14, 2014 12:01 am
Location: Bend, Or.
Contact:

Re: Simple controller

Post by timotet » Sat Oct 01, 2016 7:52 pm

I'm no expert by any means but Ive been trying this too.
Whenever I tried to bind with the host being anything but "0.0.0.0" it just would not work.
And I think it's good practice to use a higher port number like 41152.

Here is what I've been using :
[code]
port = 41152
host = '0.0.0.0'

def serverInit():

global port
global host
try:
s = socket.socket()
print("socket created")
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print("options set")
s.bind((host, port))
print("bound")
s.listen(1)
print("listening")
c, a = s.accept()
#print(c)
#print(a)
if c:
print("Client connected")
return c

except OSError as er:
print(er)
[/code]

I also noticed if I have the REPL open and do a ctrl D to reboot the board I get a [Errno 12] ENOMEM message.
If the REPL is not active it works as expected.

Hope this helps
Tim

Sorry I haven't posted enough for code blocks to work :oops:

salesioPark
Posts: 3
Joined: Fri Sep 23, 2016 8:51 am

Re: Simple controller

Post by salesioPark » Sat Oct 01, 2016 11:56 pm

The line
print "Client connected"
will cause an error on python3.
print("Client connected")

Post Reply