@@ -46,6 +46,7 @@ extern HA device_dimmer_boost;
4646extern HA device_dimmer_alarm_temp ;
4747extern xSemaphoreHandle mutex ;
4848
49+ extern bool boost ();
4950//***********************************
5051//************* Variables locales
5152//***********************************
@@ -66,7 +67,7 @@ WiFiClient espClient;
6667 }
6768
6869 const String pvname = String ("PvRouter-" ) + WiFi .macAddress ().substring (12 ,14 )+ WiFi .macAddress ().substring (15 ,17 );
69- const String topic = "homeassistant/sensor/" + pvname + "/status" ;
70+ String topic = "homeassistant/sensor/" + pvname + "/status" ;
7071 // Loop until we're reconnected
7172 while (!client .connected ()) {
7273 logging .clean_log_init ();
@@ -83,6 +84,8 @@ WiFiClient espClient;
8384 if (client .connect (pvname .c_str (), configmqtt .username , configmqtt .password )) {
8485 // Once connected, publish online to the availability topic
8586 client .publish (topic .c_str (), "online" , true);
87+ topic = "homeassistant/switch/" + pvname + "/status" ;
88+ client .publish (topic .c_str (), "online" , true);
8689 client .setKeepAlive (15 );
8790 client .setBufferSize (1024 );
8891 logging .Set_log_init (MQTT_connected ,true);
@@ -140,12 +143,15 @@ WiFiClient espClient;
140143 // Ajout d'un espace pour le caractère nul // NOSONAR
141144 char arrivage [length + 1 ];
142145 int recup = 0 ;
146+ //Serial.println("MQTT callback : topic = "+String(topic));
147+ //Serial.println(strstr(topic, "command") != NULL ? "MATCH" : "NO MATCH");
143148
144149 for (int i = 0 ;i < length ;i ++ ) {
145150 arrivage [i ] = (char )payload [i ];
146151 }
147152 arrivage [length ] = '\0' ; // Ajouter le caractère nul à la fin
148153
154+ /// gestion du topic Shelly
149155 if (strcmp ( topic , config .topic_Shelly ) == 0 ) {
150156 if (strcmp ( arrivage , "unavailable" ) == 0 ) {
151157 gDisplayValues .Shelly = -2 ;
@@ -162,9 +168,42 @@ WiFiClient espClient;
162168 } else {
163169 DEBUG_PRINTLN ("Erreur : Conversion de la chaîne en virgule flottante a échoué" );
164170 }
165- } // if (strcmp( arrivage , "unavailable" ) == 0 )
166- } // if (strcmp( topic, config.topic_Shelly ) == 0 )
171+ }
172+ }
173+
174+ /// gestion des switchs
175+ if (strstr (topic , "command" ) != NULL ) {
176+ Serial .println ("MQTT callback : dimmer = " + String (arrivage ));
177+ String returnkey ;
178+
179+ JsonDocument doc ;
180+ deserializeJson (doc , arrivage );
181+
182+ // Traitement simple et propre
183+ if (!doc ["Boost" ].isNull ()) {
184+ Serial .println ("MQTT callback : Boost = " + String (doc ["Boost" ].as < bool > ( )));
185+ boost ();
186+ returnkey = "Boost" ;
187+
188+ }
189+
190+ if (!doc ["Relay1" ].isNull ()) {
191+ Serial .println ("MQTT callback : Relay1 = " + String (doc ["Relay1" ].as < bool > ( )));
192+ digitalWrite (RELAY1 , doc ["Relay1" ].as < bool > ( ));
193+ returnkey = "Relay1" ;
194+ }
195+
196+ if (!doc ["Relay2" ].isNull ()) {
197+ Serial .println ("MQTT callback : Relay2 = " + String (doc ["Relay2" ].as < bool > ( )));
198+ digitalWrite (RELAY2 , doc ["Relay2" ].as < bool > ( ));
199+ returnkey = "Relay2" ;
200+ }
201+
202+ //envoie de la commande de confirmation au broker
203+ client .publish ((device_dimmer_boost .topic + "state" + returnkey ).c_str (), String (arrivage ).c_str (), true);
204+ }
167205
206+ /// récupération des anciennes valeurs de consommation
168207 if (strcmp ( topic , ("memory/" + compteur_grid .topic + compteur_grid .Get_name ()).c_str () ) == 0 ) {
169208 Serial .println ("MQTT callback : compteur_grid = " + String (arrivage ));
170209 // Utiliser strtol pour une conversion plus robuste
@@ -180,8 +219,9 @@ WiFiClient espClient;
180219 } else {
181220 DEBUG_PRINTLN ("Erreur : Conversion de la chaîne en virgule flottante a échoué" );
182221 }
183- } // if (strcmp( topic, ("memory/"+compteur_grid.topic+compteur_grid.Get_name()).c_str() ) == 0 )
222+ }
184223
224+ /// récupération des anciennes valeurs d'injection
185225 if (strcmp ( topic , ("memory/" + compteur_inject .topic + compteur_inject .Get_name ()).c_str () ) == 0 ) {
186226 Serial .println ("MQTT callback : compteur_inject = " + String (arrivage ));
187227 // Utiliser strtol pour une conversion plus robuste
@@ -198,12 +238,11 @@ WiFiClient espClient;
198238 else {
199239 DEBUG_PRINTLN ("Erreur : Conversion de la chaîne en virgule flottante a échoué" );
200240 }
201- } // if (strcmp( topic, ("memory/"+compteur_inject.topic+compteur_inject.Get_name()).c_str() ) == 0 )
202- if (WHtempgrid != 0 && WHtempinject != 0 ) {
203- client .unsubscribe (("memory /"+ compteur_grid .topic + "#").c_str ());
204- }
241+ }
205242
206- // TODO, faire la récupération pour le mode boost
243+ if (WHtempgrid != 0 && WHtempinject != 0 ) {
244+ client .unsubscribe (("memory /"+ compteur_grid .topic + "#").c_str ());
245+ }
207246 }
208247
209248
@@ -232,6 +271,7 @@ WiFiClient espClient;
232271 Serial .println ("memory/" + compteur_grid .topic + compteur_grid .Get_name ()) ;
233272
234273 client .subscribe (("memory/" + compteur_grid .topic + "#" ).c_str ());
274+
235275 client .loop ();
236276
237277 if (config .IDXdimmer != 0 ) {
0 commit comments