zcbvsDG
src/class_c_example.cpp@1:bd6575a12f84, 2017-09-26 (annotated)
- Committer:
- deepak15
- Date:
- Tue Sep 26 06:54:02 2017 +0000
- Revision:
- 1:bd6575a12f84
- Parent:
- 0:aa8df9825311
send data on interval of 60 sec ;
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 == CLASS_C_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 = 1; |
deepak15 | 0:aa8df9825311 | 32 | static bool adr = true; |
deepak15 | 0:aa8df9825311 | 33 | |
deepak15 | 0:aa8df9825311 | 34 | mDot* dot = NULL; |
deepak15 | 0:aa8df9825311 | 35 | lora::ChannelPlan* plan = NULL; |
deepak15 | 0:aa8df9825311 | 36 | |
deepak15 | 0:aa8df9825311 | 37 | Serial pc(USBTX, USBRX); |
deepak15 | 0:aa8df9825311 | 38 | |
deepak15 | 0:aa8df9825311 | 39 | #if defined(TARGET_XDOT_L151CC) |
deepak15 | 0:aa8df9825311 | 40 | I2C i2c(I2C_SDA, I2C_SCL); |
deepak15 | 0:aa8df9825311 | 41 | ISL29011 lux(i2c); |
deepak15 | 0:aa8df9825311 | 42 | #else |
deepak15 | 0:aa8df9825311 | 43 | AnalogIn lux(XBEE_AD0); |
deepak15 | 0:aa8df9825311 | 44 | #endif |
deepak15 | 0:aa8df9825311 | 45 | |
deepak15 | 0:aa8df9825311 | 46 | int main() { |
deepak15 | 0:aa8df9825311 | 47 | // Custom event handler for automatically displaying RX data |
deepak15 | 0:aa8df9825311 | 48 | RadioEvent events; |
deepak15 | 0:aa8df9825311 | 49 | |
deepak15 | 0:aa8df9825311 | 50 | pc.baud(115200); |
deepak15 | 0:aa8df9825311 | 51 | |
deepak15 | 0:aa8df9825311 | 52 | mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); |
deepak15 | 0:aa8df9825311 | 53 | |
deepak15 | 0:aa8df9825311 | 54 | #if CHANNEL_PLAN == CP_US915 |
deepak15 | 0:aa8df9825311 | 55 | plan = new lora::ChannelPlan_US915(); |
deepak15 | 0:aa8df9825311 | 56 | #elif CHANNEL_PLAN == CP_AU915 |
deepak15 | 0:aa8df9825311 | 57 | plan = new lora::ChannelPlan_AU915(); |
deepak15 | 0:aa8df9825311 | 58 | #elif CHANNEL_PLAN == CP_EU868 |
deepak15 | 0:aa8df9825311 | 59 | plan = new lora::ChannelPlan_EU868(); |
deepak15 | 0:aa8df9825311 | 60 | #elif CHANNEL_PLAN == CP_KR920 |
deepak15 | 0:aa8df9825311 | 61 | plan = new lora::ChannelPlan_KR920(); |
deepak15 | 0:aa8df9825311 | 62 | #elif CHANNEL_PLAN == CP_AS923 |
deepak15 | 0:aa8df9825311 | 63 | plan = new lora::ChannelPlan_AS923(); |
deepak15 | 0:aa8df9825311 | 64 | #elif CHANNEL_PLAN == CP_AS923_JAPAN |
deepak15 | 0:aa8df9825311 | 65 | plan = new lora::ChannelPlan_AS923_Japan(); |
deepak15 | 0:aa8df9825311 | 66 | #elif CHANNEL_PLAN == CP_IN865 |
deepak15 | 0:aa8df9825311 | 67 | plan = new lora::ChannelPlan_IN865(); |
deepak15 | 0:aa8df9825311 | 68 | #endif |
deepak15 | 0:aa8df9825311 | 69 | assert(plan); |
deepak15 | 0:aa8df9825311 | 70 | |
deepak15 | 0:aa8df9825311 | 71 | dot = mDot::getInstance(plan); |
deepak15 | 0:aa8df9825311 | 72 | assert(dot); |
deepak15 | 0:aa8df9825311 | 73 | |
deepak15 | 0:aa8df9825311 | 74 | logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION); |
deepak15 | 0:aa8df9825311 | 75 | |
deepak15 | 0:aa8df9825311 | 76 | // start from a well-known state |
deepak15 | 0:aa8df9825311 | 77 | logInfo("defaulting Dot configuration"); |
deepak15 | 0:aa8df9825311 | 78 | dot->resetConfig(); |
deepak15 | 0:aa8df9825311 | 79 | dot->resetNetworkSession(); |
deepak15 | 0:aa8df9825311 | 80 | |
deepak15 | 0:aa8df9825311 | 81 | // make sure library logging is turned on |
deepak15 | 0:aa8df9825311 | 82 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
deepak15 | 0:aa8df9825311 | 83 | |
deepak15 | 0:aa8df9825311 | 84 | // attach the custom events handler |
deepak15 | 0:aa8df9825311 | 85 | dot->setEvents(&events); |
deepak15 | 0:aa8df9825311 | 86 | |
deepak15 | 0:aa8df9825311 | 87 | // update configuration if necessary |
deepak15 | 0:aa8df9825311 | 88 | if (dot->getJoinMode() != mDot::OTA) { |
deepak15 | 0:aa8df9825311 | 89 | logInfo("changing network join mode to OTA"); |
deepak15 | 0:aa8df9825311 | 90 | if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) { |
deepak15 | 0:aa8df9825311 | 91 | logError("failed to set network join mode to OTA"); |
deepak15 | 0:aa8df9825311 | 92 | } |
deepak15 | 0:aa8df9825311 | 93 | } |
deepak15 | 0:aa8df9825311 | 94 | // 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 | 95 | // only one method or the other should be used! |
deepak15 | 0:aa8df9825311 | 96 | // network ID = crc64(network name) |
deepak15 | 0:aa8df9825311 | 97 | // network KEY = cmac(network passphrase) |
deepak15 | 0:aa8df9825311 | 98 | update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, public_network, ack); |
deepak15 | 0:aa8df9825311 | 99 | //update_ota_config_id_key(network_id, network_key, frequency_sub_band, public_network, ack); |
deepak15 | 0:aa8df9825311 | 100 | |
deepak15 | 0:aa8df9825311 | 101 | // configure the Dot for class C operation |
deepak15 | 0:aa8df9825311 | 102 | // the Dot must also be configured on the gateway for class C |
deepak15 | 0:aa8df9825311 | 103 | // use the lora-query application to do this on a Conduit: http://www.multitech.net/developer/software/lora/lora-network-server/ |
deepak15 | 0:aa8df9825311 | 104 | // to provision your Dot for class C operation with a 3rd party gateway, see the gateway or network provider documentation |
deepak15 | 0:aa8df9825311 | 105 | logInfo("changing network mode to class C"); |
deepak15 | 0:aa8df9825311 | 106 | if (dot->setClass("C") != mDot::MDOT_OK) { |
deepak15 | 0:aa8df9825311 | 107 | logError("failed to set network mode to class C"); |
deepak15 | 0:aa8df9825311 | 108 | } |
deepak15 | 0:aa8df9825311 | 109 | |
deepak15 | 0:aa8df9825311 | 110 | // enable or disable Adaptive Data Rate |
deepak15 | 0:aa8df9825311 | 111 | dot->setAdr(adr); |
deepak15 | 0:aa8df9825311 | 112 | |
deepak15 | 0:aa8df9825311 | 113 | // save changes to configuration |
deepak15 | 0:aa8df9825311 | 114 | logInfo("saving configuration"); |
deepak15 | 0:aa8df9825311 | 115 | if (!dot->saveConfig()) { |
deepak15 | 0:aa8df9825311 | 116 | logError("failed to save configuration"); |
deepak15 | 0:aa8df9825311 | 117 | } |
deepak15 | 0:aa8df9825311 | 118 | |
deepak15 | 0:aa8df9825311 | 119 | // display configuration |
deepak15 | 0:aa8df9825311 | 120 | display_config(); |
deepak15 | 0:aa8df9825311 | 121 | |
deepak15 | 0:aa8df9825311 | 122 | #if defined(TARGET_XDOT_L151CC) |
deepak15 | 0:aa8df9825311 | 123 | // configure the ISL29011 sensor on the xDot-DK for continuous ambient light sampling, 16 bit conversion, and maximum range |
deepak15 | 0:aa8df9825311 | 124 | lux.setMode(ISL29011::ALS_CONT); |
deepak15 | 0:aa8df9825311 | 125 | lux.setResolution(ISL29011::ADC_16BIT); |
deepak15 | 0:aa8df9825311 | 126 | lux.setRange(ISL29011::RNG_64000); |
deepak15 | 0:aa8df9825311 | 127 | #endif |
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 | // get the latest light sample and send it to the gateway |
deepak15 | 0:aa8df9825311 | 140 | light = lux.getData(); |
deepak15 | 0:aa8df9825311 | 141 | tx_data.push_back((light >> 8) & 0xFF); |
deepak15 | 0:aa8df9825311 | 142 | tx_data.push_back(light & 0xFF); |
deepak15 | 0:aa8df9825311 | 143 | logInfo("light: %lu [0x%04X]", light, light); |
deepak15 | 0:aa8df9825311 | 144 | send_data(tx_data); |
deepak15 | 0:aa8df9825311 | 145 | #else |
deepak15 | 0:aa8df9825311 | 146 | // get some dummy data and send it to the gateway |
deepak15 | 0:aa8df9825311 | 147 | light = lux.read_u16(); |
deepak15 | 0:aa8df9825311 | 148 | tx_data.push_back((light >> 8) & 0xFF); |
deepak15 | 0:aa8df9825311 | 149 | tx_data.push_back(light & 0xFF); |
deepak15 | 0:aa8df9825311 | 150 | logInfo("light: %lu [0x%04X]", light, light); |
deepak15 | 0:aa8df9825311 | 151 | send_data(tx_data); |
deepak15 | 0:aa8df9825311 | 152 | #endif |
deepak15 | 0:aa8df9825311 | 153 | |
deepak15 | 0:aa8df9825311 | 154 | // the Dot can't sleep in class C mode |
deepak15 | 0:aa8df9825311 | 155 | // it must be waiting for data from the gateway |
deepak15 | 0:aa8df9825311 | 156 | // send data every 30s |
deepak15 | 0:aa8df9825311 | 157 | logInfo("waiting for 30s"); |
deepak15 | 0:aa8df9825311 | 158 | wait(30); |
deepak15 | 0:aa8df9825311 | 159 | } |
deepak15 | 0:aa8df9825311 | 160 | |
deepak15 | 0:aa8df9825311 | 161 | return 0; |
deepak15 | 0:aa8df9825311 | 162 | } |
deepak15 | 0:aa8df9825311 | 163 | |
deepak15 | 0:aa8df9825311 | 164 | #endif |
deepak15 | 0:aa8df9825311 | 165 |