first commit

This commit is contained in:
Jérôme Delacotte
2025-03-06 11:15:32 +01:00
commit 7b30d6e298
5276 changed files with 2108927 additions and 0 deletions

View File

@@ -0,0 +1,176 @@
/*
PZEM004T vers Domoticz GET
*/
#include <ESP8266WiFi.h>
#include <SoftwareSerial.h>
#define SLEEP_DELAY_IN_SECONDS 30 //interval envoi des données
#include "math.h"
const int analogIn = A0;
int mVperAmp = 185; // 185 pour 5A, use 100 for 20A Module and 66 for 30A Module
int RawValue= 0;
int ACSoffset = 2500;
double mp_offset = 0.040;
double Voltage = 0;
double Amps = 0;
//parametre Wifi
const char* ssid = "Livebox-37cc";
const char* password = "8A6060920A8A86896F770F2C47";
// Paremetre Domoticz
const char* domoticz_server = "192.168.1.3"; //adresse IP de domoticz
int port = 81; //Domoticz port
String IDX_U = "1085"; //idx du capteur virtuels tension
String IDX_I = "1086"; //idx du capteur virtuels intensite
String IDX_W = "1087"; //idx du capteur virtuels puissance et energie
WiFiClient domoticz_client;
//-------------------------------------------------------------------------------------------------
int nbtent=0;
void setup(void) {
Serial.begin(9600);
// Connexion au WiFi
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
if (nbtent<10){
nbtent++ ;
delay(1000);
Serial.print(".");
}
else{
Serial.println("Restet");
ESP.deepSleep(2 * 1000000, WAKE_RF_DEFAULT);
}
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop(void){
Serial.println("");
int i = 0;
RawValue = 0;
// Somme du courant alternatif pendant 20 ms ==> 50hz
// Détermination du max et max pour hauteur de crete
int vmin = 1024;
int vmax = 0;
for (i = 0; i < 20; i++) {
int value = analogRead(analogIn);
if (value >= 0) {
RawValue += value;
vmax = max(value,vmax);
vmin = min(value,vmin);
} else {
i--;
}
delay(1);
}
// Serial.print("Raw Value = " );
// Serial.print(RawValue);
Serial.print("min = " );
Serial.print(vmin);
Serial.print(" max = " );
Serial.print(vmax);
// Serial.print(" i =" );
// Serial.print(i);
// RawValue = RawValue / i;
// Tension continue
// Voltage = (RawValue / 1023.0) * 5000; // Gets you mV
// Amps = ((Voltage - ACSoffset) / mVperAmp);
// La valeur maxi * racine carrée de 2 pour obtenir la tension "réelle"
// La tension efficace pour l'effet Hall étant réduite d'un facteur 0,707
Voltage = ((vmax - vmin) / 430.0) * 5000;
Amps = max(5.5 * (vmax - vmin) / 473.0 -0.0580, 0.0); // <= pour le bruit
// Serial.print(" Raw Value = " ); // shows pre-scaled value
// Serial.print(RawValue);
Serial.print("\t mV = "); // shows the voltage measured
Serial.print(Voltage,3); // the '3' after voltage allows you to display 3 digits after decimal point
Serial.print("\t Amps = "); // shows the voltage measured
Serial.print(Amps,3); // the '3' after voltage allows you to display 3 digits after decimal point
Serial.print("\t Watt = "); // shows the voltage measured
Serial.print(Amps * 220,3);
Serial.print("\t WattH = "); // shows the voltage measured
Serial.println(Amps * 220 / 120,3);
EnvoieDomoticz(IDX_U,String((vmax - vmin))); // tension
EnvoieDomoticz(IDX_I,String(Amps)); // intensite
EnvoieDomoticz(IDX_W,String(Amps * 220)+ ";" +String(Amps * 220 / 1200)); // puissance
delay(100);
Serial.println("Pause de "+String(SLEEP_DELAY_IN_SECONDS)+" secondes...");
delay(SLEEP_DELAY_IN_SECONDS * 1000);
}
int EnvoieDomoticz(String IDX, String Svalue) {
domoticz_client.connect(domoticz_server, port);
delay(500);
int Resultat=0 ;
int NbTentEnvoie=0;
String deel1 = "GET /json.htm?type=command&param=udevice&idx="+IDX+"&nvalue=0&svalue=";
String deel3 = " HTTP/1.1\r\nHost: www.local.lan\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n";
String dataInput = String(deel1 + Svalue + deel3);
while (Resultat!=1) {
if (NbTentEnvoie<10){
NbTentEnvoie++ ;
Serial.print("_");
domoticz_client.println(dataInput);
delay(500);
// Si le serveur repond lire toute les lignes
while(domoticz_client.available()){
String line = domoticz_client.readStringUntil('\r');
if (line.lastIndexOf("status")>0 and line.lastIndexOf("OK")>0){
Serial.println("Status OK");
Resultat = 1 ;
}
}
}
else{
Serial.println("Reset");
ESP.deepSleep(2 * 1000000, WAKE_RF_DEFAULT);
}
}
// deconexion
domoticz_client.println("Connection: close");
domoticz_client.println();
return(Resultat);
}