Problem using Sockets
Posted: Sat Nov 05, 2016 5:07 pm
Hi,
I'm new to MicroPython and a bit new to Python too. I'm trying to send data using sockets to an ESP8266. I want the ESP8266 to be the server and my pc to be the client but I get an error when trying to receive data and I can't find any helpfull documentation to help me in this case.
This is my code:
[code]
import machine
import network
import socket
p = machine.Pin(2, machine.Pin.OUT)
p.low()
print('connecting to network...')
sta_if = network.WLAN(network.STA_IF)
ap_if = network.WLAN(network.AP_IF)
if (ap_if.active()):
ap_if.active(False)
sta_if.active(True)
sta_if.connect('MY_AP','PASSWORD')
while not sta_if.isconnected():
pass
print('network config:', sta_if.ifconfig())
ips = sta_if.ifconfig()
def server_init():
s = socket.socket()
s.bind( ('', 3266) )
s.listen(1)
c, a = s.accept()
time.sleep(5)
while True:
data= s.recv()
print(dato)
server_init()
[/code]
It connects to the ap perfectly and when I try to connect using nc 192.168.1.133 3266, it accepts and then it fails. in the data = s.recv() line.
I try to reproduce the error using repl and when I get to the s.recv() line I get:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: 0
It just fails.
Can anyone give me any hints?
Thank you.
I'm new to MicroPython and a bit new to Python too. I'm trying to send data using sockets to an ESP8266. I want the ESP8266 to be the server and my pc to be the client but I get an error when trying to receive data and I can't find any helpfull documentation to help me in this case.
This is my code:
[code]
import machine
import network
import socket
p = machine.Pin(2, machine.Pin.OUT)
p.low()
print('connecting to network...')
sta_if = network.WLAN(network.STA_IF)
ap_if = network.WLAN(network.AP_IF)
if (ap_if.active()):
ap_if.active(False)
sta_if.active(True)
sta_if.connect('MY_AP','PASSWORD')
while not sta_if.isconnected():
pass
print('network config:', sta_if.ifconfig())
ips = sta_if.ifconfig()
def server_init():
s = socket.socket()
s.bind( ('', 3266) )
s.listen(1)
c, a = s.accept()
time.sleep(5)
while True:
data= s.recv()
print(dato)
server_init()
[/code]
It connects to the ap perfectly and when I try to connect using nc 192.168.1.133 3266, it accepts and then it fails. in the data = s.recv() line.
I try to reproduce the error using repl and when I get to the s.recv() line I get:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: 0
It just fails.
Can anyone give me any hints?
Thank you.