#define BLYNK_TEMPLATE_ID "TMPL6mW7nWxRf"
#define BLYNK_TEMPLATE_NAME "Water Level2"
#define BLYNK_AUTH_TOKEN "XM4z4sg4DQVo77uDLnSgNLrY1VeMhFbR"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <HTTPClient.h> // For sending data to ThingSpeak
// Initialize LCD with I2C address, columns, and rows
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adjust the I2C address if needed
// Ultrasonic sensor pins for ESP32
const int trigPin = 18; // GPIO pin for trigger (adjust as per wiring)
const int echoPin = 19; // GPIO pin for echo (adjust as per wiring)
// Battery voltage pin and settings
const int batteryPin = 34; // Analog pin connected to potentiometer wiper
const float maxBatteryVoltage = 5.0; // Maximum expected battery voltage
const float referenceVoltage = 3.3; // ESP32 reference voltage
const int adcResolution = 4095; // ADC resolution for ESP32
const float lowBatteryThreshold = 4.5; // Voltage threshold for low battery warning
// Tank dimensions (adjust based on your tank's actual height)
const float tankHeight = 500.0; // Height of the tank in cm
// WiFi and Blynk credentials
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
char auth[] = BLYNK_AUTH_TOKEN; // Use defined Blynk Auth Token
// ThingSpeak credentials
const char* thingSpeakAPIKey = "0B18ROG1LWRCMEXY"; // Replace with your ThingSpeak API key
const char* server = "http://api.thingspeak.com/update";
float readBatteryVoltage() {
int rawReading = analogRead(batteryPin);
float voltageRatio = maxBatteryVoltage / referenceVoltage;
float batteryVoltage = (rawReading / (float)adcResolution) * referenceVoltage * voltageRatio;
return batteryVoltage;
}
void setup() {
lcd.init(); // Initialize LCD with I2C
lcd.backlight(); // Turn on the backlight
lcd.setCursor(0, 0);
lcd.print("Tank Level:");
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(115200); // Higher baud rate for ESP32
// Connect to WiFi
Serial.print("Connecting to WiFi");
WiFi.begin(ssid, pass);
unsigned long startAttemptTime = millis();
while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < 15000) {
delay(500);
Serial.print(".");
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("\nConnected to WiFi");
} else {
Serial.println("\nFailed to connect to WiFi");
}
Blynk.config(auth);
if (Blynk.connect()) {
Serial.println("Connected to Blynk");
} else {
Serial.println("Failed to connect to Blynk");
}
}
void loop() {
Blynk.run();
float batteryVoltage = readBatteryVoltage();
Serial.print("Battery Voltage: ");
Serial.print(batteryVoltage, 2);
Serial.println(" V");
// Always send battery level to Blynk
if (batteryVoltage <= lowBatteryThreshold) {
Blynk.virtualWrite(V3, String("Low Battery: ") + String(batteryVoltage, 2) + "V");
} else {
Blynk.virtualWrite(V3, String("Battery Voltage: ") + String(batteryVoltage, 2) + "V");
}
lcd.clear();
if (batteryVoltage <= lowBatteryThreshold) {
lcd.setCursor(0, 0);
lcd.print("LOW BATTERY");
lcd.setCursor(0, 1);
lcd.print("Voltage: ");
lcd.print(batteryVoltage, 2);
lcd.print("V");
delay(5000); // Display LOW BATTERY warning for 5 seconds
} else {
lcd.setCursor(0, 0);
lcd.print("Battery Voltage:");
lcd.setCursor(0, 1);
lcd.print(batteryVoltage, 2);
lcd.print("V");
delay(5000);
}
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
float distance = (duration * 0.0343) / 2; // Convert time to distance in cm
float waterLevel = tankHeight - distance;
if (waterLevel < 0) waterLevel = 0;
if (waterLevel > tankHeight) waterLevel = tankHeight;
float percentage = (waterLevel / tankHeight) * 100;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
Serial.print("Water Level: ");
Serial.print(waterLevel);
Serial.println(" cm");
Serial.print("Percentage: ");
Serial.print(percentage);
Serial.println("%");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Tank Level:");
lcd.setCursor(0, 1);
lcd.print("Level: ");
lcd.print(percentage, 1);
lcd.print("% ");
Blynk.virtualWrite(V2, percentage); // Send percentage to Virtual Pin 2
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = String(server) + "?api_key=" + thingSpeakAPIKey + "&field1=" + String(percentage);
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.print("ThingSpeak Response Code: ");
Serial.println(httpResponseCode);
} else {
Serial.print("Error sending to ThingSpeak: ");
Serial.println(http.errorToString(httpResponseCode).c_str());
}
http.end();
}
delay(15000);
}