MySensors v2: measurement of temperature and humidity (DHT11/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 v3 atmega328p Arduino Nano (advice)
rflink nrf24l01 nRF24L01 radio module

Short distance ou long distance (1000m)

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

DHT11 or better DHT22

jumper dupont Jumper Dupont
breadboard Breadboard

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:

  • 5V-> V+
  • GND-> GND
  • D13-> SCK (green)
  • D12-> MISO (purple)
  • D11-> MOSI (blue)
  • D10-> CSN (yellow)
  • D9-> CE (orange)
  • D2-> IRQ (gray)

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.

mysensors arduino micro dht22 nrf24l01

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

  • The strength of the signal. Here it is set to LOW for privileged scope in view of the low volume of data to be transmitted and the frequency of sending.
  • The module type Radio : 2.4GHz (nRF24) or 915MHz (RFM69)
  • The identifier of the node. The v2 is stable but the automatic assignment of an identifier is still random. For the moment, I prefer to assign a number manually.
  • The parameter SLEEP_NODE allows you to sleep the module for the period SLEEP_TIME

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 <henrik.ekblad@mysensors.org>
 * Copyright (C) 2013-2015 Sensnology AB
 * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
 *
 * Documentation: http://www.mysensors.org
 * Support Forum: http://forum.mysensors.org
 *
 * 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.
 *
 *******************************
 *
 * REVISION HISTORY
 * Version 1.0 - PROJETS DIY
 * 
 * DESCRIPTION
 * Example sketch showing how to measue light level using a LM393 photo-resistor 
 * http://www.mysensors.org/build/light
 */

// 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(temperature);
      Serial.print(" | H: ");
      Serial.println(humidity);
      send(msgTemp.set(temperature, 1));
      send(msgHum.set(humidity, 1));
    } else {
      Serial.println("Temperature identique => on force l'envoi");  
      Serial.print("T: ");
      Serial.print(temperature);
      Serial.print(" | H: ");
      Serial.println(humidity);
      send(msgTemp.set(temperature, 1));
      send(msgHum.set(humidity, 1));
    }
  }  
  
  if (SLEEP_NODE) {
    Serial.println("Sleep");
    sleep(SLEEP_TIME);
  } 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.

domoticz dispositif mysensors temperature humidite dht11 dht22 arduino nano

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

domoticz dispositif mysensors temperature humidite dht11 dht22 arduino nano

Enjoy !

Subscribe to the weekly newsletter

No spam and no other use will be made of your email. You can unsubscribe anytime.

1 Comment

Leave a Reply

DIY Projects
%d bloggers like this: