#define BLYNK_TEMPLATE_ID "TMPL3CBjPyEa5"
#define BLYNK_TEMPLATE_NAME "Battery status and Temp monitor and SMS alert"
#define BLYNK_AUTH_TOKEN "D6YDE5iXtmQNZ3NFwsDrj745KajUwVmD"
#define BLYNK_PRINT Serial
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
// Blynk Auth Token
char auth[] = BLYNK_AUTH_TOKEN; // Blynk authentication token
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
// Create an OLED display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// LCD I2C library:
#include <LiquidCrystal_I2C.h>
// DHT22 sensor library:
#include <DHT.h>;
// LCD I2C address 0x27, 16 column and 2 rows!
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Constants:
#define DHTPIN 4 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor for normal 16mhz Esp32
// Variables:
float H; // Humidity value
float T; // Temperature value
int buzzer = 5;
BlynkTimer timer;
// Notify battery status
void notifyBatterystatus() {
int Batterystatus = analogRead(34);
Serial.println(Batterystatus);
if (Batterystatus <= 900 ) {
Serial.println("low battery");
Blynk.logEvent("low_battery", "low battery, so quickly turn on the power supply");
} else if (Batterystatus >= 3700 ) {
Serial.println("full battery");
Blynk.logEvent("full_battery", "battery is full, so quickly turn off the power supply");
}
}
void setup() {
// Initialize Blynk
Blynk.begin(auth, ssid, pass);
lcd.init();
lcd.backlight();
dht.begin();
pinMode(buzzer, OUTPUT);
Serial.begin(9600);
Serial.println("DHT22 sensor with Esp32!");
pinMode(14, OUTPUT);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
// Initialize OLED display with I2C address 0x3C
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("Failed to start SSD1306 OLED"));
while (1);
}
delay(2000); // Wait two seconds for initializing
oled.clearDisplay(); // Clear display
oled.setTextSize(2); // Set text size
oled.setTextColor(WHITE); // Set text color
oled.setCursor(15, 30); // Set position to display (x,y)
oled.println("Hii !!"); // Set text
oled.display(); // Display on OLED
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
delay(2000); // Wait 2 seconds for loading screen
oled.clearDisplay(); // Clear display
oled.setTextSize(2);
oled.setCursor(15, 30); // Set position to display (x,y)
oled.println("Loading..");
oled.display(); // Display on OLED
// Setup Blynk timer to check battery status every 10 seconds
timer.setInterval(10000L, notifyBatterystatus);
}
void loop() {
Blynk.run();
timer.run();
H = dht.readHumidity();
T = dht.readTemperature();
Serial.print("Humidity: ");
Serial.print(H);
Serial.println(" %; ");
Serial.print("Temperature: ");
Serial.print(T);
Serial.println(" Celsius.\n");
if (H >= 70.00 && T >= 30.00) {
digitalWrite(14, HIGH);
digitalWrite(13, LOW);
digitalWrite(12, LOW);
lcd.setCursor(0, 0);
lcd.println(" Too warm! ");
lcd.setCursor(0, 1);
lcd.println(" Cool down! ");
digitalWrite(buzzer, 1);
tone(buzzer, 900, 100);
delay(400);
digitalWrite(buzzer, 0);
tone(buzzer, 900, 100);
delay(400);
} else {
digitalWrite(14, LOW);
digitalWrite(13, LOW);
digitalWrite(12, HIGH);
lcd.setCursor(0, 0);
lcd.println("Temp. & hum. are");
lcd.setCursor(0, 1);
lcd.println("in normal limits");
digitalWrite(buzzer, 0);
}
if (H < 70.00 && T >= 30.00) {
digitalWrite(14, LOW);
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
lcd.setCursor(0, 0);
lcd.println("Be ware! ");
lcd.setCursor(0, 1);
lcd.println("Temp. too high! ");
digitalWrite(buzzer, 1);
tone(buzzer, 400, 400);
delay(400);
}
if (H >= 70.00 && T < 30.00) {
digitalWrite(14, LOW);
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
lcd.setCursor(0, 0);
lcd.println("Be ware! ");
lcd.setCursor(0, 1);
lcd.println("Hum. too high! ");
digitalWrite(buzzer, 1);
tone(buzzer, 400, 400);
delay(400);
}
int batteryLevel = analogRead(34); // Connect your battery sensor to 34
int batteryPercent = map(batteryLevel, 0, 4095, 0, 100); // Corrected mapping
oled.clearDisplay();
oled.setTextSize(2);
oled.setTextColor(SSD1306_WHITE);
oled.setCursor(0, 0);
oled.print("Battery ");
oled.setCursor(10, 17);
oled.print("Level:");
oled.setTextSize(2.5);
oled.setCursor(40, 40);
oled.print(batteryPercent);
oled.print("%");
oled.display();
delay(1000); // You can adjust the delay according to your needs
Blynk.virtualWrite(V3, T);
Blynk.virtualWrite(V1, H);
Blynk.virtualWrite(V2, batteryPercent);
}