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+