DIY Projects

MySensors v2: measurement of temperature and humidity (DHT11/DHT22)

mysensors arduino micro dht22

Arduino MySensors library allows you to very easily perform a probe temperature and humidity at base DHT11 or DHT22, which we can easily retrieve the values on a home automation software or program any. This is a good exercise to discover the MySensors library and home automation.

List of Material

Arduino Nano (advice)
nRF24L01 radio module

Short distance ou long distance (1000m)

3V3 plate adapter with tension regulator (for nRF24L01 or nRF24L01+PA+LNA module)
Temperature and humidity sensor

DHT11 or better DHT22

Jumper Dupont

Circuit : nRF24L01 and DHT22

It is preferable to use a adapter with a voltage regulator integrated to achieve a best of show / radio signal reception (and it’s also much easier to wire). The connection of the module nRFF24L01 to the Arduino Nano is the following:

The MySensors development team advises the use of a Arduino Nano as the basis for a MySensors node. It is also possible to add a node to a gateway or use an ESP8266 since version 2.

Copy the code below into a new Arduino project and change the following settings:

You may also need to add the DHT Bookstore from the Bookstore Manager. Unlike the examples found on the Web, I used the Adafruit library which is available in the Library Manager.

 * The MySensors Arduino library handles the wireless radio link and protocol
 * between your home built sensors/actuators and HA controller of choice.
 * The sensors forms a self healing radio network with optional repeaters. Each
 * repeater and gateway builds a routing tables in EEPROM which keeps track of the
 * network topology allowing messages to be routed to nodes.
 * Created by Henrik Ekblad <>
 * Copyright (C) 2013-2015 Sensnology AB
 * Full contributor list:
 * Documentation:
 * Support Forum:
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 * Version 1.0 - PROJETS DIY
 * Example sketch showing how to measue light level using a LM393 photo-resistor 

// Active le mode debug / Enable debug prints to serial monitor
#define MY_DEBUG 

// Mode Radio / Enable and select radio type attached
#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69

#define MY_RF24_PA_LEVEL RF24_PA_LOW

// Noeud de l'objet / Object Node
#define MY_NODE_ID 3

#include <SPI.h>
#include <MySensors.h>  
#include <DHT.h> 

#define CHILD_ID_TEMP 0  
#define CHILD_ID_HUM 1
#define SLEEP_NODE true // Mettre à True pour activer la mise en sommeil (Sleep Mode) / True to activate Sleep Mode
unsigned long SLEEP_TIME = 10 * 1000; // Temps de mise en sommeil (en ms) / Sleep time between reads (in milliseconds)

#define DHTPIN 3        // Broche sur laquelle est branché le DHT / what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302), DHT11

float lastTemp;
float lastHum;
boolean metric = true; 
MyMessage msgHum(CHILD_ID_HUM, V_HUM);
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);

void setup() { 

void presentation()  {
  // Présenttion du sketch / Send the sketch version information to the gateway and Controller
  sendSketchInfo("Sonde Temp/Hum DHT22", "1.0");

  // Déclaration des capteurs attachés au noeud
  present(CHILD_ID_TEMP, S_TEMP);
  present(CHILD_ID_HUM, S_HUM);

void loop()      
  DHT dht(DHTPIN, DHTTYPE,3); // Creation d'un objet dht dans la loop sinon les valeurs ne sont pas mesurées au réveil
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();
  if (isnan(temperature)) {
      Serial.println("Impossible de lire la temperature sur le DHT");
  } else {
    if (temperature != lastTemp) {
      lastTemp = temperature;
      if (!metric) {
        temperature = dht.readTemperature(true);
      Serial.print("T: ");
      Serial.print(" | H: ");
      send(msgTemp.set(temperature, 1));
      send(msgHum.set(humidity, 1));
    } else {
      Serial.println("Temperature identique => on force l'envoi");  
      Serial.print("T: ");
      Serial.print(" | H: ");
      send(msgTemp.set(temperature, 1));
      send(msgHum.set(humidity, 1));
  if (SLEEP_NODE) {
  } else {
    delay (SLEEP_TIME);

Use MySensors sensor with Domoticz

Now we can use this small Probe temperature and humidity in software automation such as Domoticz. If you are new to home automation (or Domoticz), read this article (in French) which explains how to add a network gateway at ESP8266 base.

Go to the devices. From the first transmission of data, Domoticz was added automatically to the list of available devices. Press the green arrow and give it a name.

Then go to the Temperature page to see your new probe MySensors.

Enjoy !

Click to rate this post!
[Total: 0 Average: 0]
Exit mobile version