Building a Raspberry Pi Gateway

created by hek , updated by mfalkvidd

Raspberry Pi is a small computer that uses very little power (2-3 watts). It has all the horsepower necessary to serve as a cloud-enabled all-in-one Controller.

Several controllers available here can run directly on the RPi. E.g. OpenHAB and PiDome.

Download MySensors

Login to your Raspberry Pi and get the code from MySensors repository (master branch):

git clone https://github.com/mysensors/MySensors.git --branch master
cd MySensors

To use the development version with the latest updates:

git clone https://github.com/mysensors/MySensors.git --branch development
cd MySensors

Configure

A list of options to set the gateway is available by running the command:

./configure --help

Note: In options that requires a pin number, use the Raspberry Pi physical pin number and not the GPIO numbers.

Transport

The transports available are: nrf24, rs485, rfm95, rfm69

NRF24

--my-transport=nrf24

RFM69

Note: RFM69 is only supported in the development branch.

--my-transport=rfm69 --my-rfm69-frequency=868 --my-is-rfm69hw

The frequency can be set to 315,433,868 or 915 Mhz; --my-is-rfm69hw enables high-powered rfm69hw radios.

Note: The RaspberryPi uses the new RFM69 driver which is not compatible with the old (=default) RFM69 driver, you need to add #define MY_RFM69_NEW_DRIVER to your nodes to properly communicate with the gateway (available only in development branch).

Gateway type

The types available are: ethernet, serial, mqtt

Ethernet gateway

By default, the gateway is built for ethernet and it listens on port 5003:

--my-gateway=ethernet --my-port=5003

To set the gateway as a client that connects to a controller, use:

--my-gateway=ethernet --my-controller-url-address=YOUR-CONTROLLER-ADDRESS

or

--my-gateway=ethernet --my-controller-ip-address=YOUR-CONTROLLER-IP

Serial gateway

If you are running a controller on the Raspberry Pi that doesn't support communication with the gateway through ethernet, you can use a virtual serial port:

--my-gateway=serial --my-serial-is-pty --my-serial-pty=/dev/ttyMySensorsGateway

For some controllers a more recognisable name needs to be used: e.g. /dev/ttyUSB020 (check if this is free).

MQTT

The following is an example of how to build a MQTT gateway:

--my-gateway=mqtt --my-controller-ip-address=127.0.0.1 --my-mqtt-publish-topic-prefix=mysensors-out --my-mqtt-subscribe-topic-prefix=mysensors-in --my-mqtt-client-id=mygateway1

Build

To build the gateway, run:

make

If there were no errors during compilation the file bin/mysgw will be created.

Test

The gateway needs root privilege to run, for a list of parameters that can be used, run:

sudo ./bin/mysgw -h

The first time you run the gateway, enable the debug messages to verify that it is working:

sudo ./bin/mysgw -d

The following message indicates that communication with the radio module failed:

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

A success message would be:

mysgw: Starting gateway...
mysgw: Protocol version - 2.2.0-beta
mysgw: MCO:BGN:INIT GW,CP=RNNG---,VER=2.2.0-beta
mysgw: TSF:LRT:OK
mysgw: TSM:INIT
mysgw: TSF:WUR:MS=0
mysgw: TSM:INIT:TSP OK
mysgw: TSM:INIT:GW MODE
mysgw: TSM:READY:ID=0,PAR=0,DIS=0
mysgw: MCO:REG:NOT NEEDED
mysgw: Listening for connections on 0.0.0.0:5003
mysgw: MCO:BGN:STP
mysgw: MCO:BGN:INIT OK,TSP=1

Install

To install run:

sudo make install

This will also provide instructions for adding the gateway at each boot.

Troubleshooting

When installed as a service, the output of the gateway will be directed to syslog in /var/log/syslog.
If you want full debug output from the gateway to be logged to syslog (including serial protocol) you have to edit the following file: /etc/systemd/system/mysgw.service
Add the -d parameter to the ExecStart line

[Service]
ExecStart=/usr/local/bin/mysgw -d

Then reload the systemd process and restart the gateway

sudo systemctl daemon-reload
sudo systemctl restart mysgw.service

Now the output from the gateway can be monitored by

tail -f /var/log/syslog

Mind that the gateway logging will now produce quite some output in syslog!

Wiring

Use the Pin No. and not the GPIO.

Raspberry Pi 2/3

Raspberry Pi 1

NRF24L01+ Radio

Raspberry NRF24L01+ Color
GND GND Black
3.3V(*) VCC Red
22 CE Orange
24 CSN/CS Yellow
23 SCK Green
19 MOSI Blue
21 MISO Violet
15(**) IRQ Gray

(*): If you are using the nrf24 PA/LNA version you need to use a 5V->3.3V regulator because the Raspberry Pi 3.3V can't supply enough power.

(**): Optional, more info here.

RFM69 Radio

Raspberry RFM69 Color
GND GND Black
3.3V VCC Red
24 NSS Yellow
23 SCK Green
19 MOSI Blue
21 MISO Violet
22 DI00 Gray
(*) ANA Antenna

(*): Solder a piece of wire, more info here

Improving throughput for nrf24

Normally the gateway asks the nrf24 module if a new radio message is available and then sleep a few milliseconds before asking again. This is necessary to avoid high CPU usage. During the gateway sleeping period, the module will still listen for new messages, but if multiple messages arrive, it won't take all.

There is another method were the interrupt pin from the nrf24 modules is used to inform when a new message arrives and it's immediately added to a queue for further processing. To use this method connect the interrupt pin from the nrf24 module to your Raspberry Pi (check the wiring part for more details) and use the "--my-rf24-irq-pin=PIN" option to build the gateway.

In the following example, the interrupt pin is connected to the Raspberry Pi pin 15:

--my-transport=nrf24 --my-rf24-irq-pin=15

Information LEDs

The following is an example of how to add LEDs for ERR/RX/TX blinking.

Raspberry Led Color
12 ERR Red
16 RX Yellow
18 TX Green
--my-leds-err-pin=12 --my-leds-rx-pin=16 --my-leds-tx-pin=18

Advanced

Other customizations can be made by editing the file example_linux/mysgw.cpp and MyConfig.h

It is also possible to set any define from the configure command by adding it to --extra-cxxflags. Example for turning on rfm95 debug:

--extra-cxxflags=-DMY_DEBUG_VERBOSE_RFM95

Shopping Guide

10pcs NRF24L01+ 2.4GHz Wireless Transceiver
This is what enables your sensors to communicate wirelessly across a nominal range of 60 meters.
2252 available - $9.99   Buy
In stock - $7.75   Buy
Raspberry Pi 2 with 1GB RAM
The new quad core version .
716 available - $74.94   Buy
In stock - $38.50   Buy
Raspberry Pi Model B+ 512MB
The Raspberry Pi is a low cost, credit-card sized computer.
35 available - $59.95   Buy
Stock unknown   Buy

Gateways

Sensors & Actuators

Related Content

Comments