Building a Raspberry Pi Gateway

created by hek , updated by Marcelo Aquino

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.


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

git clone --branch master
cd MySensors

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

./configure --help

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

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

To build, run:


If everything went well the file bin/mysgw will be created.

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


To install run:

sudo make install

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

Ethernet gateway

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

./configure --my-gateway=ethernet --my-port=5003

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

./configure --my-gateway=ethernet --my-controller-url-address=YOUR-CONTROLLER-ADDRESS


./configure --my-gateway=ethernet --my-controller-ip-address=YOUR-CONTROLLER-IP

Serial gateway

If for some reason you can't use the GPIO pins of the Raspberry Pi, you could connect the radio to a arduino with the GatewaySerial sketch and feed the pi through a serial port:

./configure --my-gateway=serial --my-serial-port=/dev/ttyACM0 --my-serial-baudrate=115200

To know the right value for the serial port, dmesg can give you some hint.

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:

./configure --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).


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

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


Use the Pin No. and not the GPIO.

Raspberry Pi 2/3

Raspberry Pi 1

NRF24L01+ Radio

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

(*): If you are using the nrf24 PA/LNA version you need to use a 5V->3.3V regulator.

RFM69 Radio

Raspberry RFM69 Color
3.3V VCC Red
x NSS Yellow
x SCK Green
x MOSI Blue
x MISO Violet
x DI00 Gray
ANA Antenna

Obs: RFM69 is not yet supported.

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:

./configure --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
./configure --my-leds-err-pin=12 --my-leds-rx-pin=16 --my-leds-tx-pin=18

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 - $8.16   Buy
Raspberry Pi 2 with 1GB RAM
The new quad core version .
766 available - $69.50   Buy
In stock - $42.50   Buy
Raspberry Pi Model B+ 512MB
The Raspberry Pi is a low cost, credit-card sized computer.
35 available - $59.95   Buy
In stock - $19.99   Buy


Sensors & Actuators

Related Content