Skip to content

Commit

Permalink
Modify partition table and Disable OTA for ESP32 to free up a lot of …
Browse files Browse the repository at this point in the history
…memory.

Add activeOTA flag for ESP32 S3 and include in menu. False by default for security.
Add a 5 secs delay until menu is active for better Improv-WiFi response
Looks like freeing memory fixes #145
  • Loading branch information
melkati committed Jan 30, 2024
1 parent 6953c97 commit 64e13a2
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 15 deletions.
5 changes: 3 additions & 2 deletions CO2_Gadget.ino
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ bool activeBLE = true;
bool activeWIFI = true;
bool activeMQTT = true;
bool activeESPNOW = false;
bool activeOTA = false;
bool troubledWIFI = false; // There are problems connecting to WIFI. Temporary suspend WIFI
bool troubledMQTT = false; // There are problems connecting to MQTT. Temporary suspend MQTT
uint64_t timeTroubledWIFI = 0; // Time since WIFI is troubled
Expand All @@ -54,7 +55,6 @@ bool displayShowHumidity = true;
bool displayShowBattery = true;
bool displayShowCO2 = true;
bool displayShowPM25 = true;

bool debugSensors = false;
bool inMenu = false;
uint16_t measurementInterval = 10;
Expand All @@ -63,6 +63,7 @@ int8_t selectedCO2Sensor = -1;
bool outputsModeRelay = false;
uint8_t channelESPNow = 1;
uint16_t boardIdESPNow = 0;
uint64_t timeInitializationCompleted = 0;

// Variables for Battery reading
float battery_voltage = 0;
Expand Down Expand Up @@ -494,7 +495,7 @@ void setup() {
}
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, brown_reg_temp); // enable brownout detector
Serial.println("-->[STUP] Ready.");
delay(100);
timeInitializationCompleted = millis();
}

void loop() {
Expand Down
20 changes: 17 additions & 3 deletions CO2_Gadget_Menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,8 @@ result doSetWiFiSSID(eventMask e, navNode &nav, prompt &item) {
Serial.println(e);
Serial.flush();
#endif
// Serial.printf("tempWiFiSSID: #%s#\n", tempWiFiSSID);
wifiSSID = String(tempWiFiSSID);
// Serial.printf("wifiSSID: #%s#\n", wifiSSID.c_str());
wifiSSID.trim();
// Serial.printf("wifiSSID: #%s#\n", wifiSSID.c_str());
return proceed;
}

Expand Down Expand Up @@ -367,11 +364,24 @@ TOGGLE(activeWIFI, activeWIFIMenu, "WIFI Enable: ", doNothing,noEvent, wrapStyle
,VALUE("ON", true, doSetActiveWIFI, exitEvent)
,VALUE("OFF", false, doSetActiveWIFI, exitEvent));

#ifdef SUPPORT_OTA
result doSetActiveOTA(eventMask e, navNode &nav, prompt &item) {
return proceed;
}

TOGGLE(activeOTA, activeOTAMenu, "OTA Enable: ", doNothing,noEvent, wrapStyle
,VALUE("ON", true, doSetActiveOTA, exitEvent)
,VALUE("OFF", false, doSetActiveOTA, exitEvent));
#endif

MENU(wifiConfigMenu, "WIFI Config", doNothing, noEvent, wrapStyle
,SUBMENU(activeWIFIMenu)
,EDIT("SSID", tempWiFiSSID, ssidChars, doSetWiFiSSID, exitEvent, wrapStyle)
,EDIT("Pass:", tempWiFiPasswrd, allChars, doSetWiFiPasswrd, exitEvent, wrapStyle)
,EDIT("Host:", tempHostName, allChars, doSetHostName, exitEvent, wrapStyle)
#ifdef SUPPORT_OTA
,SUBMENU(activeOTAMenu)
#endif
,EXIT("<Back"));


Expand Down Expand Up @@ -999,6 +1009,10 @@ void menuLoop() {
}
}

if (millis() < timeInitializationCompleted + 5000) { // Wait 10 seconds before starting the menu to avoid issues with Improv-WiFi
return;
}

#if defined(SUPPORT_TFT)
if (wifiChanged) {
wifiChanged = false;
Expand Down
5 changes: 5 additions & 0 deletions CO2_Gadget_Partitions-no_ota.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Extendend to 0x2D0000 for the program
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, 0x9000, 0x5000,
app0, app, ota_0, 0x10000, 0x2D0000,
spiffs, data, spiffs, 0x3D0000, 0x30000,
8 changes: 7 additions & 1 deletion CO2_Gadget_Preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ void printPreferences() {
Serial.printf("-->[PREF] activeBLE is:\t#%s# (%d)\n", ((activeBLE) ? "Enabled" : "Disabled"), activeBLE);
Serial.printf("-->[PREF] activeWIFI is:\t#%s# (%d)\n", ((activeWIFI) ? "Enabled" : "Disabled"), activeWIFI);
Serial.printf("-->[PREF] activeMQTT is:\t#%s# (%d)\n", ((activeMQTT) ? "Enabled" : "Disabled"), activeMQTT);
Serial.printf("-->[PREF] activeESPNOW is:\t#%s# (%d)\n", ((activeMQTT) ? "Enabled" : "Disabled"), activeESPNOW);
Serial.printf("-->[PREF] activeESPNOW is:\t#%s# (%d)\n", ((activeESPNOW) ? "Enabled" : "Disabled"), activeESPNOW);
Serial.printf("-->[PREF] activeOTA is:\t#%s# (%d)\n", ((activeOTA) ? "Enabled" : "Disabled"), activeOTA);
Serial.printf("-->[PREF] rootTopic:\t#%s#\n", rootTopic.c_str());
Serial.printf("-->[PREF] batDischgd:\t #%d#\n", batteryDischargedMillivolts);
Serial.printf("-->[PREF] batChargd:\t #%d#\n", batteryFullyChargedMillivolts);
Expand Down Expand Up @@ -85,6 +86,7 @@ void initPreferences() {
activeWIFI = preferences.getBool("activeWIFI", true);
activeMQTT = preferences.getBool("activeMQTT", false);
activeESPNOW = preferences.getBool("activeESPNOW", false);
activeOTA = preferences.getBool("activeOTA", false);
rootTopic = preferences.getString("rootTopic", rootTopic);
mqttClientId = preferences.getString("mqttClientId", mqttClientId);
mqttBroker = preferences.getString("mqttBroker", mqttBroker).c_str();
Expand Down Expand Up @@ -183,6 +185,7 @@ void putPreferences() {
preferences.putBool("activeWIFI", activeWIFI);
preferences.putBool("activeMQTT", activeMQTT);
preferences.putBool("activeESPNOW", activeESPNOW);
preferences.putBool("activeOTA", activeOTA);
preferences.putString("rootTopic", rootTopic);
preferences.putUInt("batDischgd", batteryDischargedMillivolts);
preferences.putUInt("batChargd", batteryFullyChargedMillivolts);
Expand Down Expand Up @@ -237,6 +240,7 @@ String getPreferencesAsJson() {
doc["activeWIFI"] = preferences.getBool("activeWIFI", false);
doc["activeMQTT"] = preferences.getBool("activeMQTT", false);
doc["activeESPNOW"] = preferences.getBool("activeESPNOW", false);
doc["activeOTA"] = preferences.getBool("activeOTA", false);
doc["rootTopic"] = preferences.getString("rootTopic", rootTopic);
doc["batDischgd"] = preferences.getInt("batDischgd", 3500);
doc["batChargd"] = preferences.getInt("batChargd", 4200);
Expand Down Expand Up @@ -293,6 +297,7 @@ String getActualSettingsAsJson() {
doc["activeWIFI"] = activeWIFI;
doc["activeMQTT"] = activeMQTT;
doc["activeESPNOW"] = activeESPNOW;
doc["activeOTA"] = activeOTA;
doc["rootTopic"] = rootTopic;
doc["batDischgd"] = batteryDischargedMillivolts;
doc["batChargd"] = batteryFullyChargedMillivolts;
Expand Down Expand Up @@ -379,6 +384,7 @@ bool handleSavePreferencesfromJSON(String jsonPreferences) {
activeWIFI = JsonDocument["activeWIFI"];
activeMQTT = JsonDocument["activeMQTT"];
activeESPNOW = JsonDocument["activeESPNOW"];
activeOTA = JsonDocument["activeOTA"];
rootTopic = JsonDocument["rootTopic"].as<String>().c_str();
batteryDischargedMillivolts = JsonDocument["batDischgd"];
batteryFullyChargedMillivolts = JsonDocument["batChargd"];
Expand Down
4 changes: 3 additions & 1 deletion CO2_Gadget_WIFI.h
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,9 @@ void wifiClientLoop() {

void OTALoop() {
#ifdef SUPPORT_OTA
AsyncElegantOTA.loop();
if ((activeWIFI) && (activeOTA) && (!troubledWIFI) && (WiFi.status() == WL_CONNECTED)) {
AsyncElegantOTA.loop();
}
#endif
}

Expand Down
15 changes: 7 additions & 8 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ monitor_speed = 115200
monitor_port = COM12
upload_port = COM12
monitor_filters = time, esp32_exception_decoder
board_build.partitions = CO2_Gadget_Partitions.csv ; Others at Windows at C:\Users\%USER%\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.5\tools\partitions
board_build.partitions = CO2_Gadget_Partitions-no_ota.csv ; Others at Windows at C:\Users\%USER%\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.5\tools\partitions
extra_scripts =
lib_ldf_mode = chain+
lib_deps =
Expand Down Expand Up @@ -57,8 +57,7 @@ build_flags =

-D MQTT_BROKER_SERVER="\"192.168.1.145"\"
-D CO2_GADGET_VERSION="\"0.8."\"

-D CO2_GADGET_REV="\"090-development"\"
-D CO2_GADGET_REV="\"092-development"\"
-D CORE_DEBUG_LEVEL=0
-DNEOPIXEL_PIN=26 ; Pinnumber for button for down/next and back / exit actions
-DNEOPIXEL_COUNT=16 ; How many neopixels to control
Expand All @@ -77,10 +76,10 @@ build_flags =
-DPIN_HYSTERESIS=100 ; Hysteresis PPM to avoid pins going ON and OFF continuously. TODO : Minimum time to switch
-DWIFI_PRIVACY ; Comment to show WiFi password in serial and the menu (intended for debugging)
-DSUPPORT_BLE ; Comment to dissable Bluetooth (makes more memory available)
-USUPPORT_ESPNOW
-DSUPPORT_OTA ; Needs SUPPORT_WIFI
-DSUPPORT_MDNS ; Needs SUPPORT_WIFI
-DSUPPORT_MQTT ; Needs SUPPORT_WIFI
-DSUPPORT_ESPNOW
-USUPPORT_OTA ;
-DSUPPORT_MDNS ;
-DSUPPORT_MQTT ;
-DSUPPORT_MQTT_DISCOVERY
-DMQTT_DISCOVERY_PREFIX="\"homeassistant/\""
-DESPNOW_PEER_MAC_ADDRESS="{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}" ; MAC Address of the ESP-NOW receiver (STA MAC). For unicast use peer address, as: {0xE8, 0x68, 0xE7, 0x0F, 0x08, 0x90}
Expand Down Expand Up @@ -237,7 +236,6 @@ platform = espressif32
board = lilygo-t-display-s3
framework = ${common_env_data.framework}
monitor_filters = ${common_env_data.monitor_filters}
; board_build.partitions = ${common_env_data.board_build.partitions}
extra_scripts = ${common_env_data.extra_scripts}
board_build.flash_mode = dio
board_upload.flash_size = 16MB
Expand All @@ -250,6 +248,7 @@ lib_deps =
${common_env_data.lib_deps}
build_flags =
${common_env_data.build_flags}
-DSUPPORT_OTA
-DARDUINO_ESP32_DEV=1
-DTDISPLAY_S3
-DBTN_UP=14 ; Pinnumber for button for up/previous and select / enter actions
Expand Down

0 comments on commit 64e13a2

Please sign in to comment.