0% found this document useful (0 votes)
29 views6 pages

Updated Uabid

Uploaded by

hafeezusman286
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views6 pages

Updated Uabid

Uploaded by

hafeezusman286
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include <Arduino.

h>
#include "RTClib.h"
#include <ZMPT101B.h>
#include <Robojax_AllegroACS_Current_Sensor.h>
#include <SD.h>
#include <SPI.h>

// RTC
RTC_DS1307 rtc;
DateTime now;
char daysOfTheWeek[7][12] = { "Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday" };

// Voltage Sensor
#define SENSITIVITY 500.0f
ZMPT101B voltageSensor(A6, 50.0);

// SD Card
int CS_PIN = 53;
File myFile;
char data;

// Current Sensor
const int VIN = A3;
const int MODEL = 3;
Robojax_AllegroACS_Current_Sensor robojax(MODEL, VIN);

// Power & Energy


float power = 0.0f;
float energy = 0.0f;
float current;
float voltage;
unsigned long lastTime = 0;

void setup() {
Serial.begin(9600); // Serial Monitor
Serial1.begin(115200); // Serial communication with ESP32

// SD Card Initialization
Serial.println("Initializing SD card...");
pinMode(CS_PIN, OUTPUT);
if (SD.begin(CS_PIN)) {
Serial.println("SD card is ready to use.");
} else {
Serial.println("SD card initialization failed");
return;
}

// RTC Initialization
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1) delay(10);
}
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}

// Voltage Sensor Initialization


voltageSensor.setSensitivity(SENSITIVITY);
}

void loop() {
DateTime currentDateTime = rtc.now();
updateVoltage();
updateCurrent();
calculatePower();
calculateEnergy();
sendDataToESP32(currentDateTime);
writeData(currentDateTime);
delay(1000); // Delay to avoid flooding the serial communication
}

void updateVoltage() {
voltage = voltageSensor.getRmsVoltage();
Serial.print("Voltage: ");
Serial.println(voltage); // Print the voltage for debugging
}

void updateCurrent() {
current = robojax.getCurrentAverage(300);
Serial.print("Current: ");
Serial.println(current); // Print the current for debugging
}

void calculatePower() {
power = voltage * current;
Serial.print("Power: ");
Serial.println(power); // Print the power for debugging
}

void calculateEnergy() {
unsigned long currentTime = millis();
unsigned long timeDiff = currentTime - lastTime;
energy += (power * timeDiff) / (3600.0 * 1000.0); // Convert milliseconds to
hours and accumulate energy
lastTime = currentTime;
Serial.print("Energy: ");
Serial.println(energy); // Print the energy for debugging
}

void sendDataToESP32(DateTime now) {


String dataToSend = String(now.year()) + "/" + String(now.month()) + "/" +
String(now.day()) + " " +
String(daysOfTheWeek[now.dayOfTheWeek()]) + " " +
String(now.hour()) + ":" + String(now.minute()) + ":" +
String(now.second()) + " " +
"Voltage: " + String(voltage) + "V " +
"Current: " + String(current, 2) + "A " +
"Power: " + String(power) + "W " +
"Energy: " + String(energy) + "Wh";

Serial1.println(dataToSend);
}

void writeData(DateTime now) {


Serial.print("Date and Time: ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();

Serial.print("Voltage: ");
Serial.print(voltage, 2);
Serial.println(" V");

Serial.print("Current: ");
Serial.print(current, 2);
Serial.println(" A");

Serial.print("Power: ");
Serial.print(power, 2);
Serial.println(" W");

Serial.print("Energy: ");
Serial.print(energy, 2);
Serial.println(" Wh");
Serial.println();

myFile = SD.open("test.txt", FILE_WRITE);


if (myFile) {
myFile.print("D:");
myFile.print(now.year(), DEC);
myFile.print('-');
myFile.print(now.month(), DEC);
myFile.print('-');
myFile.print(now.day(), DEC);
myFile.print(" ");
myFile.print("T:");
myFile.print(now.hour(), DEC);
myFile.print(':');
myFile.print(now.minute(), DEC);
myFile.print(':');
myFile.print(now.second(), DEC);
myFile.print(" ");
myFile.print("V:");
myFile.print(voltage, 2);
myFile.print("V ");
myFile.print("I:");
myFile.print(current, 2);
myFile.print("A ");
myFile.print("P:");
myFile.print(power, 2);
myFile.print("W ");
myFile.print("E:");
myFile.print(energy, 2);
myFile.println("Wh");
myFile.close();
} else {
Serial.println("Error opening test.txt");
}
}

ESP32

void setup() {
Serial.begin(115200); // Initialize serial monitor
Serial2.begin(115200, SERIAL_8N1, 16, 17); // Initialize UART2 (TX2=17,
RX2RX2=16)
}

void loop() {
if (Serial2.available()) {
String receivedData = Serial2.readStringUntil('\n');
Serial.println(receivedData);
}
}

Webpage

#include <WiFi.h>
#include <WebServer.h>

// WiFi credentials
const char* ssid = "Talha_desktop";
const char* password = "Bitcoin00";

// Create a web server on port 80


WebServer server(80);

String receivedData = ""; // Variable to store received data

void setup() {
Serial.begin(115200); // Initialize serial monitor
Serial2.begin(115200, SERIAL_8N1, 16, 17); // Initialize UART2 (TX2=17, RX2=16)

// Connect to Wi-Fi
Serial.println();
Serial.println("Connecting to Wi-Fi");
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {


delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("Wi-Fi connected");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());

// Define route for root


server.on("/", handleRoot);

// Define route for fetching data


server.on("/data", handleData);

// Start the server


server.begin();
Serial.println("Server started");
}

void loop() {
// Check if data is available from the Arduino
if (Serial2.available()) {
receivedData = Serial2.readStringUntil('\n'); // Read the incoming data
Serial.println(receivedData); // Print the received data to the serial monitor
}

// Handle client requests


server.handleClient();
}

// Handle the root URL (/)


void handleRoot() {
String html = "<!DOCTYPE html><html>";
html += "<head><title>IoT Smart Energy Meter</title>";
html += "<style>";
html += "body {";
html += " background-image:
url('https://www.logos3pl.com/wp-content/uploads/2023/08/the-revolutionizing-
impact-of-internet-of-things-iot-in-supply-chain-management-logos-logistics-
1024x512.jpg');";
html += " background-size: cover;";
html += " color: white;";
html += " font-family: Arial, sans-serif;";
html += "}";
html += "pre {";
html += " background: rgba(0, 0, 0, 0.5);";
html += " padding: 10px;";
html += " border-radius: 5px;";
html += " font-size: 18px;"; // Increase font size
html += "}";
html += "</style>";
html += "<script>";
html += "function fetchData() {";
html += " var xhr = new XMLHttpRequest();";
html += " xhr.onreadystatechange = function() {";
html += " if (xhr.readyState == 4 && xhr.status == 200) {";
html += " document.getElementById('data').innerText = xhr.responseText;";
html += " }";
html += " };";
html += " xhr.open('GET', '/data', true);";
html += " xhr.send();";
html += "}";
html += "setInterval(fetchData, 1000);"; // Fetch data every second
html += "</script>";
html += "</head><body><h1>ESP32 Web Server</h1>";
html += "<p>Received Data:</p>";
html += "<pre id='data'>" + receivedData + "</pre>";
html += "</body></html>";

server.send(200, "text/html", html);


}

// Handle the data URL (/data)


void handleData() {
server.send(200, "text/plain", receivedData);
}

You might also like