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