micropython / micropython-esp32

Old port of MicroPython to the ESP32 -- new port is at https://github.com/micropython/micropython
MIT License
673 stars 216 forks source link

OS Error 118 on sock.connect(addr) #212

Closed PikWay closed 6 years ago

PikWay commented 6 years ago

I'm trying to do simple socket conn and on sock.connect(addr) I'm getting OS Error 118. I really don't know what this means because it's absent in uerrno.

ESP is running on firmware: esp32-20171029-v1.9.2-283-gdba3796e

PikWay commented 6 years ago

I've just found that wlan.isconnected() is always True so it's not usocket issue. I was using my ESP8266 wifi script which seems to not working on ESP32.

MrSurly commented 6 years ago

Can you post the full code? Or at least enough to reproduce the issue?

nickzoic commented 6 years ago

I suspect this is related to https://github.com/micropython/micropython-esp32/issues/166#issuecomment-328731393 with the inconsistent errno definitions ...

PikWay commented 6 years ago

import network wifi_sta = network.WLAN(network.STA_IF) wifi_sta.isconnected() False wifi_sta.active(True) I (157116) wifi: mode : sta () I (157116) wifi: STA_START True wifi_sta.isconnected() False wifi_sta.ifconfig(('192.168.0.105','255.255.255.0','192.168.0.50','8.8.8.8')) wifi_sta.isconnected() True

I'm not connected yet. In ESP8266 in this stage it's showing False so i was entering following if: if not wifi_sta.isconnected()

And making the connection. This OS Error 118 is effect of above - cannot connect to socket while not connected to network.

Anyway I've moved wifi_sta.ifconfig() into "if" and it works.

PikWay commented 6 years ago

and following code will throw OS Error 118:

import usocket as socket I (1112246) modsocket: Initializing addr=socket.getaddrinfo('192.168.0.66',31883)[0][-1] sock=socket.socket() sock.connect(addr) Traceback (most recent call last): File "", line 1, in OSError: 118

dpgeorge commented 6 years ago

@PikWay your report here is not 100% clear on what your issue is. The above code with the "OSError: 118" is simply due to the network/wifi not being connected.

This OS Error 118 is effect of above - cannot connect to socket while not connected to network.

Yes, you must be connected to wifi for the socket to connect to a remote machine. What else do you expect it to do?

PikWay commented 6 years ago

@dpgeorge Ok. Will try to make it clearer. Agree that if trying to bind to socket on disconnected network should throw error.

Problem is with determining is network connected when using static IP. In esp_isconnected there's just check: return mp_obj_new_bool(info.ip.addr != 0);

So if network.WLAN.ifconfig was invoked with addressing tuple - network.WLAN.isconnected will return True before connecting to WiFi. Is there any other way of determining connection status when using static IP ?

robert-hh commented 6 years ago

The problem is, that in contrast to the ESP8266 port there is no dedicated API telling whether the device is connected. So other indications must be used, like esp_wifi_sta_get_ap_info(). That seems to work, but it is unclear to me whether this can fail even if a device is connected. Anyway, the function could look like:

STATIC mp_obj_t esp_isconnected(mp_obj_t self_in) {
    wlan_if_obj_t *self = MP_OBJ_TO_PTR(self_in);
    if (self->if_id == WIFI_IF_STA) {
        wifi_ap_record_t ap_info;
        ap_info.primary = 0;
        return mp_obj_new_bool(esp_wifi_sta_get_ap_info(&ap_info) == ESP_OK && ap_info.primary != 0);
    } else {
        wifi_sta_list_t sta;
        esp_wifi_ap_get_sta_list(&sta);
        return mp_obj_new_bool(sta.num != 0);
    }
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_isconnected_obj, esp_isconnected);
robert-hh commented 6 years ago

The option above is only a little bit better. It will not flag "connected" after setting the IP adress. But if after connecting the connection is lost, it will still indicate an exiting connection.

PikWay commented 6 years ago

Why not to use this variable: static bool wifi_sta_connected = false;? Looks like it's set properly in both - esp_connect and esp_disconnect functions.

Also I can see some events in logs, set to different values during wifi connection lifecycle. I'll try to change this myself.

robert-hh commented 6 years ago

That's more simple but fails too, because it will tell you immediately that a connection was made, before it is established, even if it gets never done. One advantage: after a wlan.disconnect() it flags disconnecet.

robert-hh commented 6 years ago

OK. Found a solution which seems solid. Adding a separate status variable, which is set True on a connect event and False on a disconnect event. I stumbled over another glitch, in that disconnect() would do that, but it triggers a disconnect event, which immediately tries to re-establish the connection.

Edit: made a PR

PikWay commented 6 years ago

Robert solution works. Issue to close.

robert-hh commented 6 years ago

You can close it yourself. Just push the 'close' button.