Arduino Serial Json Lua

Forum dédié aux problématiques concernant les scripts pour DomoticZ.
Entourez votre code et les logs avec les balises nommées code grâce au bouton <\>.
maticz
Messages : 42
Inscription : 14 juil. 2019, 11:18

Arduino Serial Json Lua

Message par maticz »

je sollicite votre connaissance (et indulgence) pour pour la création/apprentissage du tuto a venir.

Bonsoir

Enfin trouver une base pour ma futur Install Domotique sur le site de sgripon.net :roll:

le titre : Ajouter un capteur temp/humidité DHT11 pour Domoticz , simple et super connue diront certain.

Bah non ! .... perdue :lol:

Explication : d'Arduino remonter une valeur par USB (sérial) sur le serveur domoticz (lua)


le hard : Arduino Uno + DHT11 (ou22) + Cable USB et Domoticz bien huilé
le soft : croquis sur Arduino + Script Lua et serial.py installer sur le serveur Domoticz

le croquis Arduino

Code : Tout sélectionner

    #include <SimpleDHT.h>
     
    // for DHT11, 
    //      VCC: 5V or 3V
    //      GND: GND
    //      DATA: 2
    int pinDHT11 = 2;
    SimpleDHT11 dht11;
     
    void print_data_json(int temp, int hum) {
      Serial.print("{ ");
      Serial.print("\"temp\": ");
      Serial.print(temp);
      Serial.print(", \"hum\": ");
      Serial.print(hum);
      Serial.print(" }\n"); 
    }
     
    void setup() {
      Serial.begin(115200);  // adapter avec py.serial
    }
     
    void loop() {
     
      // read without samples.
      byte temperature = 0;
      byte humidity = 0;
      if (dht11.read(pinDHT11, &temperature, &humidity, NULL)) {
        // Error. -1 is returned
        print_data_json(-1, -1);
        return;
      }
     
      print_data_json((int)temperature, (int)humidity);
     
      // DHT11 sampling rate is 1HZ.
      delay(1000);
    }
Création d'un capteur virtuel sur Domoticz

et le script lua a placer dans ~/script/lua

Code : Tout sélectionner

JSON = (loadfile "/home/pi/bin/domoticz/scripts/lua/JSON.lua")() -- one-time load of the routines
 
-- Index of DHT11 device in domoticz
local deviceIdx = 1
local serialPort = "/dev/ttyACM0"
 
commandArray = {}
 
-- Start job
 
-- Open seria port and read
rserial=io.open(serialPort, "r")
read_value = rserial:read()
local json_value = JSON:decode(read_value)
 
-- Data : TEMP;HUM;HUM_STAT
-- HUM_STAT can be one of:
-- 0=Normal
-- 1=Comfortable
-- 2=Dry
-- 3=Wet
local hum_stat = 0;
if json_value.hum<30 then hum_stat=2 end
if json_value.hum>70 then hum_stat=3 end
if (json_value.hum>=45 and json_value.hum<=50) then hum_stat=1 end
 
commandArray['UpdateDevice']=deviceIdx..'|0|'..json_value.temp..';'..json_value.hum..';'..hum_stat
 
return commandArray
https://wiki.sgripon.net/doku.php/add_t ... n_domoticz

du très simple pour un début apprentissage.

question :
Domoticz a évolué/grandie, le script demande qu"elle changement/amélioration ??

j'ai déjà testé en simple copier coller, mais rien de fonctionnelle

repartir a zéro avec ???
https://easydomoticz.com/mon-premier-sc ... ondations/
https://easydomoticz.com/lua-et-json/
https://www.domoticz.com/wiki/Domoticz_ ... emperature


donc une mis a l'étrier pour préparé d'autre évolution
https://wiki.sgripon.net/doku.php/add_c ... []=arduino
https://wiki.sgripon.net/doku.php/add_t ... []=arduino

Merci
Répondre