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