MySensors without the binding (DHT22 over MQTT)

MySensors is a framework for creating incredibly cheap DIY sensors, based on Arduino boards and NRF24L01+ radios (i.e. it’s not WIFI, Zigbee or Z-Wave, although it does live in the same spectrum as WIFI and Zigbee. Other radios can also be used).

Prerequisites

Software

  • Ensure the openHAB MQTT binding is installed (V2, not V1)
  • MQTT broker - I use Mosquitto , setup as per its defaults
    • On my setup, Mosquitto and OpenHab are running on the same device, at 192.168.1.92

Hardware

The temperature sensing node is made up of the above hardware. You then also need a gateway that the node can connect to. The gateway relays the information from the node to my Mosquitto MQTT broker. My gateway is installed on the same Raspberry Pi 3B+ that is running openHAB and Mosquitto, at IP 192.168.1.92.

Setup

MySensors Gateway

I installed the MQTT gateway onto my Pi using these instructions. Of course, you will need to wire in one of the NRF24L01+ radios to the Pi too.

During the configure stage, I set the following options:

./configure --my-gateway=mqtt --my-controller-ip-address=192.168.1.92 --my-mqtt-publish-topic-prefix=mysensors-out --my-mqtt-subscribe-topic-prefix=mysensors-in --my-mqtt-client-id=mygateway1 --my-rf24-irq-pin=15

MySensors Node

The node is a simple temperature and humidity sensing node, using a DHT22. I followed the wiring and programming instructions here, mashed up with the battery sensing code from the Si7021 example. The final sketch is below.

The important thing when using the MQTT gateway is that the MY_NODE_ID must be defined explicitly. As other gateways do not require this some of the examples on the MySensors website omit this definition, so beware!

/**
 * 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: Henrik EKblad
 * Version 1.1 - 2016-07-20: Converted to MySensors v2.0 and added various improvements - Torben Woltjen (mozzbozz)
 * Version X - Hafniumzinc - Added battery sensing from the Si7021 example
 * 
 * DESCRIPTION
 * This sketch provides an example of how to implement a humidity/temperature
 * sensor using a DHT11/DHT-22.
 *  
 * For more information, please visit:
 * http://www.mysensors.org/build/humidity
 * 
 */

// Enable debug prints
#define MY_DEBUG

// Enable REPORT_BATTERY_LEVEL to measure battery level and send changes to gateway
#define REPORT_BATTERY_LEVEL

// Add node ID
#define MY_NODE_ID 2

// Enable and select radio type attached 
#define MY_RADIO_RF24
//#define MY_RADIO_RFM69
//#define MY_RS485
 
#include <SPI.h>
#include <MySensors.h>  
#include <DHT.h>

// Set this to the pin you connected the DHT's data pin to
#define DHT_DATA_PIN 3

// Set this offset if the sensor has a permanent small offset to the real temperatures.
// In Celsius degrees (as measured by the device)
#define SENSOR_TEMP_OFFSET 0

// Sleep time between sensor updates (in milliseconds)
// Must be >1000ms for DHT22 and >2000ms for DHT11
static const uint64_t UPDATE_INTERVAL = 60000;

// Force sending an update of the temperature after n sensor reads, so a controller showing the
// timestamp of the last update doesn't show something like 3 hours in the unlikely case, that
// the value didn't change since;
// i.e. the sensor would force sending an update every UPDATE_INTERVAL*FORCE_UPDATE_N_READS [ms]
static const uint8_t FORCE_UPDATE_N_READS = 10;

#define CHILD_ID_HUM 0
#define CHILD_ID_TEMP 1

float lastTemp;
float lastHum;
uint8_t nNoUpdatesTemp;
uint8_t nNoUpdatesHum;
bool metric = true;

MyMessage msgHum(CHILD_ID_HUM, V_HUM);
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
DHT dht;

#ifdef REPORT_BATTERY_LEVEL
#include <Vcc.h>
static uint8_t oldBatteryPcnt = 200;  // Initialize to 200 to assure first time value will be sent.
const float VccMin        = 1.8;      // Minimum expected Vcc level, in Volts: Brownout at 1.8V    -> 0%
const float VccMax        = 2.0*1.6;  // Maximum expected Vcc level, in Volts: 2xAA fresh Alkaline -> 100%
const float VccCorrection = 1.0;      // Measured Vcc by multimeter divided by reported Vcc
static Vcc vcc(VccCorrection); 
#endif

void presentation()  
{ 
  // Send the sketch version information to the gateway
  sendSketchInfo("TemperatureAndHumidity", "1.1");
  
  // Register all sensors to gw (they will be created as child devices)
  present(CHILD_ID_HUM, S_HUM);
  present(CHILD_ID_TEMP, S_TEMP);
  
  metric = getControllerConfig().isMetric;
}


void setup()
{
  dht.setup(DHT_DATA_PIN); // set data pin of DHT sensor
  if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) {
    Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!");
  }
  // Sleep for the time of the minimum sampling period to give the sensor time to power up
  // (otherwise, timeout errors might occure for the first reading)
  sleep(dht.getMinimumSamplingPeriod());
}


void loop()      
{  
  // Force reading sensor, so it works also after sleep()
  dht.readSensor(true);
  
  // Get temperature from DHT library
  float temperature = dht.getTemperature();
  if (isnan(temperature)) {
    Serial.println("Failed reading temperature from DHT!");
  } else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS) {
    // Only send temperature if it changed since the last measurement or if we didn't send an update for n times
    lastTemp = temperature;

    // apply the offset before converting to something different than Celsius degrees
    temperature += SENSOR_TEMP_OFFSET;

    if (!metric) {
      temperature = dht.toFahrenheit(temperature);
    }
    // Reset no updates counter
    nNoUpdatesTemp = 0;
    send(msgTemp.set(temperature, 1));

    #ifdef MY_DEBUG
    Serial.print("T: ");
    Serial.println(temperature);
    #endif
  } else {
    // Increase no update counter if the temperature stayed the same
    nNoUpdatesTemp++;
  }

  // Get humidity from DHT library
  float humidity = dht.getHumidity();
  if (isnan(humidity)) {
    Serial.println("Failed reading humidity from DHT");
  } else if (humidity != lastHum || nNoUpdatesHum == FORCE_UPDATE_N_READS) {
    // Only send humidity if it changed since the last measurement or if we didn't send an update for n times
    lastHum = humidity;
    // Reset no updates counter
    nNoUpdatesHum = 0;
    send(msgHum.set(humidity, 1));
    
    #ifdef MY_DEBUG
    Serial.print("H: ");
    Serial.println(humidity);
    #endif
  } else {
    // Increase no update counter if the humidity stayed the same
    nNoUpdatesHum++;
  }

#ifdef REPORT_BATTERY_LEVEL
  const uint8_t batteryPcnt = static_cast<uint8_t>(0.5 + vcc.Read_Perc(VccMin, VccMax));

#ifdef MY_DEBUG
  Serial.print(F("Vbat: "));
  Serial.println(vcc.Read_Volts());
  Serial.print(F("Perc: "));
  Serial.println(batteryPcnt);
#endif

  // Battery readout should only go down. So report only when new value is smaller than previous one.
  if ( batteryPcnt < oldBatteryPcnt )
  {
      sendBatteryLevel(batteryPcnt);
      oldBatteryPcnt = batteryPcnt;
  }
#endif

  // Sleep for a while to save energy
  sleep(UPDATE_INTERVAL); 
}

Upload sketch

Arduino IDE setup

  • Tools -> Board -> Arduino Pro or Pro Mini
  • Tools -> Processor -> ATmega 328P (3.3V, 8MHz)
  • Tools -> Port -> /dev/ttyUSB0
  • Tools -> Programmer -> USBasp

Uploading

All guides seem to suggest that once the sketch is complete, you can just press the upload button and the program will be written to the Arduino. This worked only once for me, the first time round on a clean board. Subsequent attempts to the same board ended errors similar to the following:

avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0xa5
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x68
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0xa0
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0xde
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x74
avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x21
avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0xe2
avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0xb2
avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x01
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0xfe

I found that to succesfully write to the Arduino you must press and hold the red reset button as the Arduino IDE says Compiling, and then release as soon as this says Uploading.

image

The IDE will then show Done uploading.

image

openHAB

The gateway publishes messages according to its serial protocol. An example topic, publishing temperature from my node (#2) is shown below:

mysensors-out/2/1/1/0/0

From the previously linked serial protocol page:

  • mysensors-out - base topic, set during the configuration stage of the MQTT gateway
  • 2 - node ID defined in the sketch
  • 1 - sensor ID
  • 1 - command type - in this case set
  • 0 - ack type - in this case, no acklowedgment required
  • 0 - message type - in this case, temperature

bridge.things

I have a separate file with my bridge to Mosquitto defined.

Bridge mqtt:broker:MosquittoMqttBroker "Mosquitto MQTT Broker" [
	host="192.168.1.92",
	secure=false,
	port=1883,
	clientID="OpenHAB2"
]

mysensors.things

I use MQTT Explorer to see what the correct topics are. Far easier to copy and paste rather than try to work out the serial protocol, at least for me!

//NODE 2
Thing mqtt:topic:msSpareRoom "Spare Room" (mqtt:broker:MosquittoMqttBroker) {
	Channels:
		Type number : temperature "Temperature" [ 
			stateTopic="mysensors-out/2/1/1/0/0"
		]
		Type number : humidity "Humidity" [ 
			stateTopic="mysensors-out/2/0/1/0/1"
		]
		Type number : battery "Battery" [ 
			stateTopic="mysensors-out/2/255/3/0/0"
		]
}

mysensors.items

// MySensors NodeID2
Number nSpareRoomTemperature { channel="mqtt:topic:msSpareRoom:temperature" }
Number nSpareRoomHumidity { channel="mqtt:topic:msSpareRoom:humidity" }
Number nSpareRoomBattery { channel="mqtt:topic:msSpareRoom:battery" }
DateTime dtSpareRoomLastUpdate (gSensors) { channel="mqtt:topic:msSpareRoom:temperature" [profile="timestamp-update"]}

Sitemap

image

Text item=nSpareRoomTemperature label="Temperature [%.1f °C]" icon="temperature"
Text item=nSpareRoomHumidity label="Humidity [%.1f %%]" icon="humidity"	
Text item=nSpareRoomBattery label="Battery [%.0f %%]" icon="battery"
Text item=dtSpareRoomLastUpdate label="Last update [%1$ta %1$tR]" icon="time"

EDIT 29/11/2020: Add information on Arduino IDE and uploading procedure.

4 Likes