ESP8266 a PZEM-004Tv3

Odpovědět
dimzone
Příspěvky: 18
Registrován: 23 bře 2020, 07:11
Reputation: 0

ESP8266 a PZEM-004Tv3

Příspěvek od dimzone » 12 kvě 2023, 13:22

Ahoj potřebuji pomoct upravit kód abych měl možnost ovládat dvě relé pomocí tlačítka na webové stránce, která je v níže zaslaném kódu a ukazoval se i stav zapnutí nebo vypnutí. Je tu nějaká dobrá duše co mi pomůže? zde je kod
-------------------------------------------------------------------------------------------------------

#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 = "<!DOCTYPE html><html lang='cs'>";
html += "<head><meta charset='UTF-8'><meta name='viewport' content='width=device-width, initial-scale=1.0'>";
html += "<title>DIMNET.CZ - SMART HOME</title>";
html += "<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstr ... ap.min.css'>";
html += "<script src='https://ajax.googleapis.com/ajax/libs/j ... '></script>";
html += "<script src='https://cdnjs.cloudflare.com/ajax/libs/ ... '></script>";
html += "<script src='https://maxcdn.bootstrapcdn.com/bootstr ... '></script>";
html += "<style>";
html += "body {font-family: 'HP Simplified Light', sans-serif; text-align: center;}";
html += "table {margin: 0 auto; border-collapse: collapse; width: 100%; box-shadow: 0 0 20px rgba(0,0,0,.15);}";
html += "th, td {padding: 10px; border: 1px solid #ddd; text-align: center;}";
html += "th {background-color: #4c61af; color: white;}";
html += "@media screen and (max-width: 600px) {table {font-size: 14px;}}";
html += "</style>";
html += "</head>";
html += "<body>";
html += "<div class='container-fluid'><h1>DIMNET.CZ - Monitorování spotřeby</h1>";
html += "<div class='table-responsive'><table class='table'>";
html += "<thead class='thead-dark'><tr><th scope='col'>Název</th><th scope='col'>Hodnota</th><th scope='col'>Jednotka</th></tr></thead>";
html += "<tbody>";
html += "<tr><td>Napětí</td><td id='voltage'>" + String(voltage) + "</td><td>V</td></tr>";
html += "<tr><td>Proud</td><td id='current'>" + String(current) + "</td><td>mA/A</td></tr>";
html += "<tr><td>Frekvence</td><td id='frequency'>" + String(pzem.frequency(), 1) + "</td><td>Hz</td></tr>";
html += "<tr><td>Výkon</td><td id='power'>" + String(power) + "</td><td>W</td></tr>";
//html += "<tr><td>Energie (Wh)</td><td id='energy'>" + String(energy) + " Wh</td><td></td></tr>";
html += "<tr><td>Celková spotřeba</td><td><span id='totalEnergy' style='color: red; font-weight: bold;'>" + String(totalEnergy, 3) + "</span></td><td>kWh</td></tr>";
html += "<tr><td>Celková cena spotřeby</td><td><span id='totalCost' style='color: green; font-weight: bold;'>" + String(totalCost, 2) + "</span></td><td>Kč</td></tr>";
html += "</tbody></table></div>";
html += "<script>setInterval(updateValues,5000); function updateValues() { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { var data = JSON.parse(this.responseText); document.getElementById('voltage').innerHTML = data.voltage; document.getElementById('current').innerHTML = data.current; document.getElementById('power').innerHTML = data.power; document.getElementById('energy').innerHTML = data.energy; document.getElementById('frequency').innerHTML = data.frequency; document.getElementById('totalEnergy, 3').innerHTML = data.totalEnergy; document.getElementById('totalCost, 2').innerHTML = data.totalCost; } }; xhr.open('GET', '/update', true); xhr.send(); }</script>";
html += "<br>Čas od spuštění měření je ";
html += cas;
html += " minut.<br><br>";
html += "</body></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();
}
}

Odpovědět

Kdo je online

Uživatelé prohlížející si toto fórum: Žádní registrovaní uživatelé a 11 hostů