Building a Orange Pi Gateway

created by Marcelo Aquino , updated

Note: The instructions here have been tested and confirmed to work on Orange Pi Zero, it is uncertain whether it will work on other types.

Prepare the system

Install the ARMbian

Download and Install the ARMbian(debian) OS image:
https://www.armbian.com/orange-pi-zero/

Install sunxi-tools

These tools are required to make changes in the boot area of the system:

sudo apt-get update
sudo apt-get install sunxi-tools

Configure spidev

Adding /dev/spidev1.0 is required since /dev/spidev0.0 is used for the onboard NAND flash.

bin2fex /boot/script.bin /tmp/orange.fex

Now edit /tmp/orange.fex:

nano /tmp/orange.fex

and add/change the following so it looks exactly like the part below:

[spi0]
spi_used = 1
spi_cs_bitmap = 1
spi_mosi = port:PC00<3><default><default><default>
spi_miso = port:PC01<3><default><default><default>
spi_sclk = port:PC02<3><default><default><default>
spi_cs0 = port:PC03<3><1><default><default>

[spi1]
spi_used = 1
spi_cs_bitmap = 1
spi_cs0 = port:PA13<2><1><default><default>
spi_sclk = port:PA14<2><default><default><default>
spi_mosi = port:PA15<2><default><default><default>
spi_miso = port:PA16<2><default><default><default>

[spi_devices]
spi_dev_num = 2

[spi_board0]
modalias = "spidev"
max_speed_hz = 33000000
bus_num = 0
chip_select = 0
mode = 0
full_duplex = 1
manual_cs = 0

[spi_board1]
modalias = "spidev"
max_speed_hz = 33000000
bus_num = 1
chip_select = 0
mode = 0
full_duplex = 1
manual_cs = 0

then recompile the fex file:

sudo fex2bin /tmp/orange.fex /boot/script.bin

Reboot the system:

sudo reboot

2 device files should exist now:

$ ls -l /dev/spidev*
crw------- 1 root root 153, 0 Jan 11 16:31 /dev/spidev0.0
crw------- 1 root root 153, 1 Jan 11 16:31 /dev/spidev1.0

Load the gpio module

sudo modprobe gpio-sunxi

add the line gpio-sunxi to /etc/modules to automagically load the module at boot time:

echo "gpio-sunxi" >> /etc/modules

Download MySensors

Note: Support for OrangePi is now part of the official MySensors development repository.

git clone https://github.com/mysensors/MySensors.git
cd MySensors

Configure

NRF24

Always use the GPIO number to set the radio pins. If you follow the connection diagram below, pin CE is connected to pin 22, which is GPIO number 2 and CS is connected to pin 24, which is GPIO number 13.

./configure --spi-spidev-device=/dev/spidev1.0 --my-transport=nrf24 --my-rf24-ce-pin=2 --my-rf24-cs-pin=13

Serial

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

MQTT

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

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

Wiring

Oragen Pi Zero

NRF24L01+ Radio

OrangePi Pin No. NRF24L01+ Color
GND GND Black
5VReg -> 3.3V VCC Red
22 / GPIO2 CE Orange
24 / GPIO13 CSN/CS Yellow
23 SCK Green
19 MOSI Blue
21 MISO Violet
Not supported IRQ Gray

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
Orange Pi Zero
Mini computer for running controller and MySensors gateway.
275 available - $15.30   Buy

Gateways

Sensors & Actuators

Related Content

Comments