Step-by-step procedure to connect the NRF24L01+ to the GPIO pins and use the Raspberry as a Serial Gateway (MySensors 1.x)


  • Mod

    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

    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 install
    

    If 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 constant
    
    

    You'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 := 3F000000
    

    VERIFY 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_LOW
    

    If 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 wires
    

    If this happens, double-check your wiring and correct any problems. Press Ctrl+Z and type

    sudo killall PiGatewaySerial
    

    to 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 start
    

    to start the gateway as a background process. Verify that it started correctly by running

    sudo cat /dev/ttyMySensorsGateway
    

    You 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-gwserial
    

    Enable 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/ttyUSB20
    

    And change /etc/rc.local from this

    exit 0
    
    

    to this

    ln -s /dev/ttyMySensorsGateway /dev/ttyUSB20
    exit 0
    
    

    OTHER 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:

    TROUBLESHOOTING
    If you get this error

    pi@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 found
    

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



  • Great and simple.
    Is connecting the nrf directly to the pi causes any problems and u need to restart it ?
    Thanks.



  • @ahmedadelhosni said:

    Is connecting the nrf directly to the pi causes any problems and u need to restart it ?

    No, I have not had any big problems. The problem may only be the nrf24l01+ - I had to have a few changed plus fix the con to make it work. Wiring could be an issue. Also pay attention to CE pin.
    CE Pin = Custom GPIO25 (I have Custom GPIO22) - this can be changed by the constructor

    Excellent write-up


  • Plugin Developer

    Very nice writeup.

    One thing that comes to mind is, the latest rasbian Jessie uses systemd instead of init. It would be good if we could add a systemd unit file to the library and a description here how to use it.


  • Admin

    Thanks for the writeup @mfalkvidd. I'll add it to the main site when updating for the 1.6 release.

    If someone feel comfortable merging the RPi specific code to the MySensors library in the development branch, let me know. Would've been nice to have one codebase as the RPi code has diverged from Arduino quite a bit since it was created.

    A configure/autoconf is also something I'd need help with.



  • Great write-up! Should we consider instructions on how to create an init.d start/stop script so it can run as a daemon? I've done this, however, I'm having a hard time getting the output to log to syslog. (would be nice for monitoring/diagnostics purposes) The logging doesn't happen until I stop/kill the service.

    pi@homeauto01 ~ $ cat /etc/init.d/PiGatewaySerial
    #! /bin/sh
    # /etc/init.d/PiGatewaySerial
    
    ### BEGIN INIT INFO
    # Provides: PiGatewaySerial
    # Required-Start: $all
    # Required-Stop:
    # Should-Start:
    # Should-Stop:
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: Start and stop PiGatewaySerial
    # Description: PiGatewaySerial
    ### END INIT INFO
    
    
    # The following part carries out specific functions depending on arguments.
    case "$1" in
      start)
        echo "Starting PiGatewaySerial"
        /root/Raspberry/PiGatewaySerial 2>&1 | logger &
        ;;
      stop)
        echo "Stopping PiGatewaySerial"
        killall PiGatewaySerial
        ;;
      *)
        echo "Usage: /etc/init.d/PiGatewaySerial {start|stop}"
        exit 1
        ;;
    esac
    
    exit 0
    

  • Plugin Developer

    Doesn't the provided init script in the library already have logging? I didn't get around to testing this setup after my initial attempt failed, so I might be wrong.

    https://github.com/mysensors/Raspberry/blob/master/initscripts/PiGatewaySerial


  • Hardware Contributor

    Excellent writeup, double plus plus good !!!




  • Plugin Developer

    @mfalkvidd I used direct NRF24l01+ with RPI long ago and I felt slow data transfer compare to USB-to-Serial(ttl)<-->MySensors Gateway connection. especially on OTA firmware update.


  • Mod

    @jkandasa said:

    @mfalkvidd I used direct NRF24l01+ with RPI long ago and I felt slow data transfer compare to USB-to-Serial(ttl)<-->MySensors Gateway connection. especially on OTA firmware update.

    Thanks. Let's add that under the other notes section.



  • This is a very good work !

    Is there anything in place for RFM69 ? Would it be difficult to port the code from arduino to rpi ? (I have never coded on RPI).

    Also, do you think any performance difference is involved in managing the radio directly from the RPI, compared to having a dedicated atmega/arduino ? Especially if a controller solution is already running on it?


  • Hardware Contributor

    My experience: the Raspberry 2 indicates usually less then 1% processor, with very occasional maximum peaks to 15%. So I would say, Raspberry 2: no worries. My setup is Domoticz + NRF24based gateway on the same Raspi.


  • Mod

    My experience is on the same level as @GertSanders's - very low cpu usage.

    I don't use RFM69 so unfortunately I have no knowledge about the feasibility to connect it directly to the Raspberry Pi.



  • @martinhjelmare said:

    Doesn't the provided init script in the library already have logging? I didn't get around to testing this setup after my initial attempt failed, so I might be wrong.

    https://github.com/mysensors/Raspberry/blob/master/initscripts/PiGatewaySerial

    Ugh! Correct. Also, @mfalkvidd did cover this with his "MAKE THE GATEWAY AUTOSTART" section.....If I only could read! I will test this soon to see how the logging works.

    Finally, by enabling ser2net and adding something like this to /etc/ser2net.conf:

    5003:raw:600/dev/ttyMySensorsGateway
    

    ....you have yourself an Ethernet Gateway! Great stuff! Thanks again!


  • Plugin Developer

    @Myna said:

    This is a very good work !

    Is there anything in place for RFM69 ? Would it be difficult to port the code from arduino to rpi ? (I have never coded on RPI).

    Also, do you think any performance difference is involved in managing the radio directly from the RPI, compared to having a dedicated atmega/arduino ? Especially if a controller solution is already running on it?

    I've got no experience myself with rfm69 yet, but I've been thinking about it and searching the web says it should work, connecting the rfm69 to the raspberry pi, for example here:
    https://github.com/abouillot/HomeAutomation/tree/master/piGateway

    There are also custom pcbs for simplicity, don't know if it's tested though:
    https://oshpark.com/profiles/thinkpeace



  • Perhaps re-word the last note from:

    A user experienced data transfer compare 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.

    to:

    A user notes that data transfer speeds are comparable to that of a USB-to-Serial(ttl)<-->MySensors Gateway connections. If you think this may be an issue for your application (e.g. OTA firmware updates), an ethernet gateway might be a better alternative.

    thanx,
    JpS


  • Mod

    @soward that wasn't quite the feedback I got, but what I wrote didn't make much sense either so I have corrected it now. Thanks!



  • I have a trouble with CPU utilization when I start the gateway : 100% all the time. I use Jessie and not Wheezy on a RPI 2. Does someone can help me to troubleshoot this problem ? I have not add a decoupling capacitor , can it be the reason ?

    Solved : see this post



  • @minscof said:

    I have a trouble with CPU utilization when I start the gateway : 100% all the time. I use Jessie and not Wheezy on a RPI 2. Does someone can help me to troubleshoot this problem ? I have not add a decoupling capacitor , can it be the reason ?

    This happened to me as well, I mixed up the boards I had at home and discover later it was a B+ and not a RPI2 :-) Maybe you've made the same mistake?



Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.