******************************************************************************
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
******************************************************************************
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
esp_wifi_set_mode
wifi_esp32.cc
esp_wifi_set_mode(WIFI_MODE_XXX)
esp_wifi_set_mode(WIFI_MODE_APSTA)
set_mode
esp_netif_new
init
ESP_NETIF_DEFAULT_WIFI_AP
ESP_NETIF_DEFAULT_WIFI_STA
ESP_NETIF_DEFAULT_WIFI_APSTA
esp_wifi_set_mode
set_mode
/**
* @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
run:
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
listen
kebab-case
jag toit tool kebabify code ...
*
jag toit doc
/**
jag decode
manager-system/main.toit:230
listen
throw "Interrupt"
cancel
http-task := task:: ... listen ...
// Something happens that want to make you stop the server.
http-task.cancel.
run:
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