Skip to content

Commit

Permalink
Update F1ATB
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed May 5, 2024
1 parent 72f3041 commit c45238b
Show file tree
Hide file tree
Showing 20 changed files with 397 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

//Class Action
Action::Action() {
Gpio=-1 ;
Gpio = -1;
}
Action::Action(int aIdx) {
Gpio=-1 ; // si le n° de pin n'est pas valid, on ne fait rien
Gpio = -1; // si le n° de pin n'est pas valid, on ne fait rien
Idx = aIdx;
T_LastAction = int(millis() / 1000);
On = false;
Expand All @@ -23,14 +23,15 @@ Action::Action(int aIdx) {
OutOff = 0;
Tempo = 0;
Repet = 0;
tOnOff = 0;
}



void Action::Arreter() {
int Tseconde = int(millis() / 1000);
if ((Tseconde - T_LastAction) >= Tempo || Idx == 0 || Actif != 1) {
if (Gpio>0 || Idx == 0) {
if (Gpio > 0 || Idx == 0) {
digitalWrite(Gpio, OutOff);
T_LastAction = Tseconde;
} else {
Expand All @@ -45,7 +46,7 @@ void Action::Arreter() {
void Action::RelaisOn() {
int Tseconde = int(millis() / 1000);
if ((Tseconde - T_LastAction) >= Tempo) {
if (Gpio>0) {
if (Gpio > 0) {
digitalWrite(Gpio, OutOn);
T_LastAction = Tseconde;
On = true;
Expand Down Expand Up @@ -153,6 +154,8 @@ byte Action::TypeEnCours(int Heure, float Temperature, int Ltarfbin) { //Retour
if (Ltarfbin > 0 && (Ltarfbin & Tarif[i]) == 0) TarifOk = false;
if (Heure >= Hdeb[i] && Heure <= Hfin[i] && TemperatureOk && TarifOk) S = Type[i];
}
if (tOnOff>0) S= 2; // Force On
if (tOnOff<0) S= 1; // Force Off
return S; //0=NO,1=OFF,2=ON,3=PW,4=Triac
}
int Action::Valmin(int Heure) { //Retourne la valeur Vmin (ex seuil Triac) à cette heure
Expand Down Expand Up @@ -192,38 +195,39 @@ void Action::InitGpio() { //Initialise les sorties GPIO pour des relais
OutOn = min(OutOn, 1);
OutOff = (OutOn + 1) % 2;

if (Gpio <= 0 || Gpio > 33) Gpio = -1; // GPIO non valide
if (Gpio <= 0 || Gpio > 33) Gpio = -1; // GPIO non valide
if (Host != "" && Host != "localhost") Gpio = -1;
if ( Gpio>0) {
if (Gpio > 0) {
pinMode(Gpio, OUTPUT);
digitalWrite(Gpio, OutOff);

}
}
}
}
void Action::CallExterne(String host, String url, int port) {
// Use WiFiClient class to create TCP connections
WiFiClient clientExt;
char hostbuf[host.length() + 1];
host.toCharArray(hostbuf, host.length() + 1);

if (!clientExt.connect(hostbuf, port)) {
StockMessage("connection to clientExt failed :" + host);
return;
}
clientExt.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (clientExt.available() == 0) {
if (millis() - timeout > 5000) {
StockMessage(">>> clientESP_Ext Timeout ! : " + host);
clientExt.stop();
if (url != "") {
// Use WiFiClient class to create TCP connections
WiFiClient clientExt;
char hostbuf[host.length() + 1];
host.toCharArray(hostbuf, host.length() + 1);

if (!clientExt.connect(hostbuf, port)) {
StockMessage("connection to clientExt failed :" + host);
return;
}
}
clientExt.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (clientExt.available() == 0) {
if (millis() - timeout > 5000) {
StockMessage(">>> clientESP_Ext Timeout ! : " + host);
clientExt.stop();
return;
}
}

// Read all the lines of the reply from server
while (clientExt.available()) {
String line = clientExt.readStringUntil('\r');
// Read all the lines of the reply from server
while (clientExt.available()) {
String line = clientExt.readStringUntil('\r');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Action {
int Gpio;
int OutOn;
int OutOff;
int tOnOff;
byte Reactivite;
byte NbPeriode;
bool On;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,8 @@ void envoiAuMQTT() { // Cette Fonction d'origine a été modifiée
clientMQTT.setServer(host.c_str(), MQTTPort);
clientMQTT.setCallback(callback); //Déclaration de la fonction de souscription
if (clientMQTT.connect(MQTTdeviceName.c_str(), MQTTUser.c_str(), MQTTPwd.c_str())) { // si l'utilisateur est connecté au mqtt
Serial.println(MQTTdeviceName + "connecté");
Debug.println(MQTTdeviceName + "connecté");

StockMessage(MQTTdeviceName + " connecté au broker MQTT");
} else { // si utilisateur pas connecté au mqtt
Serial.print("echec connexion au MQTT, code erreur= ");
Serial.println(clientMQTT.state());
Debug.print("echec connexion au MQTT, code erreur= ");
Debug.println(clientMQTT.state());
StockMessage("Echec connexion MQTT : " + host);
return;
}
Expand All @@ -52,7 +46,6 @@ void envoiAuMQTT() { // Cette Fonction d'origine a été modifiée
//Callback pour souscrire a un topic et prévoir une action
void callback(char *topic, byte *payload, unsigned int length) {
Serial.println("-------Nouveau message du broker mqtt. Non utilisé-----");
Debug.println("-------Nouveau message du broker mqtt. Non utilisé-----");
}
//*************************************************************************
//* CONFIG OF DISCOVERY MESSAGE FOR HOME ASSISTANT / DOMOTICZ *
Expand Down Expand Up @@ -83,6 +76,10 @@ void sendMQTTDiscoveryMsg_global() {
DeviceTextToDiscover("LTARF", "Option Tarifaire");
DeviceToDiscover("Code_Tarifaire", "", "", "0");
}
if (Source == "Enphase") {
DeviceToDiscover("PactProd", "W", "power", "0");
DeviceToDiscover("PactConso_M", "W", "power","0");
}

DeviceToDiscover("PuissanceS_M", "W", "power", "0");
DeviceToDiscover("PuissanceI_M", "W", "power", "0");
Expand All @@ -102,7 +99,6 @@ void sendMQTTDiscoveryMsg_global() {


Serial.println("Paramètres Auto-Discovery publiés !");
Debug.println("Paramètres Auto-Discovery publiés !");

//clientMQTT.setBufferSize(512); // go to initial value wifi/mqtt buffer
Discovered = true;
Expand Down Expand Up @@ -245,6 +241,12 @@ void SendDataToHomeAssistant() {
if (LTARF.indexOf("TEMPO_ROUGE")>=0) code=19;
doc["Code_Tarifaire"]= code;
}

if (Source == "Enphase") {
doc["PactProd"] = PactProd;
doc["PactConso_M"] = PactConso_M;
}

doc["PuissanceS_M"] = PuissanceS_M ; // Maison
doc["PuissanceI_M"] = PuissanceI_M; // Maison
doc["Tension_M"] = Tension_M;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ void Init_Server() {
server.on("/AP_SetWifi", handleAP_SetWifi);
server.onNotFound(handleNotFound);
server.begin();
Debug.println("HTTP server started");
}


Expand Down Expand Up @@ -147,6 +146,9 @@ void handleAjaxRMS() { // Envoi des dernières données brutes reçues du RMS
if (Source_data == "ShellyEm") {
S += GS + ShEm_dataBrute;
}
if (Source_data == "UxIx3") {
S += GS + MK333_dataBrute;
}
}

server.send(200, "text/html", S);
Expand All @@ -155,14 +157,28 @@ void handleAjaxHisto48h() { // Envoi Historique de 50h (600points) toutes les 5
String S = "";
String T = "";
String U = "";
String Ouverture = "";
int iS = IdxStockPW;
for (int i = 0; i < 600; i++) {
S += String(tabPw_Maison_5mn[iS]) + ",";
T += String(tabPw_Triac_5mn[iS]) + ",";
U += String(tabTemperature_5mn[iS]) + ",";
iS = (1 + iS) % 600;
}
server.send(200, "text/html", Source_data + GS + S + GS + T + GS + String(temperature) + GS + U);
for (int i = 0; i < NbActions; i++) {
if ((LesActions[i].Actif > 0) && (ITmode > 0 || i > 0)) {
iS = IdxStockPW;
if (LesActions[i].Actif > 0) {
Ouverture += GS;
for (int j = 0; j < 600; j++) {
Ouverture += String(tab_histo_ouverture[i][iS]) + RS;
iS = (1 + iS) % 600;
}
Ouverture += LesActions[i].Titre;
}
}
}
server.send(200, "text/html", Source_data + GS + S + GS + T + GS + String(temperature) + GS + U + Ouverture);
}
void handleAjaxESP32() { // Envoi des dernières infos sur l'ESP32
IT10ms = 0;
Expand All @@ -185,9 +201,9 @@ void handleAjaxESP32() { // Envoi des dernières infos sur l'ESP32
S += RS + "Horloge ESP";
}
int j = idxMessage;
for (int i = 0; i < 4; i++) {
for (int i = 0; i < 10; i++) {
S += RS + Message[j];
j = (j + 1) % 4;
j = (j + 1) % 10;
}
server.send(200, "text/html", S);
}
Expand All @@ -210,11 +226,28 @@ void handleAjaxData() { //Données page d'accueil
server.send(200, "text/html", S);
}
void handleAjax_etatActions() {
int Force = server.arg("Force").toInt();
int NumAction = server.arg("NumAction").toInt();
if (Force !=0){
if (Force>0) {
if (LesActions[NumAction].tOnOff<0) {
LesActions[NumAction].tOnOff=0;
} else {
LesActions[NumAction].tOnOff +=30;
}
} else {
if (LesActions[NumAction].tOnOff>0) {
LesActions[NumAction].tOnOff=0;
} else {
LesActions[NumAction].tOnOff -=30;
}
}
}
int NbActifs = 0;
String S = "";
String On_;
for (int i = 0; i < NbActions; i++) {
if (LesActions[i].Actif > 0) {
if ((LesActions[i].Actif > 0) && (ITmode > 0 || i > 0)) { //Pas de Triac en synchro horloge interne
S += String(i) + RS + LesActions[i].Titre + RS;
if (LesActions[i].Actif == 1 && i > 0) {
if (LesActions[i].On) {
Expand All @@ -225,6 +258,7 @@ void handleAjax_etatActions() {
} else {
S += String(100 - Retard[i]) + RS;
}
S += String(LesActions[i].tOnOff) + RS;
S += GS;
NbActifs++;
}
Expand Down Expand Up @@ -412,6 +446,7 @@ void handleAP_SetWifi() {
S += "ESP 32 connecté avec succès au wifi : " + ssid + " avec l'adresse IP : " + IP;
S += "<br><br> Connectez vous au wifi : " + ssid;
S += "<br><br> Cliquez sur l'adresse : <a href='http://" + IP + "' >http://" + IP + "</a>";
dhcpOn = 1;
EcritureEnROM();
} else {
S = "No" + RS + "ESP32 non connecté à :" + ssid + "<br>";
Expand All @@ -423,7 +458,7 @@ void handleAP_SetWifi() {


void handleNotFound() { //Page Web pas trouvé
Debug.println(F("Fichier non trouvé"));
StockMessage("Fichier Web non trouvé");
String message = "Fichier non trouvé\n\n";
message += "URI: ";
message += server.uri();
Expand Down
Loading

0 comments on commit c45238b

Please sign in to comment.