Wake up for a specific time after deep sleep
Posted: 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!
Any help will be appreciated.
Thanks!
- #include <SPI.h>
- #include <LoRa.h>
- //Libraries for OLED Display
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- //define the pins used by the LoRa transceiver module
- #define SCK 5
- #define MISO 19
- #define MOSI 27
- #define SS 18
- #define RST 14
- #define DIO0 26
- #define led 12
- //433E6 for Asia
- //866E6 for Europe
- //915E6 for North America
- #define BAND 866E6
- //OLED pins
- #define OLED_SDA 4
- #define OLED_SCL 15
- #define OLED_RST 16
- #define SCREEN_WIDTH 128 // OLED display width, in pixels
- #define SCREEN_HEIGHT 64 // OLED display height, in pixels
- #define relayPin 2
- #define ON HIGH
- #define OFF LOW
- Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST);
- byte msgCount = 0; // count of outgoing messages
- String outgoing; // outgoing message
- String LoRaData;
- byte localAddress = 0xFF; // address of this device
- byte destination = 0xBB; // destination to send to
- long lastSendTime = 0; // last send time
- int interval = 50; // interval between sends
- String statusmessage = "";
- int waterLevelPer; // global water level percentage variable
- bool pumpState = OFF; // initialize pumpState to off
- void deepSleepFunction() {
- Serial.println("Sleeping");
- // Enter deep sleep for 40 minutes
- esp_sleep_enable_timer_wakeup(10 * 1000000); // 40 minutes in microseconds
- esp_deep_sleep_start();
- }
- void setup() {
- //initialize Serial Monitor
- Serial.begin(9600);
- // pump relay pin
- pinMode(relayPin, OUTPUT);
- digitalWrite(relayPin, pumpState);
- //reset OLED display via software
- pinMode(OLED_RST, OUTPUT);
- pinMode(led,OUTPUT);
- digitalWrite(OLED_RST, LOW);
- delay(20);
- digitalWrite(OLED_RST, HIGH);
- //initialize OLED
- Wire.begin(OLED_SDA, OLED_SCL);
- if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false, false)) { // Address 0x3C for 128x32
- Serial.println(F("SSD1306 allocation failed"));
- for(;;); // Don't proceed, loop forever
- }
- display.clearDisplay();
- display.setTextColor(WHITE);
- display.setTextSize(2);
- display.setCursor(0,0);
- display.print("LORA RECEIVER ");
- display.display();
- Serial.println("LoRa Receiver Test");
- //SPI LoRa pins
- SPI.begin(SCK, MISO, MOSI, SS);
- //setup LoRa transceiver module
- LoRa.setPins(SS, RST, DIO0);
- if (!LoRa.begin(BAND)) {
- Serial.println("Starting LoRa failed!");
- while (1);
- }
- Serial.println("LoRa Initializing OK!");
- display.setCursor(0,10);
- display.println("LoRa Initializing OK!");
- display.display();
- }
- void loop() {
- // parse for a packet, and call onReceive with the result:
- onReceive(LoRa.parsePacket());
- delay(100);
- // turn pump on or off depending on the current water level
- digitalWrite(relayPin, pumpState);
- deepSleepFunction();
- }
- void onReceive(int packetSize) {
- if (packetSize == 0) return; // if there's no packet, return
- // read packet header bytes:
- int recipient = LoRa.read(); // recipient address
- byte sender = LoRa.read(); // sender address
- byte incomingMsgId = LoRa.read(); // incoming msg ID
- byte incomingLength = LoRa.read(); // incoming msg length
- String incoming = "";
- while (LoRa.available()) {
- LoRaData = LoRa.readString();
- //Serial.print(LoRaData);
- }
- String q = getValue(LoRaData, ',', 0);
- String r = getValue(LoRaData, ',', 1);
- int distance = q.toInt();
- int waterLevelPer = r.toInt();
- Serial.print("distance = ");
- Serial.println(distance);
- Serial.print("waterLevelPer = ");
- Serial.println(waterLevelPer);
- String rssi = "RSSI " + String(LoRa.packetRssi(), DEC) ;
- //Serial.println(rssi);
- delay(10);
- // Dsiplay information
- display.clearDisplay();
- display.setCursor(5,0);
- display.setTextSize(2);
- display.print("W-L-Meter");
- display.setCursor(0,25);
- display.setTextSize(2);
- display.print("Dist:"+String(distance)+"cm");
- display.setCursor(0,50);
- display.setTextSize(2);
- //display.print("W-L:");
- display.print("W-L:"+String(waterLevelPer)+"%");
- // display.setCursor(92,40);
- // display.setTextSize(2);
- // display.print(waterLevelPer);
- display.display();
- // Control the pump based on water level percentage
- if (waterLevelPer <= 50) {
- pumpState = ON; // Turn on the pump
- } else if (waterLevelPer == 100) {
- pumpState = OFF; // Turn off the pump
- }
- }
- String getValue(String data, char separator, int index)
- {
- int found = 0;
- int strIndex[] = { 0, -1 };
- int maxIndex = data.length() - 1;
- for (int i = 0; i <= maxIndex && found <= index; i++) {
- if (data.charAt(i) == separator || i == maxIndex) {
- found++;
- strIndex[0] = strIndex[1] + 1;
- strIndex[1] = (i == maxIndex) ? i+1 : i;
- }
- }
- return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
- }