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