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 »

Introduction

The QuickStart Library (QSL) is an additional library that sits on top of the SmartMesh C Library that handles most application logic, so the user need only supply low-level hardware drivers, and use a handful of APIs to connect to a network and send data. This removes the need for intricate knowledge about the SmartMesh IP mote and network, greatly reducing firmware development time for your microcontroller.  The raw C Library functions can be mixed with the QSL, giving the user full access to all mote APIs should need arise.

The library also includes example code for certain platforms (currently only the Raspberry Pi), and this guide will also cover some details on setting these up.

 

 

 

This is a step-by-step guide for how to get started with data publishing from a SmartMesh IP Mote with an external microcontroller (MCU) connected serially. By following these steps, you can make your application publish data into a SmartMesh IP network by utilizing only a few intuitive functions from the SmartMesh QuickStart Library (QSL). 

 

1.1 Necessary Hardware

  • Microcontroller/development board of your choice
    • With necessary programmer
  • SmartMesh IP Mote (DC9003A-B)
    • With 6 female-female jumper cables
  • SmartMesh IP Manager (DC2274A-A)
  • SmartMesh Interface Board (DC9006A)
    • With USB to USB-micro cable
  • Computer (should support Python and Java)

1.2 Necessary Software

  • FTDI Serial Drivers
  • IDE/programmer/debugger of your choice
  • SmartMesh C Library
  • SmartMesh QuickStart Library
  • Git (optional)

1.3 Acronyms, Abbreviations, and Definitions

APIApplication Programming Interface
CLICommand Line Interface
FSMFinite State Machine
GPIOGenearal-Purpose Input/Output
HDLCHigh-Level Data Link Control
OSOperation System
QSLQuickStart Library
RPiRaspberry Pi
UARTUniversal Asynchronous Receiver/Transmitter

Prepare Libraries

2.1 About

The SmartMesh QSL abstracts the complexity of the SmartMesh IP mote startup and network search/join procedure into a few intuitive functions. It utilizes the SmartMesh C Library, which provides a full implementation of the serial API of the SmartMesh IP mote.


While the QSL provides sufficient functionality for sending/receiving data and configuring the most important network settings on a SmartMesh IP mote, more advanced applications that need access to all parameters should access the mote serial API directly.

 

Library structure Copy

 

2.2 Download

There are two ways to download the QuickStart Library:

Clone The Source Code

  1. Clone the repository: TODO: Add link when public repo created
  2. Checkout the latest release: TODO: Refer to latest release
  3. Initialize and update the neccessary C Library submodule:

     

    (...)/QuickStart_Library $ git submodule init
    (...)/QuickStart_Library $ git submodule update

Download Zip/Tar

  1. Go to the QSL release page and download the latest zip/tar: TODO: Add link when public repo created
  2. Go to the C Library release page and download the latest zip/tar: TODO: Can possibly be added as attachment to QSL release. If not: Make a note about why it needs to be downloaded separately
  3. Unzip the files using your favourite compression tool.

2.3 Directory Structure

The QSL repository contains the following directories:

  • sm_qsl/ contains the QuickStart Library as a collection of headers (.h) and source code files (.c).
  • sm_clib/ is a submodule that contains the underlying C Library, with sample applications for its direct use.
    • sm_clib/ is the C Library itself as a collection of headers (.h) and source code files (.c).
      • ports/ contains an example of the necessary C Library ports.
    • examples/ contains sample applications which use the C Library directly.
  • examples/ contains sample applications utilizing the QSL
    • rpi/SimplePublish/ contains an example that connects and starts publishing random data, where the necessary porting functions have been implemented for the Raspberry Pi.
       

The SmartMesh C Library will be in the sm_clib/ directory from the separate unzipped folder if you chose the zip download.

 

The SmartMesh QuickStart Library, and C Library it depends on, are both designed so you can drop their directories into your application without modification, i.e. these two folders from the structure above:

  • sm_qsl/
  • sm_clib/sm_clib/

You are free to modify them to suit your needs; however, we recommend you avoid changing the contents of these two directories, as this will allow you to easily replace them with future revisions.

In addition to the files in these folders, you need to implement a handful of functions, as discussed in the next section.

2.4 Port to Your Hardware

The QSL and the underlying C Library are written to be used as-is in any C-based platform; however, you have to implement a handful of functions to adapt them to a specific platform. These functions are implemented in the example code provided for certain platforms (presented in later chapters), so you can skip the details of this section for now if you plan on using these (or simply wish to have a look at the examples first).

 The necessary functions are declared in following header files:

  • dn_uart.h: These functions allow the SmartMesh C Library to send bytes over the serial port, and receive bytes from the serial port. A "flush" function is provided in case the serial (UART) driver of the user platform is frame-oriented rather than byte-oriented.
  • dn_lock.h: These functions allow the SmartMesh C Library to operate in a multi-threaded environment. If this is the case in the user system, the implementation of these functions would typically consist of pending/posting a mutual exclusion semaphore (mutex); if not, simply use stub functions (i.e. "empty functions").
  • dn_endianness.h: These functions are used to match multi-byte API fields (which are big-endian) to the endianness of the platform.
  • dn_time.h: These functions allows the QSL to perform timing and schedule tasks.
  • dn_watchdog.h: These functions allow the QSL to make sure any watchdog in the user application is fed during processes that can take some time (e.g. searching for a network). If no watchdog is present, simply use stub functions.

 


The QSL is currently meant to be run in a single threaded environment, thus you can just use stub functions for the prototypes in dn_lock.h. If you wish to run the QSL in a multi-threaded environment, you should create your own mutex (separate from the prototypes in dn_lock.h) to be locked/unlocked before/after calls to the QSL API.

 

 

 Click here to expand more details about the functions you have to implement...

 

Install FTDI Serial Drivers

To be able to connect to and configure the mote and manager, your computer will need the necessary FTDI Serial Drivers. Most modern OSes come with FTDI drivers pre-installed (e.g. Linux), but you may have to install them manually if they do not configure automatically when you plug in the interface board or manager. Follow the installation guide corresponding to your OS if you have any trouble.

If installed correctly, the interface board or manager should appear as a group of 4 serial ports when connected via USB:

  • Windows: 4 COM ports can be viewed using the Device Manager (Control Panel -> System -> Hardware -> Device Manager -> Ports)
  • Linux: $ dmesg | grep FTDI should detect FTDI converters attached to /dev/ttyUSBn to /dev/ttyUSBn+3

The ports of interest are the third and fourth, which map to the CLI and API, respectively, as defined by Table 1

 

 

DeviceSerial Port NumberUsageBaudrateData BitsParityStop Bits
SmartMesh IP Managerthird*CLI96008No1
fourth*API115200**8**No**1**
SmartMesh IP Motethird*CLI96008No1
fourth*API115200**8**No**1**

 

Table 1: Serial port configurations

*: Refers to the serial ports created by the FTDI drivers.
**: Default values (can be changed).


Note that for Windows, the COM port assignment will change whenever you connect the interface board (or manager) to a different USB port.

Prepare Mote

4.1 Select Slave Mode

 

moteInterface2PC

 

Figure 1: DC9003 mote board (left) connected to a computer via the DC9006 interface board (center).

 

By default, the motes in starter kits are configured for master mode; a demo mode where the API is disabled and an application is run that generates sample data and controls joining. To use the mote alongside an external MCU, the mote has to be configured for slave mode; the API is enabled and the mote expects a serially attached application to control it.

  1. Start by connecting the mote to your computer via the interface board, as shown in Figure 1.
  2. Identify the port name that maps to the CLI. For example, if you are in Windows and see the four COM ports pictured below, the CLI will be accessible through COM14.
     
  3. Connect to the mote CLI with a third-party serial terminal of your choice (e.g. putty). See Table 1 for configuration details.
  4. Use the get mode command to see the current mode:

     

    > get mode
    master
  5. Use the set mode command to switch to slave mode, followed by reset for the change to take effect. After rebooting, a new get mode should confirm the persistent mode change.

     

    > set mode slave
     
    > reset
      
    > SmartMesh IP mote, ver 1.3.3.1 (0x100)
    > get mode
    slave
  6. Disconnect the mote from the interface board and make sure to remove its battery (if any), as we will power the mote from the MCU.

4.2 Connect to Your Microcontroller

You will need to connect 4 wires between your MCU and the mote for power and serial communication, as shown in Table 2In addition you need to ground the mote RX Request to Send (RTS) and TX Clear to Send (CTS); unless your MCU cannot wake on data and has to use these accordingly (the mote does not need incoming flow control).

Table 3 lists the names (equal to those found on the silkscreen) of the P1 pin header on the DC9003A mote board visible in Figure 2.

 

 

MCU
Mote
3.3 VVBAT
GroundGND
UART TXRX
UART RXTX

 

Table 2: MCU to mote pin mapping.

 

 

 

NamePin #Name
TX CTSn12TX RTSn
TX34GND
RX56RX RTSn
RX CTSn78CO TX
CO RX910GND
RESETn1112F P ENn
I MISO1314I MOSI
I SSn1516I SCK
GND1718TCK
TMS1920TDO
TDI2122VUSB_3V6
PGOOD2324GND
VBAT2526KEY
EHORBAT2728RSVD
I/O 12930I/O 2
V+3132+5V

 

Table 3: DC9003A (mote) P1 Pins

 

 

Figure 2: The DC9003A (mote) with the P1 pin header marked in red.

 

Prepare Manager

Figure 3: The DC2274A SmartMesh IP USB Manager

If you installed the correct drivers, you only have to insert the USB port in your computer: The manager will power up and start creating a network automatically.

Similar to the mote via the interface board, you should now see 4 new serial ports, where the third and fourth give you access to the CLI and API, respectively.

5.1 Factory Default Values

By default the manager is shipped with a well-known network ID and join key that your application will need to connect to its network, so you do not need to configure anything. However, it is useful to know about certain commands available through the CLI. These commands are presented in the next section, while relevant default values for its configuration parameters are listed in Table 4.

 

 

Parameter
Comment
Default Value
netidNetwork ID1229
commjoinkeyCommon Join Key (hex)44 55 53 54 4E 45 54 57 4F 52 4B 53 52 4F 43 4B 
basebwBase bandwidth, i.e. period between packets [ms]9000

 

Table 4: Relevant factory default values for the SmartMesh IP Manager

Refer to the SmartMesh IP User's Guide for a complete list of default configuration parameters for the manager.

 

5.2 Useful CLI Commands

With the manager connected to your computer via USB:

  1. Just like with the mote, identify the correct serial port name and connect to the CLI with a serial terminal of your choice (again, see Table 1 for configuration details).
  2. Unlike the mote, you will first have to login to have access to the commands we want:

    login

     

    > login user
      
  3. You now have access to a wide range of commands, where the most interesting ones are shown below.
 Click here to expand the CLI commands...

User Application

This Chapter presents how to use the QSL in your application. We begin by presenting details about the API, followed by an example of how a user application might look.

More explicit examples of code are given for certain platforms in later chapters (currently only for the Raspberry Pi).

6.1 The QuickStart API

Instead of reading the full user guide for the SmartMesh IP network and mote serial API, the QuickStart Library allows you to get started with simple data publishing by familiarizing yourself with only a handful of functions. These functions define the API and are listed below, followed by a detailed explanation.

QuickStart API

 

bool dn_qsl_init(void);
bool dn_qsl_isConnected(void);
bool dn_qsl_connect(uint16_t netID, const uint8_t* joinKey, uint16_t srcPort, uint32_t service_ms);
bool dn_qsl_send(const uint8_t* payload, uint8_t payloadSize_B, uint16_t destPort);
uint8_t dn_qsl_read(uint8_t* readBuffer);

 

 Click here to expand detailed descriptions...

6.2 Example Application

Assuming you have downloaded the libraries and ported the necessary functions, your application simply needs to include the QSL header file, dn_qsl_api.h, and use the API as described in the previous section.

Below is a short example of how an application can send a data struct every 5 seconds, followed by parsing any downstream data received since the last transmission.

 Click here to expand example...

6.3 Further Tips

Base Bandwidth vs Service Request

Every mote is given a base bandwidth (service) upon joining the network (default 9000 ms) that specifies how often the mote can publish data. As noted in the QSL API, you can specify that the mote should request a specific service when connecting. However, this process can add between 20 - 60 seconds for connect to complete. Unless the motes in your network actually need individual bandwidths, you should rather change the base bandwidth granted by the manager: This is easily changed with the set config CLI command.

Change Network ID and Join Key

The default network ID and join key is well known, so unless you change these, anyone can potentially connect to your network. You can easily change both with the set config CLI command on the manager, and then use the same in your application.

Aggregate Data

If your application wants to publish data periodically etc. you should (if possible) aggregate many measurements into each transmission, rather than sending one for each measurement. The SmartMesh IP Network is designed with power consumption in mind, and its motes use very little power, except when transmitting data. Also, since the total bandwidth of a network is limited (36.4 packets/sec for the embedded manager), you will run into trouble if 40 motes requests a bandwidth to transmit messages every second (i.e. a service of 1000 ms).

Back Off Mechanism

Sending can fail if the mote is very busy. The following back off algorithm is recommended such that you only publish at a level that the network can tolerate:

  • If send fails (but you are still connected), double the interval between packets. Upon subsequent failures, increase to 3x, 4x, ..., 255x.
  • When send succeeds after failing, decrease the interval along the same pattern: 5x, 4x, ..., 1x.

Use the Access Control List

The manager maintains an Access Control List (ACL) which associates a mote's MAC address with a mote-specific join key. Once an ACL entry has been set, the manager will not let motes join the network that are not in the ACL. You can add a mote manually to the ACL through the set acl CLI command.

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

Test Downstream Data

The aforementioned DustLink apps that you can create has currently not implemented support for sending downstream messages to motes (although you might have noticed the "Fields toMote" that implies that its support is in the pipline). As an alternative, we present two simple ways to test sending downstream data to a mote. Both of these use tools part of the SmartMesh SDK, so the first thing you will have to do is to fetch this:

9.1 SimpleIPDownstreamMgr

SimpleIPDownstreamMgr is a simple console application that only asks you to specify the name of the port that the serial API of the manager is connected to (e.g. COM15). It will then connect to said manager, fetch a list of all operational motes connected to the network and start sending "Hello, World!" to each of them every 5 seconds, addressed to port 60000. The SimplePublish example application is set to listen to the same port, and should be able to parse and print the message.


If you have DustLink running and connected to the manager, it will occupy the port with the manager serial API, thus preventing any other application to do the same. In other words, you have to stop DustLink when using this application.

9.2 APIExplorer

APIExplorer is a GUI that allows you to interact with the full API of all SmartMesh devices. The interesting API command here is sendData:

  1. Set network type to SmartMesh IP, device type to manager and press load.
  2. Enter the port name that the serial API of the manager is connected to (e.g. COM15) and press connect.
  3. Select sendData from the command drop down list
  4. Enter the necessary fields (options has to be 0) and press send.


If you have DustLink running and connected to the manager, it will occupy the port with the manager serial API, thus preventing any other application to do the same. In other words, you have to stop DustLink when using this application.

 

 

 

 

  • No labels