Shoalhaven Water / Mbed OS Cloudtracker

Dependencies:   libmDot-mbed5 ISL29011

Files at this revision

API Documentation at this revision

Comitter:
Jason Reiss
Date:
Thu May 02 09:29:05 2019 -0500
Parent:
33:15ea8f985c54
Child:
35:cb358e793d1a
Child:
36:f1053cb17d4f
Commit message:
Add FOTA exampe

Changed in this revision

examples/example_config.h Show annotated file Show diff for this revision Revisions of this file
examples/inc/RadioEvent.h Show annotated file Show diff for this revision Revisions of this file
examples/src/fota_example.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/examples/example_config.h	Wed Apr 24 12:50:08 2019 +0000
+++ b/examples/example_config.h	Thu May 02 09:29:05 2019 -0500
@@ -7,10 +7,11 @@
 #define PEER_TO_PEER_EXAMPLE     4  // see peer_to_peer_example.cpp
 #define CLASS_C_EXAMPLE          5  // see class_c_example.cpp
 #define CLASS_B_EXAMPLE          6  // see class_b_example.cpp
+#define FOTA_EXAMPLE             7  // see fota_example.cpp
 
 // the active example is the one that will be compiled
 #if !defined(ACTIVE_EXAMPLE)
-#define ACTIVE_EXAMPLE  OTA_EXAMPLE
+#define ACTIVE_EXAMPLE  FOTA_EXAMPLE
 #endif
 
 // the active channel plan is the one that will be compiled
--- a/examples/inc/RadioEvent.h	Wed Apr 24 12:50:08 2019 +0000
+++ b/examples/inc/RadioEvent.h	Thu May 02 09:29:05 2019 -0500
@@ -3,15 +3,26 @@
 
 #include "dot_util.h"
 #include "mDotEvent.h"
+#include "Fota.h"
 
 class RadioEvent : public mDotEvent
 {
- 
+
 public:
     RadioEvent() {}
- 
+
     virtual ~RadioEvent() {}
- 
+
+    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, bool dupRx) {
+        mDotEvent::PacketRx(port, payload, size, rssi, snr, ctrl, slot, retries, address, dupRx);
+
+#if ACTIVE_EXAMPLE == FOTA_EXAMPLE
+        if(port == 200 || port == 201 || port == 202) {
+            Fota::getInstance()->processCmd(payload, port, size);
+        }
+#endif
+    }
+
     /*!
      * MAC layer event callback prototype.
      *
@@ -19,7 +30,7 @@
      * \param [IN] info  Details about MAC events occurred
      */
     virtual void MacEvent(LoRaMacEventFlags* flags, LoRaMacEventInfo* info) {
- 
+
         if (mts::MTSLog::getLogLevel() == mts::MTSLog::TRACE_LEVEL) {
             std::string msg = "OK";
             switch (info->Status) {
@@ -51,19 +62,19 @@
                     break;
             }
             logTrace("Event: %s", msg.c_str());
- 
+
             logTrace("Flags Tx: %d Rx: %d RxData: %d RxSlot: %d LinkCheck: %d JoinAccept: %d",
                      flags->Bits.Tx, flags->Bits.Rx, flags->Bits.RxData, flags->Bits.RxSlot, flags->Bits.LinkCheck, flags->Bits.JoinAccept);
             logTrace("Info: Status: %d ACK: %d Retries: %d TxDR: %d RxPort: %d RxSize: %d RSSI: %d SNR: %d Energy: %d Margin: %d Gateways: %d",
                      info->Status, info->TxAckReceived, info->TxNbRetries, info->TxDatarate, info->RxPort, info->RxBufferSize,
                      info->RxRssi, info->RxSnr, info->Energy, info->DemodMargin, info->NbGateways);
         }
- 
+
         if (flags->Bits.Rx) {
-            
+
             logDebug("Rx %d bytes", info->RxBufferSize);
             if (info->RxBufferSize > 0) {
-                // print RX data as string and hexadecimal 
+                // print RX data as string and hexadecimal
                 std::string rx((const char*)info->RxBuffer, info->RxBufferSize);
                 printf("Rx data: %s [%s]\r\n", rx.c_str(), mts::Text::bin2hexString(info->RxBuffer, info->RxBufferSize).c_str());
             }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/src/fota_example.cpp	Thu May 02 09:29:05 2019 -0500
@@ -0,0 +1,198 @@
+#include "dot_util.h"
+#include "RadioEvent.h"
+
+#if ACTIVE_EXAMPLE == FOTA_EXAMPLE
+
+/////////////////////////////////////////////////////////////////////////////
+// -------------------- DOT LIBRARY REQUIRED ------------------------------//
+// * 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.                    //
+// * https://developer.mbed.org/teams/MultiTech/code/libmDot-dev-mbed5/    //
+// * https://developer.mbed.org/teams/MultiTech/code/libmDot-mbed5/        //
+// * https://developer.mbed.org/teams/MultiTech/code/libxDot-dev-mbed5/    //
+// * https://developer.mbed.org/teams/MultiTech/code/libxDot-mbed5/        //
+/////////////////////////////////////////////////////////////////////////////
+
+
+////////////////////////////////////////////////////////////////////////////
+// -------------------- DEFINITIONS REQUIRED ---------------------------- //
+// mDot - add define for FOTA in mbed_app.json or on command line         //
+//   Command line                                                         //
+//     mbed compile -t GCC_ARM -m MTS_MDOT_F411RE -DFOTA=1                //
+//   mbed_app.json                                                        //
+//     {                                                                  //
+//        "macros": [                                                     //
+//          "FOTA=1"                                                      //
+//        ]                                                               //
+//     }                                                                  //
+//                                                                        //
+// xDot - DO NOT define FOTA, there is no file system support available   //
+//        Only multicast is supported for xDot, external MCU and Flash    //
+//        are required to support Fragmentation                           //
+////////////////////////////////////////////////////////////////////////////
+
+
+/////////////////////////////////////////////////////////////
+// * these options must match the settings on your gateway //
+// * edit their values to match your configuration         //
+// * frequency sub band is only relevant for the 915 bands //
+// * either the network name and passphrase can be used or //
+//     the network ID (8 bytes) and KEY (16 bytes)         //
+/////////////////////////////////////////////////////////////
+
+static std::string network_name = "MultiTech";
+static std::string network_passphrase = "MultiTech";
+static uint8_t network_id[] = { 0x6C, 0x4E, 0xEF, 0x66, 0xF4, 0x79, 0x86, 0xA6 };
+static uint8_t network_key[] = { 0x1F, 0x33, 0xA1, 0x70, 0xA5, 0xF1, 0xFD, 0xA0, 0xAB, 0x69, 0x7A, 0xAE, 0x2B, 0x95, 0x91, 0x6B };
+static uint8_t frequency_sub_band = 0;
+static lora::NetworkType network_type = lora::PUBLIC_LORAWAN;
+static uint8_t join_delay = 5;
+static uint8_t ack = 1;
+static bool adr = true;
+
+mDot* dot = NULL;
+lora::ChannelPlan* plan = NULL;
+
+Serial pc(USBTX, USBRX);
+
+#if defined(TARGET_XDOT_L151CC)
+I2C i2c(I2C_SDA, I2C_SCL);
+ISL29011 lux(i2c);
+#else
+AnalogIn lux(XBEE_AD0);
+#endif
+
+int main() {
+    // Custom event handler for automatically displaying RX data
+    RadioEvent events;
+
+    pc.baud(115200);
+
+#if defined(TARGET_XDOT_L151CC)
+    i2c.frequency(400000);
+#endif
+
+    mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
+
+#if CHANNEL_PLAN == CP_US915
+    plan = new lora::ChannelPlan_US915();
+#elif CHANNEL_PLAN == CP_AU915
+    plan = new lora::ChannelPlan_AU915();
+#elif CHANNEL_PLAN == CP_EU868
+    plan = new lora::ChannelPlan_EU868();
+#elif CHANNEL_PLAN == CP_KR920
+    plan = new lora::ChannelPlan_KR920();
+#elif CHANNEL_PLAN == CP_AS923
+    plan = new lora::ChannelPlan_AS923();
+#elif CHANNEL_PLAN == CP_AS923_JAPAN
+    plan = new lora::ChannelPlan_AS923_Japan();
+#elif CHANNEL_PLAN == CP_IN865
+    plan = new lora::ChannelPlan_IN865();
+#endif
+    assert(plan);
+
+    dot = mDot::getInstance(plan);
+    assert(dot);
+
+    logInfo("mbed-os library version: %d.%d.%d", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
+
+    // Initialize FOTA singleton
+    Fota::getInstance(dot);
+
+
+    // start from a well-known state
+    logInfo("defaulting Dot configuration");
+    dot->resetConfig();
+    dot->resetNetworkSession();
+
+    // make sure library logging is turned on
+    dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
+
+    // attach the custom events handler
+    dot->setEvents(&events);
+
+    // update configuration if necessary
+    if (dot->getJoinMode() != mDot::OTA) {
+        logInfo("changing network join mode to OTA");
+        if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) {
+            logError("failed to set network join mode to OTA");
+        }
+    }
+    // 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
+    // only one method or the other should be used!
+    // network ID = crc64(network name)
+    // network KEY = cmac(network passphrase)
+    update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, network_type, ack);
+    //update_ota_config_id_key(network_id, network_key, frequency_sub_band, network_type, ack);
+
+    // configure the Dot for class C operation
+    // the Dot must also be configured on the gateway for class C
+    // use the lora-query application to do this on a Conduit: http://www.multitech.net/developer/software/lora/lora-network-server/
+    // to provision your Dot for class C operation with a 3rd party gateway, see the gateway or network provider documentation
+    logInfo("changing network mode to class C");
+    if (dot->setClass("C") != mDot::MDOT_OK) {
+        logError("failed to set network mode to class C");
+    }
+
+    // enable or disable Adaptive Data Rate
+    dot->setAdr(adr);
+
+    // Configure the join delay
+    dot->setJoinDelay(join_delay);
+
+    // save changes to configuration
+    logInfo("saving configuration");
+    if (!dot->saveConfig()) {
+        logError("failed to save configuration");
+    }
+
+    // display configuration
+    display_config();
+
+    while (true) {
+        uint16_t light;
+        std::vector<uint8_t> tx_data;
+
+        // join network if not joined
+        if (!dot->getNetworkJoinStatus()) {
+            join_network();
+        }
+
+#if defined(TARGET_XDOT_L151CC)
+        // configure the ISL29011 sensor on the xDot-DK for continuous ambient light sampling, 16 bit conversion, and maximum range
+        lux.setMode(ISL29011::ALS_CONT);
+        lux.setResolution(ISL29011::ADC_16BIT);
+        lux.setRange(ISL29011::RNG_64000);
+
+        // get the latest light sample and send it to the gateway
+        light = lux.getData();
+        tx_data.push_back((light >> 8) & 0xFF);
+        tx_data.push_back(light & 0xFF);
+        logInfo("light: %lu [0x%04X]", light, light);
+        send_data(tx_data);
+
+        // put the LSL29011 ambient light sensor into a low power state
+        lux.setMode(ISL29011::PWR_DOWN);
+#else
+        // get some dummy data and send it to the gateway
+        light = lux.read_u16();
+        tx_data.push_back((light >> 8) & 0xFF);
+        tx_data.push_back(light & 0xFF);
+        logInfo("light: %lu [0x%04X]", light, light);
+        send_data(tx_data);
+#endif
+
+        // the Dot can't sleep in class C mode
+        // it must be waiting for data from the gateway
+        // send data every 30s
+        logInfo("waiting for 30s");
+        wait(30);
+    }
+
+    return 0;
+}
+
+#endif
+