Crash on WiFi Enable

Hi scaprile,

Thanks for the advice.

I think I have found the solution. This problem goes away if I add a corresponding config_wifi_sta structure to the C code. (This problem has nothing to do with interrupts, and can be completely reproduced with a timed loop that alternately enables and disables). Here is the fixed example using a javacript timed loop.

load(‘api_timer.js’);

let tapon=ffi(‘int turnAPOn(void)’); // ffi to Enable AP WiFi
let tapoff=ffi(‘int turnAPOff(void)’); // ffi to Disable AP WiFi
let i=0;

Timer.set(60000, Timer.REPEAT, function() { //toggle endlessly between Wifi On and Wifi Off
if (i%2===1) {print (“turnon”, i); tapon(); }
else {print (“turnoff”, i); tapoff(); }
i++;
}, null);

and here is the C

#include “mongoose.h”
#include “mgos.h”
#include <xtensa/hal.h>

struct mgos_config_wifi_ap ap_cfg; //initialize a structure to get a copy of the WiFi AP configuration
struct mgos_config_wifi_sta sta_cfg; //initialize a structure to get a copy of the WiFi STA configuration

int turnAPOn(void){ //ffi to turn Wifi AP On
sta_cfg.enable=true;
ap_cfg.enable = true; //change the WiFi AP structure’s enable value
mgos_wifi_setup_sta(&sta_cfg); //setup Wifi STA
mgos_wifi_setup_ap(&ap_cfg); //setup Wifi AP
return 1;
}

int turnAPOff(void){ //ffi to turn Wifi AP Off
sta_cfg.enable=true;
ap_cfg.enable = false; //change the WiFi AP structure’s enable value
mgos_wifi_setup_sta(&sta_cfg); //setup Wifi STA
mgos_wifi_setup_ap(&ap_cfg); //setup Wifi AP
return 1;
}

enum mgos_app_init_result mgos_app_init(void) {
memcpy(&ap_cfg, mgos_sys_config_get_wifi_ap(), sizeof(ap_cfg)); //copy the current wifi AP configuration into the local structure
memcpy(&sta_cfg, mgos_sys_config_get_wifi_sta(), sizeof(sta_cfg)); //copy the current wifi STA configuration into the local structure
return MGOS_APP_INIT_SUCCESS;
}