Page 1 of 1

Simple controller

Posted: Thu Sep 29, 2016 11:10 pm
by AaronKelly
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?

Re: Simple controller

Posted: Sat Oct 01, 2016 7:52 pm
by timotet
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:

Re: Simple controller

Posted: Sat Oct 01, 2016 11:56 pm
by salesioPark
The line
print "Client connected"
will cause an error on python3.
print("Client connected")