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.
Bill Of Materials
Units | Description |
1 | NRF24L01+ 2.4GHz Wireless RF |
1 | Jumper Cables |
1 | Raspberry Pi Kit |
Wiring Things Up!
For the gateway, the only wiring necessary it to connect the Raspberry Pi to the radio, the NRF24L01. This step is very simple but you have to pay a lot of attention. THE GPIO pins in the Raspberry Pi are not labeled which means that you are going to be counting pins until you go blind or get your radio to work 🙂
Let’s hope for the latter…
Wiring the Radio
ID | PIN (Raspy) | NRF24L01 | ID | PIN (Raspy) | NRF24L01 |
1 | 3.3V POWER | VCC | 2 | 5V POWER | |
3 | GPIO 2 | 4 | 5V POWER | ||
5 | GPIO 3 | 6 | GND | GND | |
7 | GPIO 4 | 8 | UART0 TX | ||
9 | GND | 10 | UART0 RX | ||
11 | GPIO 17 | 12 | GPIO 18 | ||
13 | GPIO 27 | 14 | GND | ||
15 | GPIO 22 | 16 | GPIO 23 | ||
17 | 3.3V POWER | 18 | GPIO 24 | ||
19 | GPIO 10 | MOSI | 20 | GND | |
21 | GPIO 9 | MISO | 22 | GPIO 25 | CE |
23 | GPIO 11 | SCK | 24 | GPIO 8 | CSN |
25 | GND | 26 | GPIO 7 | ||
27 | Reserved | 28 | Reserved | ||
29 | GPIO 5 | 30 | GND | ||
31 | GPIO 6 | 32 | GPIO 12 | ||
33 | GPIO 13 | 34 | GND | ||
35 | GPIO 19 | 36 | GPIO 16 | ||
37 | GPIO 26 | 38 | GPIO 20 | ||
39 | GND | 40 | GPIO 21 |
Installing MySensors
1. SSH into your Raspberry Pi
2. Clone MySensors repository.
1 |
git clone https://github.com/mysensors/MySensors.git --branch development |
In the command, you can see that I am using the development branch. The reason is that I find it to be a lot more stable for the Raspberry Pi than the current version of MySensors. Besides, it is already an RC so it is probably close to a final release.
3. Navigate to the directory created
1 |
cd MySensors |
4. Run ./Configure:
1 |
./configure --my-gateway=mqtt --my-controller-ip-address=127.0.0.1 --my-mqtt-publish-topic-prefix=mygateway1-out --my-mqtt-subscribe-topic-prefix=mygateway1-in --my-mqtt-client-id=mysensors-1 --my-mqtt-user=openhabian --my-mqtt-password=smarthomeblog |
Make sure you change the password in the command above by the one you used when you installed the MQTT Broker!!
3. Execute make
1 |
make |
4. Any Errors?
If everything looks green you can go ahead and test the connectivity. If this is not the case no worries, I got you covered, go to the troubleshooting section at the end of the post.
5. Let’s test to see if everything went fine.
1 |
sudo ./bin/mysgw |
If everything went ok, your gateway log will look like the image above.
6. Install the gateway as a service
1 2 |
sudo make install sudo systemctl enable mysgw.service |
The last step is to install the gateway as a service. In this way it will be automatically started after you reboot your Raspberry Pi.
7. Reboot the Raspberry Pi to test the changes
If you want to verify that the gateway is up and running, you can check the logs here:
1 |
tail -f /var/log/syslog |
All set?
It is time to build the first sensor then. Go to the last step of the tutorial, Low-Cost DIY Home Automation Sensors: Your First Sensor.
Troubleshooting
No supported SPI driver detected
If the step 3 finishes with an error related to SPI.h it means that you need to enable SPI in your raspberry pi.
1. Enter into the raspy-config tool.
1 |
sudo raspi-config |
If you get an error saying that it doesn’t find raspy-config you will have to install it.
1 |
sudo apt-get raspi-config |
2. Enable the SPI interface in raspy-config
3. Reboot the Raspberry Pi and run make again.
2. MySensors Gateway fails to connect
If you see a log like this one when testing the gateway, 98% of the cases it means that the wiring is incorrect. Review every connection between the Raspberry Pi and the Radio and make sure they are aligned with the table above. Very rarely, it can happen to you that the NRF24L01 is broken. I had to replace mine when I was doing the tutorial…NRF24L01
1 2 3 4 5 6 7 8 |
mysgw: Starting gateway... mysgw: Protocol version - 2.2.0-beta mysgw: MCO:BGN:INIT GW,CP=RNNG---,VER=2.2.0-beta mysgw: TSM:INIT mysgw: TSF:WUR:MS=0 mysgw: !TSM:INIT:TSP FAIL mysgw: TSM:FAIL:CNT=1 mysgw: TSM:FAIL:PDT |
For step 5, mysgw rejected the -d argument. It seemed to work ok just by dropping the -d argument.
Thanks for adding the info Ken, I will check it out!
Yah same here, rejected -d argument..
and I spent 2 hrs troubleshooting.. only to findout that yes my nRF24 was also fried hahaha! should of tried to exchange it before wasting 2 hrs 🙁
Hey Nar,
I just removed the -d to avoid confusion. Do you get it working finally?
Thanks David,
Not over MQTT, however your tutorial helped me a lot 🙂 I got it to work over Serial instead which enough for my needs for now 🙂
Hi CK,
Do you see messages into your MQTT broker?