mdot-examples triage
Dependencies: libmDot-dev-mbed5-deprecated ISL29011
examples/src/manual_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 | #include "Lora.h" |
Maciej Pijanowski |
0:f3d9c16b08d7 | 4 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 5 | #if ACTIVE_EXAMPLE == MANUAL_EXAMPLE |
Maciej Pijanowski |
0:f3d9c16b08d7 | 6 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 7 | ///////////////////////////////////////////////////////////////////////////// |
Maciej Pijanowski |
0:f3d9c16b08d7 | 8 | // -------------------- DOT LIBRARY REQUIRED ------------------------------// |
Maciej Pijanowski |
0:f3d9c16b08d7 | 9 | // * Because these example programs can be used for both mDot and xDot // // 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 | ///////////////////////////////////////////////////////////// |
Maciej Pijanowski |
0:f3d9c16b08d7 | 23 | static uint8_t network_address[] = { 0x26, 0x01, 0x16, 0xE5 }; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 24 | static uint8_t network_session_key[] = { 0x60, 0xB0, 0xD0, 0x88, 0x7F, 0xB7, 0xB8, 0x33, 0xD1, 0xE9, 0x64, 0x2C, 0xF2, 0xEE, 0xEE, 0x63 }; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 25 | static uint8_t data_session_key[] = { 0x68, 0x08, 0x51, 0x18, 0xB1, 0x64, 0x17, 0xC2, 0x5B, 0x9C, 0xDA, 0x2F, 0xF6, 0xC5, 0xFC, 0x18 }; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 26 | static uint8_t frequency_sub_band = 6; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 27 | static bool public_network = true; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 28 | static uint8_t ack = 0; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 29 | static bool adr = false; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 30 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 31 | // deepsleep consumes slightly less current than sleep |
Maciej Pijanowski |
0:f3d9c16b08d7 | 32 | // in sleep mode, IO state is maintained, RAM is retained, and application will resume after waking up |
Maciej Pijanowski |
0:f3d9c16b08d7 | 33 | // in deepsleep mode, IOs float, RAM is lost, and application will start from beginning after waking up |
Maciej Pijanowski |
0:f3d9c16b08d7 | 34 | // if deep_sleep == true, device will enter deepsleep mode |
Maciej Pijanowski |
0:f3d9c16b08d7 | 35 | static bool deep_sleep = true; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 36 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 37 | mDot* dot = NULL; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 38 | lora::ChannelPlan* plan = NULL; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 39 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 40 | Serial pc(USBTX, USBRX); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 41 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 42 | #if defined(TARGET_XDOT_L151CC) |
Maciej Pijanowski |
0:f3d9c16b08d7 | 43 | I2C i2c(I2C_SDA, I2C_SCL); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 44 | ISL29011 lux(i2c); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 45 | #else |
Maciej Pijanowski |
0:f3d9c16b08d7 | 46 | AnalogIn lux(XBEE_AD0); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 47 | #endif |
Maciej Pijanowski |
0:f3d9c16b08d7 | 48 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 49 | int main() { |
Maciej Pijanowski |
0:f3d9c16b08d7 | 50 | // Custom event handler for automatically displaying RX data |
Maciej Pijanowski |
0:f3d9c16b08d7 | 51 | RadioEvent events; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 52 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 53 | pc.baud(115200); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 54 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 55 | mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 56 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 57 | #if CHANNEL_PLAN == CP_US915 |
Maciej Pijanowski |
0:f3d9c16b08d7 | 58 | plan = new lora::ChannelPlan_US915(); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 59 | #elif CHANNEL_PLAN == CP_AU915 |
Maciej Pijanowski |
0:f3d9c16b08d7 | 60 | plan = new lora::ChannelPlan_AU915(); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 61 | #elif CHANNEL_PLAN == CP_EU868 |
Maciej Pijanowski |
0:f3d9c16b08d7 | 62 | plan = new lora::ChannelPlan_EU868(); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 63 | #elif CHANNEL_PLAN == CP_KR920 |
Maciej Pijanowski |
0:f3d9c16b08d7 | 64 | plan = new lora::ChannelPlan_KR920(); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 65 | #elif CHANNEL_PLAN == CP_AS923 |
Maciej Pijanowski |
0:f3d9c16b08d7 | 66 | plan = new lora::ChannelPlan_AS923(); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 67 | #elif CHANNEL_PLAN == CP_AS923_JAPAN |
Maciej Pijanowski |
0:f3d9c16b08d7 | 68 | plan = new lora::ChannelPlan_AS923_Japan(); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 69 | #endif |
Maciej Pijanowski |
0:f3d9c16b08d7 | 70 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 71 | assert(plan); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 72 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 73 | dot = mDot::getInstance(plan); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 74 | assert(dot); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 75 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 76 | // attach the custom events handler |
Maciej Pijanowski |
0:f3d9c16b08d7 | 77 | dot->setEvents(&events); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 78 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 79 | if (!dot->getStandbyFlag()) { |
Maciej Pijanowski |
0:f3d9c16b08d7 | 80 | logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 81 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 82 | // start from a well-known state |
Maciej Pijanowski |
0:f3d9c16b08d7 | 83 | logInfo("defaulting Dot configuration"); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 84 | dot->resetConfig(); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 85 | dot->resetNetworkSession(); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 86 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 87 | // make sure library logging is turned on |
Maciej Pijanowski |
0:f3d9c16b08d7 | 88 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 89 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 90 | // update configuration if necessary |
Maciej Pijanowski |
0:f3d9c16b08d7 | 91 | if (dot->getJoinMode() != mDot::MANUAL) { |
Maciej Pijanowski |
0:f3d9c16b08d7 | 92 | logInfo("changing network join mode to MANUAL"); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 93 | if (dot->setJoinMode(mDot::MANUAL) != mDot::MDOT_OK) { |
Maciej Pijanowski |
0:f3d9c16b08d7 | 94 | logError("failed to set network join mode to MANUAL"); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 95 | } |
Maciej Pijanowski |
0:f3d9c16b08d7 | 96 | } |
Maciej Pijanowski |
0:f3d9c16b08d7 | 97 | // in MANUAL join mode there is no join request/response transaction |
Maciej Pijanowski |
0:f3d9c16b08d7 | 98 | // as long as the Dot is configured correctly and provisioned correctly on the gateway, it should be able to communicate |
Maciej Pijanowski |
0:f3d9c16b08d7 | 99 | // network address - 4 bytes (00000001 - FFFFFFFE) |
Maciej Pijanowski |
0:f3d9c16b08d7 | 100 | // network session key - 16 bytes |
Maciej Pijanowski |
0:f3d9c16b08d7 | 101 | // data session key - 16 bytes |
Maciej Pijanowski |
0:f3d9c16b08d7 | 102 | // to provision your Dot with a Conduit gateway, follow the following steps |
Maciej Pijanowski |
0:f3d9c16b08d7 | 103 | // * ssh into the Conduit |
Maciej Pijanowski |
0:f3d9c16b08d7 | 104 | // * provision the Dot using the lora-query application: http://www.multitech.net/developer/software/lora/lora-network-server/ |
Maciej Pijanowski |
0:f3d9c16b08d7 | 105 | // lora-query -a 01020304 A 0102030401020304 <your Dot's device ID> 01020304010203040102030401020304 01020304010203040102030401020304 |
Maciej Pijanowski |
0:f3d9c16b08d7 | 106 | // * if you change the network address, network session key, or data session key, make sure you update them on the gateway |
Maciej Pijanowski |
0:f3d9c16b08d7 | 107 | // to provision your Dot with a 3rd party gateway, see the gateway or network provider documentation |
Maciej Pijanowski |
0:f3d9c16b08d7 | 108 | update_manual_config(network_address, network_session_key, data_session_key, frequency_sub_band, public_network, ack); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 109 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 110 | // enable or disable Adaptive Data Rate |
Maciej Pijanowski |
0:f3d9c16b08d7 | 111 | dot->setAdr(adr); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 112 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 113 | // set data rate - spreading factor 7 |
Maciej Pijanowski |
0:f3d9c16b08d7 | 114 | if (dot->getTxDataRate() != mDot::DR5) { |
Maciej Pijanowski |
0:f3d9c16b08d7 | 115 | logInfo("Setting TX data rate..."); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 116 | if (dot->setTxDataRate(mDot::DR5) != mDot::MDOT_OK) |
Maciej Pijanowski |
0:f3d9c16b08d7 | 117 | logInfo("Failed to change data rate."); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 118 | } |
Maciej Pijanowski |
0:f3d9c16b08d7 | 119 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 120 | // save changes to configuration |
Maciej Pijanowski |
0:f3d9c16b08d7 | 121 | logInfo("saving configuration"); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 122 | if (!dot->saveConfig()) { |
Maciej Pijanowski |
0:f3d9c16b08d7 | 123 | logError("failed to save configuration"); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 124 | } |
Maciej Pijanowski |
0:f3d9c16b08d7 | 125 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 126 | // display configuration |
Maciej Pijanowski |
0:f3d9c16b08d7 | 127 | display_config(); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 128 | } else { |
Maciej Pijanowski |
0:f3d9c16b08d7 | 129 | // restore the saved session if the dot woke from deepsleep mode |
Maciej Pijanowski |
0:f3d9c16b08d7 | 130 | // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep |
Maciej Pijanowski |
0:f3d9c16b08d7 | 131 | logInfo("restoring network session from NVM"); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 132 | dot->restoreNetworkSession(); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 133 | } |
Maciej Pijanowski |
0:f3d9c16b08d7 | 134 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 135 | while (true) { |
Maciej Pijanowski |
0:f3d9c16b08d7 | 136 | uint16_t light; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 137 | std::vector<uint8_t> tx_data; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 138 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 139 | #if defined(TARGET_XDOT_L151CC) |
Maciej Pijanowski |
0:f3d9c16b08d7 | 140 | // configure the ISL29011 sensor on the xDot-DK for continuous ambient light sampling, 16 bit conversion, and maximum range |
Maciej Pijanowski |
0:f3d9c16b08d7 | 141 | lux.setMode(ISL29011::ALS_CONT); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 142 | lux.setResolution(ISL29011::ADC_16BIT); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 143 | lux.setRange(ISL29011::RNG_64000); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 144 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 145 | // get the latest light sample and send it to the gateway |
Maciej Pijanowski |
0:f3d9c16b08d7 | 146 | light = lux.getData(); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 147 | tx_data.push_back((light >> 8) & 0xFF); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 148 | tx_data.push_back(light & 0xFF); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 149 | logInfo("light: %lu [0x%04X]", light, light); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 150 | send_data(tx_data); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 151 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 152 | // put the LSL29011 ambient light sensor into a low power state |
Maciej Pijanowski |
0:f3d9c16b08d7 | 153 | lux.setMode(ISL29011::PWR_DOWN); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 154 | #else |
Maciej Pijanowski |
0:f3d9c16b08d7 | 155 | // get some dummy data and send it to the gateway |
Maciej Pijanowski |
0:f3d9c16b08d7 | 156 | light = lux.read_u16(); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 157 | tx_data.push_back((light >> 8) & 0xFF); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 158 | tx_data.push_back(light & 0xFF); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 159 | logInfo("light: %lu [0x%04X]", light, light); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 160 | send_data(tx_data); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 161 | #endif |
Maciej Pijanowski |
0:f3d9c16b08d7 | 162 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 163 | // if going into deepsleep mode, save the session so we don't need to join again after waking up |
Maciej Pijanowski |
0:f3d9c16b08d7 | 164 | // not necessary if going into sleep mode since RAM is retained |
Maciej Pijanowski |
0:f3d9c16b08d7 | 165 | if (deep_sleep) { |
Maciej Pijanowski |
0:f3d9c16b08d7 | 166 | logInfo("saving network session to NVM"); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 167 | dot->saveNetworkSession(); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 168 | } |
Maciej Pijanowski |
0:f3d9c16b08d7 | 169 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 170 | // ONLY ONE of the three functions below should be uncommented depending on the desired wakeup method |
Maciej Pijanowski |
0:f3d9c16b08d7 | 171 | //sleep_wake_rtc_only(deep_sleep); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 172 | //sleep_wake_interrupt_only(deep_sleep); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 173 | sleep_wake_rtc_or_interrupt(deep_sleep); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 174 | } |
Maciej Pijanowski |
0:f3d9c16b08d7 | 175 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 176 | return 0; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 177 | } |
Maciej Pijanowski |
0:f3d9c16b08d7 | 178 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 179 | #endif |