Hello, I need help to modify the code so that I can control two relays using the button on the website, which is in the code sent below, and the on or off status was also shown. Is there any kind soul out there who can help me? here is the code
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <PZEM004Tv30.h>
IPAddress staticIP(192, 168, 1, 33); // nastavení statické IP adresy
IPAddress gateway(192, 168, 1, 1); // adresa brány
IPAddress subnet(255, 255, 255, 0); // maska podsítě
//IPAddress dns(1, 2, 3, 4); // DNS primární
// WiFi settings
const char* ssid = "Doma";
const char* password = "doma123";
// Create an instance of the web server
ESP8266WebServer server(80);
// Energy calculation settings
const float unitPrice = 5.0; // Cena za jednotku energie (za kWh)
const int bufferLength = 5; // Délka pole s cyklickým bufferem pro uchovávání posledních hodnot energie
float energyValues[5] = {0}; // Pole s cyklickým bufferem pro uchovávání posledních hodnot energie
int bufferIndex = 0; // Index pro cyklický buffer
/* Use software serial for the PZEM
- Pin 5 Rx (Connects to the Tx pin on the PZEM)
- Pin 4 Tx (Connects to the Rx pin on the PZEM)
*/
PZEM004Tv30 pzem(5, 4);
// Timer for updating PZEM values
unsigned long lastUpdate = 0;
const unsigned long updateInterval = 3000; // 3 seconds
// Variables for total energy and cost calculation
float lastEnergy = 0;
float totalEnergy = 0;
float energyCost = 0;
void handleRoot() {
// Get the measurement values from the PZEM
float voltage = pzem.voltage();
float current = pzem.current();
float power = pzem.power();
float energy = pzem.energy();
float frequency = pzem.frequency();
//float totalCost = 0.0; // deklarace a inicializace proměnné pro celkovou cenu
// Calculate the total energy and cost
totalEnergy += (energy - lastEnergy) / 10.0; // Convert from Wh to kWh
energyCost = totalEnergy * 5.02; // 5.02 Kč/kWh
// Save the current energy for the next calculation
lastEnergy = energy;
float totalEnergy = 0;
for (int i = 0; i < bufferLength; i++) {
totalEnergy += energyValues;
}
float totalCost = totalEnergy * unitPrice;
String cas = String(millis() / 60000);
String html = "";
html += "";
html += "DIMNET.CZ - SMART HOME";
html += "";
html += "";
html += "
Čas od spuštění měření je ";
html += cas;
html += " minut.
";
html += "";
// Send the HTML content to the client
server.send(200, "text/html", html);
}
void handleUpdate() {
// Get the measurement values from the PZEM
float voltage = pzem.voltage();
float current = pzem.current();
float power = pzem.power();
float energy = pzem.energy();
float frequency = pzem.frequency();
// Calculate total energy and cost
float totalEnergy = (energy / 10.0);
float totalCost = (totalEnergy * 5.02);
// Create a JSON object with the measurement values
String json = "{"voltage": " + String(voltage) + ", "current": " + String(current) + ", "power": " + String(power) + ", "energy": " + String(energy) + ", "frequency": " + String(frequency) + ", "totalEnergy": " + String(totalEnergy, 3) + ", "totalCost": " + String(totalCost, 2) + "}";
// Send the JSON object to the client
server.send(200, "application/json", json);
}
void setup() {
// Start the serial communication
Serial.begin(115200);
// Connect to the WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
// Nastavení statické IP adresy
WiFi.config(staticIP, gateway, subnet);
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
Serial.println("Connected to WiFi");
// Start the web server
server.on("/", handleRoot);
server.on("/update", handleUpdate);
server.begin();
Serial.println("Web server started");
// Set the PZEM update interval
lastUpdate = millis();
}
void loop() {
server.handleClient();
// Check if it's time to update the PZEM values
if (millis() - lastUpdate >= updateInterval) {
// Update the PZEM values
float voltage = pzem.voltage();
float current = pzem.current();
float power = pzem.power();
float energy = pzem.energy();
float frequency = pzem.frequency();
// Store current energy value in buffer
energyValues[bufferIndex] = energy;
bufferIndex = (bufferIndex + 1) % bufferLength; // Increment buffer index with wrap-around
// Calculate total energy and cost
float totalEnergy = (energy / 10.0);
float totalCost = (totalEnergy * 5.02);
// Update the last update time
lastUpdate = millis();
}
}