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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
#include <DHT.h> #define MY_DEBUG #define MY_RADIO_NRF24 #define MY_NODE_ID 1 #define CHILD_ID_TEMP 0 #define CHILD_ID_HUM 1 #define DHT_DATA_PIN 3 #define SENSOR_TEMP_OFFSET 0 #include <MySensors.h> /**************************************************/ /****************** CONSTANTS *********************/ /**************************************************/ static const uint64_t UPDATE_INTERVAL = 10000; static const uint8_t FORCE_UPDATE_N_READS = 10; /**************************************************/ /****************** VARIABLES *********************/ /**************************************************/ float lastTemp; float lastHum; float temperature; float humidity; uint8_t nNoUpdatesTemp; uint8_t nNoUpdatesHum; /**************************************************/ /****************** MESSAGES **********************/ /**************************************************/ MyMessage msgHum(CHILD_ID_HUM, V_HUM); MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); DHT dht; void presentation() { present(CHILD_ID_HUM, S_HUM); present(CHILD_ID_TEMP, S_TEMP); } void setup() { delay(2000); //Wait 2 seconds before starting sequence if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) { Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!"); } sleep(dht.getMinimumSamplingPeriod()); dht.setup(DHT_DATA_PIN); } void loop() { sendTemperatureHumiditySensor(); wait(UPDATE_INTERVAL); } /**************************************************/ /**************** AUX. FUNCTIONS ******************/ /**************************************************/ void sendTemperatureHumiditySensor() { dht.readSensor(true); temperature = dht.getTemperature(); humidity = dht.getHumidity(); if (isnan(temperature)) { Serial.println("Failed reading temperature from DHT!"); } else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS) { lastTemp = temperature; nNoUpdatesTemp = 0; temperature += SENSOR_TEMP_OFFSET; send(msgTemp.set(temperature, 1)); #ifdef MY_DEBUG Serial.print("T: "); Serial.println(temperature); #endif } else { nNoUpdatesTemp++; } if (isnan(humidity)) { Serial.println("Failed reading humidity from DHT"); } else if (humidity != lastHum || nNoUpdatesHum == FORCE_UPDATE_N_READS) { lastHum = humidity; nNoUpdatesHum = 0; send(msgHum.set(humidity, 1)); #ifdef MY_DEBUG Serial.print("H: "); Serial.println(humidity); #endif } else { nNoUpdatesHum++; } } |
Adding new Items to OpenHab
The Arduino sketch you just uploaded will be publishing values in the following topics.
1 2 3 |
#define MY_NODE_ID 1 #define CHILD_ID_TEMP 0 #define CHILD_ID_HUM 1 |
If you change the parameters on the sketch, please make sure you modify the topics accordingly.
1 2 |
Number mqtt_bedroom_temperature "Temperature [%.1f]" {mqtt="<[mosquitto:mygateway1-out/1/0/1/0/0:state:default]"} Number mqtt_bedroom_humidity "Humidity [%.1f]" {mqtt="<[mosquitto:mygateway1-out/1/1/1/0/1:state:default]"} |
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.
The sketch isn’t compiling with the library you mention !
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!
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)
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!
I choosed vpn mainly for privacy/security.
I don’t trust to put my home sensors on public sites 🙂
Are you a-lrto planning to use signed messages with MySensors?
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
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?
No, The rest of the tut is clear (i,m not a programmer) and It’s working fine now.
Thanks again.
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
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.
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/
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.