Page 1 sur 4

Domoticz vers ESP8266

Publié : 13 déc. 2022, 13:55
par fcBourgogne
Bonjour,

La majeure partie des transmissions pour les données se fait vers Domoticz, on trouve beaucoup de posts.
Le contraire semble moins fourni.
Je suis donc à la recherche d'une méthode pour rafraîchir une variable dans l'esp8266, via le wifi.
Deux possibilités, domoticz envoi la donnée à chaque changement d'état, ou l'esp8266 interroge domoticz et son idx.
J'ai bien essayé comme avec ce bout de code dans l'esp8266:
"const char* serverName = "http://192.168.esp/json.htm?type=devices&rid=563";"
La valeur se trouve présente dans une liste, visible dans le moniteur série de l'IDE Arduino, mais je ne sais pas l'extraire et l'affectée à une variable "int"...
Quelle solution serait elle la meilleur?
Par avance merci de vos réponses.
A bientôt.

Re: Domoticz vers ESP8266

Publié : 13 déc. 2022, 21:36
par fcBourgogne
Re,

Après moult recherches et essais, j'ai trouvé la soluce.
Il m'a fallu passer par "deserializeJson"
Cela n'a pas été facile car la donnée recherchée se trouvait dans une sous-liste, une troisième imbrication.
Je peux maintenant interroger Domoticz et récupérer la valeur d'un device, comme par exemple la température d'une sonde.
Je dis "je" mais c'est l'ESP8266 NodeCmu qui interroge Domoticz.
Voici le code de l'ESP8266:

Code : Tout sélectionner

/*
  Sur une base du script de Rui Santos
  
*/

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <Arduino_JSON.h>
#include <ArduinoJson.h>
const char* ssid = "*******";
const char* password = "************";


//Your Domain name with URL path or IP address with path
String serverName = "http://192.168.*.*:**/json.htm?type=devices&rid=563";

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;

void setup() {
  Serial.begin(115200); 

  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
 
  Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");

}

void loop() {
  // Send an HTTP POST request depending on timerDelay
  if ((millis() - lastTime) > timerDelay) {
    //Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED){
      WiFiClient client;
      HTTPClient http;

      String serverPath = serverName;
      
      // Your Domain name with URL path or IP address with path
      http.begin(client, serverPath.c_str());
  
      // If you need Node-RED/server authentication, insert user and password below
      //http.setAuthorization("REPLACE_WITH_SERVER_USERNAME", "REPLACE_WITH_SERVER_PASSWORD");
        
      // Send HTTP GET request
      int httpResponseCode = http.GET();
      
      if (httpResponseCode>0) {
        Serial.print("HTTP Response code: ");
        Serial.println(httpResponseCode);
        String payload = http.getString();
        Serial.println(payload);
        DynamicJsonDocument doc(3000);
        DeserializationError error = deserializeJson(doc, payload);
        
        String obj = JSON.parse(payload); if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.f_str());
    return;
  }
       const char* Data = doc["result"][0]["Data"];
       Serial.print("Data: "); 
       Serial.println(Data); 
      }
      else {
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
      }
      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}
le résultat dans l'IDE de l'arduino, la dernière ligne:

Code : Tout sélectionner

20:47:00.691 -> HTTP Response code: 200
20:47:00.691 -> {
20:47:00.691 -> 	"ActTime" : 1670960820,
20:47:00.691 -> 	"AstrTwilightEnd" : "18:47",
20:47:00.691 -> 	"AstrTwilightStart" : "06:35",
20:47:00.729 -> 	"CivTwilightEnd" : "17:29",
20:47:00.729 -> 	"CivTwilightStart" : "07:52",
20:47:00.729 -> 	"DayLength" : "08:24",
20:47:00.729 -> 	"NautTwilightEnd" : "18:09",
20:47:00.729 -> 	"NautTwilightStart" : "07:13",
20:47:00.729 -> 	"ServerTime" : "2022-12-13 20:47:00",
20:47:00.729 -> 	"SunAtSouth" : "12:41",
20:47:00.729 -> 	"Sunrise" : "08:29",
20:47:00.729 -> 	"Sunset" : "16:53",
20:47:00.729 -> 	"app_version" : "2022.2",
20:47:00.729 -> 	"result" : 
20:47:00.729 -> 	[
20:47:00.729 -> 		{
20:47:00.729 -> 			"AddjMulti" : 1.0,
20:47:00.729 -> 			"AddjMulti2" : 1.0,
20:47:00.729 -> 			"AddjValue" : 0.0,
20:47:00.729 -> 			"AddjValue2" : 0.0,
20:47:00.729 -> 			"BatteryLevel" : 255,
20:47:00.729 -> 			"CustomImage" : 137,
20:47:00.776 -> 			"Data" : "23.5",
20:47:00.776 -> 			"Description" : "",
20:47:00.776 -> 			"Favorite" : 1,
20:47:00.776 -> 			"HardwareDisabled" : false,
20:47:00.776 -> 			"HardwareID" : 55,
20:47:00.776 -> 			"HardwareName" : "ThermoAmbiant",
20:47:00.776 -> 			"HardwareType" : "Dummy (Does nothing, use for virtual switches only)",
20:47:00.776 -> 			"HardwareTypeVal" : 15,
20:47:00.776 -> 			"HaveTimeout" : false,
20:47:00.776 -> 			"ID" : "0014283",
20:47:00.776 -> 			"Image" : "Heating3",
20:47:00.776 -> 			"LastUpdate" : "2022-12-13 20:20:36",
20:47:00.776 -> 			"Name" : "ThermostAmbiant",
20:47:00.776 -> 			"Notifications" : "false",
20:47:00.776 -> 			"PlanID" : "0",
20:47:00.776 -> 			"PlanIDs" : 
20:47:00.776 -> 			[
20:47:00.776 -> 				0
20:47:00.776 -> 			],
20:47:00.776 -> 			"Protected" : false,
20:47:00.776 -> 			"SetPoint" : "23.5",
20:47:00.776 -> 			"ShowNotifications" : true,
20:47:00.824 -> 			"SignalLevel" : "-",
20:47:00.824 -> 			"SubType" : "SetPoint",
20:47:00.824 -> 			"Timers" : "false",
20:47:00.824 -> 			"Type" : "Thermostat",
20:47:00.824 -> 			"TypeImg" : "override_mini",
20:47:00.824 -> 			"Unit" : 1,
20:47:00.824 -> 			"Used" : 1,
20:47:00.824 -> 			"XOffset" : "0",
20:47:00.824 -> 			"YOffset" : "0",
20:47:00.824 -> 			"idx" : "563"
20:47:00.824 -> 		}
20:47:00.824 -> 	],
20:47:00.824 -> 	"status" : "OK",
20:47:00.824 -> 	"title" : "Devices"
20:47:00.824 -> }
20:47:00.824 -> 
20:47:00.824 -> Data: 23.5
Voilà si ça intéresse quelqu'un.
A+

Re: Domoticz vers ESP8266

Publié : 14 déc. 2022, 11:16
par Doudy
fcBourgogne a écrit : 13 déc. 2022, 21:36 Voilà si ça intéresse quelqu'un.
A+
👍👍👍👍
Eh bien merci champion.
Ca fait des jours que je cherche de transférer de ESP8266 vers Domoticz et grâce à toi enfin un script qui fonctionne à la perfection.
1000x merci

;)

Re: Domoticz vers ESP8266

Publié : 14 déc. 2022, 11:52
par fcBourgogne
Bonjour Doudy,

Merci, mais je ne comprends pas bien.
tu dis
Ca fait des jours que je cherche de transférer de ESP8266 vers Domoticz et grâce à toi enfin un script qui fonctionne à la perfection.
Mais le script travail dans l'autre sens, le transfert se fait de Domoticz vers ESP8266. L'ESP interroge.
???
A+

Re: Domoticz vers ESP8266

Publié : 14 déc. 2022, 13:09
par Doudy
Effectivement c'est de Domoticz vers ESP8266.
Je n'ai pas bien relu mon texte...
:?

Re: Domoticz vers ESP8266

Publié : 15 déc. 2022, 10:29
par Doudy
Bonjour,
Petite question :
Comment tu fais pour une valeur qui n'est pas string ou caractère ?
P.ex : "BatteryLevel" : 255,

Je suppose que c'est

Code : Tout sélectionner

const char* Data = doc["result"][0]["Data"];
qui doit être adapté ! Mais comment ?
Lorsque je fait

Code : Tout sélectionner

const int* Data = doc["result"][0]["Temp"];
J'ai une erreur sur

Code : Tout sélectionner

Serial.println(Data); 
Compilation error: no matching function for call to 'println(const int*&)'

Une petite idée ?
;)

Re: Domoticz vers ESP8266

Publié : 15 déc. 2022, 12:00
par fcBourgogne
Bonjour,
Je me suis rendu compte que les valeurs sans " ", ne sont pas lues.

Alors, j'en ai ajouté une en changeant le type par "double", pour des nombres.

Code : Tout sélectionner

const char* Data = doc["result"][0]["Data"];
       double Data1 = doc["result"][0]["Temp"];
A+

Re: Domoticz vers ESP8266

Publié : 15 déc. 2022, 13:41
par Doudy
Comme je disais dans mon script plus haut : Tu es un champion 👍👍👍👍
Résultat :

Code : Tout sélectionner

13:40:23.339 -> Data : 10.6 C, 41 %
13:40:23.383 -> Température Garage : 10.60
Merci
;)

Re: Domoticz vers ESP8266

Publié : 15 déc. 2022, 14:46
par fcBourgogne
Re,

Je vois que cela fonctionne!

Si tu le veux tu peux également afficher le taux d'humidité.

Code : Tout sélectionner

double Data1 = doc["result"][0]["Temp"][0];    //Température
double Data2 = doc["result"][0]["Temp"][1];    //Humidité
A essayer.

Re: Domoticz vers ESP8266

Publié : 15 déc. 2022, 16:04
par Doudy
Je viens d'essayer et il m'affiche ceci :

Code : Tout sélectionner

16:02:02.220 -> Data : 11.1 C, 41 %
16:02:02.220 -> Température Garage : 0.00
16:02:02.220 -> Humidité Garage : 0.00

Code : Tout sélectionner

      // const char* Data = doc["result"][0]["Data"];
      const char* Data = doc["result"][0]["Data"];
             double Data1 = doc["result"][0]["Temp"][0];    //Température
             double Data2 = doc["result"][0]["Temp"][1];    //Humidité      
        Serial.print("Data : "); 
        Serial.println(Data); 
        Serial.print("Température Garage : "); 
        Serial.println(Data1); 
        Serial.print("Humidité Garage : "); 
        Serial.println(Data2); 
:?

J'ai constaté également :

Code : Tout sélectionner

16:02:12.267 -> HTTP Response code: 200