I am including the code I used to test the wakeup using LoRa interrupt.
If I input "SLEEP*" via LoRa or Serial, ESP32 will go to sleep but will not wake up with further commands, even though the interrupt is working correctly.
#include <LoRa.h>
#define LORA_MISO 19
#define LORA_MOSI 27
#define LORA_SCK 5
#define LORA_CS 18
#define LORA_RST 14
#define LORA_DIO0 26
#define LORA_DIO0_INT GPIO_NUM_26
volatile bool loraInt = false;
void setup() {
Serial.begin(115200);
print_wakeup_reason();
loraSetup();
}
void loraSendMessage(String message) {
LoRa.beginPacket();
LoRa.println(message);
LoRa.endPacket();
LoRa.receive();
}
ICACHE_RAM_ATTR void loraInterrupt() {
// Serial.println("I");
loraInt = true;
}
void loraSetup() {
SPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_CS);
LoRa.setSPI(SPI);
LoRa.setPins(LORA_CS, LORA_RST, LORA_DIO0);
if (!LoRa.begin(867E6)) {
Serial.println("LoRa init failed. Check your connections.");
while (true);
}
Serial.println("LoRa init succeeded.");
LoRa.setSyncWord(65);
LoRa.setTxPower(17);
LoRa.setSignalBandwidth(125E3);
LoRa.setSpreadingFactor(7);
LoRa.setCodingRate4(5);
LoRa.setPreambleLength(8);
LoRa.enableCrc();
pinMode(LORA_DIO0, INPUT);
attachInterrupt(digitalPinToInterrupt(LORA_DIO0), loraInterrupt, RISING);
loraSendMessage("Hello LoRa");
}
void sleep_controller(long seconds) { //Deep Sleep ESP32
Serial.println("Sleeping for " + String(seconds) + " secs");
esp_sleep_enable_timer_wakeup(1000000LL * seconds);
// rtc_gpio_init(LORA_DIO0_INT);
// rtc_gpio_set_direction(LORA_DIO0_INT,RTC_GPIO_MODE_INPUT_ONLY);
// rtc_gpio_pulldown_dis(LORA_DIO0_INT);
// rtc_gpio_pullup_en(LORA_DIO0_INT);
// rtc_gpio_hold_en(LORA_DIO0_INT);
esp_sleep_enable_ext0_wakeup(LORA_DIO0_INT, 1);
// esp_sleep_enable_ext1_wakeup(0x4000000, ESP_EXT1_WAKEUP_ANY_HIGH);
esp_deep_sleep_start();
}
void sleep_lora() {
// LoRa.sleep();
}
void wake_lora() {
loraSetup();
}
void sleep_all(long seconds = 300) {
sleep_lora();
sleep_controller(seconds);
}
void loop() {
String input;
bool serialFlag = false, loraFlag = false;
if (Serial.available()) {
input = "";
while (Serial.available()) {
input = Serial.readStringUntil('\n');
serialFlag = true;
}
}
if (loraInt) {
// Serial.println("I");
int packetSize = LoRa.parsePacket();
if (packetSize) {
input = "";
while (LoRa.available()) {
input = LoRa.readStringUntil('\n');
loraInt = false;
loraFlag = true;
}
LoRa.receive();
}
}
if (serialFlag) {
loraSendMessage(input);
serialFlag = false;
}
if (loraFlag) {
Serial.println(input);
loraFlag = false;
}
int del_1 = input.indexOf('*', 0);
String command = input.substring(0, del_1);
if (command == "SLEEP") {
sleep_all(50);
}
}
void print_wakeup_reason() {
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch (wakeup_reason)
{
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.println("Wakeup was not caused by deep sleep:" + String(wakeup_reason)); break;
}
}