User Tools

Site Tools


add_temperature_and_humidity_sensor_dht11_in_domoticz

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
add_temperature_and_humidity_sensor_dht11_in_domoticz [2016/11/11 18:41]
sgripon [Assembly]
add_temperature_and_humidity_sensor_dht11_in_domoticz [2016/11/11 19:38] (current)
sgripon [The domoticz part]
Line 42: Line 42:
 Sample OK: 18 *C, 37 % Sample OK: 18 *C, 37 %
 </​code>​ </​code>​
 +
 +Then, I changed the sketch in order to send the temperature and humidity in json:
 +
 +<file c dht11.ino>​
 +#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);​
 +}
 +
 +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);​
 +}
 +</​file>​
 +===== The domoticz part =====
 +
 +Follow the same instruction as found on previous tutorial [[add_temperature_sensor_in_domoticz#​domoticz|Add temperature sensor in Domiticz]].
 +
 +And the lua script for DHT11 is really simple:
 +
 +<file lua script_time_DHT11.lua>​
 +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
 +</​file>​
 +
 +**Share this page:**
 +
 +~~socialite~~
 +
 +
add_temperature_and_humidity_sensor_dht11_in_domoticz.1478886104.txt.gz ยท Last modified: 2016/11/11 18:41 by sgripon