// ======================= // WIFI // ======================= // ======================= // WIFI // ======================= #ifdef ESP8266 #include #include #include ESP8266WebServer server(80); #elif defined(ESP32) #include #include #include WebServer server(80); #endif // Your WiFi credentials. // Set password to "" for open networks. const char* ssid = "Livebox-37cc"; const char* pass = "8A6060920A8A86896F770F2C47"; IPAddress gateway(192, 168, 1, 1); IPAddress subnet(255, 255, 0, 0); IPAddress DNS(192, 168, 1, 1); //ESP8266WebServer server(80); //#include #include #include // ------------------------------ // LED // ------------------------------ const byte LED_PIN = 13; #define DEBUG TRUE //// ------------------------------ //// Energie //// ------------------------------ //#include "EmonLib.h" // Include Emon Library //EnergyMonitor emon1; // Create an instance int boucle = 0; // ---------------- // PZEM for arduino // ----------------- #include #ifdef ESP8266 #define PIN_PZEM_1 D5 #define PIN_PZEM_2 D6 PZEM004Tv30 pzem(PIN_PZEM_1, PIN_PZEM_2); #elif defined(ESP32) #include #define RXD2 16 #define TXD2 17 PZEM004Tv30 pzem(&Serial2); #endif // ---------------------------- // Interruption // ---------------------------- const byte interruptPin = 3; //volatile byte backlight_status = LOW; /******************************************************************/ #define SolaireProduction "1087" #define Consommation_Apparente "1123" #define CONSOMMATION_GENERALE "1115" double voltage; double current; double pf; double power; double energy; double frequency; void setup() { Serial.begin(115200); #ifdef ESP32 Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2); #endif Serial.println("Booting"); WiFi.mode(WIFI_STA); WiFi.begin(ssid, pass); while (WiFi.waitForConnectResult() != WL_CONNECTED) { Serial.println("Connection Failed! Rebooting..."); delay(5000); ESP.restart(); } // Définissez les gestionnaires pour les différentes URL server.on("/", HTTP_GET, handleRoot); server.on("/getData", HTTP_GET, handleData); // Démarrer le serveur server.begin(); Serial.println("Serveur Web démarré"); // Port defaults to 8266 ArduinoOTA.setPort(8266); // Hostname defaults to esp8266-[ChipID] // ArduinoOTA.setHostname("myesp8266"); // No authentication by default // ArduinoOTA.setPassword("admin"); // Password can be set with it's md5 value as well // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3 // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3"); ArduinoOTA.onStart([]() { String type; if (ArduinoOTA.getCommand() == U_FLASH) { type = "sketch"; } else { // U_FS type = "filesystem"; } // NOTE: if updating FS this would be the place to unmount FS using FS.end() Serial.println("Start updating " + type); }); ArduinoOTA.onEnd([]() { Serial.println("\nEnd"); }); ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { Serial.printf("Progress: %u%%\r", (progress / (total / 100))); }); ArduinoOTA.onError([](ota_error_t error) { Serial.printf("Error[%u]: ", error); if (error == OTA_AUTH_ERROR) { Serial.println("Auth Failed"); } else if (error == OTA_BEGIN_ERROR) { Serial.println("Begin Failed"); } else if (error == OTA_CONNECT_ERROR) { Serial.println("Connect Failed"); } else if (error == OTA_RECEIVE_ERROR) { Serial.println("Receive Failed"); } else if (error == OTA_END_ERROR) { Serial.println("End Failed"); } }); ArduinoOTA.begin(); Serial.println("Ready"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); // pinMode(LED_PIN, OUTPUT); // pinMode(interruptPin, INPUT); Serial.println("Serial communication & wifi"); // emon1.voltage(A1, 320 , 2.6); // Voltage: input pin, calibration, phase_shift // emon1.current(A2, 30); // Current: input pin, calibration. // delay(1000); Serial.println("-------PZEM-----------"); // Start Print Test to Line 2 pzem.resetEnergy(); delay(1000); //digitalWrite(interruptPin, LOW); } void loop() { ArduinoOTA.handle(); double Irms[2]; boucle ++; // Gérez les requêtes du serveur server.handleClient(); // emon1.calcVI(20, 200); // 1 Demande a Emonlib de tout calculer, (puissance relle, volts moyen, ampère moyen et facteur de puissance) // // Irms[0] = emon1.calcIrms(5440) * 230; //emon1.apparentPower); // Irms[0] = emon1.apparentPower; // float verif_voltage = emon1.Vrms; // 1 creation de la variable "volts moyen" (mesurable avec un voltmètre pour l'etalonnage) // float verif_ampere = emon1.Irms; // 1 creation de la variable "Ampères Moyen" (mesurable avec une pince ampèremétrique pour l'etalonnage)) // float Cos_phi = emon1.powerFactor; // // Serial.print(verif_voltage); // Serial.print(" V "); // Serial.print(verif_ampere); // Serial.print(" A "); // Serial.print(emon1.realPower); // Serial.print(" Wr "); // Serial.print(emon1.apparentPower); // Calculate Irms only // Serial.print(" Wcap "); // Serial.print(Irms[0]); // Calculate Irms only // Serial.print(" Wc "); if (boucle < 0) { Serial.println("Calibration :" + String(boucle)); delay (100); } else { // double value = pzemRead(); // // // ESPserial.print(getJson(String(SolaireProduction), value)); // delay(100); // Serial.print(value); // Calculate Irms only // Serial.print(" Wr "); // ESPserial.print(getJson(String(Consommation_Apparente), emon1.realPower)); // delay(500); // // ESPserial.print(getJson(String(CONSOMMATION_GENERALE), emon1.apparentPower)); // // if (boucle % 2 == 0) { // Serial.println("Cso:" + String(emon1.apparentPower, 0) + " R " + String(emon1.realPower, 0)); // Serial.println("Sol:" + String(value, 0) + " V " + String(emon1.Vrms, 1)); // } // delay (2000); if (boucle > 1000) { boucle = 11; } } Serial.println(); } double pzemRead() { voltage = pzem.voltage(); current = pzem.current(); pf = pzem.pf(); power = pzem.power(); energy = pzem.energy(); frequency = pzem.frequency(); if ( !isnan(voltage) ) { Serial.print("Voltage: "); Serial.print(voltage); Serial.println("V"); //Serial.println(String(voltage,1) + " V "); } else { Serial.println("Error reading voltage"); } if ( !isnan(current) ) { Serial.print("Current: "); Serial.print(current); Serial.println("A"); // Serial.println(String(current,1) + "A "); } else { Serial.println("Error reading current"); } if (boucle % 2 == 1) { if ( !isnan(pf) ) { //Serial.print("PF: "); Serial.println(pf); Serial.println(String(pf, 3) + "pf "); if (pf != 0) { Serial.println(String(power / pf, 0) + "Wa"); } } else { //Serial.println("Error reading power factor"); } if ( !isnan(power) ) { //Serial.print("Power: "); Serial.print(power); Serial.println("W"); if (pf > 0) { Serial.println("+" + String(power, 1) + "W "); } else { Serial.println("-" + String(power, 1) + "Wa"); } } else { //Serial.println("Error reading power"); } if ( !isnan(energy) ) { if (energy < 1000) { Serial.println(String(energy * 1000, 0) + "Wh "); } else { Serial.println(String(energy, 1) + "kWh "); } Serial.print("Energy: "); Serial.print(energy, 3); Serial.println("kWh"); } else { Serial.println("Error reading energy"); } if ( !isnan(frequency) ) { Serial.print("Frequency: "); Serial.print(frequency, 1); Serial.println("Hz"); } else { Serial.println("Error reading frequency"); } } return power; } // Méthode pour gérer la requête '/getData' void handleData() { pzemRead(); // Créer un objet JSON DynamicJsonDocument doc(200); // Remplir l'objet JSON avec les données PZEM doc["voltage"] = voltage; doc["current"] = current; doc["pf"] = pf; doc["power"] = power; doc["energy"] = energy; doc["frequency"] = frequency; // Convertir l'objet JSON en chaîne String jsonData; serializeJson(doc, jsonData); // Envoyer la réponse JSON au client server.send(200, "application/json", jsonData); } void handleGetData() { pzemRead(); // Créer un objet JSON DynamicJsonDocument doc(200); // Remplir l'objet JSON avec les données PZEM doc["voltage"] = voltage; doc["current"] = current; doc["pf"] = pf; doc["power"] = power; doc["energy"] = energy; doc["frequency"] = frequency; // Convertir l'objet JSON en chaîne String jsonData; serializeJson(doc, jsonData); } void handleRoot() { // Générer la page HTML avec CSS String htmlCode = R"( Informations PZEM

Informations du PZEM

Mesure Valeur
Tension (V) {{voltage}}
Courant (A) {{current}}
Facteur de puissance {{pf}}
Puissance (W) {{power}}
Énergie (Wh) {{energy}}
Fréquence (Hz) {{frequency}}
)"; // Remplacement des balises par les valeurs actuelles htmlCode.replace("{{voltage}}", String(voltage)); htmlCode.replace("{{current}}", String(current)); htmlCode.replace("{{pf}}", String(pf)); htmlCode.replace("{{power}}", String(power)); htmlCode.replace("{{energy}}", String(energy)); htmlCode.replace("{{frequency}}", String(frequency)); // Envoyer la page HTML au client server.send(200, "text/html", htmlCode); }