Family Encyclopedia >> Home & Garden

Smart Home Sensors DIY with Arduino, MySensors and OpenHAB

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

Fortunately, the hard work has already been done by the people behind MySensors.org. Today I'm going to show you how to use the MySensors framework to create a cheap range of smart home sensors that can be integrated into an OpenHAB installation (see our Getting Started with OpenHAB on Raspberry Pi guide) Getting Started with OpenHAB Home Automation on Raspberry Pi Getting started with OpenHAB Home Automation on Raspberry Pi OpenHAB is a mature, open source home automation platform that runs on a variety of hardware and is protocol agnostic, meaning it can connect to almost any home automation hardware on the market. current. Read more . I assume you already have an MQTT server up and running and understand the basics of the MQTT messaging format; otherwise, be sure to read the OpenHAB guide follow-up, which includes instructions for installing an MQTT server. , the open source home automation software, far exceeds the capabilities of other home automation systems on the market, but it's not easy to set up. In fact, it can be downright frustrating. Read more.

Although I will be dealing specifically with sending the MySensors data from a network of Arduinos, using MQTT, to an OpenHAB installation, I should note that the MySensors framework can be incorporated into a number of different home automation controllers. I presented this here as a complete solution that works for me, but check out the MySensors site as it may also suit your similar purpose, but with slightly different hardware or different messaging protocol. It's incredibly versatile..

Smart Home Sensors DIY with Arduino, MySensors and OpenHAB

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 temperature and humidity sensor for example is approx. $1 each, a high voltage relay is about $3).

What we are doing

The basic idea is that instead of adding to our current cluttered local network using unreliable Wi-Fi or expensive Ethernet shields, we create a fully independent mesh-capable network just for the Arduinos; then transfer it to the local network using a single gateway node, which has an Ethernet connection and a radio connection to the other Arduinos. So we're making a couple of sensor nodes, which collect data; and a gateway node, which transmits that 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 with unnecessary data. If you're happy with Wi-Fi, look at the inexpensive Arduino-compatible ESP8266. Meet the Arduino Killer:ESP8266 Meet the Arduino Killer:ESP8266 What if I told you there is an Arduino-compatible development board with built-in Wi-Fi? -Fi for less than $10? Well, there is. Read More

Important note for users of El Capitan and Arduino clones: Apple managed to break the serial drivers used to communicate with a number of Arduino clone boards in the latest El Capitan release thanks to new security measures. To see if it's affected, look at your card and the chip closest to the USB port. If it says CH340, you are affected by this. Follow the instructions here to disable kext driver signing, reinstall CH340 serial drivers.

Required Components

For entry, you will need:

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

For each sensor node:

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

Additional / Optional:

  • 10uF capacitors, one for each RF module you have (link is for a pack of 50!)
  • Power supply with 5v and 3.3v output (YwRobot MB102 works fine and cost $1 each) - Required if using an Arduino clone. You'll also need a 9-12v DC power supply for these.
  • Prototyping prototypes, or short male-female jumper wires.

Working with the NRF24L01 modules

Let's start with the NRF24L01 modules, since they are the most complex part of the project. I opted for the more expensive and longer range version of these:technically known as NRF24L01 + PA + LNA . They come with an on-board signal booster circuit and an antenna connection, though I'd suggest trying the cheaper no-antenna version of the modules first if you have a normal home with regular walls, not the one meter solid stone wall thickness I do. The claimed range of these is about a kilometer, more than enough for me to put them in the garden shed.

However, these things are Really difficult to work with; If you go ahead and connect everything without reading these tips first, you'll be disappointed.

  • The module needs 3.3V input power to the VCC pin. - not 5v . If you put 5v, you will fry it.
  • Solder a 10uF capacitor across the VCC/GND terminals. The solid gray line on the capacitor indicates the negative / GND side.
  • Use short, high-quality jump cables; or better yet, solder directly to a prototyping shield to keep wire lengths to a minimum and connections solid.
  • If you're using an Arduino clone, the voltage regulator doesn't provide enough for them on the 3.3v pin; you'll have to use an external power supply board (linked above), available for about $1 each. These provide a stable 3.3v. If you are using an original Arduino Uno brand, this doesn't seem to be a problem..

I suggest you do some basic tests first to establish that your radio is working. Connect two radios as shown on the MySensors page. It doesn't matter that your diagram shows Arduino Micro boards, the same pin numbers are used. Note that the diagram shows the NRF24L01 from the top; You will actually be connecting things from the bottom . Mentally adjust accordingly. Ignore the gray IRQ pin, not currently used. In short:

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

Smart Home Sensors DIY with Arduino, MySensors and OpenHAB

You will need two fully wired nodes to test. Download the RF24 library and load the simplest one. Getting started example. Power on both modules, but leave one USB connected and open the serial console. Type “T” and send, to switch it to broadcast mode, at which point you should receive debug messages indicating that you are successfully pinging the other node.

Building the MySensors MQTT Client Gateway

Ok, 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 be fine with later versions as well. We are using the development branch because at the time of writing, the MQTT client The gateway is not yet part of the main package.

Although there is MQTTGateway 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 the MySensors data to that. Again, if this isn't what you want, if you prefer not to use MQTT, check out EthernetGateway or SerialGateway, which are also supported by OpenHAB.

It's worth noting that the download package not only includes essential MySensors files, but also includes supporting libraries needed for every possible sensor. To avoid conflicts, I'd suggest just making a backup of the entire current libraries folder and replacing it with the ones from the download package.

Wiring for the gateway is a bit 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 proceedings. Do this by removing the // to uncomment it, it's around line 309 in mine.

We need to do this because both the radio module and the network shield use SPI, and they are incompatible; so we just change the SPI radio module to some other pins and do the SPI communication in software (thus, smooth SPI).

Load the MySensors / GatewayW5100MQTTClient Sketch. If you don't see this in the MySensors menu, you don't have the development branch installed. Use the link provided above to re-download the entire library.

Smart Home Sensors DIY with Arduino, MySensors and OpenHAB

You will need to define a static IP address for the controller, the IP of your network and your network, and the IP address of your existing MQTT server. Feel free to modify the theme prefixes as well if you wish. Go up and connect that thing to the network. Briefly review the Serial console for obvious errors, such as not being able to connect to your MQTT server; otherwise, set it aside (but leave it on).

Smart Home Sensors DIY with Arduino, MySensors and OpenHAB

Building the sensor nodes

First, comment that #define SOFTSPI line in the MyConfig.h file again, putting the // at the beginning again. Only needed for the gateway - we are using standard NRF24L01 cabling for the sensor nodes, which use SPI hardware. If you need to remember:

  • 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 rail
  • CE to pin 9
  • CSN / CS to pin 10
  • MOSI to pin 11
  • MISO to pin 12
  • SCK to pin 13

Next, connect your sensor of choice; I use the DHT11 humidity and temperature sensor for testing, but if you scroll down to the list of sensors and actuators in the sidebar of the MySensors page, you will find a large selection of other options:doors, rain gauge, light, motion , even RFID - and loads more. You can see that I also added a relay to the node shown below, but more on that later.

Smart Home Sensors DIY with Arduino, MySensors and OpenHAB

Finally, load the Humidity sensor MySensors menu example and add the following line immediately after the comments.

#define MY_NODE_ID 2

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

Enable debug output too:

#define MY_DEBUG

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

# define HUMIDITY_SENSOR_DIGITAL_PIN 7

Then upload!

It's worth opening up the stock console to take a look. The crucial bit to look at is st = , what is the status of the message. st =fail it means the message was not sent You may not have defined a unique node ID or your gateway is offline. I faked these failures by simply unplugging the gateway:

Smart Home Sensors DIY with Arduino, MySensors and OpenHAB

If everything is working, you should start to see some data reads coming into your MQTT server. Putting them into OpenHAB is beyond the scope of this tutorial, but was covered in part 2 of the OpenHAB guide. Open source home automation software far exceeds the capabilities of other home automation systems on the market, but it's not easy to set up. In fact, it can be downright frustrating. Read More

Combining the sensor code

While getting a single sensor node up and running is relatively simple, it gets a bit more complicated when you want to add multiple sensors to each node. You are essentially combining the code snippets from two different examples. The easiest way to show you this is with a video! Here I am combining our basic humidity sensor with a relay.

You can find the completed code for the humidity sensor and relay here, which has already been modified with a non-blocking loop as I mentioned in the video. For more information on the MQTT command structure needed to trigger the relay, check out the Serial API, but suffice it to say that 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 will say this:The most reliable sensors in my smart home are not the $80 commercial Z-Wave modules, but the custom MySensor ones.

I'll wrap it up there today, but if you're having trouble, feel free to ask in the comments or on the very active MySensors user forums. Will they be putting together their own cheap sensor nodes? How's your smart home going?