简体   繁体   中英

Socket error ECONNABORTED connecting two MicroPython boards

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.")

The stupid answer to this question is that I eventualy switched the programs between the boards, so both of them was running with an 'almost' identical (ESSID and password) Access Point. Although the client was correctly connected to the server AP, I guess that some IP conflict was avoiding the socket to connect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM