Skip to content

Commit

Permalink
fix esp32 sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
dimecho committed Aug 21, 2024
1 parent 5991ed0 commit e877ba3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
55 changes: 35 additions & 20 deletions ESPTiny-Plant.ino
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ uint16_t PLANT_SOIL_MOISTURE = 480; //ADC value
uint32_t PLANT_MANUAL_TIMER = 0; //manual sleep timer - hours
uint16_t PLANT_SOIL_TYPE = 2; //['Sand', 'Clay', 'Dirt', 'Loam', 'Moss'];
uint16_t PLANT_TYPE = 0; //['Bonsai', 'Monstera', 'Palm'];
int DEEP_SLEEP = 4; //auto sleep timer - minutes
int DEEP_SLEEP = 5; //auto sleep timer - minutes
//=============================
//String EMAIL_ALERT = "";
//String SMTP_SERVER = "";
Expand All @@ -375,15 +375,14 @@ String DEMO_PASSWORD = ""; //public demo
//String TIMEZONE_OFFSET = "-28800"; //UTC offset in seconds
char DEMO_AVAILABILITY[] = "00000000618"; //M, T, W, T, F, S, S + Time Range
//=============================
uint32_t WEB_SLEEP = 360000; //6 minutes = 6 x 60x 1000
//=============================
uint16_t delayBetweenAlertEmails = 0; //2 hours = 2 x 60 = 120 minutes = x4 30 min loops
uint16_t delayBetweenRefillReset = 0; //2 hours = 2 x 60 = 120 minutes = x4 30 min loops
uint16_t delayBetweenOverfloodReset = 0; //8 hours = 8 x 60 = 480 minutes = x16 30 min loops
uint8_t ON_TIME = 0; //from 6am
uint8_t OFF_TIME = 0; //to 6pm
uint16_t LOG_INTERVAL_S = 0;
uint16_t DEEP_SLEEP_S = 0;
uint16_t LOG_INTERVAL_S = 0; //seconds
uint16_t DEEP_SLEEP_S = 0; //seconds
uint16_t DEEP_SLEEP_MS = 0; //milliseconds
char PNP_ADC[] = "010"; //0=NPN|1=PNP, ADC sensitivity, Water Level Sensor 0=Disable|1=Enable
//uint8_t ADC_ERROR_OFFSET = 64; //WAKE_RF_DISABLED offset
#if TIMECLIENT_NTP
Expand Down Expand Up @@ -555,15 +554,21 @@ void setup() {
#if DEBUG
Serial.printf("Wakeup Reason:%u\n", wakeupReason);
#endif

if (wakeupReason == 5) { // && WiFi.getMode() == WIFI_OFF) {
LOG_INTERVAL = 0; //going into sleep mode anyway, do not delay in loop()
WEB_SLEEP = 0;
#ifdef ESP32
if (wakeupReason == 8) { //ESP_RST_DEEPSLEEP (8)
#else
if (wakeupReason == 5) { //REASON_DEEP_SLEEP_AWAKE (5)
#endif
LOG_INTERVAL = 0; //going into sleep mode anyway, do not delay in loop()
if (DATA_LOG > 0) //writing logs during sleep
LittleFS.begin();
} else {
//Emergency Recover (RST to GND)
if (wakeupReason == 6) {
#ifdef ESP32
if (wakeupReason == 2) { //ESP_RST_EXT (2)
#else
if (wakeupReason == 6) { //REASON_EXT_SYS_RST (6)
#endif
LOG_INTERVAL = 300; //prevent WiFi from sleeping 5 minutes
ALERTS[0] = '1'; //email DHCP IP
ALERTS[1] = '0'; //low voltage
Expand Down Expand Up @@ -614,7 +619,6 @@ void setupWiFi(uint8_t timeout) {
WiFi.setOutputPower(WIRELESS_PHY_POWER);
#else
WiFi.setTxPower((wifi_power_t)WIRELESS_PHY_POWER);
//btStop();
#endif

const String WIRELESS_SSID = NVRAM_Read(_WIRELESS_SSID);
Expand Down Expand Up @@ -891,6 +895,7 @@ void setupWebServer() {
//Async Web Server HTTP_GET, HTTP_POST, HTTP_ANY
//==============================================
server.on("/api", HTTP_GET, [](AsyncWebServerRequest *request) {
webTimer = millis();
if (request->hasParam("adc")) {
uint8_t adc = request->getParam("adc")->value().toInt();
uint16_t moisture = 0;
Expand Down Expand Up @@ -1114,8 +1119,11 @@ void setupWebServer() {
if (!request->authenticate("", DEMO_PASSWORD.c_str()))
return request->requestAuthentication();
#endif
String updateURL = "http://" + NETWORK_IP + "/update";
String updateHTML = "<!DOCTYPE html><html><body><form method=POST action='" + updateURL + "' enctype='multipart/form-data'><input type=file accept='.bin,.signed' name=firmware><input type=submit value='Update Firmware'></form><br><form method=POST action='" + updateURL + "' enctype='multipart/form-data'><input type=file accept='.bin,.signed' name=filesystem><input type=submit value='Update Filesystem'></form></body></html>";
char updateHTML[512];
snprintf(updateHTML, sizeof(updateHTML), "<!DOCTYPE html><html><body>");
strncat(updateHTML, buildFormPostButton("Firmware"), sizeof(updateHTML) - strlen(updateHTML) - 1);
strncat(updateHTML, buildFormPostButton("Filesystem"), sizeof(updateHTML) - strlen(updateHTML) - 1);
strncat(updateHTML, "</body></html>", sizeof(updateHTML) - strlen(updateHTML) - 1);
AsyncWebServerResponse *response = request->beginResponse(200, text_html, updateHTML);
request->send(response);
});
Expand Down Expand Up @@ -1209,7 +1217,6 @@ void setupWebServer() {

server.onNotFound([](AsyncWebServerRequest *request) {
//Serial.println((request->method() == HTTP_GET) ? "GET" : "POST");
webTimer = millis();

String file = request->url();
#if DEBUG
Expand Down Expand Up @@ -1296,6 +1303,14 @@ void setupWebServer() {
//server.onRequestBody(serverCompression);
}

char* buildFormPostButton(char name[]) {
static char buffer[256];
snprintf(buffer, sizeof(buffer),
"<form method=POST action='http://%s/update' enctype='multipart/form-data'><input type=file accept='.bin,.signed' name=%s><input type=submit value='Update %s'></form><br>",
NETWORK_IP, name, name);
return buffer;
}

void loop() {
//delay(1); //enable Modem-Sleep
//ArduinoOTA.handle();
Expand Down Expand Up @@ -1389,7 +1404,7 @@ void loop() {
}

byte WiFiClientCount = 0;
if (millis() - webTimer < WEB_SLEEP) { //track web activity for 5 minutes
if (LOG_INTERVAL > 0 && (millis() - webTimer) < DEEP_SLEEP_MS) { //track web activity for 5 minutes
WiFiClientCount = 1;
//} else if (WiFi.getMode() == WIFI_AP) {
// WiFiClientCount = WiFi.softAPgetStationNum(); //counts all wifi clients (refresh may take 5 min to register station leave)
Expand Down Expand Up @@ -1508,8 +1523,9 @@ void readySleep() {
//GPIO16 (D0) needs to be tied to RST to wake from deepSleep
if (DEEP_SLEEP_S > 1) {
rtcData.runTime += DEEP_SLEEP_S; //add sleep time, when we wake up will be accurate.
WiFi.disconnect(); //disassociate properly (easier to reconnect)
//delay(100);
WiFi.disconnect(true); //disassociate properly (easier to reconnect)
WiFi.mode(WIFI_OFF);

#ifdef ESP32
esp_sleep_disable_wifi_wakeup();
esp_sleep_enable_timer_wakeup(DEEP_SLEEP);
Expand Down Expand Up @@ -2110,8 +2126,8 @@ void offsetTiming() {
DEEP_SLEEP_S = DEEP_SLEEP; //store in seconds - no need to convert in loop()
LOG_INTERVAL_S = LOG_INTERVAL; //store in seconds - no need to convert in loop()

DEEP_SLEEP = DEEP_SLEEP * 1000; //milliseconds millis()
DEEP_SLEEP = DEEP_SLEEP * 1000; //microseconds micros()
DEEP_SLEEP_MS = DEEP_SLEEP * 1000; //milliseconds millis()
DEEP_SLEEP = DEEP_SLEEP_MS * 1000; //microseconds micros()
LOG_INTERVAL = LOG_INTERVAL * 1000; //milliseconds millis()
}

Expand Down Expand Up @@ -2155,7 +2171,6 @@ String getContentType(String filename) {
//===============
void WebUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
if (!index) {

//WARNING: Do not save RTC memory after Update.begin(). "chksum" will be different after reboot, firmware will not flash
/*
memset(&rtcData, 0, sizeof(rtcData)); //reset RTC memory (set all zero)
Expand Down
2 changes: 1 addition & 1 deletion Web/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ function loadSVG(svgfile) {
var moistureMax = 1000;
var moistureStep = 10;
if(ESP32) {
moistureMin = 100;
moistureMin = 50;
moistureMax = 2500;
}

Expand Down
2 changes: 1 addition & 1 deletion littlefs-build-win.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,5 @@ if($littlefsSelect -eq 1) {
Start-Process $mklittlefs -ArgumentList "-c .\data -b 8192 -p 256 -s 2072576 .\build\flash-littlefs.bin" -NoNewWindow -PassThru -Wait
}else{
$mklittlefs = "$env:LOCALAPPDATA\Arduino15\packages\esp32\tools\mklittlefs\3.0.0-gnu12-dc7f933\mklittlefs.exe"
Start-Process $mklittlefs -ArgumentList "-c .\data -b 8192 -p 256 -s 1036288 .\build\flash-littlefs.bin" -NoNewWindow -PassThru -Wait
Start-Process $mklittlefs -ArgumentList "-c .\data -b 4096 -p 256 -s 518144 .\build\flash-littlefs.bin" -NoNewWindow -PassThru -Wait
}

0 comments on commit e877ba3

Please sign in to comment.