Socket connection in AP mode not working

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
manuel2258
Posts: 2
Joined: Mon Jul 29, 2019 2:21 pm

Socket connection in AP mode not working

Post by manuel2258 » Mon Jul 29, 2019 2:42 pm

Hey guys,
I'm currently facing a pretty odd network error. Firstly I'm using a Wemos D1 mini with the newest firmware installed. I'm trying to setup a simple network configuration mode over a access point, therefor I currently also I develop an app to communicate with the microcontroller.
The entire code for the microcontroller is written and works perfect when the controller is connected to my wifi. However when trying the same using an access point the controller seams to never receive any communication.

So to make sure that my understanding of networking is right. Beeing in the same network as the controller which is hosted by the controller (access point) and the socket is bound to the address that the socket gives me desolving the '0.0.0.0' address, I should be able to simply make request targeting the address of the controller (192.168.4.1) and then the socket should receive them, right?

That also what my code does and it basicly the same as other tutorials that I have found.
So here are some code bits. (I'm new here and don't know whether I can use code formation, sorry for that)
I set up my AP using this code:

Code: Select all

    sta_if = network.WLAN(network.STA_IF)
    sta_if.active(False)
    ap_if = network.WLAN(network.AP_IF)
    ap_if.active(True)
    mac = ubinascii.hexlify(network.WLAN().config('mac'), ':').decode()
    ap_if.config(essid="epsLightServer_{}".format(mac[:5]))
    ap_if.config(authmode=network.AUTH_OPEN)
Which works fine. I see the network from my phone and can connect to it.

I set up my socket using this code:

Code: Select all

        address_info = socket.getaddrinfo('0.0.0.0', 80)
        print("Hostinfo: {}".format(address_info))
        address = address_info[0][-1]
        self._socket = socket.socket()
        self._socket.bind(address)
        self._socket.listen(1)
        self._socket.setblocking(False)
        self._socket.settimeout(5)
And then poll for a incoming connection like that:

Code: Select all

        try:
            client, address = self._socket.accept()
            print("New connection from: {}".format(address))
            data = self.receive_all_data(client)
        except OSError:
            pass
However I can't register any incoming connection. Said like before that same code works using the STA network mode.

I don't know where to go from here. I can't find anyone online with the same problem. I also have 2 boards which both produce the same error. The even run on different firmware versions, still the same problem.

Does anyone know what I'm doing wrong? Or is it a bug thats bound to the wemos d1 mini?

Thanks for help in advance!
Manuel S.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Socket connection in AP mode not working

Post by jimmo » Tue Jul 30, 2019 12:43 am

Hi,

I was able to see the same thing connecting from my Android phone (Chrome), but interestingly no problem if I joined the ESP8266 AP from my laptop then used `nc` to connect. i.e. using your exact code, binding to 0.0.0.0, accept a socket, write to it.

Same problem with `nc` on my phone (yay Termux), but then ping in Termux said "packet filtered" from a 10/8 address... wut. My guess is that Android is preferentially using the cellular data rather than WiFi because it knows it has no internet access on the WiFi.

I tried putting the phone into aeroplane mode and only enabling WiFi, and now it works from Chrome.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Socket connection in AP mode not working

Post by jimmo » Tue Jul 30, 2019 12:48 am

I looked briefly and don't see an Android option to force it to use the WiFi...although if you're writing your own client then perhaps you can force it to bind to a specific interface?

manuel2258
Posts: 2
Joined: Mon Jul 29, 2019 2:21 pm

Re: Socket connection in AP mode not working

Post by manuel2258 » Tue Jul 30, 2019 10:10 am

Oh wow yeah, that was the problem ... I feel so stupid right now ^^ Simply disabeling the mobile connection forces it to use the non internet AP connection and the microcontroller receives the requests!
It works now, thanks a lot!
Should I somehow mark this thread as solved or something? (I'm new here ...)

Post Reply