guild icon
Toit
#Can not use wifi STA when using wifi AP mode
Thread channel in help
Rikke
Rikke 06/13/2023 09:27 AM
Hello, I'm trying to scan the network on wifi STA mode, while wifi AP mode is up and running. But in the toit code, AP and STA modes cannot run at the same time.
What is the reason for this?

Trying to scan while network is established on AP:
****************************************************************************** Decoding by `jag`, device has version <2.0.0-alpha.69> ****************************************************************************** EXCEPTION error. wifi already connected or established 0: WifiServiceProvider.scan /home/runner/work/toit/toit/system/extensions/esp32/wifi.toit:145:7 1: WifiServiceProvider.handle /home/runner/work/toit/toit/system/extensions/esp32/wifi.toit:63:14 2: ServiceManager_.<lambda> <sdk>/system/services.toit:640:15 3: RpcRequest_.process.<block> <sdk>/rpc/broker.toit:98:26 4: RpcRequest_.process <sdk>/rpc/broker.toit:95:3 5: RpcRequestQueue_.ensure_processing_task_.<lambda>.<block>.<block> <sdk>/rpc/broker.toit:214:20 6: RpcRequestQueue_.ensure_processing_task_.<lambda>.<block> <sdk>/rpc/broker.toit:209:9 7: RpcRequestQueue_.ensure_processing_task_.<lambda> <sdk>/rpc/broker.toit:204:85 ******************************************************************************
Line 143-153 in system/extensions/esp32/wifi.toit
scan config/Map -> List: if state_.module: throw "wifi already connected or established" module := WifiModule.sta this "" "" try: channels := config.get wifi.CONFIG_SCAN_CHANNELS passive := config.get wifi.CONFIG_SCAN_PASSIVE period := config.get wifi.CONFIG_SCAN_PERIOD return module.scan channels passive period finally: module.disconnect
Rikke
Rikke 06/13/2023 09:45 AM
Does this AP+STA mode complicate things for toit, or is it because no one had the need for this? I just want to know if I should throw some hours into this
floitsch
floitsch 06/13/2023 10:17 AM
To be honest, I didn't even think it was possible.
floitsch
floitsch 06/13/2023 10:23 AM
Just searched a bit.
AP+STA clearly should be possible.
floitsch
floitsch 06/13/2023 10:24 AM
From my few minutes of searching it might be possible to just change the esp_wifi_set_mode calls in wifi_esp32.cc.
Rikke
Rikke 06/13/2023 10:24 AM
I know tasmota uses both AP and STA in their captive portal, so it should be possible
floitsch
floitsch 06/13/2023 10:25 AM
I agree. I also found examples.
floitsch
floitsch 06/13/2023 10:25 AM
The big question is how much change is needed to Toit to support it.
Rikke
Rikke 06/13/2023 10:26 AM
Yes thats why I asked, I dont know how toit would work with it
floitsch
floitsch 06/13/2023 10:26 AM
My first attempt would be to just change all esp_wifi_set_mode(WIFI_MODE_XXX) to esp_wifi_set_mode(WIFI_MODE_APSTA) in wifi_esp32.cc.
Rikke
Rikke 06/13/2023 10:26 AM
Let me see if I can find tasmota's way of starting AP and STA seperetely
floitsch
floitsch 06/13/2023 10:26 AM
If we are lucky, you could then just use the wifi in both modes.
floitsch
floitsch 06/13/2023 10:27 AM
We might have some checks a bit earlier that make sure you don't try to use both at the same time.
In that case we would need to remove/adjust them.
Rikke
Rikke 06/13/2023 10:27 AM
Yes theres checks in wifi.connect making sure only 1 connection is active etc
floitsch
floitsch 06/13/2023 10:27 AM
If that works, we should keep track of which mode is currently in use, and adjust the set_mode accordingly. (you don't want to enable AP, if it's not used).
RikkeOPRikke
Yes theres checks in wifi.connect making sure only 1 connection is active etc
floitsch
floitsch 06/13/2023 10:28 AM
can you point me to it? (which file)
Rikke
Rikke 06/13/2023 10:28 AM
I can easily test it, I have setup for using AP and STA
Rikke
Rikke 06/13/2023 10:29 AM
system/extentions/esp32/wifi.toit line 90
floitsch
floitsch 06/13/2023 10:29 AM
thanks.
Rikke
Rikke 06/13/2023 10:29 AM
Same with establish and wifi scan really
floitsch
floitsch 06/13/2023 10:29 AM
For testing I would just remove the check.
Rikke
Rikke 06/13/2023 10:30 AM
Yes
floitsch
floitsch 06/13/2023 10:30 AM
that said. It tries to create a new network resource. Not sure if that's allowed.
Rikke
Rikke 06/13/2023 10:31 AM
Yeah looks like some changes has to be made. Having control over multiple networks might not be easy
floitsch
floitsch 06/13/2023 10:32 AM
Looks all relatively straightforward but yes: not just 2 lines that need to be changed.
Rikke
Rikke 06/13/2023 10:32 AM
Looks like i can also look into this :🙂: seems possible
floitsch
floitsch 06/13/2023 10:33 AM
@bitphlipphar has some TODOs in there.
He might have some insights on how best to proceed.
floitsch
floitsch 06/13/2023 10:39 AM
Continued looking a bit.
floitsch
floitsch 06/13/2023 10:40 AM
And currently stuck at esp_netif_new in the init function (wifi_esp32.cc).
It initializes it with ESP_NETIF_DEFAULT_WIFI_AP or ESP_NETIF_DEFAULT_WIFI_STA.
But there isn't any ESP_NETIF_DEFAULT_WIFI_APSTA. So trying to see what should be done there...
floitsch
floitsch 06/13/2023 10:52 AM
There might be an easier way:
potentially you could have multiple wifi interfaces.
I have no idea how much we rely on it (so this might be extremely stupid), but you could add another entry to the resource pool (line 50 of wifi_esp32.cc) and allocate the two
Wifi networks separately.
Clearly that also requires to change the esp_wifi_set_mode , and potentially, calling set_mode clearl the existing configuration (in which case things get a bit more complicated again).
Rikke
Rikke 06/13/2023 11:46 AM
Hmm I will explore a bit now, and see if I can make it work
Rikke
Rikke 06/13/2023 12:52 PM
/** * @brief Start WiFi according to current configuration * If mode is WIFI_MODE_STA, it create station control block and start station * If mode is WIFI_MODE_AP, it create soft-AP control block and start soft-AP * If mode is WIFI_MODE_APSTA, it create soft-AP and station control block and start soft-AP and station * * @return * - ESP_OK: succeed * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init * - ESP_ERR_INVALID_ARG: invalid argument * - ESP_ERR_NO_MEM: out of memory * - ESP_ERR_WIFI_CONN: WiFi internal error, station or soft-AP control block wrong * - ESP_FAIL: other WiFi internal errors */ esp_err_t esp_wifi_start(void);
third_party/esp-idf/components/esp_wifi/include/esp_wifi.h line 294-308
It is definitely possible, but it does require some testing
floitsch
floitsch 06/13/2023 12:55 PM
From what I could see the problem is that we mix wifi and netif setup.
floitsch
floitsch 06/13/2023 12:55 PM
That is, we want two netif objects (one for sta and one for ap), but the wifi setup only exists once.
Rikke
Rikke 06/13/2023 12:55 PM
Yes netif did not have APSTA mode :😦:
floitsch
floitsch 06/13/2023 12:55 PM
I think you should have two netif objects. (Which is actually much nicer anyway)
Rikke
Rikke 06/13/2023 12:56 PM
Also I noticed how some esp projects restart after having STA and AP open, to clear the esp after captive portal setup, and only use STA afterwards
floitsch
floitsch 06/13/2023 12:56 PM
Yes. You don't want to run either side if you don't need it.
floitsch
floitsch 06/13/2023 12:56 PM
(power...)
Rikke
Rikke 06/13/2023 12:57 PM
Yes but maybe restarting is easier than closing one of them :😄:
floitsch
floitsch 06/13/2023 12:57 PM
probably :🙂:
Rikke
Rikke 06/13/2023 12:59 PM
I will make a quick workaround with my current project, this looks a bit more difficult than what i wouldve imagined
floitsch
floitsch 06/13/2023 12:59 PM
Not sure what the easiest way forward is. (Mainly because there are many different strategies all with their advantages/disadvantages).
floitsch
floitsch 06/13/2023 12:59 PM
sounds good.
44 messages in total