Page 1 sur 1

Connection Domoticz avec GET /json.htm?

Publié : 19 août 2019, 22:18
par ohaldi
Comment débugger une connection avec Domoticz?

J'ai mis le code suivant dans mon ESP8266:
Le but est d'informer un switch Domoticz sur l'état d'une porte.

void InfoDomo()
{
// Lecture de la pin 14 (D5), Switch de la porte
int door = digitalRead(14);

if (client.connect(domoticz_server,port)) {
Serial.println("Sending Values to server...:");
Serial.println("idx,door,server,port:");
Serial.println(idx);
Serial.println(door);
Serial.println(domoticz_server);
Serial.println(":");
Serial.println(port);

client.print("GET /json.htm?type=command&param=udevice&idx=");
client.print(idx);
client.print("&nvalue=0&svalue=");
client.print(door);
client.println(" HTTP/1.1");
client.print( "Host: ");
client.print(domoticz_server);
client.print(":");
client.println(port);
client.println("User-Agent: Arduino-ethernet");
client.println("Connection: close");
client.println();
client.println();
client.stop();
}
}
Le moniteur série m'affiche correctement mes lignes d'informations, mais je ne vois rien dans le log de Domoticz

Si je met dans mon browser l'url suivante :
http://192.168.90.10:8084/json.htm?type ... 0&svalue=1

Cela m'affiche :
{
"status" : "OK",
"title" : "Update Device"
}
Mais toujours rien dans le log de Domoticz.

J'ai lu des dizaines d'exemples tous ressemble au code ci-dessus.
Comment peut-on débugger ce genre de problème?
Merci d'avance pour votre aide.

Re: Connection Domoticz avec GET /json.htm?

Publié : 12 févr. 2021, 13:52
par mike_78
Bonjour

j'ai le même problème.
pour ma part c'est lié aux codes d'accès à Domoticz, (pas au réseau local, qui se passe bien)
si je mets un mdp, l'accès est refusé.
si pas de mdp alors le capteur est reçu

il y a des info ici: https://www.domoticz.com/wiki/Domoticz_ ... s#Security
mais je ne sais quel code écrire sous l'IDE Arduino ...
je cherche ....

avez vous trouvé?

Re: Connection Domoticz avec GET /json.htm?

Publié : 12 févr. 2021, 14:36
par Chrominator
Pas de source pour vérifier ton code, mais s'il s'agit d'un appel à l'API de DZ, la syntaxe figure dans le wiki :
Capture.PNG
Capture.PNG (15.91 Kio) Consulté 3266 fois

Si la connexion à DZ est faite à partir du lan ou du wlan, on peut aussi paramétrer DZ pour qu'il ne demande pas de mot de passe dans ce cas spécifique (Dans Setup/Settings/System) :
Capture2.PNG
Capture2.PNG (7 Kio) Consulté 3263 fois

Re: Connection Domoticz avec GET /json.htm?

Publié : 12 févr. 2021, 15:56
par mike_78
Bonjour

Merci pour ce retour rapide.
en effet ça marche bien sur ma démo.

mais DZ sera dans un local associatif, je pense que l'on va vouloir garder l'auth.

mon code pour un ESP32, ci dessous.
on est connecté mais DZ ignore les données semble-t-il.
j'ai lu le wiki mais je ne vois pas comment faire l'authentification dans le code IDE arduino ?


Code : Tout sélectionner

#include <Arduino.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
......
WiFiClient client;
......

void printInfo()
 {

 // Domoticz format alerte  /json.htm?type=command&param=udevice&idx= IDX &nvalue= LEVEL &svalue= TEXT
    // IDX = id of your device (This number can be found in the devices tab in the column "IDX")
    // Level = (0=gray, 1=green, 2=yellow, 3=orange, 4=red)
    // TEXT = Text you want to display



    if (client.connect(domoticz_server,port)) 
    {

       // Serial.println("Tx to DOMOTICZ");
       // Serial.println(Level);
       // Serial.println(Text);
        client.print("GET /json.htm?type=command&param=udevice&idx=");
        client.print(idx);
        client.print("&nvalue=");
        client.print(Level);
        client.print("&svalue=");
        client.print(Text);

        
        client.println(" HTTP/1.1");
        client.print("Host: ");
        client.print(domoticz_server);
        client.print(":");
        client.println(port);
        client.println("User-Agent: Arduino-ethernet");
        client.println("Connection: close");
        client.println();
        client.stop();
        
        Serial.println("end Tx to Domoticz");
     }
     else
     {Serial.println("no access to DOMOTICZ!!!"); // Domoticz pas lancé.
     }
}

Re: Connection Domoticz avec GET /json.htm?

Publié : 12 févr. 2021, 18:13
par Chrominator
Je n'utilise pas l'IDE Arduino, je préfère Visual Studio Code / Platformio.

Si la bibliothèque ESP8266httpUpdate est disponible dans ton environnement, tu peux mettre en œuvre la fonction que j’utilise pour les appels à l'API json de DZ.

La bibliothèque :

Code : Tout sélectionner

#include <ESP8266httpUpdate.h>
La déclaration :

Code : Tout sélectionner

HTTPClient http;
Il faut juste former l'url et appeler la fonction ensuite :

Code : Tout sélectionner

/*****************************************************************
 * Call Domoticz JSON API                                        *
 *****************************************************************/

String sendDomoticz(String url){
  String reply = "";
  if (DEBUG) {
    Serial.print("connecting to ");
    Serial.println(host);
    Serial.print("Requesting URL: ");
    Serial.println(url);
  }
  http.begin(host, port, url);
  int httpCode = http.GET();
    if (httpCode) {
      if (httpCode == 200) {
        reply = http.getString();
        if (DEBUG) {
          Serial.println("Domoticz response ");
          Serial.println(reply);
        }
      }
    }
  if (DEBUG) { Serial.println("closing connection"); }
  http.end();
  return reply;
Bien sûr, il faut encoder l'url, j'utilise cette fonction :

Code : Tout sélectionner

/*****************************************************************
 * encode url                                                    *
 *****************************************************************/

String urlencode(String str)
{
    String encodedString="";
    char c;
    char code0;
    char code1;
    char code2;
    for (uint8 i =0; i < str.length(); i++){
      c=str.charAt(i);
      if (c == ' '){
        encodedString+= '+';
      } else if (isalnum(c)){
        encodedString+=c;
      } else{
        code1=(c & 0xf)+'0';
        if ((c & 0xf) >9){
            code1=(c & 0xf) - 10 + 'A';
        }
        c=(c>>4)&0xf;
        code0=c+'0';
        if (c > 9){
            code0=c - 10 + 'A';
        }
        code2='\0';
        encodedString+='%';
        encodedString+=code0;
        encodedString+=code1;
        //encodedString+=code2;
      }
      yield();
    }
    return encodedString;
}
Et par exemple pour inscrire du texte dans le log de DZ, je fais comme ça :

Code : Tout sélectionner

/*****************************************************************
 * Add log to Domoticz                                           *
 *****************************************************************/

void write2log(String msg, const int level){
  if (level <= DEBUG_LEVEL) {
    if (DEBUG) { Serial.println(msg); }
    sendDomoticz("/json.htm?type=command&param=addlogmessage&message="+urlencode(String(msg));
  }  
}
En espérant que ça t'aide. ;)

Re: Connection Domoticz avec GET /json.htm?

Publié : 12 févr. 2021, 22:00
par mike_78
OK.
oh! il va falloir que je digère ça ...
oui la librairie existe pour l'IDE Arduino, je l'ai installée

en fait, j'arrive bien a passer le message type alerte à DZ :
rouge.JPG
rouge.JPG (18.67 Kio) Consulté 3218 fois
vert.JPG
vert.JPG (16.96 Kio) Consulté 3218 fois
mais comment faire pour passer la barrière du mot de passe quand il est activé dans DZ ?
comment passer les ID et Pswd ?
en tout cas , en copiant le navigateur Web, le format de la commande ci dessous ça marche.
http://<username:password@>domoticz-ip<:port>/json.htm?type=command&param=udevice&idx=1&nvalue=2&svalue=TEXT

maintenant il faut que ce soit le ESP32 qui fasse le job. voilà j'espère avoir été plus clair.

Re: Connection Domoticz avec GET /json.htm?

Publié : 12 févr. 2021, 23:12
par mike_78
bonsoir,

finalement j'ai trouvé:
cle "id : pswd" à générer avec l'algo base64
To encode a string using base64 you could use : codebeautify.org/base64-encode

ligne a mettre dans le code C++ ESP32: client.println("Authorization: Basic <clé base64>");
..........
client.println(" HTTP/1.1");
client.print("Host: ");
client.print(domoticz_server);
client.print(":");
client.println(port);
client.println("Authorization: Basic <clé base64>");
client.println("User-Agent: Arduino-ethernet");
client.println("Connection: close");
client.println();
client.stop();
..............

https://www.domoticz.com/wiki/Domoticz_ ... parameters

https://forum.arduino.cc/index.php?topic=131722.0

à bientôt !
Mike_78