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 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.

mysensors-1 hardware

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

posting from mysensors page

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.

It is worth noting that the download package includes not only the required MySensors files, but also the compatible libraries required for each possible sensor. To avoid conflicts, I would recommend simply backing up the entire current libraries folder and replacing it with all the files from the download package.

The wiring for the gateway is slightly different; once you have an ethernet shield, use the following pins for the radio:

  • CE for pin 5
  • DNS to pin 6
  • SCK to pin A0
  • MOSI to attach A1
  • MISO to attach A2

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

We have to do this because both the radio and the firewall use SPI and are not compatible; so we just shift the SPI of the radio to some other pins and do the SPI communication in software instead (hence program SPI).

Upload sketch MySensors / GatewayW5100MQTTClient . If you don’t see this in the MySensors menu, you don’t have development branch . Use the link above to redownload the entire library.

mysensors gateway

You need to define a static IP address for the controller, the IP address of your network router and subnet, and the IP address of your existing MQTT server. Feel free to change the theme prefixes if you like. Download and connect this thing to the network. Briefly check the Serial console for obvious errors such as being unable to connect to your MQTT server, otherwise set it aside (but leave it enabled).

IP addresses to change

Creating Sensor Nodes

First, comment out this line #define SOFTSPI in file MyConfig.h else times by putting // at the beginning. This is only needed for the gateway — we use standard NRF24L01 wiring for sensor nodes that use hardware SPI. If you need to be reminded:

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

Then connect the sensor of your choice; I’m using a DHT11 humidity and temperature sensor for testing, but if you scroll through the list of sensors and actuators on the sidebar of the MySensors page, you’ll find a huge selection of other options — doors, rain sensor, light, motion, and even RFID — and loads more. You can see that I also added a relay to the node below, but more on that later.

misensor hardware -2

Finally download the example HumiditySensor from the MySensors menu and add the following line right after the comments.

  #define MY_NODE_ID 2 

Because we’re using a custom version of the controller that simply redirects data to our own MQTT server, it doesn’t have a standard controller function that automatically assigns node IDs to each new node. Instead, we’re just going to manually define it each time. Write this number down somewhere for your own entries and change it for each node.

Enable debug output too:

  #define MY_DEBUG 

Finally, check that your DHT11 sensor output is correct.


Then download!

It’s worth opening up the serial console to take a look. The key bit to look at is st= which is the status of the message. st = fail means the message was not sent. You may not have defined a unique host ID or your gateway is disabled. I faked these errors by simply disabling the gateway:


If everything works, you should start seeing some data coming into your MQTT server. Putting them into OpenHAB is outside the scope of this tutorial, but was covered in Part 2 of the OpenHAB guide. Management so you can go back there.

Merging sensor code

While it’s relatively easy to get a single sensor node up and running, it gets a little tricky when you want to add multiple sensors to each node. Basically, you are going to mix code snippets from two different examples. The easiest way to show this is with a video example! Here I am combining our main humidity sensor with a relay.

You can find the complete humidity sensor and relay code 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 a relay, check out the Serial API — but suffice it to say that the following channel controls the first relay in the code I gave (with message body 0 or 1):

  mysensors в / 9/1/1/0/2 

Your only limit right now is the amount of memory on the Arduino, and I’ll put it this way: The most reliable sensors in my smart home are not $80 commercial Z-Wave modules, but custom MySensors modules.

I’ll finish this up today, but if you run into problems, you can post them in the comments or on the very active MySensors user forums. Will you build your own cheap sensor nodes? How is your smart home coming along?

Похожие записи