User Tools

Site Tools


Add temperature and humidity sensor DHT11 in Domoticz


In previous post, I have shown how to record temperature from Lexibook ASM30 sensor with Arduino. Here, we will use the same hardware platform for DHT11 sensor.

Needed hardware

  • Arduino Uno R3 board (available on Amazon) with USB cable
  • DHT11 temperature and humidity sensor. I use a 3 pin version found on Amazon.


It is very simple. Just connect the DHT11 sensor like this:

  • G pin to Arduino pin GND
  • V pin to Arduino pin 5V
  • S pin to Arduino DIGITAL pin 2

Then I suppose you have the Arduino connected with a USB cable to a PC running the Arduino development environment.

The Arduino sketch

The first thing to do is to test the assembly with the DHT11 library SimpleDHT. It can be installed easily through library manager in Arduino environment.

Once installed, just choose the SimpleDHT example, export it on the Arduino and check the temperature and humidity returned on serial monitor.

It shoud display something like that:

Sample DHT11...
Sample OK: 18 *C, 37 %
Sample DHT11...
Sample OK: 18 *C, 37 %
Sample DHT11...
Sample OK: 18 *C, 37 %

Then, I changed the sketch in order to send the temperature and humidity in json:

#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(", \"hum\": ");
  Serial.print(" }\n"); 
void setup() {
void loop() {
  // read without samples.
  byte temperature = 0;
  byte humidity = 0;
  if (, &temperature, &humidity, NULL)) {
    // Error. -1 is returned
    print_data_json(-1, -1);
  print_data_json((int)temperature, (int)humidity);
  // DHT11 sampling rate is 1HZ.

The domoticz part

Follow the same instruction as found on previous tutorial Add temperature sensor in Domiticz.

And the lua script for DHT11 is really simple:

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, "r")
read_value = rserial:read()
local json_value = JSON:decode(read_value)
-- 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
return commandArray

Share this page:

add_temperature_and_humidity_sensor_dht11_in_domoticz.txt · Last modified: 2016/11/11 19:38 by sgripon

Except where otherwise noted, content on this wiki is licensed under the following license: Public Domain
Public Domain Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki