Rikke
06/13/2023, 9:27 AM******************************************************************************
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
06/13/2023, 9:45 AMfloitsch
06/13/2023, 10:17 AMfloitsch
06/13/2023, 10:23 AMfloitsch
06/13/2023, 10:24 AMesp_wifi_set_mode
calls in wifi_esp32.cc
.Rikke
06/13/2023, 10:24 AMfloitsch
06/13/2023, 10:25 AMfloitsch
06/13/2023, 10:25 AMRikke
06/13/2023, 10:26 AMfloitsch
06/13/2023, 10:26 AMesp_wifi_set_mode(WIFI_MODE_XXX)
to esp_wifi_set_mode(WIFI_MODE_APSTA)
in wifi_esp32.cc.Rikke
06/13/2023, 10:26 AMfloitsch
06/13/2023, 10:26 AMfloitsch
06/13/2023, 10:27 AMRikke
06/13/2023, 10:27 AMfloitsch
06/13/2023, 10:27 AMset_mode
accordingly. (you don't want to enable AP, if it's not used).floitsch
06/13/2023, 10:28 AMRikke
06/13/2023, 10:28 AMRikke
06/13/2023, 10:29 AMfloitsch
06/13/2023, 10:29 AMRikke
06/13/2023, 10:29 AMfloitsch
06/13/2023, 10:29 AMRikke
06/13/2023, 10:30 AMfloitsch
06/13/2023, 10:30 AMRikke
06/13/2023, 10:31 AMfloitsch
06/13/2023, 10:32 AMRikke
06/13/2023, 10:32 AMfloitsch
06/13/2023, 10:33 AMfloitsch
06/13/2023, 10:39 AMfloitsch
06/13/2023, 10:40 AMesp_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
06/13/2023, 10:52 AMesp_wifi_set_mode
, and potentially, calling set_mode
clearl the existing configuration (in which case things get a bit more complicated again).Rikke
06/13/2023, 11:46 AMRikke
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 testingfloitsch
06/13/2023, 12:55 PMfloitsch
06/13/2023, 12:55 PMRikke
06/13/2023, 12:55 PMfloitsch
06/13/2023, 12:55 PMRikke
06/13/2023, 12:56 PMfloitsch
06/13/2023, 12:56 PMfloitsch
06/13/2023, 12:56 PMRikke
06/13/2023, 12:57 PMfloitsch
06/13/2023, 12:57 PMRikke
06/13/2023, 12:59 PMfloitsch
06/13/2023, 12:59 PMfloitsch
06/13/2023, 12:59 PMexklibur
06/15/2025, 2:29 PMfloitsch
06/15/2025, 2:37 PMexklibur
06/15/2025, 7:12 PMrun:
while true:
// Communicate with module network
log.info "establishing wifi in AP mode ($CAPTIVE_PORTAL_SSID)"
server-exception := catch:
run-server
network.close
if server-exception:
log.info "Server: $server-exception"
sleep --ms=1000
// Connect to LAN
log.info "Attempting to connect to external WiFi"
client-exception := catch:
run-client
network.close
if client-exception:
log.info "Client: $client-exception"
sleep --ms=1000
run-server:
try:
wifi-exception := catch:
network = wifi.establish
--ssid=CAPTIVE-PORTAL-SSID
--password=CAPTIVE-PORTAL-PASSWORD
log.info "wifi established"
if wifi-exception:
log.info "failed to establish AP"
log.error wifi-exception
exception := catch:
with-timeout (Duration --m=1):
run-http
if exception:
log.info "Breaking"
log.info exception
if exception == "Interrupt":
throw exception
log.info "wifi closing"
finally:
network.close
run-client:
try:
exception := catch:
network = wifi.open --ssid=EXTERNAL-WIFI-SSID --password=EXTERNAL-WIFI-PASSWORD
log.info "Connected to external WiFi"
client := mqtt.Client --host=MQTT-HOST
options := mqtt.SessionOptions
--client-id=CLIENT-ID
--username=MQTT-USERNAME
--password=MQTT-PASSWORD
--clean-session=true
client.start --options=options
payload := json.encode {
"module": CLIENT-ID,
"modules": modules.values
}
client.publish "mtsc" payload
client.close
log.info "MQTT message sent"
if exception:
log.error "Client: $exception"
finally:
network.close
floitsch
06/15/2025, 7:14 PMfloitsch
06/15/2025, 7:16 PMexklibur
06/15/2025, 7:38 PMexklibur
06/15/2025, 7:39 PMexklibur
06/15/2025, 7:39 PMfloitsch
06/15/2025, 7:42 PMfloitsch
06/15/2025, 7:42 PMlisten
calls the lambda on a different task.floitsch
06/15/2025, 7:43 PMfloitsch
06/15/2025, 7:50 PMkebab-case
. You can use jag toit tool kebabify code ...
to update your code.
- toitdoc doesn't use leading *
. You can use jag toit doc
to see how your comments are currently interpreted.
- we prefer to finish comments with a "."
- toitdocs (/**
with two stars) are only really used on methods, classes, ... but not inside functionsfloitsch
06/15/2025, 7:51 PMexklibur
06/15/2025, 8:12 PMfloitsch
06/15/2025, 8:13 PMfloitsch
06/15/2025, 8:14 PMjag decode
only works with the corresponding snapshot. Can you decode it?floitsch
06/15/2025, 8:26 PMexklibur
06/15/2025, 8:27 PMfloitsch
06/15/2025, 8:28 PMfloitsch
06/17/2025, 7:38 AMmanager-system/main.toit:230
.
You call listen
with a lambda, but sometimes throw (like throw "Interrupt"
). The http package doesn't like that.
Maybe (would need tests), it would be better to cancel
the http-task when it doesn't behave the way you want.
http-task := task:: ... listen ...
// Something happens that want to make you stop the server.
http-task.cancel.
exklibur
06/17/2025, 4:59 PMexklibur
06/17/2025, 5:02 PMrun:
while true:
// Communicate with module network
log.info "establishing wifi in AP mode ($AP-SSID)"
server-exception := catch:
run-server
if server-exception:
log.info "Server: $server-exception"
log.info (interrupt ? "Server interrupted, stopping..." : "Server timeout, sending info to external server...")
if interrupt:
break
sleep --ms=1000
// Connect to LAN
log.info "Connecting to external network ($EXTERNAL-WIFI-SSID)"
client-exception := catch:
run-client
if client-exception:
log.info "Client: $client-exception"
sleep --ms=1000
run-server:
try:
wifi-exception := catch:
network = wifi.establish
--ssid=AP-SSID
--password=AP-PASSWORD
log.info "AP established"
if wifi-exception:
log.error "failed to establish AP"
log.error wifi-exception
exception := catch:
log.info "Starting HTTP server"
socket := network.tcp-listen 80
server := http.Server --max-tasks=3
http-task := task::
server.listen socket:: | request writer |
exception := catch:
handle-http-request request writer
if exception == "Interrupt":
interrupt = true
else if exception:
log.error "Exception: HTTP - $exception"
writer.close
sleep (Duration --m=1)
http-task.cancel
if exception:
log.error exception
finally:
log.info "HTTP server closing"
network.close
exklibur
06/21/2025, 8:55 PMexklibur
06/21/2025, 8:55 PMfloitsch
06/21/2025, 11:26 PMfloitsch
06/21/2025, 11:28 PMexklibur
06/22/2025, 3:45 AMfloitsch
06/22/2025, 5:01 PMexklibur
06/22/2025, 5:36 PMfloitsch
06/23/2025, 7:37 AMexklibur
06/24/2025, 3:24 AM