Raspberry Pi
SimplePublish for Raspberry Pi
The QSL includes a SimplePublish example for the RPi: Along with files that implement the necessary porting functions, this example code is a single main.c file that attempts to connect with the default network ID and join key. Upon success, it starts publishing random data with an interval matching the service it requested. The example also prints any downstream user data that arrives between each transmission.
In the following sections we will guide you through the steps necessary to get the example up and running. We use a Raspberry Pi 3 Model B v1.2 running Raspbian Jessie, but the instructions will include tips on how to make it work on other models and for older versions of Raspbian.
The example code is written for the RPi running Raspbian Jessie, but can in theory be used by any platform with an available UART, running a newer Linux distribution, by just setting the correct portname in dn_uart.c.
Prepare the Raspberry Pi
We assume that you have already installed the latest release of Raspbian Jessie on a RPi, and that you wish to use the UART available on the GPIO header.
If you use an external USB TTL Serial cable (+3.3 V signaling!) to connect your RPi to the mote, you can skip ahead to the next section, but you have to change the defined UART from INTERNAL to EXTERNAL in dn_uart.c. You can also use the interface board (like you did to configure the mote as a slave), but will then instead have to change the defined UART to the port assigned to the API (default ttyUSB3, as per Table 1).
Disable Serial Console
The built in serial port that we want to use to communicate with the mote is by default used by the console, thus we have to disable this. To make things more complicated, the available UART has changed for the Raspberry Pi 3: The added Bluetooth module has "stolen" the high performance hardware serial port from the GPIO header; replacing it with a port that is partly software and a bit flaky. Hence, depending on which model you are using, the device name of interest to you will differ:
- Raspberry Pi 3: /dev/ttyS0
- Raspberry Pi 1-2: /dev/ttyAMA0
In Raspbian Jessie (as of May 2016) a serial port alias has been introduced to mend the effects of the changed serial portname: serial0 will point to the UART available on the GPIO header, i.e. ttyS0 for the RPi 3 and ttyAMA0 for older models. This alias is used in the SimplePublish example, thus it should work on any RPi model running the latest release of Jessie.
It seems the serial port alias does not work for the Raspberry Pi 1: You will have to edit the UART portname in dn_uart.c to ttyAMA0.
Because the serial port alias is not present in Raspbian Wheezy, you will have to edit the UART portname in dn_uart.c to ttyS0 or ttyAMA0.
Also, make the following changes to the steps below:
Enable Serial and Lock CPU Core Frequency
The serial port is not disabled by default, so you can skip adding the enable_uart=1 (although you should still lock the CPU frequency for the RPi 3).
Stop and Disable Getty Service
You can't stop/disable getty via the command line, instead manually edit /etc/inittab:
$ sudo nano /etc/inittab
Scroll until you find the line specifying getty for the serial port we want (or use CTRL+W to search) and comment it out:
#Spawn a getty on Raspberry Pi serial line #T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
Enable Serial and Lock CPU Core Frequency
The serial port is disabled by default in Jessie. Also, on the RPi 3 the flaky serial port (ttyS0) can give you trouble unless you lock the CPU frequency.
To fix this, edit /boot/config.txt:
$ sudo nano /boot/config.txt
and add the following line(s) at the bottom:
enable_uart=1 core_freq=250
enable_uart=1
You can also enable the GPIO serial port via the GUI: Menu > Preferences > Raspberry Pi Configuration > Interfaces > Serial
Stop and Disable Getty Service
$ sudo systemctl stop serial-getty@ttyS0.service $ sudo systemctl disable serial-getty@ttyS0.service
$ sudo systemctl stop serial-getty@ttyAMA0.service $ sudo systemctl disable serial-getty@ttyAMA0.service
Remove Console from Commandline
First, make a backup of /boot/cmdline.txt in case you mess something up (it contains kernel parameters):
$ sudo cp /boot/cmdline.txt /boot/cmdline_backup.txt
Then edit the original:
$ sudo nano /boot/cmdline.txt
You should see something like the example below, where serialDeviceName can be serial0 or ttyAMA0, depending on which Raspberry Pi you use.
dwc_otg.lpm_enable=0 console=<serialDeviceName>,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes root wait
Remove the line console=<serialDeviceName>,115200, before saving and rebooting for the changes to take effect.
Connect to Mote
The GPIO pins for the RPi is listed in Table 5, with the relevant pins color coded. Table 6 shows the mapping between the RPi and the mote. Note that you can of course just connect the RX RTSn and TX CTSn to GND on the mote itself, as can be seen in the example in Figure 4.
If you are using an external USB TTL Serial cable, you simply connect its Ground, TX and RX to the mote instead (but keep the 3.3 V from the RPi directly, as the VCC from TTL cables are usually 5 V!).
If you choose to connect the mote via the DC9006 interface board, you should not connect any other wires (not even the RX RTSn and TX CTSn to GND), as this will lead to an API bus conflict.
Long cables may corrupt the API bus communication: e.g. using an official I/O extender cable for the RPi might not work.
Note the the two 5 volt pins right above the ground and two UART pins: You might fry the mote if you mistakenly connect to these!
Name | Pin # | Name | |
---|---|---|---|
3.3 V | 1 | 2 | 5 V |
SDA1, I2C | 3 | 4 | 5 V |
SCL1, I2C | 5 | 6 | GND |
GPIO 04 | 7 | 8 | TXD0 |
GND | 9 | 10 | RXD0 |
GPIO 17 | 11 | 12 | GPIO 18 |
GPIO 27 | 13 | 14 | GND |
GPIO 22 | 15 | 16 | GPIO 23 |
3.3 V | 17 | 18 | GPIO 24 |
SPI MOSI | 19 | 20 | GND |
SPI MISO | 21 | 22 | GPIO 25 |
SPI CLK | 23 | 24 | SPI CE0 N |
GND | 25 | 26 | SPI CE1 N |
ID SD | 27 | 28 | ID SC |
GPIO 05 | 29 | 30 | GND |
GPIO 06 | 31 | 32 | GPIO 12 |
GPIO 13 | 33 | 34 | GND |
GPIO 19 | 35 | 36 | GPIO 16 |
GPIO 26 | 37 | 38 | GPIO 20 |
GND | 39 | 40 | GPIO 21 |
Table 5: Raspberry Pi GPIO Header
RPi | Mote |
---|---|
3.3 V | VBAT |
GND | GND |
TXD0 | RX |
RXD0 | TX |
GND | RX RTSn |
GND | TX CTSn |
Table 6: RPi to mote pin mapping.
The GPIO header for models older than Raspberry Pi B+ is similar, but does not have pins 27 - 40.
Compile and Run
This section shows you two ways to compile and run the QSL with the provided example SimplePublish code:
- Directly on the RPi with a makefile.
- Remotely on a computer using NetBeans.
The former is quick and easy, but the latter is recommended if you plan on further developing any code for the RPi.