Wake up for a specific time after deep sleep

GooseGoose
Posts: 4
Joined: Mon Apr 15, 2024 1:25 pm

Wake up for a specific time after deep sleep

Postby GooseGoose » Mon Apr 15, 2024 1:43 pm

Good day everyone, I am using Arduino for my ESP32 and have implemented a deep sleep and want to wake it up for a specific amount of time (20 seconds) to receive and read data from two other ESP32's. I have used the delay function almost everywhere I can think of being applicable but when I do it doesn't even execute the code and stays in a stagnant "on" state.
Any help will be appreciated.
Thanks!
  1. #include <SPI.h>
  2. #include <LoRa.h>
  3.  
  4. //Libraries for OLED Display
  5. #include <Wire.h>
  6. #include <Adafruit_GFX.h>
  7. #include <Adafruit_SSD1306.h>
  8.  
  9. //define the pins used by the LoRa transceiver module
  10. #define SCK 5
  11. #define MISO 19
  12. #define MOSI 27
  13. #define SS 18
  14. #define RST 14
  15. #define DIO0 26
  16. #define led 12
  17. //433E6 for Asia
  18. //866E6 for Europe
  19. //915E6 for North America
  20. #define BAND 866E6
  21.  
  22. //OLED pins
  23. #define OLED_SDA 4
  24. #define OLED_SCL 15
  25. #define OLED_RST 16
  26. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  27. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  28.  
  29. #define relayPin 2
  30. #define ON HIGH
  31. #define OFF LOW
  32.  
  33. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST);
  34. byte msgCount = 0;            // count of outgoing messages
  35.  
  36. String outgoing;              // outgoing message
  37. String LoRaData;
  38. byte localAddress = 0xFF;     // address of this device
  39. byte destination = 0xBB;      // destination to send to
  40. long lastSendTime = 0;        // last send time
  41. int interval = 50;          // interval between sends
  42. String statusmessage = "";
  43.  
  44. int waterLevelPer;  // global water level percentage variable
  45. bool pumpState = OFF;  // initialize pumpState to off
  46.  
  47. void deepSleepFunction() {
  48.   Serial.println("Sleeping");
  49.   // Enter deep sleep for 40 minutes
  50.   esp_sleep_enable_timer_wakeup(10 * 1000000); // 40 minutes in microseconds
  51.   esp_deep_sleep_start();
  52. }
  53.  
  54.  
  55. void setup() {
  56.   //initialize Serial Monitor
  57.   Serial.begin(9600);
  58.  
  59.     // pump relay pin
  60.   pinMode(relayPin, OUTPUT);
  61.   digitalWrite(relayPin, pumpState);
  62.  
  63.   //reset OLED display via software
  64.   pinMode(OLED_RST, OUTPUT);
  65.   pinMode(led,OUTPUT);
  66.  
  67.  
  68.   digitalWrite(OLED_RST, LOW);
  69.   delay(20);
  70.   digitalWrite(OLED_RST, HIGH);
  71.  
  72.   //initialize OLED
  73.   Wire.begin(OLED_SDA, OLED_SCL);
  74.   if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false, false)) { // Address 0x3C for 128x32
  75.     Serial.println(F("SSD1306 allocation failed"));
  76.     for(;;); // Don't proceed, loop forever
  77.   }
  78.  
  79.   display.clearDisplay();
  80.   display.setTextColor(WHITE);
  81.   display.setTextSize(2);
  82.   display.setCursor(0,0);
  83.   display.print("LORA RECEIVER ");
  84.   display.display();
  85.  
  86.   Serial.println("LoRa Receiver Test");
  87.  
  88.   //SPI LoRa pins
  89.   SPI.begin(SCK, MISO, MOSI, SS);
  90.   //setup LoRa transceiver module
  91.   LoRa.setPins(SS, RST, DIO0);
  92.  
  93.   if (!LoRa.begin(BAND)) {
  94.     Serial.println("Starting LoRa failed!");
  95.     while (1);
  96.   }
  97.   Serial.println("LoRa Initializing OK!");
  98.   display.setCursor(0,10);
  99.   display.println("LoRa Initializing OK!");
  100.   display.display();  
  101. }
  102.  
  103. void loop() {
  104.   // parse for a packet, and call onReceive with the result:
  105.   onReceive(LoRa.parsePacket());
  106.   delay(100);
  107.  
  108.   // turn pump on or off depending on the current water level
  109.   digitalWrite(relayPin, pumpState);
  110.   deepSleepFunction();
  111. }
  112. void onReceive(int packetSize) {
  113.   if (packetSize == 0) return;          // if there's no packet, return
  114.  
  115.   // read packet header bytes:
  116.   int recipient = LoRa.read();          // recipient address
  117.   byte sender = LoRa.read();            // sender address
  118.   byte incomingMsgId = LoRa.read();     // incoming msg ID
  119.   byte incomingLength = LoRa.read();    // incoming msg length
  120.  
  121.   String incoming = "";
  122.  
  123.     while (LoRa.available()) {
  124.       LoRaData = LoRa.readString();
  125.       //Serial.print(LoRaData);
  126.     }
  127.  
  128. String q = getValue(LoRaData, ',', 0);
  129. String r = getValue(LoRaData, ',', 1);
  130. int distance = q.toInt();
  131. int waterLevelPer = r.toInt();
  132. Serial.print("distance = ");
  133. Serial.println(distance);
  134. Serial.print("waterLevelPer = ");
  135. Serial.println(waterLevelPer);
  136.  
  137.  
  138.     String rssi = "RSSI " + String(LoRa.packetRssi(), DEC) ;
  139.     //Serial.println(rssi);
  140.  
  141.  
  142.   delay(10);
  143.  
  144.  
  145.    // Dsiplay information
  146.    display.clearDisplay();
  147.    display.setCursor(5,0);
  148.     display.setTextSize(2);
  149.    display.print("W-L-Meter");
  150.  
  151.    display.setCursor(0,25);
  152.     display.setTextSize(2);
  153.        display.print("Dist:"+String(distance)+"cm");    
  154.  
  155.    display.setCursor(0,50);
  156.     display.setTextSize(2);
  157.    //display.print("W-L:");
  158.     display.print("W-L:"+String(waterLevelPer)+"%");    
  159.  
  160. //   display.setCursor(92,40);
  161. //    display.setTextSize(2);
  162. //   display.print(waterLevelPer);
  163.    display.display();  
  164.  
  165.     // Control the pump based on water level percentage
  166.   if (waterLevelPer <= 50) {
  167.     pumpState = ON;  // Turn on the pump
  168.   } else if (waterLevelPer == 100) {
  169.     pumpState = OFF;  // Turn off the pump
  170.   }
  171. }
  172.  
  173. String getValue(String data, char separator, int index)
  174. {
  175.     int found = 0;
  176.     int strIndex[] = { 0, -1 };
  177.     int maxIndex = data.length() - 1;
  178.  
  179.     for (int i = 0; i <= maxIndex && found <= index; i++) {
  180.         if (data.charAt(i) == separator || i == maxIndex) {
  181.             found++;
  182.             strIndex[0] = strIndex[1] + 1;
  183.             strIndex[1] = (i == maxIndex) ? i+1 : i;
  184.         }
  185.     }
  186.     return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
  187. }

ESP_Sprite
Posts: 9727
Joined: Thu Nov 26, 2015 4:08 am

Re: Wake up for a specific time after deep sleep

Postby ESP_Sprite » Tue Apr 16, 2024 1:48 am

'delay' doesn't do anything wrt sleep modes. You probably want to read up on deep sleep a bit and retry writing your code.

GooseGoose
Posts: 4
Joined: Mon Apr 15, 2024 1:25 pm

Re: Wake up for a specific time after deep sleep

Postby GooseGoose » Tue Apr 16, 2024 6:44 am

I have looked it up but found nothing that can help me

GooseGoose
Posts: 4
Joined: Mon Apr 15, 2024 1:25 pm

Re: Wake up for a specific time after deep sleep

Postby GooseGoose » Tue Apr 16, 2024 6:58 am

All I want to know is if there is a way to keep the esp32 awake for a certain time just like setting a timer for it's sleep?

ESP_Sprite
Posts: 9727
Joined: Thu Nov 26, 2015 4:08 am

Re: Wake up for a specific time after deep sleep

Postby ESP_Sprite » Tue Apr 16, 2024 7:00 am

Ah, never mind, I initially read over your deepSleepFunction code. What do you mean with 'it doesn't even execute the code and stays in a stagnant "on" state.'?

ESP_Sprite
Posts: 9727
Joined: Thu Nov 26, 2015 4:08 am

Re: Wake up for a specific time after deep sleep

Postby ESP_Sprite » Tue Apr 16, 2024 7:06 am

And yes, there is. You need to find some way to call deepSleepFunction 20 seconds after power up. You can either do that manually, by making sure the code path before the call to that function lasts 20 seconds (inserting delays strategically would indeed help there) or you could schedule a timer callback or something with a delay of 20 seconds and have that call deepSleepFunction.

GooseGoose
Posts: 4
Joined: Mon Apr 15, 2024 1:25 pm

Re: Wake up for a specific time after deep sleep

Postby GooseGoose » Tue Apr 16, 2024 7:17 am

When I was figuring out a way to try and delay the deep sleep I used the delay function and it caused the ESP32 to only turn on and not execute code. However I now see that using delay would only cause problems more so if I try to implement it inside my loop because I have multiple other LORA sensors that needs to relay data back to this one each with their own timer to eliminate multiple signals coming in at once; so the only way to do so is to make sure the main loop executes a few times and I just realized I answered my own question LOL!

Thank you for your time and replies

Who is online

Users browsing this forum: Baidu [Spider], Cimby1 and 99 guests