Frederick Huang / Mbed OS LD100-Examples-201907

Dependencies:   libmDot-mbed5 ISL29011

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers fota_example.cpp Source File

fota_example.cpp

00001 #include "dot_util.h"
00002 #include "RadioEvent.h"
00003 
00004 #if ACTIVE_EXAMPLE == FOTA_EXAMPLE
00005 
00006 /////////////////////////////////////////////////////////////////////////////
00007 // -------------------- DOT LIBRARY REQUIRED ------------------------------//
00008 // * Because these example programs can be used for both mDot and xDot     //
00009 //     devices, the LoRa stack is not included. The libmDot library should //
00010 //     be imported if building for mDot devices. The libxDot library       //
00011 //     should be imported if building for xDot devices.                    //
00012 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-dev-mbed5/    //
00013 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-mbed5/        //
00014 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-dev-mbed5/    //
00015 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-mbed5/        //
00016 /////////////////////////////////////////////////////////////////////////////
00017 
00018 
00019 ////////////////////////////////////////////////////////////////////////////
00020 // -------------------- DEFINITIONS REQUIRED ---------------------------- //
00021 // mDot - add define for FOTA in mbed_app.json or on command line         //
00022 //   Command line                                                         //
00023 //     mbed compile -t GCC_ARM -m MTS_MDOT_F411RE -DFOTA=1                //
00024 //   mbed_app.json                                                        //
00025 //     {                                                                  //
00026 //        "macros": [                                                     //
00027 //          "FOTA=1"                                                      //
00028 //        ]                                                               //
00029 //     }                                                                  //
00030 //                                                                        //
00031 // xDot - DO NOT define FOTA, there is no file system support available   //
00032 //        Only multicast is supported for xDot, external MCU and Flash    //
00033 //        are required to support Fragmentation                           //
00034 ////////////////////////////////////////////////////////////////////////////
00035 
00036 
00037 /////////////////////////////////////////////////////////////
00038 // * these options must match the settings on your gateway //
00039 // * edit their values to match your configuration         //
00040 // * frequency sub band is only relevant for the 915 bands //
00041 // * either the network name and passphrase can be used or //
00042 //     the network ID (8 bytes) and KEY (16 bytes)         //
00043 /////////////////////////////////////////////////////////////
00044 
00045 static std::string network_name = "MultiTech";
00046 static std::string network_passphrase = "MultiTech";
00047 static uint8_t network_id[] = { 0x6C, 0x4E, 0xEF, 0x66, 0xF4, 0x79, 0x86, 0xA6 };
00048 static uint8_t network_key[] = { 0x1F, 0x33, 0xA1, 0x70, 0xA5, 0xF1, 0xFD, 0xA0, 0xAB, 0x69, 0x7A, 0xAE, 0x2B, 0x95, 0x91, 0x6B };
00049 static uint8_t frequency_sub_band = 0;
00050 static lora::NetworkType network_type = lora::PUBLIC_LORAWAN;
00051 static uint8_t join_delay = 5;
00052 static uint8_t ack = 1;
00053 static bool adr = true;
00054 
00055 mDot* dot = NULL;
00056 lora::ChannelPlan* plan = NULL;
00057 
00058 Serial pc(USBTX, USBRX);
00059 
00060 #if defined(TARGET_XDOT_L151CC)
00061 I2C i2c(I2C_SDA, I2C_SCL);
00062 ISL29011 lux(i2c);
00063 #else
00064 AnalogIn lux(XBEE_AD0);
00065 #endif
00066 
00067 int main() {
00068     // Custom event handler for automatically displaying RX data
00069     RadioEvent events;
00070 
00071     pc.baud(115200);
00072 
00073 #if defined(TARGET_XDOT_L151CC)
00074     i2c.frequency(400000);
00075 #endif
00076 
00077     mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
00078 
00079 #if CHANNEL_PLAN == CP_US915
00080     plan = new lora::ChannelPlan_US915();
00081 #elif CHANNEL_PLAN == CP_AU915
00082     plan = new lora::ChannelPlan_AU915();
00083 #elif CHANNEL_PLAN == CP_EU868
00084     plan = new lora::ChannelPlan_EU868();
00085 #elif CHANNEL_PLAN == CP_KR920
00086     plan = new lora::ChannelPlan_KR920();
00087 #elif CHANNEL_PLAN == CP_AS923
00088     plan = new lora::ChannelPlan_AS923();
00089 #elif CHANNEL_PLAN == CP_AS923_JAPAN
00090     plan = new lora::ChannelPlan_AS923_Japan();
00091 #elif CHANNEL_PLAN == CP_IN865
00092     plan = new lora::ChannelPlan_IN865();
00093 #endif
00094     assert(plan);
00095 
00096     dot = mDot::getInstance(plan);
00097     assert(dot);
00098 
00099     logInfo("mbed-os library version: %d.%d.%d", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
00100 
00101     // Initialize FOTA singleton
00102     Fota::getInstance(dot);
00103 
00104 
00105     // start from a well-known state
00106     logInfo("defaulting Dot configuration");
00107     dot->resetConfig();
00108     dot->resetNetworkSession();
00109 
00110     // make sure library logging is turned on
00111     dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
00112 
00113     // attach the custom events handler
00114     dot->setEvents(&events);
00115 
00116     // update configuration if necessary
00117     if (dot->getJoinMode() != mDot::OTA) {
00118         logInfo("changing network join mode to OTA");
00119         if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) {
00120             logError("failed to set network join mode to OTA");
00121         }
00122     }
00123     // 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
00124     // only one method or the other should be used!
00125     // network ID = crc64(network name)
00126     // network KEY = cmac(network passphrase)
00127     update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, network_type, ack);
00128     //update_ota_config_id_key(network_id, network_key, frequency_sub_band, network_type, ack);
00129 
00130     // configure the Dot for class C operation
00131     // the Dot must also be configured on the gateway for class C
00132     // use the lora-query application to do this on a Conduit: http://www.multitech.net/developer/software/lora/lora-network-server/
00133     // to provision your Dot for class C operation with a 3rd party gateway, see the gateway or network provider documentation
00134     logInfo("changing network mode to class C");
00135     if (dot->setClass("C") != mDot::MDOT_OK) {
00136         logError("failed to set network mode to class C");
00137     }
00138 
00139     // enable or disable Adaptive Data Rate
00140     dot->setAdr(adr);
00141 
00142     // Configure the join delay
00143     dot->setJoinDelay(join_delay);
00144 
00145     // save changes to configuration
00146     logInfo("saving configuration");
00147     if (!dot->saveConfig()) {
00148         logError("failed to save configuration");
00149     }
00150 
00151     // display configuration
00152     display_config();
00153 
00154     while (true) {
00155         uint16_t light;
00156         std::vector<uint8_t> tx_data;
00157 
00158         // join network if not joined
00159         if (!dot->getNetworkJoinStatus()) {
00160             join_network();
00161         }
00162 
00163 #if defined(TARGET_XDOT_L151CC)
00164         // configure the ISL29011 sensor on the xDot-DK for continuous ambient light sampling, 16 bit conversion, and maximum range
00165         lux.setMode(ISL29011::ALS_CONT);
00166         lux.setResolution(ISL29011::ADC_16BIT);
00167         lux.setRange(ISL29011::RNG_64000);
00168 
00169         // get the latest light sample and send it to the gateway
00170         light = lux.getData();
00171         tx_data.push_back((light >> 8) & 0xFF);
00172         tx_data.push_back(light & 0xFF);
00173         logInfo("light: %lu [0x%04X]", light, light);
00174         send_data(tx_data);
00175 
00176         // put the LSL29011 ambient light sensor into a low power state
00177         lux.setMode(ISL29011::PWR_DOWN);
00178 #else
00179         // get some dummy data and send it to the gateway
00180         light = lux.read_u16();
00181         tx_data.push_back((light >> 8) & 0xFF);
00182         tx_data.push_back(light & 0xFF);
00183         logInfo("light: %lu [0x%04X]", light, light);
00184         send_data(tx_data);
00185 #endif
00186 
00187         // the Dot can't sleep in class C mode
00188         // it must be waiting for data from the gateway
00189         // send data every 30s
00190         if (Fota::getInstance()->timeToStart() != 0) {
00191             logInfo("waiting for 30s");
00192             wait(30);
00193         } else {
00194             // Reduce uplinks during FOTA, dot cannot receive while transmitting
00195             // Too many lost packets will cause FOTA to fail
00196             logInfo("FOTA starting in %d seconds", Fota::getInstance()->timeToStart());
00197             logInfo("waiting for 300s");
00198             wait(300);
00199         }
00200 
00201     }
00202 
00203     return 0;
00204 }
00205 
00206 #endif
00207