Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

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.

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

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

 Click here if you are using Raspian Weezy instead of Jessie...

 

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:

Raspberry Pi 3

 

enable_uart=1
core_freq=250

 

Older models

 

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 

Raspberry Pi 3

 

sudo systemctl stop serial-getty@ttyS0.service
sudo systemctl disable serial-getty@ttyS0.service

 

Older models

 

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.

/boot/cmdline.txt

 

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

7.1.2 Connect to Mote

Figure 4: Mote connected to a Raspberry Pi 3.

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!

 

NamePin #Name
3.3 V125 V
SDA1, I2C345 V
SCL1, I2C 56GND
GPIO 04 78TXD0
GND910RXD0
GPIO 17 1112GPIO 18 
GPIO 27 1314GND
GPIO 22 1516GPIO 23 
3.3 V 1718GPIO 24 
SPI MOSI 1920GND 
SPI MISO 2122GPIO 25 
SPI CLK 2324SPI CE0 N 
GND2526SPI CE1 N
ID SD 2728ID SC 
GPIO 05 2930GND
GPIO 06 3132GPIO 12 
GPIO 13 3334GND
GPIO 19 3536GPIO 16 
GPIO 26 3738GPIO 20 
GND3940GPIO 21 

 

Table 5: Raspberry Pi GPIO Header

 

RPi
Mote
3.3 VVBAT
GNDGND
TXD0RX
RXD0TX
GNDRX RTSn
GNDTX 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.

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

7.2.1 Makefile
 

 Click here to expand detailed instructions...

 

7.2.2 NetBeans

NetBeans is a free, open source IDE that can be installed on any OS that support Java. By simply adding the RPi as a remote build host, NetBeans will make sure to synchronize files and build the project remotely; returning any console output as you run the application on your RPi.
 

 Click here to expand detailed instructions...

  • No labels