Step-by-step procedure to connect the NRF24L01+ to the GPIO pins and use the Raspberry as a Serial Gateway (MySensors 1.x)
-
Note: This guide is only applicable to MySensors 1.x. For MySensors 2, use this guide.
I noticed that a step-by-step procedure to connect the NRF24L01 to the GPIO pins and use the Raspberry as a Serial Gateway was listed in the document for MySensors 1.6.
Since I have three gateways configured this way, and I need to set up 3 more for covering different offices around the world, and I had already written the instructions in Swedish for Datormagazin, I thought I'd help out.
This is the first draft. Any feedback is welcome.
INTRODUCTION
The radio module NRF24L01+ is cheap and power efficient, but it is unable to communicate with regular wifi. The bridge between NRF24L01+ nodes and the "computer world" is called a Gateway. You can use other types of gateways, but connecting the NRF24L01+ module directly to the Raspberry Pi is a simple and cheap alternative.Wiring Things Up
Connect the NRF20L01+ radio module to the Raspberry Pi like this:
![raspi_mysensors[1].png](/uploads/files/1448223387663-raspi_mysensors-1.png)
Raspberry Pi NRF24L01+ Color 6 / GND GND Black 1 / 3.3V DC VCC Red 22 / GPIO25 CE Orange 24 / GPIO 8 CSN/CS Yellow 23 / GPIO11 / SPI_CLK SCK Green 19 / GPIO10 / SPI_MOSI MOSI Blue 21 / GPIO9 / SPI_MISO MISO Violet The IRQ pin on NRF24L01 is not currently used.
For a comprehensive view of the Raspberry Pi pins, see http://pinout.xyz/
You should also connect a decoupling capacitor to the radio. See this guide.
COMPILING THE GATEWAY
Login to your Raspberry Pi (using SSH or open a terminal on the graphical console) and run these commands:git clone https://github.com/TMRh20/RF24.git cd RF24 make all && sudo make install cd .. git clone https://github.com/mysensors/Raspberry.git cd Raspberry make all && sudo make installIf you get the following error:
pi@raspberrypi ~/RF24 $ make all && sudo make install g++ -Wall -fPIC -Ofast -mfpu=vfp -mfloat-abi=hard -mtune=arm1176jzf-s -march=armv7-a -D BCM2835_PERI_BASE=0x -c RF24.cpp gcc -Wall -fPIC -Ofast -mfpu=vfp -mfloat-abi=hard -mtune=arm1176jzf-s -march=armv7-a -D BCM2835_PERI_BASE=0x -c bcm2835.c bcm2835.c: In function ‘bcm2835_init’: bcm2835.c:1207:28: error: invalid suffix "x" on integer constantYou've probably run into the problem discussed in this thread. People have had luck with different solutions. The simplest is to make the following change to ~/Raspberry/librf24-bcm/Makefile
#IOBASE := $(shell cat /proc/iomem | grep bcm2708_vcio | cut -f 1 -d "-") IOBASE := 3F000000VERIFY THE GATEWAY
Run sudo /usr/local/sbin/PiGatewaySerial. The output should look like this:pi@raspberrypi ~/Raspberry $ sudo /usr/local/sbin/PiGatewaySerial Starting PiGatewaySerial... Protocol version - 1.4 Created PTY '/dev/pts/1' Gateway tty: /dev/ttyMySensorsGateway ================ SPI Configuration ================ CSN Pin = CE0 (PI Hardware Driven) CE Pin = Custom GPIO25 Clock Speed = 8 Mhz ================ NRF Configuration ================ STATUS = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0 RX_ADDR_P0-1 = 0xe7e7e7e7e7 0xc2c2c2c2c2 RX_ADDR_P2-5 = 0xff 0xc4 0xc5 0xc6 TX_ADDR = 0xe7e7e7e7e7 RX_PW_P0-6 = 0x00 0x00 0x20 0x00 0x00 0x00 EN_AA = 0x3b EN_RXADDR = 0x07 RF_CH = 0x4c RF_SETUP = 0x23 CONFIG = 0x0e DYNPD/FEATURE = 0x3f 0x06 Data Rate = 250KBPS Model = nRF24L01+ CRC Length = 16 bits PA Power = PA_LOWIf the NRF24L01+ isn't correctly wired, the following error will be shown
pi@raspberrypi ~ $ sudo /usr/local/sbin/PiGatewaySerial Starting PiGatewaySerial... Protocol version - 1.4 Created PTY '/dev/pts/2' Gateway tty: /dev/ttyMySensorsGateway check wiresIf this happens, double-check your wiring and correct any problems. Press Ctrl+Z and type
sudo killall PiGatewaySerialto get rid of the non-functioning Gateway. Then run sudo /usr/local/sbin/PiGatewaySerial again
If all is well, exit PiGatewaySerial by pressing Ctrl+C. Then run
sudo /etc/init.d/PiGatewaySerial startto start the gateway as a background process. Verify that it started correctly by running
sudo cat /dev/ttyMySensorsGatewayYou should see the message "Gateway startup complete". Exit by typing Ctrl+C.
MAKE THE GATEWAY AUTOSTART
To make sure the Gateway is started when your Raspberry Pi boots up, run the following command:sudo make enable-gwserialEnable the gateway for use with Domoticz
Domoticz (and maybe other home automation systems) has trouble reading from the default path created by PiGatewaySerial. You might need to run the following command:sudo ln -s /dev/ttyMySensorsGateway /dev/ttyUSB20And change /etc/rc.local from this
exit 0to this
ln -s /dev/ttyMySensorsGateway /dev/ttyUSB20 exit 0OTHER NOTES
-
The 3.3V power on the Raspberry Pi is rated for a maximum of 50mA. A regular NRF24L01+ only needs 15mA, but if you are using a power amplified version you might exceed what the Raspberry Pi can output. In that case, an external power supply might be required. If you use an external power source gnd must be connected to the Raspberry Pi's gnd.
-
Connecting the NRF24L01+ directly to your Raspberry Pi will prevent you from using the Raspberry Pi's gpio ports for other things, like a Z-wave board.
-
A user experienced slow data transfer compared to USB-to-Serial(ttl)<-->MySensors Gateway connection, especially on OTA firmware update. If you think this will cause a problem for you, an ethernet gateway might be a better alternative.
As always, I stand on the shoulders of giants. Related posts:
- http://forum.mysensors.org/topic/1151/tutorial-raspberry-pi-nrf24l01-direct-connection
- http://forum.mysensors.org/topic/1974/domoticz-as-controller-and-a-gateway-for-mysensor-nodes-running-on-a-raspberry-pi-2
TROUBLESHOOTING
If you get this errorpi@Domoticz3:~/Raspberry$ sudo /etc/init.d/PiGatewaySerial start [....] Starting PiGatewaySerial (via systemctl): PiGatewaySerial.serviceFailed to start PiGatewaySerial.service: Unit PiGatewaySerial.service failed to load: No such file or directory. failed!or this error
pi@raspberrypi ~ $ sudo /usr/local/sbin/PiGatewaySerial sudo: /usr/local/sbin/PiGatewaySerial: command not foundyou have probably forgotten to run sudo make install. Read the instructions again, and follow them this time :-) User @sineverba reported that a reboot of the Pi after running make install helped.
@mfalkvidd Hi there and thanks for a great and super detailed step-by-step tutorial.
I've got mine running in no time with your instructions, but after a couple of minutes I've got the following error:
2015-12-30 20:51:30.356 Error: MySensors: Unhandled sensor (sub-type=32), please report with log!
2015-12-30 20:52:01.390 Error: Serial Port closed!... Error: End of file
2015-12-30 20:52:02.315 MySensors: retrying in 30 seconds...
2015-12-30 20:52:31.319 MySensors: Using serial port: /dev/ttyUSB20
2015-12-30 20:52:31.320 Error: MySensors: Error opening serial port!
2015-12-30 20:52:32.321 MySensors: retrying in 30 seconds...
2015-12-30 20:53:01.324 MySensors: Using serial port: /dev/ttyUSB20I also attached a partial screen capture showing that it was working before 8-)

Any idea on why is this happening and how to solve it?
Southern regards and happy new year to all!
-
-
When I try to run the init.d script:
sudo /etc/init.d/PiGatewaySerial startI get the following error:
[....] Starting PiGatewaySerial (via systemctl): PiGatewaySerial.serviceFailed to start PiGatewaySerial.service: Unit PiGatewaySerial.service failed to load: No such file or directory. failed!No such file or directory.... Obviously a missing folder or file, but I am not savvy enough to debug this.... Any help appreciated..
-
When I try to run the init.d script:
sudo /etc/init.d/PiGatewaySerial startI get the following error:
[....] Starting PiGatewaySerial (via systemctl): PiGatewaySerial.serviceFailed to start PiGatewaySerial.service: Unit PiGatewaySerial.service failed to load: No such file or directory. failed!No such file or directory.... Obviously a missing folder or file, but I am not savvy enough to debug this.... Any help appreciated..
-
@mfalkvidd Yesterday I stopped trying and shutdown the Pi. This morning I tried the same again and it works !!
Pretty sure I haven't done anything else.
Thanks anyway.Sander
-
Hello, If followed the instruction and everything seemed to work without any problem.
However when I enter the command : sudo cat /dev/ttyMySensorsGateway
I get the message : cat: /dev/ttyMySensorsGateway: No such file or directory
Does anybody have any idea how to solve this ? -
Hello, If followed the instruction and everything seemed to work without any problem.
However when I enter the command : sudo cat /dev/ttyMySensorsGateway
I get the message : cat: /dev/ttyMySensorsGateway: No such file or directory
Does anybody have any idea how to solve this ? -
@mfalkvidd said:
sudo /etc/init.d/PiGatewaySerial start
No output and no error message. Just a new command line.
pi@raspberrypi ~ $ sudo /etc/init.d/PiGatewaySerial start
pi@raspberrypi ~ $ -
When I
ls -l ttyMySensorsGatewayI get
lrwxrwxrwx 1 root root 10 Jan 3 20:21 ttyMySensorsGateway -> /dev/pts/0It's all root. This results in all kinds of acessability issues. I followed all instructions exactly. Anyone any ideas how to change this ?
Sander.
-
When I
ls -l ttyMySensorsGatewayI get
lrwxrwxrwx 1 root root 10 Jan 3 20:21 ttyMySensorsGateway -> /dev/pts/0It's all root. This results in all kinds of acessability issues. I followed all instructions exactly. Anyone any ideas how to change this ?
Sander.
Try writing a udev rule to set owner of the device (ttyMysensorsGateway) at boot. If you don't need to use the link you could write a rule that names the pts device to the same name every boot, I think. At least for usb devices this works. Haven't tried it in this particular case.
-
When I
ls -l ttyMySensorsGatewayI get
lrwxrwxrwx 1 root root 10 Jan 3 20:21 ttyMySensorsGateway -> /dev/pts/0It's all root. This results in all kinds of acessability issues. I followed all instructions exactly. Anyone any ideas how to change this ?
Sander.
@Sander-Teunissen said:
When I
ls -l ttyMySensorsGatewayI get
lrwxrwxrwx 1 root root 10 Jan 3 20:21 ttyMySensorsGateway -> /dev/pts/0It's all root. This results in all kinds of acessability issues. I followed all instructions exactly. Anyone any ideas how to change this ?
The permissions for a soft link are not used (and if they were, any user would be allowed to do anything since everything is rwx). The permissions for the target is used instead. So it is the result of
ls -l /dev/pts/0you need to worry about.
-
Hello @mfalkvidd , I read the topic again but I am a bit confused. Normally we have a sketch on the Arduino board to enable signing for example or implement our desired code. I don't understand how to edit this before compiling ?
-
Sorry @ahmedadelhosni, I haven't tried to compile the Raspberry gateway for signing. @Anticimex will probably chide me for that, and rightly so :)
It should just be a matter of modifying MyConfig.h to enable signing, as per the instructions in http://forum.mysensors.org/topic/1021/security-introducing-signing-support-to-mysensors but maybe the MySensor files in the Raspberry repo haven't been updated with the signing features. -
The signing backends are updated for multi arch support thanks to @tbowmo and his SAMD gateway project, but they still assume Arduino compatible api:s for io, so if the raspberry repo/port provide that, it should work. If not, it should just be a matter of adding support for the raspberry spi:s for setting pin direction and states and (if sw backend is to be used) provide a random number generator. The code is C/C++ and hw independent in other aspects.
-
If anyone would like to add support for rPi to the signing backends, I suggest using a Linux generic approach using sysfs to handle io. Same for rng (using an appropriate device node, rpi has a hw rng that should be used).
If the design is Linux generic and uses proper design to abstract raspberry board specifics, it opens up for reuse by any hw that uses a Linux kernel :) -
I've been struggling with this setup for a few weeks now. I've resisted posting to try and figure it out, but I'm not making any progress. When I first setup the rpi with the nrf24 directly connected, everything worked great. I was running a node with temp and motion pretty reliably. I had some difficulty getting the relay node to work, but I got that working eventually as well, but not reliably. While trying to resolve this, I started getting these errors (log from Domoticz):
2016-02-19 10:43:36.007 MySensors: Using serial port: /dev/ttyUSB20
2016-02-19 10:43:36.008 Error: MySensors: Error opening serial port!
2016-02-19 10:43:37.008 MySensors: retrying in 30 seconds...I see the post above about this being related to using sensors not supported. Since it started with the relay node, I took that out of the setup, but I'm still getting the error.
I've rebuilt the entire rpi, mysensors, and domoticz, but I can't get it to work reliably. If I start the gateway manually via a putty session, it will work for a few minutes, but then it stops and goes back to the command prompt in putty and I can see the log in Domoticz start showing the above errors.
I have setup the gateway to start automatically, but it doesn't seem to restart correctly after a reboot.
I suspect I have a version mismatch in my code somewhere, but I can't seem to find it. My last effort was to remove all code from my computer and start over, ie reinstall Arduino and a fresh download of MySensors code (v1.5).
I've also tried changing the permissions on the tty devices, but that didn't seem to help either.
I was real excited about this project when I first read about it. When I finally got some parts in, I had some quick success that kept me motivated, but my enthusiasm is waning a bit with my total lack of progress on this issue. :(
Thanks in advance for any help.
-
Which capacitor(s) are you using?
Do you use a power amplified version of the nrf24l01+ or the regular version?
What is the output of /usr/local/sbin/PiGatewaySerial until the gateway stops working?@mfalkvidd
I have a 4.7uf electrolytic cap soldered directly to the pins on the radio module.
Radio is standard, ie antenna via board traces.I just did another batch of testing to try and answer your last question.
I started the gateway manually, ie sudo /usr/local/sbin/PiGatewaySerial
I plugged in a temp sensor (dallas 18b20, 3.3v apm, via 3.3v FTDI) into the computer so I could monitor the serial port.The first time I turned on the temp sensor, the gateway closed after 5 "read:"'s. I tried it again and the gateway stopped (meaning I was back at the prompt in putty) after 2 reads.
I decided to reboot the rpi. When it came back up, I had the Domoticz log open and I could see the gateway start before I could reopen a putty session. It appears the auto start worked correctly, and so far, it's still up.
I've been sending temp data successfully for ~10 minutes now....time passes...
I typed out the above, but didn't hit submit as I wanted to go put the relay node back together and test it.After gathering up those parts, I check the Domoticz log and it's still reading temp. While it IS working, the temps are displaying wrong in Domoticz. My sensor is sending F, but domoticz thinks it's C and it converts.
I'm mentioning this because, when I open the serial monitor to confirm the node was sending F values, it of course resets the node. This killed the gateway. I got the following in my domoticz log:
2016-02-19 15:00:30.362 (MySensorsSerial) Temp (Temp)
2016-02-19 15:00:38.371 (MySensorsSerial) Temp (Temp)
2016-02-19 15:00:46.380 (MySensorsSerial) Temp (Temp)
2016-02-19 15:00:50.384 (MySensorsSerial) Temp (Temp)
2016-02-19 15:00:54.389 (MySensorsSerial) Temp (Temp)
2016-02-19 15:00:58.393 (MySensorsSerial) Temp (Temp)
2016-02-19 15:01:02.398 (MySensorsSerial) Temp (Temp)
2016-02-19 15:01:06.403 Error: Serial Port closed!... Error: End of file
2016-02-19 15:01:06.864 MySensors: retrying in 30 seconds...
2016-02-19 15:01:35.867 MySensors: Using serial port: /dev/ttyUSB20
2016-02-19 15:01:35.868 Error: MySensors: Error opening serial port!
2016-02-19 15:01:36.869 MySensors: retrying in 30 seconds...
2016-02-19 15:02:05.871 MySensors: Using serial port: /dev/ttyUSB20
2016-02-19 15:02:05.872 Error: MySensors: Error opening serial port!
2016-02-19 15:02:06.872 MySensors: retrying in 30 seconds...
2016-02-19 15:02:20.010 Active notification subsystems: (0/10)
2016-02-19 15:02:35.875 MySensors: Using serial port: /dev/ttyUSB20
2016-02-19 15:02:35.875 Error: MySensors: Error opening serial port!
2016-02-19 15:02:36.875 MySensors: retrying in 30 seconds...I ran sudo cat /dev/ttyMySensorsGateway and get 'No such file' message. I interpret this to mean the gateway has stopped running, right?
Next I manually started the gateway again. It immediately starts reading the temp sensor again. After the next 30 second timer expires, Domoticz picks it right back up too.
I think I figured something out... If I restart a node with the gateway running, it kills the gateway and my above symptoms start. I'm able to repeat this without failure.
I'm going to do some more testing, but it seems that when the node sends the s=255 message, the gateway is dieing.
I want to see what happens if I turn on another node while the first is alraedy running. Perhaps I have to have all my nodes on before I start the gateway?I am very encouraged as I at least finally feel like I'm moving forward. Was getting frustrated for awhile there. :)
Thanks again for the write to help me get started. -
Did some more testing. Now I know why it seemed related to the relay node.
The relay node needs the gateway to be listening when it wakes up. If it fails when it presents, ie the gateway is down, it doesn't try again.I did have the relay sketch working at some point, so I don't think it's that sketch. I'm thinking I have a problem with the gateway. I work as a developer, so I've been trying to acquaint myself with the code. I mostly use SQL and VB.net in my day job. I loved C++ in college, but it's been almost that long since I used it. :)
Does anyone have any ideas what might be causing this? I'm going to go reread the section on the mysensors protocol and see I can spot anything wrong in those presentation messages.