DIY Smart Home Sensors with Arduino, MySensors and OpenHAB

Smart home sensors cost a ridiculous amount of money. At $60-$100 just for a motion detector or humidity sensor, wiring a whole house becomes the domain of those with silly amounts of disposable income. Let’s build our own.

Luckily, the hard work has already been done by the fine folks behind MySensors.org. Today I’ll be showing you how to use the MySensors framework to create a cheap array of smart home sensors that can feed into an OpenHAB install (see our guide to Getting Started with OpenHAB on Raspberry Pi). I’m assuming you have a working MQTT server already, and understand the basics of the MQTT messaging format; if not, be sure to read the follow up to the OpenHAB guide, which includes instructions for installing an MQTT server.

Although I’ll be dealing specifically with forwarding the MySensors data from a network of Arduinos, using MQTT, to an OpenHAB install, I should point out that the MySensors framework can feed into a number of different home automation controllers. I’ve presented this here as a complete solution that works for me, but please check out the MySensors site as you might find it also suits your similar purpose, but using slightly different hardware or a different messaging protocol. It’s incredibly versatile.

mysensors hardware -1

The hardware cost for my setup is less than $10 per node (a little more for the gateway), but additional sensors and actuators can be added for very little (the DHT11 humidity and temperature sensor for instance is about $1 each; a high voltage relay is around $3).

What We’re Making

The basic idea is that rather than adding onto our existing crowded local network using unreliable Wi-Fi or expensive Ethernet shields, we create an entirely separate mesh-capable network just for the Arduinos; then bridge that to the local network using a single gateway node, which has both an Ethernet connection and a radio connection to the other Arduinos. So we’re making a couple of sensor nodes, which gather data; and a gateway node, which relays that data onto the OpenHAB server.

Again, this is what works for me, because my Wi-Fi reception is so horrendous and I don’t want to clutter it with unnecessary data. If you’re happy with Wi-Fi, look at using the low cost ESP8266 Arduino-compatible with built-in Wi-Fi boards – MySensors supports those too.

Important Note for El Capitan Users and Arduino Clones: Apple managed to break the serial drivers used to communicate with a number of Arduino clone boards in the latest release of El Capitan thanks to new security measures. To see if you’re affected, look at your board and the chip closest the USB port. If it says CH340, you’re affected by this. Follow the instructions here to disable driver kext signing, install CH340 serial drivers again.

Components Needed

For the gateway, you’ll need:

  • Arduino Uno
  • Ethernet Shield (W5100 based)
  • NRF24L01 module – I’ve used the +PA+LNA versions throughout, which have an increased range of up to 1km. The wiring is the same whichever you choose.

For each sensor node:

  • Arduino Uno
  • NRF24L01 module
  • Sensors (to start with, I’d suggest a temperature and humidity DHT11 or DHT22 module)

Additional / Optional:

  • 10uF capacitors, one for each RF module you have (the link is for a pack of 50!)
  • Power Supply with 5v and 3.3v output (YwRobot MB102 works well and they’re $1 each) – required if using a clone Arduino. You’ll also need a 9-12v DC power supply for these.
  • Prototyping Shields, or short male-female jumper wires.

Working with NRF24L01 Modules

Let’s start with the NRF24L01 modules, as they’re most complex part of the project. I went for the more expensive, longer range version of these: technically known as NRF24L01+PA+LNA. They come with an on-board signal amplification circuit and antenna connection, though I suggest you try the cheaper non-antenna version of the modules first if you have a normal home with regular walls, not the meter-thick solid stone kind of wall that I do. The claimed range of these is about a kilometer, more than enough for me put into the garden shed.

However, these things are really difficult to work with; if you go ahead and plug everything in without reading these tips first, you will be disappointed.

  • The module needs 3.3v input power to the VCC pin – not 5v. If you put in 5v, you’ll fry it.
  • Solder a 10uF capacitor across the VCC/GND terminals. The solid grey line on the capacitor indicates the negative/GND side.
  • Use short, high quality jump cables; or better still, solder them directly to a prototyping shield to keep cable length to a minimum and solid connections.
  • If you’re using an Arduino clone, the voltage regulator doesn’t quite provide enough for them on 3.3v pin – you’ll need to use an external power supply board (linked above), available for about $1 each. These provide a stable 3.3v. If you’re using an original Arduino brand Uno, this doesn’t appear to be an issue.

I strongly suggest you do some basic tests first just to establish your radio is working. Wire two radios up as shown on the MySensors page. It doesn’t matter that their diagram show Arduino Micro boards – the same pin numbers are used. Note that the diagram shows the NRF24L01 from the top; you’ll actually be plugging things in from the underside. Mentally adjust accordingly. Ignore the gray IRQ pin, it’s not currently used. In summary:

  • VCC goes to 3.3v on your external power supply
  • GND goes to common ground rail
  • CE to pin 9
  • CSN/CS to pin 10
  • MOSI to pin 11
  • MISO to pin 12
  • SCK to pin 13

wiring from mysensors page

You’ll need two nodes fully wired to test with. Download the RF24 library and load the simplest Getting Started example. Power on both modules, but leave one connected over USB and open up the serial console. Type “T” and send, to switch it into transmit mode, at which point you should get debug messages saying it’s successfully pinging a message to the other node.

Building the MySensors MQTT Client Gateway

Ok, now that we know the RF24 radios are wired in and working correctly, go ahead and download the development branch of MySensors Arduino package. This tutorial was written using version 1.5, but should be ok with later versions too. We’re using the development branch because at the time of writing, the MQTT client gateway isn’t yet a part of the main package.

Although there is MQTTGateway available in the master branch, it acts as a server too, which we don’t want because we already have a stable MQTT server running on the Raspberry Pi. We just want to forward the MySensors data onto that. Again, if this isn’t what you want – if you’d rather not use MQTT at all – then look at EthernetGateway or SerialGateway, both of which are also compatible with OpenHAB.

It’s worth noting that the download package doesn’t just include essential MySensors files, it also includes compatible libraries needed for every possible sensor. To avoid conflicts, I’d suggest just backing up your entire current libraries folder and replacing it all with the ones in the download package.

Wiring for the gateway is a little different; once you have your Ethernet shield, use the following pins for the radio module:

  • CE to pin 5
  • CSN to pin 6
  • SCK to pin A0
  • MOSI to pin A1
  • MISO to pin A2

You also need to enable the #define SOFTSPI line in the libraries/mySensors/MyConfig.h file. Do this by removing the // to uncomment it, it’s around line 309 on mine.

We need to do this because both the radio module and network shield use SPI, and they’re incompatible; so we just shift the radio module SPI to some other pins and perform SPI communication in software instead (hence, soft SPI).

Load up the MySensors/GatewayW5100MQTTClient sketch. If you don’t see this under the MySensors menu, you don’t have the development branch installed. Use the link provided above to re-download the whole library.

mysensors gateway

You’ll need to define a static IP address for the controller, the IP of your network router and subnet, and the IP address of your existing MQTT server. Feel free to modify the topic prefixes too if you like. Upload, and plug that thing into the network. Briefly check the Serial console for any glaring errors like being unable to connect to your MQTT server, otherwise set it aside (but leave it on).

ip addresses to change

Building the Sensor Nodes

First, comment out that #define SOFTSPI line in the MyConfig.h file again by putting the // back at the start. It’s only needed for the gateway – we’re using the standard NRF24L01 wiring for the sensor nodes, which use hardware SPI. If you need reminding:

  • VCC goes to 3.3v on your external power supply (or on the Arduino itself if it’s an original and not a clone)
  • GND goes to common ground rail
  • CE to pin 9
  • CSN/CS to pin 10
  • MOSI to pin 11
  • MISO to pin 12
  • SCK to pin 13

Next, wire up your choice of sensor; I use DHT11 moisture and temperature sensor for testing, but if you scroll down to the sensors and actuators list on the sidebar of the MySensors page, you’ll find a huge selection of other options: doors, rain gauge, light, motion, even RFID – and loads more. You can see I also added a relay to the node pictured below, but more on that later.

mysensors hardware -2

Finally, load up the HumiditySensor example from the MySensors menu and add the following line immediately after the comments.

#define MY_NODE_ID 2

Since we’re using a special version of the controller which just forwards things onto our own MQTT server, it doesn’t have the standard controller feature that would automatically assign node IDs to each new node. Instead we’re just going to manually define it each time. Note down this number somewhere for your own records, and change it for each node.

Enable the debug output too:

#define MY_DEBUG

Finally, check the data pin of your DHT11 sensor is correct.

#define HUMIDITY_SENSOR_DIGITAL_PIN 7

Then, upload!

It’s worth opening up the serial console to take a look. The crucial bit to look is st=, which is the status of the message. st=fail means the message wasn’t sent. It’s possible you haven’t defined a unique node ID, or that your gateway is offline. I faked these failures by simply unplugging the gateway:

st-fail

If everything is working, you should start to see some data readings come into your MQTT server. Putting those into OpenHAB is outside the scope of this tutorial, but was covered in part 2 of the OpenHAB guide so you can refer back there.

Combining Sensor Code

Although getting a single sensor node up and running is relatively simple, it gets a little trickier when you want to add multiple sensors to each node. Essentially you’re going to blend together the code snippets from from two different examples. The easiest way to show you this is by example with a video! Here I’m combining our basic humidity sensor with a relay.

You can find the completed code for humidity sensor and relay here, which has already been modified with a non-blocking loop as I mentioned in the video. To learn more about the MQTT command structure needed to activate the relay, check the Serial API –but suffice to say the following channel controls the first relay in the code I’ve given (with a message body of 0 or 1):

mysensors-in/9/1/1/0/2

Your only limitation now is the amount of memory on the Arduino, and I’ll say this – the most reliable sensors in my smart home are not the commercial Z-Wave modules that cost $80, but the MySensors custom ones.

I’ll end it there today, but if you have problems you can ask away in the comments or the very active user forums of MySensors. Will you be putting together your own cheap sensor nodes? How is your smart home coming?