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.

 

 

If you use Raspbian Wheezy...

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:

/etc/inittab
#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:

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

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!

Name

Pin #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.

 

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.

 

Makefile

Assuming that you have downloaded the libraries to the RPi in one of the ways specified in the Download section:

A Makefile that compiles the necessary files is provided in simple_makefile/. By default, it expects to find the directories for the QSL and C Library as they are placed in the repository; simply copy the Makefile into the example folder (where main.c is located) and proceed to Build and Run below. However, if you prefer to build the example somewhere else, follow these steps first:

  1. Copy the SimplePublish example files and folders from examples/rpi/SimplePublish/ into a project folder of your choice on the RPi
  2. Copy the two library directories into the same project folder; i.e. these two:
    1. sm_qsl/
    2. sm_clib/sm_clib/
  3. Copy the Makefile found in simple_makefile/ into your project folder (where main.c is located).
  4. Open the Makefile (e.g. $ nano Makefile) and edit the path to the two library folders by uncommenting the correct relative path:

    ### Directory names for QuickStart and C Library
    QSL		= sm_qsl
    CLIB	= sm_clib
    
    ### Relative path to library directories: Uncomment correct one or modify
    ## Inside same folder
    DIR_QSL		= $(QSL)
    DIR_CLIB	= $(CLIB)
    ## Repository structure
    #DIR_CLIB	= ../../../sm_clib/$(CLIB)
    #DIR_QSL		= ../../../$(QSL)

Build and Run

Build the example by calling make:

$ make

This will compile the necessary object files inside an object folder, while a target binary will be created in the same directory as the Makefile.

Run the built target binary:

$ ./main
You can delete the object files and binary by calling $ make clean, while $ make remake will clean before building again.

 

 

 

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.

Before starting, make sure that the RPi is powered up and connected to the network. You also need to figure out its IP address (e.g $ ifconfig on the RPi).

Install NetBeans for C/C++

On your computer, install the correct download bundle found here: https://netbeans.org/downloads/

Enable Remote Root Login

The easiest way to let NetBeans run an application that needs access the IO pins is to let it log in remotely as root. There are ways to get around this by configuring the IO, but during development it is much easier to have full root access. By default the root account is disabled in Raspbian, so first we have to enable this by setting a password. On the RPi, use the following command:

Set root password
$ sudo passwd root

You can now log on as "root" with the password you set.

Now, edit the SSH configuration:

Edit ssh config
$ sudo nano /etc/ssh/sshd_config 

Search for PermitRootLogin and change it to yes (the default without-password does not work).

/etc/ssh/sshd_config
# Authentication:
LoginGraceTime 120
PermitRootLogin yes
StrictModes yes

Save and exit, before rebooting the RPi for the changes to take effect.

Create Project

Assuming that you have downloaded the libraries to your computer in one of the ways specified in the Download section, you can either set up the NetBeans project directly from the library structure or move the necessary folders to a project directory of your liking. NetBeans uses a concept of logical folders, and the only thing that changes is where you locate the files and libraries in the steps below, as NetBeans will take care of the necessary compilation and linking:

  1. Menu > File > New Project, select C/C++ Application and press Next.
  2. Set project name and location to your liking, as per above.
  3. Make sure to leave Create Main File unchecked, leave the Build Host and Tool Collection to their default values for now and press Finish.

Add Raspberry Pi as Build Host

  1. Select the Services tab in the left window.
  2. Right click C/C++ Build Hosts > Add New Host.
  3. Hostname: Enter the IP address of the Raspberry
    Port: 22
    Click Next.
  4. Enter the login user name (root) and press Next.
  5. NetBeans will now attempt to connect and prompt for the password you previously set.
    Press OK.
  6. When the automatic configuration to completes, you should see a summary where NetBeans has found and set the GNU tool collection as default.
    Click Finish.
  7. Select the Projects tab in the left window.
  8. Right click your project > Properties.
  9. Build > Build Host: Select the build host you just created.
  10. Run > Console Type: Standard Output.
  11. Press Apply.

You can not assign a new IP address to an existing build host; you have to re-create it if the RPi's IP changes. Hence, it might be worth setting a static IP.

Add QSL and C Library to Include Directories

While still inside project properties:

  1. Build > C Compiler > Include Directories: Add the two library directories (sm_qsl and sm_clib/sm_clib/ if you add them from the repository).
  2. Click Apply.

 

If you use Raspbian Wheezy...

Add librt to Libraries in Linker

Raspbian Wheezy comes with an older version of glibc, where one of the functions used in dn_time.c is not part of the main C library. Instead, we have to add the "Real Time" library to the linker. While still inside project properties:

  1. Build > Linker > Libraries: Press the ... button
  2. Press Add Option... and select Other Option.
  3. Enter -lrt and press OK.

 

Add Posix Threads to Libraries in Linker

While still inside project properties:

  1. Build > Linker > Libraries: Press the ... button
  2. Press Add Standard Library... and select Posix Threads.
  3. Press OK until you exit the project properties.
The example uses phtread to create a daemon that listens for incoming serial data, as interrupts are not supposed to be handled in user space on Linux.

 

 

Add source and header files

The last thing that remains is to add all the source and header files to the project, i.e those originally placed in the repository as:

  • examples/rpi/SimplePublish/
  • sm_qsl/
  • sm_clib/sm_clib/
    • Exceptions:
      • ports/: This contains examples for another platform, and NetBeans will complain when trying to find its dependants if you add it to the project.
      • dn_whmt.c/.h: API for the Wireless Heart Mote that we do not need.
      • dn_serial_mg.c/.h: API for the SmartMesh Manager that we do not need.

Note that it is the sm_clib/sm_clib/ directory we want, not the whole sm_clib/ directory!

Option 1: The Clean Way

NetBeans will already have created a standard set of logical folders for you, so the cleanest way is to add the different files where they belong (skip ahead for a quicker solution):

  1. Header files:
    1. Right click the folder Header Files > Add Existing Item...
    2. Navigate to each of the directories mentioned above (wherever you have chosen to place them) and select all the header files (except those noted as exceptions).
  2. Source files:
    1. Right click the folder Source Files > Add Existing Item...
    2. Navigate to each of the directories mentioned above (wherever you have chosen to place them) and select all the source files (except those noted as exceptions).

Using Add File to create new files in a project will place them in their respective logical folder

Option 2: The Quick Way

Which logical folders you put what in is of no matter to NetBeans. They exist as an organizational tool for you, so a quicker alternative is to just add the three folders as a whole:

  1. Right click your project > Add Existing Items from Folders...
  2. Click Add Folder... and navigate to each of the directiories mentioned above (wherever you have chosen to place them).
    This will add the folders you specify as new logical folder, along with all their contents, files and subfolders alike. Thus, if you have organized the library directories inside the folder containg the example code, you only need to add the latter.
  3. Because we add everything in these folders, you have to remove those noted as exceptions above:
    1. For folders: Right click > Remove
    2. For files: Right click > Remove from Project

      Remove will only remove the files from the project. Using Delete or the Delete key will also delete them from disk!

Build and Run

Make sure to set your project as main: Right click your project > Set as Main Project

You can now build your project by pressing the large hammer button (or F11 by default).
This will open a new tab in the lower Output window showing the progress as NetBeans makes sure to transfer the necessary files to the Pi, and perform the necessary commands to build the project remotely.

If the build succeeds, you can now run the project remotely by pressing the green play button (or F6 by default).
A second new tab will open in the lower Output window to display the console output from the remotely running application.

NetBeans also supports full on-remote debugging!

 

Build Files and Executable

By default, NetBeans will synchronise the project files to the RPi with a directory structure identical to that on your computer, placed inside a root folder:

/root/.netbeans/remote/<RPi IP-address>/<computer name>-<operating system>/<original path>

Within this structure, build files are placed in a build/ directory inside your project folder, while the executable is placed in a similar dist/ folder.

Say, for example, that your RPi has an IP address of 192.168.100.1, your computer is running 64 bit Windows and has the name defaultuser. If your project folder is located at C:\SimplePublishProject on your computer, the dist/ directory containing the executable can be found at:

/root/.netbeans/remote/192.168.100.1/defaultuser-Windows-x86_64/C/SimplePublishProject/dist/Debug/GNU-Linux/

You need to be logged in as root to access the above directories.