I'm using MicroPython with two NodeMCU ESP8266 dev boards. My goal is to connect one to the other, so they can interchange information. One of the boards is running a server program and its AP is up. The other connects to the other board's AP and try to connect.
The server is running fine, and I can connect to it with Kitty using a RAW connection (connecting my PC to the ESP8266 AP). The client, instead, fails in socket.connect() and throws a ECONNABORTED exception. I've used differents settings, but none of them seems to work. How can I connect my two boards? I'm a newbie with sockets, so this may be not a MicroPython specific problem but a Python one.
EDIT: There's no problem when connecting from a PC using the same code. The problem seems to be exclusive of a client ESP8266 connecting to a server ESP8266 through the server Access Point. Maybe a MicroPython bug?
Server code:
import network
import socket
def runServer():
try:
ap_if = network.WLAN(network.AP_IF)
ap_if.active(True)
ap_if.config(essid='MicroPy-AP', password='micropythoN')
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind( ('', 8266) )
s.listen(1)
print("Waiting for a client...")
client, client_ip = s.accept()
print("Connected!")
finally:
print("Closing socket...", end=' ')
s.close()
print("Done.")
Client code:
import network
import socket
def runClient():
try:
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('MicroPy-AP', 'micropythoN')
while not sta_if.isconnected():
pass
sta_if.ifconfig()
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print("Connecting...")
s.connect( ('192.168.4.1', 8266) )
finally:
print("Closing socket...", end=' ')
s.close()
print("Done.")