Problem using RPC Wifi.Scan

Hi all,

  1. My goal is: To be able to scan for wifi networks using the RPC Wifi.Scan on ESP32.
  2. My actions are: In the mos.yml config section I enable wifi.sta, and enter a dummy wifi.sta.ssid and wifi.sta.pass field. I include the mongoose-os-libs/rpc-service-wifi library in the libs section. I’m building with the latest mos version (2.12.1).
  3. The result I see is: If the SSID cannot be joined or is not present the Wifi.Scan RPC fails with the following message:
    Error: context deadline exceeded
    /Users/mos/tmp/mos_gopath/src/cesanta.com/common/go/mgrpc/mgrpc.go:442:
    /Users/mos/tmp/mos_gopath/src/cesanta.com/mos/dev/dev_conn_impl.go:152:
    /Users/mos/tmp/mos_gopath/src/cesanta.com/mos/dev/dev_conn_impl.go:165:
    /Users/mos/tmp/mos_gopath/src/cesanta.com/mos/main.go:178: call failed
  4. My expectation & question is: I would expect the Wifi.Scan call to operate also when the ESP32 is failing to connect to a network, since this is one of the important scenarios where it would come in useful. Is this a bug, a technical limitation, or am I missing something?
1 Like

Hi bitfab. You’re right:

...
[Apr 10 22:47:53.212] mgos_net.c:85           WiFi STA: connecting
[Apr 10 22:47:55.599] esp32_wifi.c:60         Disconnected from foobar, reason: 201
[Apr 10 22:47:55.603] mgos_net.c:81           WiFi STA: disconnected
{"jsonrpc": "2.0", "method": "Wifi.Scan", "params": {}, "id": 1}
[Apr 10 22:48:21.811]  mg_rpc.c:293            Wifi.Scan via UART UART0
[Apr 10 22:48:21.819] W (29364) wifi: Now is connecting, user scan invalid now!
[Apr 10 22:48:23.191] mgos_wifi.c:346         WiFi STA: Connect timeout
[Apr 10 22:48:23.195] mgos_wifi.c:88          WiFi STA: Using config 0 (foobar)
...

Message wifi: Now is connecting, user scan invalid now! comes from underlying ESP-IDF. Maybe try to google it :slight_smile:.

Still JSON-RPC call could in that case respond with JSON-PRC error message and not timeout. Feel free to report it.

Hi Martin,
Thanks for the lead. I looked into it, and confirmed that this does come from ESP-IDF (https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/wifi.html#scan-when-wi-fi-is-connecting).

The good news is that tripflex (https://github.com/tripflex) has implemented a fix for this that calls esp_wifi_disconnect before performing the scan. I’ve built using his repo (https://github.com/tripflex/wifi) and it solves the problem for me. A pull request has been issued for this: https://github.com/mongoose-os-libs/wifi/pull/24. Let’s hope it gets merged soon.

Cheers!

1 Like