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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ota_example.cpp Source File

ota_example.cpp

00001 #include "dot_util.h"
00002 #include "RadioEvent.h"
00003 
00004 #if ACTIVE_EXAMPLE == OTA_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 // * these options must match the settings on your gateway //
00020 // * edit their values to match your configuration         //
00021 // * frequency sub band is only relevant for the 915 bands //
00022 // * either the network name and passphrase can be used or //
00023 //     the network ID (8 bytes) and KEY (16 bytes)         //
00024 /////////////////////////////////////////////////////////////
00025 static std::string network_name = "MultiTech";
00026 static std::string network_passphrase = "MultiTech";
00027 static uint8_t network_id[] = { 0x6C, 0x4E, 0xEF, 0x66, 0xF4, 0x79, 0x86, 0xA6 };
00028 static uint8_t network_key[] = { 0x1F, 0x33, 0xA1, 0x70, 0xA5, 0xF1, 0xFD, 0xA0, 0xAB, 0x69, 0x7A, 0xAE, 0x2B, 0x95, 0x91, 0x6B };
00029 static uint8_t frequency_sub_band = 0;
00030 static lora::NetworkType network_type = lora::PUBLIC_LORAWAN;
00031 static uint8_t join_delay = 5;
00032 static uint8_t ack = 0;
00033 static bool adr = true;
00034 
00035 // deepsleep consumes slightly less current than sleep
00036 // in sleep mode, IO state is maintained, RAM is retained, and application will resume after waking up
00037 // in deepsleep mode, IOs float, RAM is lost, and application will start from beginning after waking up
00038 // if deep_sleep == true, device will enter deepsleep mode
00039 static bool deep_sleep = false;
00040 
00041 mDot* dot = NULL;
00042 lora::ChannelPlan* plan = NULL;
00043 
00044 mbed::UnbufferedSerial pc(USBTX, USBRX);
00045 
00046 #if defined(TARGET_XDOT_L151CC)
00047 I2C i2c(I2C_SDA, I2C_SCL);
00048 ISL29011 lux(i2c);
00049 #else
00050 AnalogIn lux(XBEE_AD0);
00051 #endif
00052 
00053 int main() {
00054     // Custom event handler for automatically displaying RX data
00055     RadioEvent events;
00056 
00057     pc.baud(115200);
00058 
00059 #if defined(TARGET_XDOT_L151CC)
00060     i2c.frequency(400000);
00061 #endif
00062 
00063     mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
00064 
00065     // Create channel plan
00066     plan = create_channel_plan();
00067     assert(plan);
00068 
00069     dot = mDot::getInstance(plan);
00070     assert(dot);
00071 
00072     // attach the custom events handler
00073     dot->setEvents(&events);
00074 
00075     if (!dot->getStandbyFlag() && !dot->getPreserveSession()) {
00076         logInfo("mbed-os library version: %d.%d.%d", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
00077 
00078         // start from a well-known state
00079         logInfo("defaulting Dot configuration");
00080         dot->resetConfig();
00081         dot->resetNetworkSession();
00082 
00083         // make sure library logging is turned on
00084         dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
00085 
00086         // update configuration if necessary
00087         if (dot->getJoinMode() != mDot::OTA) {
00088             logInfo("changing network join mode to OTA");
00089             if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) {
00090                 logError("failed to set network join mode to OTA");
00091             }
00092         }
00093 
00094         // To preserve session over power-off or reset enable this flag
00095         // dot->setPreserveSession(true);
00096 
00097         // 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
00098         // only one method or the other should be used!
00099         // network ID = crc64(network name)
00100         // network KEY = cmac(network passphrase)
00101         update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, network_type, ack);
00102         //update_ota_config_id_key(network_id, network_key, frequency_sub_band, network_type, ack);
00103 
00104         // configure network link checks
00105         // network link checks are a good alternative to requiring the gateway to ACK every packet and should allow a single gateway to handle more Dots
00106         // check the link every count packets
00107         // declare the Dot disconnected after threshold failed link checks
00108         // for count = 3 and threshold = 5, the Dot will ask for a link check response every 5 packets and will consider the connection lost if it fails to receive 3 responses in a row
00109         update_network_link_check_config(3, 5);
00110 
00111         // enable or disable Adaptive Data Rate
00112         dot->setAdr(adr);
00113 
00114         // Configure the join delay
00115         dot->setJoinDelay(join_delay);
00116 
00117         // save changes to configuration
00118         logInfo("saving configuration");
00119         if (!dot->saveConfig()) {
00120             logError("failed to save configuration");
00121         }
00122 
00123         // display configuration
00124         display_config();
00125     } else {
00126         // restore the saved session if the dot woke from deepsleep mode
00127         // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep
00128         logInfo("restoring network session from NVM");
00129         dot->restoreNetworkSession();
00130     }
00131 
00132     while (true) {
00133         uint16_t light;
00134         std::vector<uint8_t> tx_data;
00135 
00136         // join network if not joined
00137         if (!dot->getNetworkJoinStatus()) {
00138             join_network();
00139         }
00140 
00141 #if defined(TARGET_XDOT_L151CC)
00142         // configure the ISL29011 sensor on the xDot-DK for continuous ambient light sampling, 16 bit conversion, and maximum range
00143         lux.setMode(ISL29011::ALS_CONT);
00144         lux.setResolution(ISL29011::ADC_16BIT);
00145         lux.setRange(ISL29011::RNG_64000);
00146 
00147         // get the latest light sample and send it to the gateway
00148         light = lux.getData();
00149         tx_data.push_back((light >> 8) & 0xFF);
00150         tx_data.push_back(light & 0xFF);
00151         logInfo("light: %lu [0x%04X]", light, light);
00152         send_data(tx_data);
00153 
00154         // put the LSL29011 ambient light sensor into a low power state
00155         lux.setMode(ISL29011::PWR_DOWN);
00156 #else
00157         // get some dummy data and send it to the gateway
00158         light = lux.read_u16();
00159         tx_data.push_back((light >> 8) & 0xFF);
00160         tx_data.push_back(light & 0xFF);
00161         logInfo("light: %lu [0x%04X]", light, light);
00162         send_data(tx_data);
00163 #endif
00164 
00165         // if going into deepsleep mode, save the session so we don't need to join again after waking up
00166         // not necessary if going into sleep mode since RAM is retained
00167         if (deep_sleep) {
00168             logInfo("saving network session to NVM");
00169             dot->saveNetworkSession();
00170         }
00171 
00172         // ONLY ONE of the three functions below should be uncommented depending on the desired wakeup method
00173         //sleep_wake_rtc_only(deep_sleep);
00174         //sleep_wake_interrupt_only(deep_sleep);
00175         sleep_wake_rtc_or_interrupt(deep_sleep);
00176     }
00177 
00178     return 0;
00179 }
00180 
00181 #endif