#define uS_TO_S_FACTOR 1000000ULL
#define TIME_TO_SLEEP 300
#define BLYNK_PRINT Serial
#define maxRecords 1
#define LDRPIN 15
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = "";
char ssid[] = "";
char pass[] = "";
RTC_DATA_ATTR int last_ldrval = digitalRead(15);
RTC_DATA_ATTR int bootCount = 0;
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.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
}
}
void setup()
{
setCpuFrequencyMhz(80);
Serial.begin(9600);
delay(10);
++bootCount;
Serial.println("Boot number: " + String(bootCount));
print_wakeup_reason();
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) + " Seconds");
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
int wifit_ctr = 0;
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Blynk.begin(auth, ssid, pass);
Blynk.run();
//read input on digital pin 15:
int ldrval = digitalRead(15); //LDR
Serial.print("LDR: "); Serial.println(ldrval);
Serial.print("LDR_1: "); Serial.println(last_ldrval);
if (ldrval != last_ldrval)
{
Blynk.notify("Alert Power!");
}
last_ldrval = ldrval;
delay(500);
Serial.println("Going to sleep now");
Serial.flush();
esp_deep_sleep_start();
}
void loop(){
//This is not going to be called
}
IoT Fire Alarm with ESP32
A dummies guide to making a cheap DIY but useful fire alarm under 6 USD. We use the popular development board ESP32 Dev as our microcontroller for this project. I like this board since it is versatile and cheap. With onboard WiFi and Bluetooth, it makes IoT projects a breeze.
Write a comment ...