Smart home sensors cost a ridiculous amount of money. From $60 to $100 just for a motion detector or a humidity sensor, the wiring of the whole house becomes the property of those who have a stupid income. Let’s build our own.
Luckily, the hard work has already been done by the good folks at MySensors.org. Today I’ll show you how to use the MySensors framework to create a cheap set of smart home sensors that can be used to install OpenHAB (see our guide to getting started with OpenHAB on Raspberry Pi) I’m assuming you already have a working MQTT server and understand the basics of the MQTT message format; if not, be sure to read the continuation of the OpenHAB guide, which includes instructions for installing an MQTT server.
While I will be specifically concerned with forwarding MySensors data from the Arduinos network using MQTT to install OpenHAB , I should note that the MySensors framework can be used in various home automation controllers. I have presented it here as a complete solution that works for me, but please visit the MySensors site as you may find that it also suits your similar purposes but using slightly different hardware or a different messaging protocol. It’s incredibly versatile.
The hardware cost for my installation is less than $10 per node (slightly more for a gateway), but additional sensors and actuators can be added at very little cost (e.g., a DHT11 humidity and temperature sensor costs about $1 each; voltage is about $3).
What are we doing
The basic idea is that instead of adding to our existing crowded LAN using unreliable Wi-Fi or expensive Ethernet shields, we create a completely separate mesh-enabled network just for Arduinos; then connect this to the LAN using a single gateway node that has both an ethernet connection and a radio link to other Arduinos. So we create a couple of sensor nodes that collect data; and a gateway node that relays this data to the OpenHAB server.
Again, this is what works for me because my Wi-Fi reception is so terrible and I don’t want to clutter it up with unnecessary data. If you’re happy with Wi-Fi, look into using an inexpensive ESP8266 compatible with built-in Wi-Fi cards — MySensors also supports them.
Important note for El Capitan and Arduino clone users: 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 to the USB port. If CH340 is written, it will affect you. Follow the instructions here to disable driver kext signing, install the CH340 serial port drivers again.
Required Components
For the gateway you will need:
- Arduino Uno
- Ethernet Shield (based on W5100)
- NRF24L01 module — I used versions + PA + LNA with extended range up to 1 km. The wiring diagram is the same no matter which one you choose.
For each sensor node:
- Arduino Uno
- NRF24L01 module
- Sensors (for starters, I would suggest a DHT11 or DHT22 temperature and humidity module)
Optional / Optional:
- 10uF capacitors, one for each RF module you have (link to pack of 50!)
- Power supply with 5V and 3.3V output (YwRobot MB102 works well and costs $1 each) – required when using an Arduino clone. You will also need a 9-12V DC power supply for this.
- Prototyping shields, or short jumpers between men and women.
Working with NRF24L01 modules
Let’s start with the NRF24L01 modules as they are the most complex part of the project. I chose a more expensive version with a longer range: technically known as NRF24L01+PA+LNA . They come with built-in signal amplification circuitry and an antenna connection, although I suggest trying the cheaper version without the modules antenna first if you have a regular house with regular walls, rather than a meter-thick solid stone wall that I do. The stated range of these is about a kilometer, more than enough for me to put in a garden shed.
However, with these things really difficult to work; if you go ahead and include everything without first reading these tips, you will be disappointed.
- The module needs 3.3V power on the VCC pin, and not 5 V. If you plug in 5v you will fry it.
- Solder a 10uF capacitor across the VCC/GND terminals. The solid gray line on the capacitor indicates the negative side of /GND.
- Use short, high quality connection cables; or better yet, solder them directly to the prototype screen to keep cable length to a minimum and secure connections.
- If you’re using an Arduino clone, then the voltage regulator doesn’t provide enough 3.3V pin voltage for them — you need to use an external power supply board (see above), available for about $1 each. They provide stable 3.3v. If you are using original Uno brand from Arduino, that’s not a problem.
I highly recommend that you do some basic testing first to make sure your radio is working. Connect two radios as shown on the MySensors page. It doesn’t matter that their diagram shows Arduino Micro boards — they use the same pin numbers. Please note the diagram shows NRF24L01 above ; you will actually connect things on the back side . Mentally adjust accordingly. Ignore the gray IRQ pin, it is currently unused. Eventually:
- VCC goes to 3.3V on your external power supply
- GND goes to common ground rail
- CE for pin 9
- CSN/CS for output 10
- MOSI to pin 11
- MISO for output 12
- SCK for pin 13
You will need two fully connected nodes to test. Download the RF24 library and download the simplest example » Getting Started » . Turn on both modules but leave one connected via USB and open the serial console. Type «T» and send to switch it to transmit mode, at which point you should get debug messages that it successfully sends a message to the other node.
Creating a MySensors MQTT Client Gateway
Okay, now that we know the RF24 radios are connected and working properly, go ahead and download the development branch of the MySensors Arduino package. This tutorial was written using version 1.5, but should also work for later versions. We use the development branch because at the time of writing client the MQTT gateway was not part of the core package yet.
While MQTTGateway is available on the master branch, it also acts as a server, which we don’t want because we already have a stable MQTT server running on the Raspberry Pi. We just want to forward MySensors data to this. Again, if that’s not what you’re looking for — if you don’t want to use MQTT at all — then look into EthernetGateway or SerialGateway , both of which are also compatible with OpenHAB.