zcbvsDG
src/ota_example.cpp@0:aa8df9825311, 2017-09-25 (annotated)
- Committer:
- deepak15
- Date:
- Mon Sep 25 09:41:00 2017 +0000
- Revision:
- 0:aa8df9825311
- Child:
- 1:bd6575a12f84
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 == 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 = "Test-LORA"; |
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 | //for dev1 |
deepak15 | 0:aa8df9825311 | 30 | static uint8_t network_id[] = { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x00, 0x70, 0xD0 };//{ 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x07, 0x86, 0xA6 };//appEUI |
deepak15 | 0:aa8df9825311 | 31 | static uint8_t network_key[] ={ 0x95, 0x43, 0xDC, 0xC2, 0x62, 0x66, 0x13, 0x16, 0x1B, 0x24, 0x03, 0x8D, 0x70, 0x83, 0xEB, 0x94 }; |
deepak15 | 0:aa8df9825311 | 32 | //for dev2 |
deepak15 | 0:aa8df9825311 | 33 | //9543DCC2626613161B24038D7083EB94 |
deepak15 | 0:aa8df9825311 | 34 | //70b3d57ed00070d0 |
deepak15 | 0:aa8df9825311 | 35 | //static uint8_t network_id[] = { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x00, 0x70, 0xD1 };//appEUI |
deepak15 | 0:aa8df9825311 | 36 | //static uint8_t network_key[] ={ 0xE8, 0xB2, 0x08, 0x26, 0x24, 0xFB, 0x5C, 0xDE, 0x78, 0x54, 0xE8, 0x72, 0x14, 0x1D, 0x58, 0x23 };//appkey |
deepak15 | 0:aa8df9825311 | 37 | static uint8_t frequency_sub_band = 0; |
deepak15 | 0:aa8df9825311 | 38 | static bool public_network = true; |
deepak15 | 0:aa8df9825311 | 39 | static uint8_t ack = 0; |
deepak15 | 0:aa8df9825311 | 40 | static bool adr = true; |
deepak15 | 0:aa8df9825311 | 41 | |
deepak15 | 0:aa8df9825311 | 42 | // deepsleep consumes slightly less current than sleep |
deepak15 | 0:aa8df9825311 | 43 | // in sleep mode, IO state is maintained, RAM is retained, and application will resume after waking up |
deepak15 | 0:aa8df9825311 | 44 | // in deepsleep mode, IOs float, RAM is lost, and application will start from beginning after waking up |
deepak15 | 0:aa8df9825311 | 45 | // if deep_sleep == true, device will enter deepsleep mode |
deepak15 | 0:aa8df9825311 | 46 | static bool deep_sleep = false; |
deepak15 | 0:aa8df9825311 | 47 | |
deepak15 | 0:aa8df9825311 | 48 | mDot* dot = NULL; |
deepak15 | 0:aa8df9825311 | 49 | lora::ChannelPlan* plan = NULL; |
deepak15 | 0:aa8df9825311 | 50 | |
deepak15 | 0:aa8df9825311 | 51 | //Serial pc(USBTX, USBRX); |
deepak15 | 0:aa8df9825311 | 52 | |
deepak15 | 0:aa8df9825311 | 53 | #if defined(TARGET_XDOT_L151CC) |
deepak15 | 0:aa8df9825311 | 54 | I2C i2c(I2C_SDA, I2C_SCL); |
deepak15 | 0:aa8df9825311 | 55 | ISL29011 lux(i2c); |
deepak15 | 0:aa8df9825311 | 56 | #else |
deepak15 | 0:aa8df9825311 | 57 | AnalogIn lux(XBEE_AD0); |
deepak15 | 0:aa8df9825311 | 58 | #endif |
deepak15 | 0:aa8df9825311 | 59 | |
deepak15 | 0:aa8df9825311 | 60 | AnalogIn input(A0); |
deepak15 | 0:aa8df9825311 | 61 | Serial pc(PA_9, PA_10); |
deepak15 | 0:aa8df9825311 | 62 | |
deepak15 | 0:aa8df9825311 | 63 | int main() { |
deepak15 | 0:aa8df9825311 | 64 | // Custom event handler for automatically displaying RX data |
deepak15 | 0:aa8df9825311 | 65 | RadioEvent events; |
deepak15 | 0:aa8df9825311 | 66 | Timer t; |
deepak15 | 0:aa8df9825311 | 67 | pc.baud(115200); |
deepak15 | 0:aa8df9825311 | 68 | |
deepak15 | 0:aa8df9825311 | 69 | mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); |
deepak15 | 0:aa8df9825311 | 70 | |
deepak15 | 0:aa8df9825311 | 71 | #if CHANNEL_PLAN == CP_US915 |
deepak15 | 0:aa8df9825311 | 72 | plan = new lora::ChannelPlan_US915(); |
deepak15 | 0:aa8df9825311 | 73 | #elif CHANNEL_PLAN == CP_AU915 |
deepak15 | 0:aa8df9825311 | 74 | plan = new lora::ChannelPlan_AU915(); |
deepak15 | 0:aa8df9825311 | 75 | #elif CHANNEL_PLAN == CP_EU868 |
deepak15 | 0:aa8df9825311 | 76 | plan = new lora::ChannelPlan_EU868(); |
deepak15 | 0:aa8df9825311 | 77 | #elif CHANNEL_PLAN == CP_KR920 |
deepak15 | 0:aa8df9825311 | 78 | plan = new lora::ChannelPlan_KR920(); |
deepak15 | 0:aa8df9825311 | 79 | #elif CHANNEL_PLAN == CP_AS923 |
deepak15 | 0:aa8df9825311 | 80 | plan = new lora::ChannelPlan_AS923(); |
deepak15 | 0:aa8df9825311 | 81 | #elif CHANNEL_PLAN == CP_AS923_JAPAN |
deepak15 | 0:aa8df9825311 | 82 | plan = new lora::ChannelPlan_AS923_Japan(); |
deepak15 | 0:aa8df9825311 | 83 | #elif CHANNEL_PLAN == CP_IN865 |
deepak15 | 0:aa8df9825311 | 84 | plan = new lora::ChannelPlan_IN865(); |
deepak15 | 0:aa8df9825311 | 85 | #endif |
deepak15 | 0:aa8df9825311 | 86 | assert(plan); |
deepak15 | 0:aa8df9825311 | 87 | |
deepak15 | 0:aa8df9825311 | 88 | dot = mDot::getInstance(plan); |
deepak15 | 0:aa8df9825311 | 89 | assert(dot); |
deepak15 | 0:aa8df9825311 | 90 | |
deepak15 | 0:aa8df9825311 | 91 | // attach the custom events handler |
deepak15 | 0:aa8df9825311 | 92 | dot->setEvents(&events); |
deepak15 | 0:aa8df9825311 | 93 | |
deepak15 | 0:aa8df9825311 | 94 | if (!dot->getStandbyFlag()) { |
deepak15 | 0:aa8df9825311 | 95 | logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION); |
deepak15 | 0:aa8df9825311 | 96 | |
deepak15 | 0:aa8df9825311 | 97 | // start from a well-known state |
deepak15 | 0:aa8df9825311 | 98 | logInfo("defaulting Dot configuration"); |
deepak15 | 0:aa8df9825311 | 99 | dot->resetConfig(); |
deepak15 | 0:aa8df9825311 | 100 | dot->resetNetworkSession(); |
deepak15 | 0:aa8df9825311 | 101 | |
deepak15 | 0:aa8df9825311 | 102 | // make sure library logging is turned on |
deepak15 | 0:aa8df9825311 | 103 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
deepak15 | 0:aa8df9825311 | 104 | |
deepak15 | 0:aa8df9825311 | 105 | // update configuration if necessary |
deepak15 | 0:aa8df9825311 | 106 | if (dot->getJoinMode() != mDot::OTA) { |
deepak15 | 0:aa8df9825311 | 107 | logInfo("changing network join mode to OTA"); |
deepak15 | 0:aa8df9825311 | 108 | if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) { |
deepak15 | 0:aa8df9825311 | 109 | logError("failed to set network join mode to OTA"); |
deepak15 | 0:aa8df9825311 | 110 | } |
deepak15 | 0:aa8df9825311 | 111 | } |
deepak15 | 0:aa8df9825311 | 112 | // 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 | 113 | // only one method or the other should be used! |
deepak15 | 0:aa8df9825311 | 114 | // network ID = crc64(network name) |
deepak15 | 0:aa8df9825311 | 115 | // network KEY = cmac(network passphrase) |
deepak15 | 0:aa8df9825311 | 116 | //update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, public_network, ack); |
deepak15 | 0:aa8df9825311 | 117 | update_ota_config_id_key(network_id, network_key, frequency_sub_band, public_network, ack); |
deepak15 | 0:aa8df9825311 | 118 | |
deepak15 | 0:aa8df9825311 | 119 | // configure network link checks |
deepak15 | 0:aa8df9825311 | 120 | // 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 | 121 | // check the link every count packets |
deepak15 | 0:aa8df9825311 | 122 | // declare the Dot disconnected after threshold failed link checks |
deepak15 | 0:aa8df9825311 | 123 | // 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 | 124 | update_network_link_check_config(3, 5); |
deepak15 | 0:aa8df9825311 | 125 | |
deepak15 | 0:aa8df9825311 | 126 | // enable or disable Adaptive Data Rate |
deepak15 | 0:aa8df9825311 | 127 | dot->setAdr(adr); |
deepak15 | 0:aa8df9825311 | 128 | |
deepak15 | 0:aa8df9825311 | 129 | // save changes to configuration |
deepak15 | 0:aa8df9825311 | 130 | logInfo("saving configuration"); |
deepak15 | 0:aa8df9825311 | 131 | if (!dot->saveConfig()) { |
deepak15 | 0:aa8df9825311 | 132 | logError("failed to save configuration"); |
deepak15 | 0:aa8df9825311 | 133 | } |
deepak15 | 0:aa8df9825311 | 134 | |
deepak15 | 0:aa8df9825311 | 135 | // display configuration |
deepak15 | 0:aa8df9825311 | 136 | display_config(); |
deepak15 | 0:aa8df9825311 | 137 | } else { |
deepak15 | 0:aa8df9825311 | 138 | // restore the saved session if the dot woke from deepsleep mode |
deepak15 | 0:aa8df9825311 | 139 | // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep |
deepak15 | 0:aa8df9825311 | 140 | logInfo("restoring network session from NVM"); |
deepak15 | 0:aa8df9825311 | 141 | dot->restoreNetworkSession(); |
deepak15 | 0:aa8df9825311 | 142 | } |
deepak15 | 0:aa8df9825311 | 143 | |
deepak15 | 0:aa8df9825311 | 144 | while (true) { |
deepak15 | 0:aa8df9825311 | 145 | uint16_t light; |
deepak15 | 0:aa8df9825311 | 146 | std::vector<uint8_t> tx_data; |
deepak15 | 0:aa8df9825311 | 147 | |
deepak15 | 0:aa8df9825311 | 148 | // join network if not joined |
deepak15 | 0:aa8df9825311 | 149 | if (!dot->getNetworkJoinStatus()) { |
deepak15 | 0:aa8df9825311 | 150 | join_network(); |
deepak15 | 0:aa8df9825311 | 151 | } |
deepak15 | 0:aa8df9825311 | 152 | |
deepak15 | 0:aa8df9825311 | 153 | #if defined(TARGET_XDOT_L151CC) |
deepak15 | 0:aa8df9825311 | 154 | // configure the ISL29011 sensor on the xDot-DK for continuous ambient light sampling, 16 bit conversion, and maximum range |
deepak15 | 0:aa8df9825311 | 155 | lux.setMode(ISL29011::ALS_CONT); |
deepak15 | 0:aa8df9825311 | 156 | lux.setResolution(ISL29011::ADC_16BIT); |
deepak15 | 0:aa8df9825311 | 157 | lux.setRange(ISL29011::RNG_64000); |
deepak15 | 0:aa8df9825311 | 158 | |
deepak15 | 0:aa8df9825311 | 159 | // get the latest light sample and send it to the gateway |
deepak15 | 0:aa8df9825311 | 160 | light = lux.getData(); |
deepak15 | 0:aa8df9825311 | 161 | light = 0xABCD; |
deepak15 | 0:aa8df9825311 | 162 | tx_data.push_back((light >> 8) & 0xFF); |
deepak15 | 0:aa8df9825311 | 163 | tx_data.push_back(light & 0xFF); |
deepak15 | 0:aa8df9825311 | 164 | logInfo("light...: %lu [0x%04X]", light, light); |
deepak15 | 0:aa8df9825311 | 165 | send_data(tx_data); |
deepak15 | 0:aa8df9825311 | 166 | |
deepak15 | 0:aa8df9825311 | 167 | // put the LSL29011 ambient light sensor into a low power state |
deepak15 | 0:aa8df9825311 | 168 | lux.setMode(ISL29011::PWR_DOWN); |
deepak15 | 0:aa8df9825311 | 169 | #else |
deepak15 | 0:aa8df9825311 | 170 | // get some dummy data and send it to the gateway |
deepak15 | 0:aa8df9825311 | 171 | //light = lux.read_u16(); |
deepak15 | 0:aa8df9825311 | 172 | uint16_t samples; |
deepak15 | 0:aa8df9825311 | 173 | pc.baud(115200); |
deepak15 | 0:aa8df9825311 | 174 | pc.printf("Results:\n"); |
deepak15 | 0:aa8df9825311 | 175 | // for(int i=0; i<1024; i++) { |
deepak15 | 0:aa8df9825311 | 176 | samples = input.read_u16(); |
deepak15 | 0:aa8df9825311 | 177 | // pc.printf(" %d\n", samples); |
deepak15 | 0:aa8df9825311 | 178 | light = samples; |
deepak15 | 0:aa8df9825311 | 179 | tx_data.push_back((light >> 8) & 0xFF); |
deepak15 | 0:aa8df9825311 | 180 | tx_data.push_back(light & 0xFF); |
deepak15 | 0:aa8df9825311 | 181 | // logInfo("light: %lu [0x%04X]", light, light); |
deepak15 | 0:aa8df9825311 | 182 | if(samples<50000){ |
deepak15 | 0:aa8df9825311 | 183 | pc.printf(" send when value is less than 50000=="); |
deepak15 | 0:aa8df9825311 | 184 | send_data(tx_data); |
deepak15 | 0:aa8df9825311 | 185 | pc.printf("====================================================="); |
deepak15 | 0:aa8df9825311 | 186 | pc.printf("%d",light); |
deepak15 | 0:aa8df9825311 | 187 | } |
deepak15 | 0:aa8df9825311 | 188 | time_t seconds_1=0; |
deepak15 | 0:aa8df9825311 | 189 | time_t seconds = time(NULL); |
deepak15 | 0:aa8df9825311 | 190 | |
deepak15 | 0:aa8df9825311 | 191 | if (((seconds - seconds_1)>(12))){ |
deepak15 | 0:aa8df9825311 | 192 | |
deepak15 | 0:aa8df9825311 | 193 | seconds_1 = seconds; |
deepak15 | 0:aa8df9825311 | 194 | pc.printf("Seconds : %d\n", seconds - seconds_1); |
deepak15 | 0:aa8df9825311 | 195 | // pc.printf("Seconds : %d\n", seconds); |
deepak15 | 0:aa8df9825311 | 196 | } |
deepak15 | 0:aa8df9825311 | 197 | //uint32_t time_1; |
deepak15 | 0:aa8df9825311 | 198 | // time_1= t.read(); |
deepak15 | 0:aa8df9825311 | 199 | // if(time_1>5){ |
deepak15 | 0:aa8df9825311 | 200 | // pc.printf(" send after 10 sec ====================================================="); |
deepak15 | 0:aa8df9825311 | 201 | // send_data(tx_data); |
deepak15 | 0:aa8df9825311 | 202 | // } |
deepak15 | 0:aa8df9825311 | 203 | |
deepak15 | 0:aa8df9825311 | 204 | wait(1); |
deepak15 | 0:aa8df9825311 | 205 | #endif |
deepak15 | 0:aa8df9825311 | 206 | |
deepak15 | 0:aa8df9825311 | 207 | // if going into deepsleep mode, save the session so we don't need to join again after waking up |
deepak15 | 0:aa8df9825311 | 208 | // not necessary if going into sleep mode since RAM is retained |
deepak15 | 0:aa8df9825311 | 209 | |
deepak15 | 0:aa8df9825311 | 210 | /*if (deep_sleep) { |
deepak15 | 0:aa8df9825311 | 211 | logInfo("saving network session to NVM"); |
deepak15 | 0:aa8df9825311 | 212 | dot->saveNetworkSession(); |
deepak15 | 0:aa8df9825311 | 213 | } |
deepak15 | 0:aa8df9825311 | 214 | |
deepak15 | 0:aa8df9825311 | 215 | // ONLY ONE of the three functions below should be uncommented depending on the desired wakeup method |
deepak15 | 0:aa8df9825311 | 216 | sleep_wake_rtc_only(deep_sleep); |
deepak15 | 0:aa8df9825311 | 217 | //sleep_wake_interrupt_only(deep_sleep); |
deepak15 | 0:aa8df9825311 | 218 | //sleep_wake_rtc_or_interrupt(deep_sleep); |
deepak15 | 0:aa8df9825311 | 219 | */ |
deepak15 | 0:aa8df9825311 | 220 | } |
deepak15 | 0:aa8df9825311 | 221 | |
deepak15 | 0:aa8df9825311 | 222 | return 0; |
deepak15 | 0:aa8df9825311 | 223 | } |
deepak15 | 0:aa8df9825311 | 224 | |
deepak15 | 0:aa8df9825311 | 225 | #endif |