zcbvsDG
src/auto_ota_example.cpp@0:aa8df9825311, 2017-09-25 (annotated)
- Committer:
- deepak15
- Date:
- Mon Sep 25 09:41:00 2017 +0000
- Revision:
- 0:aa8df9825311
test _1;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
deepak15 | 0:aa8df9825311 | 1 | #include "dot_util.h" |
deepak15 | 0:aa8df9825311 | 2 | #include "RadioEvent.h" |
deepak15 | 0:aa8df9825311 | 3 | |
deepak15 | 0:aa8df9825311 | 4 | #if ACTIVE_EXAMPLE == AUTO_OTA_EXAMPLE |
deepak15 | 0:aa8df9825311 | 5 | |
deepak15 | 0:aa8df9825311 | 6 | ///////////////////////////////////////////////////////////////////////////// |
deepak15 | 0:aa8df9825311 | 7 | // -------------------- DOT LIBRARY REQUIRED ------------------------------// |
deepak15 | 0:aa8df9825311 | 8 | // * Because these example programs can be used for both mDot and xDot // |
deepak15 | 0:aa8df9825311 | 9 | // devices, the LoRa stack is not included. The libmDot library should // |
deepak15 | 0:aa8df9825311 | 10 | // be imported if building for mDot devices. The libxDot library // |
deepak15 | 0:aa8df9825311 | 11 | // should be imported if building for xDot devices. // |
deepak15 | 0:aa8df9825311 | 12 | // * https://developer.mbed.org/teams/MultiTech/code/libmDot-dev-mbed5/ // |
deepak15 | 0:aa8df9825311 | 13 | // * https://developer.mbed.org/teams/MultiTech/code/libmDot-mbed5/ // |
deepak15 | 0:aa8df9825311 | 14 | // * https://developer.mbed.org/teams/MultiTech/code/libxDot-dev-mbed5/ // |
deepak15 | 0:aa8df9825311 | 15 | // * https://developer.mbed.org/teams/MultiTech/code/libxDot-mbed5/ // |
deepak15 | 0:aa8df9825311 | 16 | ///////////////////////////////////////////////////////////////////////////// |
deepak15 | 0:aa8df9825311 | 17 | |
deepak15 | 0:aa8df9825311 | 18 | ///////////////////////////////////////////////////////////// |
deepak15 | 0:aa8df9825311 | 19 | // * these options must match the settings on your gateway // |
deepak15 | 0:aa8df9825311 | 20 | // * edit their values to match your configuration // |
deepak15 | 0:aa8df9825311 | 21 | // * frequency sub band is only relevant for the 915 bands // |
deepak15 | 0:aa8df9825311 | 22 | // * either the network name and passphrase can be used or // |
deepak15 | 0:aa8df9825311 | 23 | // the network ID (8 bytes) and KEY (16 bytes) // |
deepak15 | 0:aa8df9825311 | 24 | ///////////////////////////////////////////////////////////// |
deepak15 | 0:aa8df9825311 | 25 | static std::string network_name = "MultiTech"; |
deepak15 | 0:aa8df9825311 | 26 | static std::string network_passphrase = "MultiTech"; |
deepak15 | 0:aa8df9825311 | 27 | static uint8_t network_id[] = { 0x6C, 0x4E, 0xEF, 0x66, 0xF4, 0x79, 0x86, 0xA6 }; |
deepak15 | 0:aa8df9825311 | 28 | static uint8_t network_key[] = { 0x1F, 0x33, 0xA1, 0x70, 0xA5, 0xF1, 0xFD, 0xA0, 0xAB, 0x69, 0x7A, 0xAE, 0x2B, 0x95, 0x91, 0x6B }; |
deepak15 | 0:aa8df9825311 | 29 | static uint8_t frequency_sub_band = 0; |
deepak15 | 0:aa8df9825311 | 30 | static bool public_network = false; |
deepak15 | 0:aa8df9825311 | 31 | static uint8_t ack = 0; |
deepak15 | 0:aa8df9825311 | 32 | static bool adr = true; |
deepak15 | 0:aa8df9825311 | 33 | |
deepak15 | 0:aa8df9825311 | 34 | // deepsleep consumes slightly less current than sleep |
deepak15 | 0:aa8df9825311 | 35 | // in sleep mode, IO state is maintained, RAM is retained, and application will resume after waking up |
deepak15 | 0:aa8df9825311 | 36 | // in deepsleep mode, IOs float, RAM is lost, and application will start from beginning after waking up |
deepak15 | 0:aa8df9825311 | 37 | // if deep_sleep == true, device will enter deepsleep mode |
deepak15 | 0:aa8df9825311 | 38 | static bool deep_sleep = true; |
deepak15 | 0:aa8df9825311 | 39 | |
deepak15 | 0:aa8df9825311 | 40 | mDot* dot = NULL; |
deepak15 | 0:aa8df9825311 | 41 | lora::ChannelPlan* plan = NULL; |
deepak15 | 0:aa8df9825311 | 42 | |
deepak15 | 0:aa8df9825311 | 43 | Serial pc(USBTX, USBRX); |
deepak15 | 0:aa8df9825311 | 44 | |
deepak15 | 0:aa8df9825311 | 45 | #if defined(TARGET_XDOT_L151CC) |
deepak15 | 0:aa8df9825311 | 46 | I2C i2c(I2C_SDA, I2C_SCL); |
deepak15 | 0:aa8df9825311 | 47 | ISL29011 lux(i2c); |
deepak15 | 0:aa8df9825311 | 48 | #else |
deepak15 | 0:aa8df9825311 | 49 | AnalogIn lux(XBEE_AD0); |
deepak15 | 0:aa8df9825311 | 50 | #endif |
deepak15 | 0:aa8df9825311 | 51 | |
deepak15 | 0:aa8df9825311 | 52 | int main() { |
deepak15 | 0:aa8df9825311 | 53 | // Custom event handler for automatically displaying RX data |
deepak15 | 0:aa8df9825311 | 54 | RadioEvent events; |
deepak15 | 0:aa8df9825311 | 55 | |
deepak15 | 0:aa8df9825311 | 56 | pc.baud(115200); |
deepak15 | 0:aa8df9825311 | 57 | |
deepak15 | 0:aa8df9825311 | 58 | mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); |
deepak15 | 0:aa8df9825311 | 59 | |
deepak15 | 0:aa8df9825311 | 60 | #if CHANNEL_PLAN == CP_US915 |
deepak15 | 0:aa8df9825311 | 61 | plan = new lora::ChannelPlan_US915(); |
deepak15 | 0:aa8df9825311 | 62 | #elif CHANNEL_PLAN == CP_AU915 |
deepak15 | 0:aa8df9825311 | 63 | plan = new lora::ChannelPlan_AU915(); |
deepak15 | 0:aa8df9825311 | 64 | #elif CHANNEL_PLAN == CP_EU868 |
deepak15 | 0:aa8df9825311 | 65 | plan = new lora::ChannelPlan_EU868(); |
deepak15 | 0:aa8df9825311 | 66 | #elif CHANNEL_PLAN == CP_KR920 |
deepak15 | 0:aa8df9825311 | 67 | plan = new lora::ChannelPlan_KR920(); |
deepak15 | 0:aa8df9825311 | 68 | #elif CHANNEL_PLAN == CP_AS923 |
deepak15 | 0:aa8df9825311 | 69 | plan = new lora::ChannelPlan_AS923(); |
deepak15 | 0:aa8df9825311 | 70 | #elif CHANNEL_PLAN == CP_AS923_JAPAN |
deepak15 | 0:aa8df9825311 | 71 | plan = new lora::ChannelPlan_AS923_Japan(); |
deepak15 | 0:aa8df9825311 | 72 | #elif CHANNEL_PLAN == CP_IN865 |
deepak15 | 0:aa8df9825311 | 73 | plan = new lora::ChannelPlan_IN865(); |
deepak15 | 0:aa8df9825311 | 74 | #endif |
deepak15 | 0:aa8df9825311 | 75 | assert(plan); |
deepak15 | 0:aa8df9825311 | 76 | |
deepak15 | 0:aa8df9825311 | 77 | dot = mDot::getInstance(plan); |
deepak15 | 0:aa8df9825311 | 78 | assert(dot); |
deepak15 | 0:aa8df9825311 | 79 | |
deepak15 | 0:aa8df9825311 | 80 | // attach the custom events handler |
deepak15 | 0:aa8df9825311 | 81 | dot->setEvents(&events); |
deepak15 | 0:aa8df9825311 | 82 | |
deepak15 | 0:aa8df9825311 | 83 | if (!dot->getStandbyFlag()) { |
deepak15 | 0:aa8df9825311 | 84 | logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION); |
deepak15 | 0:aa8df9825311 | 85 | |
deepak15 | 0:aa8df9825311 | 86 | // start from a well-known state |
deepak15 | 0:aa8df9825311 | 87 | logInfo("defaulting Dot configuration"); |
deepak15 | 0:aa8df9825311 | 88 | dot->resetConfig(); |
deepak15 | 0:aa8df9825311 | 89 | dot->resetNetworkSession(); |
deepak15 | 0:aa8df9825311 | 90 | |
deepak15 | 0:aa8df9825311 | 91 | // make sure library logging is turned on |
deepak15 | 0:aa8df9825311 | 92 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
deepak15 | 0:aa8df9825311 | 93 | |
deepak15 | 0:aa8df9825311 | 94 | // update configuration if necessary |
deepak15 | 0:aa8df9825311 | 95 | // in AUTO_OTA mode the session is automatically saved, so saveNetworkSession and restoreNetworkSession are not needed |
deepak15 | 0:aa8df9825311 | 96 | if (dot->getJoinMode() != mDot::AUTO_OTA) { |
deepak15 | 0:aa8df9825311 | 97 | logInfo("changing network join mode to AUTO_OTA"); |
deepak15 | 0:aa8df9825311 | 98 | if (dot->setJoinMode(mDot::AUTO_OTA) != mDot::MDOT_OK) { |
deepak15 | 0:aa8df9825311 | 99 | logError("failed to set network join mode to AUTO_OTA"); |
deepak15 | 0:aa8df9825311 | 100 | } |
deepak15 | 0:aa8df9825311 | 101 | } |
deepak15 | 0:aa8df9825311 | 102 | // 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 |
deepak15 | 0:aa8df9825311 | 103 | // only one method or the other should be used! |
deepak15 | 0:aa8df9825311 | 104 | // network ID = crc64(network name) |
deepak15 | 0:aa8df9825311 | 105 | // network KEY = cmac(network passphrase) |
deepak15 | 0:aa8df9825311 | 106 | update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, public_network, ack); |
deepak15 | 0:aa8df9825311 | 107 | //update_ota_config_id_key(network_id, network_key, frequency_sub_band, public_network, ack); |
deepak15 | 0:aa8df9825311 | 108 | |
deepak15 | 0:aa8df9825311 | 109 | // configure network link checks |
deepak15 | 0:aa8df9825311 | 110 | // 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 |
deepak15 | 0:aa8df9825311 | 111 | // check the link every count packets |
deepak15 | 0:aa8df9825311 | 112 | // declare the Dot disconnected after threshold failed link checks |
deepak15 | 0:aa8df9825311 | 113 | // 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 |
deepak15 | 0:aa8df9825311 | 114 | update_network_link_check_config(3, 5); |
deepak15 | 0:aa8df9825311 | 115 | |
deepak15 | 0:aa8df9825311 | 116 | // enable or disable Adaptive Data Rate |
deepak15 | 0:aa8df9825311 | 117 | dot->setAdr(adr); |
deepak15 | 0:aa8df9825311 | 118 | |
deepak15 | 0:aa8df9825311 | 119 | // save changes to configuration |
deepak15 | 0:aa8df9825311 | 120 | logInfo("saving configuration"); |
deepak15 | 0:aa8df9825311 | 121 | if (!dot->saveConfig()) { |
deepak15 | 0:aa8df9825311 | 122 | logError("failed to save configuration"); |
deepak15 | 0:aa8df9825311 | 123 | } |
deepak15 | 0:aa8df9825311 | 124 | |
deepak15 | 0:aa8df9825311 | 125 | // display configuration |
deepak15 | 0:aa8df9825311 | 126 | display_config(); |
deepak15 | 0:aa8df9825311 | 127 | } |
deepak15 | 0:aa8df9825311 | 128 | |
deepak15 | 0:aa8df9825311 | 129 | while (true) { |
deepak15 | 0:aa8df9825311 | 130 | uint16_t light; |
deepak15 | 0:aa8df9825311 | 131 | std::vector<uint8_t> tx_data; |
deepak15 | 0:aa8df9825311 | 132 | |
deepak15 | 0:aa8df9825311 | 133 | // join network if not joined |
deepak15 | 0:aa8df9825311 | 134 | if (!dot->getNetworkJoinStatus()) { |
deepak15 | 0:aa8df9825311 | 135 | join_network(); |
deepak15 | 0:aa8df9825311 | 136 | } |
deepak15 | 0:aa8df9825311 | 137 | |
deepak15 | 0:aa8df9825311 | 138 | #if defined(TARGET_XDOT_L151CC) |
deepak15 | 0:aa8df9825311 | 139 | // configure the ISL29011 sensor on the xDot-DK for continuous ambient light sampling, 16 bit conversion, and maximum range |
deepak15 | 0:aa8df9825311 | 140 | lux.setMode(ISL29011::ALS_CONT); |
deepak15 | 0:aa8df9825311 | 141 | lux.setResolution(ISL29011::ADC_16BIT); |
deepak15 | 0:aa8df9825311 | 142 | lux.setRange(ISL29011::RNG_64000); |
deepak15 | 0:aa8df9825311 | 143 | |
deepak15 | 0:aa8df9825311 | 144 | // get the latest light sample and send it to the gateway |
deepak15 | 0:aa8df9825311 | 145 | light = lux.getData(); |
deepak15 | 0:aa8df9825311 | 146 | tx_data.push_back((light >> 8) & 0xFF); |
deepak15 | 0:aa8df9825311 | 147 | tx_data.push_back(light & 0xFF); |
deepak15 | 0:aa8df9825311 | 148 | logInfo("light: %lu [0x%04X]", light, light); |
deepak15 | 0:aa8df9825311 | 149 | send_data(tx_data); |
deepak15 | 0:aa8df9825311 | 150 | |
deepak15 | 0:aa8df9825311 | 151 | // put the LSL29011 ambient light sensor into a low power state |
deepak15 | 0:aa8df9825311 | 152 | lux.setMode(ISL29011::PWR_DOWN); |
deepak15 | 0:aa8df9825311 | 153 | #else |
deepak15 | 0:aa8df9825311 | 154 | // get some dummy data and send it to the gateway |
deepak15 | 0:aa8df9825311 | 155 | light = lux.read_u16(); |
deepak15 | 0:aa8df9825311 | 156 | tx_data.push_back((light >> 8) & 0xFF); |
deepak15 | 0:aa8df9825311 | 157 | tx_data.push_back(light & 0xFF); |
deepak15 | 0:aa8df9825311 | 158 | logInfo("light: %lu [0x%04X]", light, light); |
deepak15 | 0:aa8df9825311 | 159 | send_data(tx_data); |
deepak15 | 0:aa8df9825311 | 160 | #endif |
deepak15 | 0:aa8df9825311 | 161 | |
deepak15 | 0:aa8df9825311 | 162 | // ONLY ONE of the three functions below should be uncommented depending on the desired wakeup method |
deepak15 | 0:aa8df9825311 | 163 | //sleep_wake_rtc_only(deep_sleep); |
deepak15 | 0:aa8df9825311 | 164 | //sleep_wake_interrupt_only(deep_sleep); |
deepak15 | 0:aa8df9825311 | 165 | sleep_wake_rtc_or_interrupt(deep_sleep); |
deepak15 | 0:aa8df9825311 | 166 | } |
deepak15 | 0:aa8df9825311 | 167 | |
deepak15 | 0:aa8df9825311 | 168 | return 0; |
deepak15 | 0:aa8df9825311 | 169 | } |
deepak15 | 0:aa8df9825311 | 170 | |
deepak15 | 0:aa8df9825311 | 171 | #endif |