L-Tek / Mbed OS Dot-Examples

Dependencies:   libxDot-dev-mbed5-deprecated ISL29011

Fork of Dot-Examples by MultiTech

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 bool public_network = false;
00031 static uint8_t ack = 0;
00032 static bool adr = true;
00033 
00034 // deepsleep consumes slightly less current than sleep
00035 // in sleep mode, IO state is maintained, RAM is retained, and application will resume after waking up
00036 // in deepsleep mode, IOs float, RAM is lost, and application will start from beginning after waking up
00037 // if deep_sleep == true, device will enter deepsleep mode
00038 static bool deep_sleep = false;
00039 
00040 mDot* dot = NULL;
00041 lora::ChannelPlan* plan = NULL;
00042 
00043 Serial pc(USBTX, USBRX);
00044 
00045 #ifdef TARGET_FF1705_L151CC
00046 uint16_t lux[3]={1,2,3};
00047 #elif defined(TARGET_XDOT_L151CC)
00048 I2C i2c(I2C_SDA, I2C_SCL);
00049 ISL29011 lux(i2c);
00050 #else
00051 AnalogIn lux(XBEE_AD0);
00052 #endif
00053 
00054 int main() {
00055     // Custom event handler for automatically displaying RX data
00056     RadioEvent events;
00057 
00058     pc.baud(115200);
00059 
00060     mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
00061     
00062 #if CHANNEL_PLAN == CP_US915
00063     plan = new lora::ChannelPlan_US915();
00064 #elif CHANNEL_PLAN == CP_AU915
00065     plan = new lora::ChannelPlan_AU915();
00066 #elif CHANNEL_PLAN == CP_EU868
00067     plan = new lora::ChannelPlan_EU868();
00068 #elif CHANNEL_PLAN == CP_KR920
00069     plan = new lora::ChannelPlan_KR920();
00070 #elif CHANNEL_PLAN == CP_AS923
00071     plan = new lora::ChannelPlan_AS923();
00072 #elif CHANNEL_PLAN == CP_AS923_JAPAN
00073     plan = new lora::ChannelPlan_AS923_Japan();
00074 #elif CHANNEL_PLAN == CP_IN865
00075     plan = new lora::ChannelPlan_IN865();
00076 #endif
00077     assert(plan);
00078 
00079     dot = mDot::getInstance(plan);
00080     assert(dot);
00081 
00082     // attach the custom events handler
00083     dot->setEvents(&events);
00084 
00085     if (!dot->getStandbyFlag()) {
00086         logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
00087 
00088         // start from a well-known state
00089         logInfo("defaulting Dot configuration");
00090         dot->resetConfig();
00091         dot->resetNetworkSession();
00092 
00093         // make sure library logging is turned on
00094         dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
00095 
00096         // update configuration if necessary
00097         if (dot->getJoinMode() != mDot::OTA) {
00098             logInfo("changing network join mode to OTA");
00099             if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) {
00100                 logError("failed to set network join mode to OTA");
00101             }
00102         }
00103         // 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
00104         // only one method or the other should be used!
00105         // network ID = crc64(network name)
00106         // network KEY = cmac(network passphrase)
00107         update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, public_network, ack);
00108         //update_ota_config_id_key(network_id, network_key, frequency_sub_band, public_network, ack);
00109 
00110         // configure network link checks
00111         // 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
00112         // check the link every count packets
00113         // declare the Dot disconnected after threshold failed link checks
00114         // 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
00115         update_network_link_check_config(3, 5);
00116 
00117         // enable or disable Adaptive Data Rate
00118         dot->setAdr(adr);
00119     
00120         // save changes to configuration
00121         logInfo("saving configuration");
00122         if (!dot->saveConfig()) {
00123             logError("failed to save configuration");
00124         }
00125 
00126         // display configuration
00127         display_config();
00128     } else {
00129         // restore the saved session if the dot woke from deepsleep mode
00130         // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep
00131         logInfo("restoring network session from NVM");
00132         dot->restoreNetworkSession();
00133     }
00134 
00135     while (true) {
00136         uint16_t light;
00137         std::vector<uint8_t> tx_data;
00138 
00139         // join network if not joined
00140         if (!dot->getNetworkJoinStatus()) {
00141             join_network();
00142         }
00143 
00144 #ifdef TARGET_FF1705_L151CC
00145        // get some dummy data and send it to the gateway
00146        static int cnt = 0;
00147        light = lux[cnt++];
00148        if (cnt >= sizeof(lux)) cnt=0;
00149             
00150        tx_data.push_back((light >> 8) & 0xFF);
00151        tx_data.push_back(light & 0xFF);
00152        logInfo("light: %lu [0x%04X]", light, light);
00153        send_data(tx_data);
00154        
00155 #elif defined(TARGET_XDOT_L151CC)
00156         // configure the ISL29011 sensor on the xDot-DK for continuous ambient light sampling, 16 bit conversion, and maximum range
00157         lux.setMode(ISL29011::ALS_CONT);
00158         lux.setResolution(ISL29011::ADC_16BIT);
00159         lux.setRange(ISL29011::RNG_64000);
00160 
00161         // get the latest light sample and send it to the gateway
00162         light = lux.getData();
00163         tx_data.push_back((light >> 8) & 0xFF);
00164         tx_data.push_back(light & 0xFF);
00165         logInfo("light: %lu [0x%04X]", light, light);
00166         send_data(tx_data);
00167 
00168         // put the LSL29011 ambient light sensor into a low power state
00169         lux.setMode(ISL29011::PWR_DOWN);
00170 #else 
00171         // get some dummy data and send it to the gateway
00172         light = lux.read_u16();
00173         tx_data.push_back((light >> 8) & 0xFF);
00174         tx_data.push_back(light & 0xFF);
00175         logInfo("light: %lu [0x%04X]", light, light);
00176         send_data(tx_data);
00177 #endif
00178 
00179         // if going into deepsleep mode, save the session so we don't need to join again after waking up
00180         // not necessary if going into sleep mode since RAM is retained
00181         if (deep_sleep) {
00182             logInfo("saving network session to NVM");
00183             dot->saveNetworkSession();
00184         }
00185 
00186         // ONLY ONE of the three functions below should be uncommented depending on the desired wakeup method
00187         //sleep_wake_rtc_only(deep_sleep);
00188         //sleep_wake_interrupt_only(deep_sleep);
00189         sleep_wake_rtc_or_interrupt(deep_sleep);
00190     }
00191  
00192     return 0;
00193 }
00194 
00195 #endif