#include <WiFi.h>
#include <HTTPClient.h>
// ---------------- WiFi Configuration ----------------
const char* wifiSsid = "Wokwi-GUEST";
const char* wifiPassword = "";
// ---------------- Server Configuration ----------------
const char* baseApiUrl = "https://energymeter.pythonanywhere.com/addlog";
const char* deviceId = "dev123";
// ---------------- ADC Pins ----------------
const int voltagePotPin = 34;
const int currentPotPin = 35;
const int powerPotPin = 32;
const int energyPotPin = 33;
// ---------------- Button Pin ----------------
const int uploadButtonPin = 25; // LOW = upload
// ---------------- Function Declarations ----------------
float readScaledValue(int pin, float minValue, float maxValue);
void connectToWiFi();
void uploadEnergyData(float voltage, float current, float power, float energy);
void printValuesToSerial(float voltage, float current, float power, float energy);
// ---------------- Setup ----------------
void setup() {
Serial.begin(115200);
pinMode(voltagePotPin, INPUT);
pinMode(currentPotPin, INPUT);
pinMode(powerPotPin, INPUT);
pinMode(energyPotPin, INPUT);
pinMode(uploadButtonPin, INPUT_PULLUP);
connectToWiFi();
}
// ---------------- Loop ----------------
void loop() {
float voltage = readScaledValue(voltagePotPin, 0.0, 260.0);
float current = readScaledValue(currentPotPin, 0.0, 3.0);
float power = readScaledValue(powerPotPin, 0.0, 1000.0);
float energy = readScaledValue(energyPotPin, 0.0, 100.0);
if (digitalRead(uploadButtonPin) == LOW) {
uploadEnergyData(voltage, current, power, energy);
delay(10000); // wait 10 sec after upload
} else {
printValuesToSerial(voltage, current, power, energy);
delay(500);
}
}
// ------------------------------------------------------
// Reads ADC value and scales it to required range
// ------------------------------------------------------
float readScaledValue(int pin, float minValue, float maxValue) {
int rawValue = analogRead(pin);
if (rawValue < 0) {
Serial.println("ADC read error");
return minValue;
}
return ((float)rawValue / 4095.0) * (maxValue - minValue) + minValue;
}
// ------------------------------------------------------
// Connects ESP32 to WiFi
// ------------------------------------------------------
void connectToWiFi() {
WiFi.begin(wifiSsid, wifiPassword);
Serial.print("Connecting to WiFi");
int retryCount = 0;
while (WiFi.status() != WL_CONNECTED && retryCount < 20) {
delay(500);
Serial.print(".");
retryCount++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("\nWiFi Connected");
} else {
Serial.println("\nWiFi Connection Failed");
}
}
// ------------------------------------------------------
// Uploads data using HTTP GET
// ------------------------------------------------------
void uploadEnergyData(float voltage, float current, float power, float energy) {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi not connected, upload skipped");
return;
}
HTTPClient http;
String fullUrl = String(baseApiUrl) +
"?deviceid=" + deviceId +
"&voltage=" + String(voltage, 2) +
"¤t=" + String(current, 2) +
"&power=" + String(power, 2) +
"&energy=" + String(energy, 2);
Serial.println("Uploading Data:");
Serial.println(fullUrl);
http.begin(fullUrl);
int httpCode = http.GET();
if (httpCode > 0) {
Serial.println("Server Response: " + http.getString());
} else {
Serial.print("HTTP Error: ");
Serial.println(http.errorToString(httpCode));
}
http.end();
}
// ------------------------------------------------------
// Prints values to Serial Monitor
// ------------------------------------------------------
void printValuesToSerial(float voltage, float current, float power, float energy) {
Serial.print("Voltage: "); Serial.print(voltage, 2); Serial.print(" V | ");
Serial.print("Current: "); Serial.print(current, 2); Serial.print(" A | ");
Serial.print("Power: "); Serial.print(power, 2); Serial.print(" W | ");
Serial.print("Energy: "); Serial.print(energy, 2); Serial.println(" units");
}