Example programs for MultiTech Dot devices demonstrating how to use the Dot devices and the Dot libraries for LoRa communication.

Dependencies:   ISL29011

Dependents:   Dot-Examples-delujoc

This project has moved to github

Please see GitHub Dot-Examples

Dot Library Not Included!

Because these example programs can be used for both mDot and xDot devices, the LoRa stack is not included. The libmDot library should be imported if building for mDot devices. The libxDot library should be imported if building for xDot devices.

Dot Library Limitations

Commit messages in Dot Library repositories specify the version of the library and the version of mbed-os it was compiled against. We recommend building your application with the version of mbed-os specified in the commit message of the version of the Dot library you're using. This will ensure that you don't run into any runtime issues caused by differences in the mbed-os versions.

Example Programs Description

This application contains multiple example programs. Each example demonstrates a different way to configure and use a Dot. A short summary of each example is provided below. Common code used by multiple examples is in the dot_utils.cpp file.

All examples print logging, including RX data, on the USB debug port at 115200 baud. Each example defaults the Dot's configuration and saves the new configuration to NVM.

OTA Example

This example demonstrates configuring the Dot for OTA join mode and entering sleep or deepsleep mode between transactions with the gateway. If deepsleep mode is used, the session is saved and restored so that a rejoin is not necessary after waking up even though RAM contents have been lost. ACKs are disabled, but network link checks are configured - if enough link checks are missed, the Dot will no longer be considered joined to the network and will attempt to rejoin before transmitting more data.

AUTO_OTA Example

This example demonstrates configuring the Dot for AUTO_OTA join mode and entering sleep or deepsleep mode between transactions with the gateway. AUTO_OTA join mode automatically saves and restores the session when deepsleep mode is used, so the manual saving and restoring of the session is not necessary. ACKs are disabled, but network link checks are configured - if enough link checks are missed, the Dot will no longer be considered joined to the network and will attempt to rejoin before transmitting more data.

Manual Example

This example demonstrates configuring the Dot for MANUAL join mode and entering sleep or deepsleep mode between transactions with the gateway. The Dot must be provisioned on the gateway before its packets will be accepted! Follow these steps to provision the Dot on a Conduit gateway:

  • ssh into the conduit
  • use the lorq-query application to provision the Dot on the gateway
    • lora-query -a 01020304 A 0102030401020304 <your Dot's device ID> 01020304010203040102030401020304 01020304010203040102030401020304
    • if any of the credentials change on the Dot side, they must be updated on the gateway side as well

To provision a Dot on a third-party gateway, see the gateway or network provider documentation.

Class B Example

This example demonstrates how to configure the dot for an OTA join, how to acquire a lock on a GPS synchronized beacon, and then to subsequently enter class B mode of operation. After a successful join, the device will request to the dot-library to switch to class B. When this happens, the library will send an uplink to the network server (hence we must be joined first before entering this mode) requesting the GPS time to calculate when the next beacon is expected. Once this time elapses, the dot will open an rx window to demodulate the broadcasted beacon and fire an mDotEvent::BeaconRx event upon successful reception. After the beacon is received, the example sends an uplink which will have the class B bit in the packet's frame control set to indicate to the network server that downlinks may now be scheduled on ping slots. The lora-query application can be used to configure a Conduit gateway to communicate with a Dot in class B mode. For information on how to inform a third-party gateway that a Dot is operating in class B mode, see the gateway or network provider documentation.

Class C Example

This example demonstrates configuring the Dot for OTA join mode and communicating with the gateway using class C mode. In class C mode the gateway can send a packet to the Dot at any time, so it must be listening whenever it is not transmitting. This means that the Dot cannot enter sleep or deepsleep mode. The gateway will not immediately send packets to the Dot (outside the receive windows following a transmission from the Dot) until it is informed that the Dot is operating in class C mode. The lora-query application can be used to configure a Conduit gateway to communicate with a Dot in class C mode. For information on how to inform a third-party gateway that a Dot is operating in class C mode, see the gateway or network provider documentation.

FOTA Example

Full FOTA support is available on mDot and on xDot with external flash. See this article for details on adding external flash for xDot FOTA.

Without external flash xDot can use the FOTA example to dynamically join a multicast session only. After joining the multicast session the received Fragmentation packets could be handed to a host MCU for processing and at completion the firmware can be loaded into the xDot using the bootloader and y-modem. See xDot Developer Guide.

This example demonstrates how to incorporate over-the-air updates to an application. The example uses a Class C application. Class A or B functionality could also be used. The device will automatically enter into Class C operation for the FOTA operation, Class B would be disabled during the FOTA transfer.

  • Add the following code to allow Fota to use the Dot instance

examples/src/fota_example.cpp

    // Initialize FOTA singleton
    Fota::getInstance(dot);
  • Add fragmentation and multicast handling the the PacketRx event

examples/inc/RadioEvent.h

    virtual void PacketRx(uint8_t port, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr, lora::DownlinkControl ctrl, uint8_t slot, uint8_t retries, uint32_t address, uint32_t fcnt, bool dupRx) {
        mDotEvent::PacketRx(port, payload, size, rssi, snr, ctrl, slot, retries, address, fcnt, dupRx);

#if ACTIVE_EXAMPLE == FOTA_EXAMPLE
        if(port == 200 || port == 201 || port == 202) {
            Fota::getInstance()->processCmd(payload, port, size);
        }
#endif
    }

A definition is needed to enable FOTA.

mbed_app.json

{
    "macros": [
        "FOTA=1"
    ]
}


Peer to Peer Example

This example demonstrates configuring Dots for peer to peer communication without a gateway. It should be compiled and run on two Dots. Peer to peer communication uses LoRa modulation but uses a single higher throughput (usually 500kHz or 250kHz) datarate. It is similar to class C operation - when a Dot isn't transmitting, it's listening for packets from the other Dot. Both Dots must be configured exactly the same for peer to peer communication to be successful.


Choosing An Example Program and Channel Plan

Only the active example is compiled. The active example can be updated by changing the ACTIVE_EXAMPLE definition in the examples/example_config.h file.

By default the OTA_EXAMPLE will be compiled and the US915 channel plan will be used.

example_config.h

#ifndef __EXAMPLE__CONFIG_H__
#define __EXAMPLE__CONFIG_H__

#define OTA_EXAMPLE              1  // see ota_example.cpp
#define AUTO_OTA_EXAMPLE         2  // see auto_ota_example.cpp
#define MANUAL_EXAMPLE           3  // see manual_example.cpp
#define PEER_TO_PEER_EXAMPLE     4  // see peer_to_peer_example.cpp
#define CLASS_C_EXAMPLE          5  // see class_c_example.cpp

// the active example is the one that will be compiled
#if !defined(ACTIVE_EXAMPLE)
#define ACTIVE_EXAMPLE  OTA_EXAMPLE
#endif

// the active channel plan is the one that will be compiled
// options are :
//      CP_US915
//      CP_AU915
//      CP_EU868
//      CP_KR920
//      CP_AS923
//      CP_AS923_JAPAN
#if !defined(CHANNEL_PLAN)
#define CHANNEL_PLAN CP_US915
#endif

#endif


Compile the AUTO_OTA_EXAMPLE and use the EU868 channel plan instead.

example_config.h

#ifndef __EXAMPLE__CONFIG_H__
#define __EXAMPLE__CONFIG_H__

#define OTA_EXAMPLE              1  // see ota_example.cpp
#define AUTO_OTA_EXAMPLE         2  // see auto_ota_example.cpp
#define MANUAL_EXAMPLE           3  // see manual_example.cpp
#define PEER_TO_PEER_EXAMPLE     4  // see peer_to_peer_example.cpp
#define CLASS_C_EXAMPLE          5  // see class_c_example.cpp

// the active example is the one that will be compiled
#if !defined(ACTIVE_EXAMPLE)
#define ACTIVE_EXAMPLE  AUTO_OTA_EXAMPLE
#endif

// the active channel plan is the one that will be compiled
// options are :
//      CP_US915
//      CP_AU915
//      CP_EU868
//      CP_KR920
//      CP_AS923
//      CP_AS923_JAPAN
#if !defined(CHANNEL_PLAN)
#define CHANNEL_PLAN CP_EU868
#endif

#endif



Dot Libraries

Stable and development libraries are available for both mDot and xDot platforms. The library chosen must match the target platform. Compiling for the mDot platform with the xDot library or vice versa will not succeed.

mDot Library

Development library for mDot.

libmDot-dev

Stable library for mDot.

libmDot-stable


For mbed-os 5 use:

Import librarylibmDot-mbed5

Stable version of the mDot library for mbed 5. This version of the library is suitable for deployment scenarios. See lastest commit message for version of mbed-os library that has been tested against.

xDot Library

Development library for xDot.

libxDot-dev

Stable library for xDot.

libxDot-stable


For mbed-os 5 use:

Import librarylibxDot-mbed5

Stable version of the xDot library for mbed 5. This version of the library is suitable for deployment scenarios.

Committer:
Taylor Heck
Date:
Tue May 25 09:54:33 2021 -0500
Revision:
42:20f6b29a9903
Parent:
33:15ea8f985c54
Target mbed-os 6 and Dot Library version 4.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jreiss 33:15ea8f985c54 1 #include "dot_util.h"
jreiss 33:15ea8f985c54 2 #include "RadioEvent.h"
jreiss 33:15ea8f985c54 3
jreiss 33:15ea8f985c54 4 #if ACTIVE_EXAMPLE == CLASS_B_EXAMPLE
jreiss 33:15ea8f985c54 5
jreiss 33:15ea8f985c54 6 /////////////////////////////////////////////////////////////////////////////
jreiss 33:15ea8f985c54 7 // -------------------- DOT LIBRARY REQUIRED ------------------------------//
jreiss 33:15ea8f985c54 8 // * Because these example programs can be used for both mDot and xDot //
jreiss 33:15ea8f985c54 9 // devices, the LoRa stack is not included. The libmDot library should //
jreiss 33:15ea8f985c54 10 // be imported if building for mDot devices. The libxDot library //
jreiss 33:15ea8f985c54 11 // should be imported if building for xDot devices. //
jreiss 33:15ea8f985c54 12 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-dev-mbed5/ //
jreiss 33:15ea8f985c54 13 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-mbed5/ //
jreiss 33:15ea8f985c54 14 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-dev-mbed5/ //
jreiss 33:15ea8f985c54 15 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-mbed5/ //
jreiss 33:15ea8f985c54 16 /////////////////////////////////////////////////////////////////////////////
jreiss 33:15ea8f985c54 17
jreiss 33:15ea8f985c54 18 /////////////////////////////////////////////////////////////
jreiss 33:15ea8f985c54 19 // * these options must match the settings on your gateway //
jreiss 33:15ea8f985c54 20 // * edit their values to match your configuration //
jreiss 33:15ea8f985c54 21 // * frequency sub band is only relevant for the 915 bands //
jreiss 33:15ea8f985c54 22 // * either the network name and passphrase can be used or //
jreiss 33:15ea8f985c54 23 // the network ID (8 bytes) and KEY (16 bytes) //
jreiss 33:15ea8f985c54 24 /////////////////////////////////////////////////////////////
jreiss 33:15ea8f985c54 25 static std::string network_name = "MultiTech";
jreiss 33:15ea8f985c54 26 static std::string network_passphrase = "MultiTech";
jreiss 33:15ea8f985c54 27 static uint8_t network_id[] = { 0x6C, 0x4E, 0xEF, 0x66, 0xF4, 0x79, 0x86, 0xA6 };
jreiss 33:15ea8f985c54 28 static uint8_t network_key[] = { 0x1F, 0x33, 0xA1, 0x70, 0xA5, 0xF1, 0xFD, 0xA0, 0xAB, 0x69, 0x7A, 0xAE, 0x2B, 0x95, 0x91, 0x6B };
jreiss 33:15ea8f985c54 29 static uint8_t frequency_sub_band = 1;
jreiss 33:15ea8f985c54 30 static lora::NetworkType public_network = lora::PUBLIC_LORAWAN;
jreiss 33:15ea8f985c54 31 static uint8_t join_delay = 5;
jreiss 33:15ea8f985c54 32 static uint8_t ack = 3;
jreiss 33:15ea8f985c54 33 static bool adr = true;
jreiss 33:15ea8f985c54 34
jreiss 33:15ea8f985c54 35 // Number of ping slots to open per beacon interval - see mDot.h
jreiss 33:15ea8f985c54 36 static uint8_t ping_periodicity = 4;
jreiss 33:15ea8f985c54 37
jreiss 33:15ea8f985c54 38 mDot* dot = NULL;
jreiss 33:15ea8f985c54 39 lora::ChannelPlan* plan = NULL;
jreiss 33:15ea8f985c54 40
Taylor Heck 42:20f6b29a9903 41 mbed::UnbufferedSerial pc(USBTX, USBRX);
jreiss 33:15ea8f985c54 42
jreiss 33:15ea8f985c54 43 #if defined(TARGET_XDOT_L151CC)
jreiss 33:15ea8f985c54 44 I2C i2c(I2C_SDA, I2C_SCL);
jreiss 33:15ea8f985c54 45 ISL29011 lux(i2c);
jreiss 33:15ea8f985c54 46 #else
jreiss 33:15ea8f985c54 47 AnalogIn lux(XBEE_AD0);
jreiss 33:15ea8f985c54 48 #endif
jreiss 33:15ea8f985c54 49
jreiss 33:15ea8f985c54 50 int main() {
jreiss 33:15ea8f985c54 51 // Custom event handler for automatically displaying RX data
jreiss 33:15ea8f985c54 52 RadioEvent events;
jreiss 33:15ea8f985c54 53
jreiss 33:15ea8f985c54 54 pc.baud(115200);
jreiss 33:15ea8f985c54 55
jreiss 33:15ea8f985c54 56 #if defined(TARGET_XDOT_L151CC)
jreiss 33:15ea8f985c54 57 i2c.frequency(400000);
jreiss 33:15ea8f985c54 58 #endif
jreiss 33:15ea8f985c54 59
jreiss 33:15ea8f985c54 60 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
Taylor Heck 42:20f6b29a9903 61
Taylor Heck 42:20f6b29a9903 62 // Create channel plan
Taylor Heck 42:20f6b29a9903 63 plan = create_channel_plan();
jreiss 33:15ea8f985c54 64 assert(plan);
jreiss 33:15ea8f985c54 65
jreiss 33:15ea8f985c54 66 dot = mDot::getInstance(plan);
jreiss 33:15ea8f985c54 67 assert(dot);
jreiss 33:15ea8f985c54 68
jreiss 33:15ea8f985c54 69 logInfo("mbed-os library version: %d.%d.%d", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
jreiss 33:15ea8f985c54 70
jreiss 33:15ea8f985c54 71 // start from a well-known state
jreiss 33:15ea8f985c54 72 logInfo("defaulting Dot configuration");
jreiss 33:15ea8f985c54 73 dot->resetConfig();
jreiss 33:15ea8f985c54 74 dot->resetNetworkSession();
jreiss 33:15ea8f985c54 75
jreiss 33:15ea8f985c54 76 // make sure library logging is turned on
jreiss 33:15ea8f985c54 77 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
jreiss 33:15ea8f985c54 78
jreiss 33:15ea8f985c54 79 // attach the custom events handler
jreiss 33:15ea8f985c54 80 dot->setEvents(&events);
jreiss 33:15ea8f985c54 81
jreiss 33:15ea8f985c54 82 // update configuration if necessary
jreiss 33:15ea8f985c54 83 if (dot->getJoinMode() != mDot::OTA) {
jreiss 33:15ea8f985c54 84 logInfo("changing network join mode to OTA");
jreiss 33:15ea8f985c54 85 if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) {
jreiss 33:15ea8f985c54 86 logError("failed to set network join mode to OTA");
jreiss 33:15ea8f985c54 87 }
jreiss 33:15ea8f985c54 88 }
jreiss 33:15ea8f985c54 89 // in OTA and AUTO_OTA join modes, the credentials can be passed to the library as a name and passphrase or an ID and KEY
jreiss 33:15ea8f985c54 90 // only one method or the other should be used!
jreiss 33:15ea8f985c54 91 // network ID = crc64(network name)
jreiss 33:15ea8f985c54 92 // network KEY = cmac(network passphrase)
jreiss 33:15ea8f985c54 93 update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, public_network, ack);
jreiss 33:15ea8f985c54 94 //update_ota_config_id_key(network_id, network_key, frequency_sub_band, public_network, ack);
jreiss 33:15ea8f985c54 95
jreiss 33:15ea8f985c54 96 // enable or disable Adaptive Data Rate
jreiss 33:15ea8f985c54 97 dot->setAdr(adr);
jreiss 33:15ea8f985c54 98
jreiss 33:15ea8f985c54 99 // Configure the join delay
jreiss 33:15ea8f985c54 100 dot->setJoinDelay(join_delay);
jreiss 33:15ea8f985c54 101
jreiss 33:15ea8f985c54 102 // Configure the class B ping periodicity
jreiss 33:15ea8f985c54 103 dot->setPingPeriodicity(ping_periodicity);
jreiss 33:15ea8f985c54 104
jreiss 33:15ea8f985c54 105 // save changes to configuration
jreiss 33:15ea8f985c54 106 logInfo("saving configuration");
jreiss 33:15ea8f985c54 107 if (!dot->saveConfig()) {
jreiss 33:15ea8f985c54 108 logError("failed to save configuration");
jreiss 33:15ea8f985c54 109 }
jreiss 33:15ea8f985c54 110
jreiss 33:15ea8f985c54 111 // display configuration
jreiss 33:15ea8f985c54 112 display_config();
jreiss 33:15ea8f985c54 113
jreiss 33:15ea8f985c54 114 // join the network - must do this before attempting to switch to class B
jreiss 33:15ea8f985c54 115 join_network();
jreiss 33:15ea8f985c54 116
jreiss 33:15ea8f985c54 117 // configure the Dot for class B operation
jreiss 33:15ea8f985c54 118 // the Dot must also be configured on the gateway for class B
jreiss 33:15ea8f985c54 119 // use the lora-query application to do this on a Conduit: http://www.multitech.net/developer/software/lora/lora-network-server/
jreiss 33:15ea8f985c54 120 // to provision your Dot for class B operation with a 3rd party gateway, see the gateway or network provider documentation
jreiss 33:15ea8f985c54 121 // Note: we won't actually switch to class B until we receive a beacon (mDotEvent::BeaconRx fires)
jreiss 33:15ea8f985c54 122 logInfo("changing network mode to class B");
jreiss 33:15ea8f985c54 123
jreiss 33:15ea8f985c54 124 if (dot->setClass("B") != mDot::MDOT_OK) {
jreiss 33:15ea8f985c54 125 logError("Failed to set network mode to class B");
jreiss 33:15ea8f985c54 126 logInfo("Reset the MCU to try again");
jreiss 33:15ea8f985c54 127 return 0;
jreiss 33:15ea8f985c54 128 }
jreiss 33:15ea8f985c54 129
jreiss 33:15ea8f985c54 130 // Start a timer to check the beacon was acquired
jreiss 33:15ea8f985c54 131 LowPowerTimer bcn_timer;
jreiss 33:15ea8f985c54 132 bcn_timer.start();
jreiss 33:15ea8f985c54 133
jreiss 33:15ea8f985c54 134 while (true) {
jreiss 33:15ea8f985c54 135 std::vector<uint8_t> tx_data;
jreiss 33:15ea8f985c54 136 static bool send_uplink = true;
jreiss 33:15ea8f985c54 137
jreiss 33:15ea8f985c54 138 // Check if we locked the beacon yet and send an uplink to notify the network server
jreiss 33:15ea8f985c54 139 // To receive data from the gateway in class B ping slots, we must have received a beacon
jreiss 33:15ea8f985c54 140 // already, and sent one uplink to signal to the network server that we are in class B mode
jreiss 33:15ea8f985c54 141 if (events.BeaconLocked && send_uplink) {
jreiss 33:15ea8f985c54 142 logInfo("Acquired a beacon lock");
jreiss 33:15ea8f985c54 143
jreiss 33:15ea8f985c54 144 // Add a random delay before trying the uplink to avoid collisions w/ other motes
jreiss 33:15ea8f985c54 145 srand(dot->getRadioRandom());
jreiss 33:15ea8f985c54 146 uint32_t rand_delay = rand() % 5000;
jreiss 33:15ea8f985c54 147 logInfo("Applying a random delay of %d ms before class notification uplink", rand_delay);
Taylor Heck 42:20f6b29a9903 148 ThisThread::sleep_for(std::chrono::milliseconds(rand_delay));
jreiss 33:15ea8f985c54 149
jreiss 33:15ea8f985c54 150 // Ensure the link is idle before trying to transmit
jreiss 33:15ea8f985c54 151 while (!dot->getIsIdle()) {
Taylor Heck 42:20f6b29a9903 152 ThisThread::sleep_for(10ms);
jreiss 33:15ea8f985c54 153 }
jreiss 33:15ea8f985c54 154
jreiss 33:15ea8f985c54 155 if (send_data(tx_data) != mDot::MDOT_OK) {
jreiss 33:15ea8f985c54 156 logError("Failed to inform the network server we are in class B");
jreiss 33:15ea8f985c54 157 logInfo("Reset the MCU to try again");
jreiss 33:15ea8f985c54 158 return 0;
jreiss 33:15ea8f985c54 159 }
jreiss 33:15ea8f985c54 160
jreiss 33:15ea8f985c54 161 logInfo("Enqueued packets may now be scheduled on class B ping slots");
jreiss 33:15ea8f985c54 162 send_uplink = false;
jreiss 33:15ea8f985c54 163 bcn_timer.stop();
jreiss 33:15ea8f985c54 164 } else if (!events.BeaconLocked) {
jreiss 33:15ea8f985c54 165 logInfo("Waiting to receive a beacon..");
jreiss 33:15ea8f985c54 166
jreiss 33:15ea8f985c54 167 if (bcn_timer.read() > lora::DEFAULT_BEACON_PERIOD) {
jreiss 33:15ea8f985c54 168 if (dot->setClass("B") != mDot::MDOT_OK) {
jreiss 33:15ea8f985c54 169 logError("Failed to set network mode to class B");
jreiss 33:15ea8f985c54 170 logInfo("Reset the MCU to try again");
jreiss 33:15ea8f985c54 171 return 0;
jreiss 33:15ea8f985c54 172 }
jreiss 33:15ea8f985c54 173
jreiss 33:15ea8f985c54 174 bcn_timer.reset();
jreiss 33:15ea8f985c54 175 }
jreiss 33:15ea8f985c54 176 }
Taylor Heck 42:20f6b29a9903 177 ThisThread::sleep_for(10s);
jreiss 33:15ea8f985c54 178 }
jreiss 33:15ea8f985c54 179
jreiss 33:15ea8f985c54 180 return 0;
jreiss 33:15ea8f985c54 181 }
jreiss 33:15ea8f985c54 182
jreiss 33:15ea8f985c54 183 #endif