Hi All
-
I am trying to enable an ESP-32 system as a WiFi AP based on interrupts. The system will become enabled as an AP for 15 minutes (or so) when an interrupt is detected. After 15 minutes, it will automatically disable itself as an AP until the next interrupt.
-
My current implementation of this is with a javascript program which calls some simple C ffi functions, as follows:
load('api_timer.js');
load('api_gpio.js');
let tapon=ffi('int turnAPOn(void)'); // ffi to Enable AP WiFi
let tapoff=ffi('int turnAPOff(void)'); // ffi to Disable AP WiFi
let valvepin=14;
GPIO.setup_input(valvepin,GPIO.PULL_UP);
GPIO.set_int_handler (valvepin, GPIO.INT_EDGE_POS, function (valvepin) {
print ("WiFi AP Enabled by Javascript");
tapon();
Timer.set (20000,0, function (){
print ("WiFi AP Disabled by Javascript");
tapoff();
},null);
},null);
GPIO.enable_int(valvepin);
The relevant C code is:
#include "mongoose.h"
#include "mgos.h"
#include "mgos_i2c.h"
#include <xtensa/hal.h>
#include <math.h>
#include "string.h"
#include <esp_system.h>
#include <esp_adc_cal.h>
struct mgos_config_wifi_ap ap_cfg; //initialize a structure to get a copy of the WiFi configuration
int turnAPOn(void){ //ffi to turn Wifi On
ap_cfg.enable = true; //change the WiFi structure's enable value
mgos_wifi_setup_ap(&ap_cfg); //setup Wifi
return 1;
}
int turnAPOff(void){ //ffi to turn Wifi On
ap_cfg.enable = false; //change the WiFi structure's enable value
mgos_wifi_setup_ap(&ap_cfg); //setup Wifi
return 1;
}
enum mgos_app_init_result mgos_app_init(void) {
memcpy(&ap_cfg, mgos_sys_config_get_wifi_ap(), sizeof(ap_cfg)); //copy WiFi Config
return MGOS_APP_INIT_SUCCESS;
}
- This simple program crashes intermittently, depending on the state of the wifi configuration:
a) “wifi”: {“sta”: {“enable”:true,“ssid”:“GOODNET”, “pass”:“goodpassword”}
this case consistently works, if the timeout is longer than 20 seconds
b) “wifi”: {“sta”: {“enable”:false,“ssid”:"",“pass”:""}
this case will crash at the entry of the second interupt
c) “wifi”: {“sta”: {“enable”:true,“ssid”:“NoNetThere”,“pass”:""}
this case crashes randomly
There are several different error messages on crash conditions. Here’s a representative error message:
WiFi AP Enabled by Javascript
[Jun 29 18:42:17.293] esp32_wifi.c:196 WiFi mode: AP
[Jun 29 18:42:17.293] I (67119) wifi: mode : softAP (24:0a:c4:17:d2:6d)
[Jun 29 18:42:17.293] I (67129) wifi: Total power save buffer number: 16
[Jun 29 18:42:17.294] esp32_wifi.c:450 WiFi AP: SSID RS1235, channel 6
[Jun 29 18:42:17.294] esp32_wifi.c:635 WiFi AP: protocol BGN (0x7)
[Jun 29 18:42:17.294] esp32_wifi.c:507 WiFi AP IP: 192.168.4.1/255.255.255.0 gw 192.168.4.1, DHCP range 192.168.4.2 - 192.168.4.100
[Jun 29 18:42:17.295] esp32_wifi.c:512 WiFi AP: SSID RS1235, channel 6
[Jun 29 18:42:17.901] assertion "heap != NULL && "realloc() pointer is outside heap areas"" failed: file "/opt/Espressif/esp-idf/components/heap/heap_caps.c", line 292, function: heap_caps_realloc
[Jun 29 18:42:17.909] abort() was called at PC 0x400d1fa7 on core 0
- Is this a valid way of proceeding?
Any recommendations on how to make it work?
Can this be accomplished using some combination of “trigger_on_gpio” and “keep_enabled”?
Thanks as always for your help,
JSW