DIY Home Automation Sensors: The First Sensor

This post is part of a tutorial on building Low-Cost Home Automation Sensors. If you have landed on this page directly I suggest you check first Low-Cost DIY Home Automation Sensors. In this part of the tutorial, you will build the gateway. The gateway will handle the communications for all the nodes in your network.

If you are starting with Arduino you should check the Arduino for Dummies Guide, it will help you get up to speed.

Bill Of Materials

 

Units Description
1 NRF24L01+ 2.4GHz Wireless RF
1 Arduino Nano V3
1 Jumper Cables
1 DHT22 Temperature and Humidity Sensor

 

Build The Circuit

For this particular sensor, I am using an Arduino Nano. It has a very small footprint and works great for nodes that you plan to have connected to a power outlet.

 

 

 

When you buy the DHT22 sensor you might receive one that looks slightly different from the one in the diagram. If you get something that looks like this one, don’t worry.  It is the same sensor but some manufacturers ship it integrated on a PCB only with the 3 pins that are relevant. You just have to connect Out  to D3 and the other 2 to VCC and GND.

Install Additional Libraries

The code for Arduino that you have below requires 2 libraries, MySensors and DHT22.

MySensors

1.Download the latest RC of the MySensors library from this link.

 

2. Add zip file as a new library.

 

 

DHT22

When I wrote this tutorial initially, I was using the library downloaded directly from the library manager in the Arduino IDE. Thanks to the feedback from Nucutza (in the comments) I got to know that the library has been upgraded and the code no longer compiled. As per his suggestion, I am adding the link to the MySensor External Libraries. Among other things, it contains a modified version of the library to interface with DHT11 and DHT22 sensors.

Upload the Sketch to the Arduino

You can find a link on how to upload sketches to Arduino if you are not familiar with the platform yet.

 

Adding new Items to OpenHab

The Arduino sketch you just uploaded will be publishing values in the following topics.

 

Temperature ->  mygateway1-out/1/0/1/0/0
Humidity        -> mygateway1-out/1/1/1/0/1
The definition of the topic above will depend on the parameters defined on your sketch, mostly the node identifier and the id’s that identify each of the sensors.

 

If you change the parameters on the sketch, please make sure you modify the topics accordingly.

 

Temperature ->  mygateway1-out/MY_NODE_ID/CHILD_ID_TEMP/1/0/0
Humidity        -> mygateway1-out/MY_NODE_ID/CHILD_ID_HUM/1/0/1
If you are using OpenHab, make sure you have a look at the MQTT and OpenHab tutorial.
As a reference, here you have the definition of the items for the sketch above.

 

If you want to learn more about the communication protocol of the MySensor library check MySensors Communication Protocol.

 

I hope you liked the tutorial, if you have questions or suggestions please post some comments.

 

This Post Has 13 Comments

  1. nucutza

    The sketch isn’t compiling with the library you mention !

  2. David C.

    Hi Nucutza,

    Thanks for the heads up. I will update it tonight when I get home.

    I noticed when I Did this tutorial http://smarthomeblog.net/smart-arduino-thermostat/

    I had to change a few things o the function that sends the temperatura.

    I will update this code and let you know.

    Thanks again for the heads up!

  3. nucutza

    No problem. However it will compile with the DHT library from MySensorsArduinoExamples

    Thanks for your nice tutoria-lrt. I managed to configure a rpi gateway with rfm69hw radio.
    Furthermore I put an openvpn server on the same pi to acces openhab from internet. (pivpn)
    Now I try to learn more about openhab syntax/configuration. (sitemap, items)

  4. David C.

    Hi Nucutza,

    I have upgraded the post with your feedback, thanks for taking the time on letting me know. I will a-lrto update this week the part where I download the library for MySensor. MySensors 2.3 is already released so it doesn’t make sense to go for the development branch anymore.

    Just out of curiosity, why have you decided to go with a vpn approach instead of using the OpenHab Cloud to grant external access?

    Thanks for your feedback again!

  5. nucutza

    I choosed vpn mainly for privacy/security.
    I don’t trust to put my home sensors on public sites 🙂

  6. David C.

    Are you a-lrto planning to use signed messages with MySensors?

  7. Stefan

    Thx for your tutorial although it took me 2 days to figure out that

    mygateway1-out/7/0/1/0/0 should be mygateway1-out/1/0/1/0/0 and mygateway1-out/1/1/1/0/1

  8. David C.

    Hi Stefan,

    I read it again and that part was actually poorly explained, sorry about that. I have added some detai-lrt and this weekend will review the full post to provide additional explanation.

    Thanks for pointing it out.

    Anything e-lrte that you had difficulties with?

  9. Stefan

    No, The rest of the tut is clear (i,m not a programmer) and It’s working fine now.
    Thanks again.

  10. Jonas

    I followed your tutorial, but I couldn’t get the last step to work.
    The arduino’s log is showing this error message:
    16 MCO:BGN:INIT NODE,CP=RNNNA—,REL=255,VER=2.3.1
    26 TSM:INIT
    27 TSF:WUR:MS=0
    33 TSM:INIT:TSP OK
    35 TSM:INIT:STATID=1
    37 TSF:SID:OK,ID=1
    39 TSM:FPAR
    75 TSF:MSG:SEND,1-1-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    2082 !TSM:FPAR:NO REPLY
    2084 TSM:FPAR
    2120 TSF:MSG:SEND,1-1-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    4128 !TSM:FPAR:NO REPLY
    4130 TSM:FPAR
    4166 TSF:MSG:SEND,1-1-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    6174 !TSM:FPAR:NO REPLY
    6176 TSM:FPAR
    6212 TSF:MSG:SEND,1-1-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    8220 !TSM:FPAR:FAIL
    8221 TSM:FAIL:CNT=1
    8223 TSM:FAIL:DIS
    8225 TSF:TDI:TSL

    Does this mean the gateway isn’t working? Could the arduino be bronken?

    Thank you for your help, I really like your tutorial, I just haven’t been able to get this to work

  11. Paul

    How would you recommend powering a sensor like this? I’ve read a number of things about powering mysensors sensors from batteries, but most use lower voltages than you are using here in order to increase battery lifetime, but that tends to increase some of the complexity of assembly and require other boards and some modifications. Wondering what you use for this type of setup.
    Thanks.

  12. David C.

    Hi Paul,

    The Arduino is not a good candidate to run on batteries, the life of your sensor will be very short. If you want to run on batteries I would do a modified Arduino Mini running with 2 AA batteries. I have a lot of those sensors for by contacts on the doors and windows and the battery lasts for over a year.

    Here you have a tutorial.

    http://smarthomeblog.net/diy-door-sensors-arduino/

  13. David C.

    Hi Jonas,

    This is indeed a communication issue. The node is unable to find a parent. A couple of things:
    – Did you provide a Node ID in the code?
    – Make sure the radio is correctly power at 3.3v.
    – Sometimes the radio chip is defective and also causes this error.

Leave a Reply