#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h> // Include the WiFi library for ESP32
#include <HTTPClient.h> // Include the HTTPClient library for HTTP requests
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 columns and 2 rows
const int batteryPin = 34; // Connect the battery to pin 34 (analog pin)
const int tempPin = 35; // Connect the temperature sensor to pin 35 (analog pin)
// Blynk credentials
#define BLYNK_TEMPLATE_ID "TMPL34IoYx_BG"
#define BLYNK_TEMPLATE_NAME "EV Sample1"
#define BLYNK_AUTH_TOKEN "iiY7TaV4ikjwwUV8-lUqK9J3XNj8h0Z8"
char ssid[] = "Wokwi-GUEST"; // Enter the Wokwi Guest WiFi SSID
char pass[] = ""; // Wokwi Guest network does not require a password
const char* blynkServer = "http://blynk.cloud"; // Blynk cloud server
void setup() {
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on backlight
lcd.clear(); // Clear the screen
Serial.begin(9600); // Initialize serial communication
delay(1000); // Wait for Serial Monitor to initialize
Serial.println("Initializing...");
// Connect to WiFi
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to WiFi");
lcd.setCursor(0, 0);
lcd.print("WiFi connected!");
delay(2000); // Display the message for 2 seconds
lcd.clear();
}
void loop() {
float voltage = readBatteryVoltage(); // Read the battery voltage
int batteryPercentage = calculateBatteryPercentage(voltage); // Calculate battery percentage
float temperature = readTemperature(); // Read the temperature
// Print readings to LCD
lcd.setCursor(0, 0);
lcd.print("Battery: ");
lcd.print(batteryPercentage);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print("C");
// Print readings to Serial Monitor
Serial.print("Battery Percentage: ");
Serial.print(batteryPercentage);
Serial.print("% | Temperature: ");
Serial.print(temperature);
Serial.println("C");
// Send data to Blynk using HTTP
sendToBlynk(batteryPercentage, temperature);
delay(1000); // Wait for 1 second before taking next readings
}
void sendToBlynk(int batteryPercentage, float temperature) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Construct the Blynk API URLs
String url1 = String(blynkServer) + "/auth/" + BLYNK_AUTH_TOKEN + "/update/V1?value=" + String(batteryPercentage);
String url2 = String(blynkServer) + "/auth/" + BLYNK_AUTH_TOKEN + "/update/V2?value=" + String(temperature);
// Send the battery percentage to Blynk
http.begin(url1.c_str());
int httpResponseCode1 = http.GET();
if (httpResponseCode1 > 0) {
Serial.println("Battery percentage sent to Blynk");
} else {
Serial.print("Error sending battery percentage: ");
Serial.println(httpResponseCode1);
}
http.end();
// Send the temperature to Blynk
http.begin(url2.c_str());
int httpResponseCode2 = http.GET();
if (httpResponseCode2 > 0) {
Serial.println("Temperature sent to Blynk");
} else {
Serial.print("Error sending temperature: ");
Serial.println(httpResponseCode2);
}
http.end();
} else {
Serial.println("WiFi not connected");
}
}
float readBatteryVoltage() {
// Simulate battery voltage reading
float voltage = random(36, 42) / 10.0; // Simulate voltage between 3.6V and 4.2V
return voltage;
}
int calculateBatteryPercentage(float voltage) {
float minVoltage = 3.6; // Minimum voltage of the battery
float maxVoltage = 4.2; // Maximum voltage of the battery
// Calculate battery percentage based on the voltage
int batteryPercentage = map(voltage * 100, minVoltage * 100, maxVoltage * 100, 0, 100); // map function needs integer values, hence multiplied by 100
batteryPercentage = constrain(batteryPercentage, 0, 100); // Limit the percentage to 0-100
return batteryPercentage;
}
float readTemperature() {
// Simulate temperature reading
float temperature = random(200, 400) / 10.0; // Simulate temperature between 20°C and 40°C
return temperature;
}