mdot-examples triage
Dependencies: libmDot-dev-mbed5-deprecated ISL29011
examples/src/peer_to_peer_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 == PEER_TO_PEER_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 between the two devices in // |
Maciej Pijanowski |
0:f3d9c16b08d7 | 20 | // order for communication to be successful |
Maciej Pijanowski |
0:f3d9c16b08d7 | 21 | ///////////////////////////////////////////////////////////// |
Maciej Pijanowski |
0:f3d9c16b08d7 | 22 | static uint8_t network_address[] = { 0x01, 0x02, 0x03, 0x04 }; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 23 | static uint8_t network_session_key[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 }; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 24 | static uint8_t data_session_key[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 }; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 25 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 26 | mDot* dot = NULL; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 27 | lora::ChannelPlan* plan = NULL; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 28 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 29 | Serial pc(USBTX, USBRX); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 30 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 31 | #if defined(TARGET_XDOT_L151CC) |
Maciej Pijanowski |
0:f3d9c16b08d7 | 32 | I2C i2c(I2C_SDA, I2C_SCL); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 33 | ISL29011 lux(i2c); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 34 | #else |
Maciej Pijanowski |
0:f3d9c16b08d7 | 35 | AnalogIn lux(XBEE_AD0); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 36 | #endif |
Maciej Pijanowski |
0:f3d9c16b08d7 | 37 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 38 | int main() { |
Maciej Pijanowski |
0:f3d9c16b08d7 | 39 | // Custom event handler for automatically displaying RX data |
Maciej Pijanowski |
0:f3d9c16b08d7 | 40 | RadioEvent events; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 41 | uint32_t tx_frequency; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 42 | uint8_t tx_datarate; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 43 | uint8_t tx_power; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 44 | uint8_t frequency_band; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 45 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 46 | pc.baud(115200); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 47 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 48 | mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 49 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 50 | #if CHANNEL_PLAN == CP_US915 |
Maciej Pijanowski |
0:f3d9c16b08d7 | 51 | plan = new lora::ChannelPlan_US915(); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 52 | #elif CHANNEL_PLAN == CP_AU915 |
Maciej Pijanowski |
0:f3d9c16b08d7 | 53 | plan = new lora::ChannelPlan_AU915(); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 54 | #elif CHANNEL_PLAN == CP_EU868 |
Maciej Pijanowski |
0:f3d9c16b08d7 | 55 | plan = new lora::ChannelPlan_EU868(); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 56 | #elif CHANNEL_PLAN == CP_KR920 |
Maciej Pijanowski |
0:f3d9c16b08d7 | 57 | plan = new lora::ChannelPlan_KR920(); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 58 | #elif CHANNEL_PLAN == CP_AS923 |
Maciej Pijanowski |
0:f3d9c16b08d7 | 59 | plan = new lora::ChannelPlan_AS923(); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 60 | #elif CHANNEL_PLAN == CP_AS923_JAPAN |
Maciej Pijanowski |
0:f3d9c16b08d7 | 61 | plan = new lora::ChannelPlan_AS923_Japan(); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 62 | #endif |
Maciej Pijanowski |
0:f3d9c16b08d7 | 63 | assert(plan); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 64 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 65 | dot = mDot::getInstance(plan); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 66 | assert(dot); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 67 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 68 | logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 69 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 70 | // start from a well-known state |
Maciej Pijanowski |
0:f3d9c16b08d7 | 71 | logInfo("defaulting Dot configuration"); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 72 | dot->resetConfig(); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 73 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 74 | // make sure library logging is turned on |
Maciej Pijanowski |
0:f3d9c16b08d7 | 75 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 76 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 77 | // attach the custom events handler |
Maciej Pijanowski |
0:f3d9c16b08d7 | 78 | dot->setEvents(&events); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 79 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 80 | // update configuration if necessary |
Maciej Pijanowski |
0:f3d9c16b08d7 | 81 | if (dot->getJoinMode() != mDot::PEER_TO_PEER) { |
Maciej Pijanowski |
0:f3d9c16b08d7 | 82 | logInfo("changing network join mode to PEER_TO_PEER"); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 83 | if (dot->setJoinMode(mDot::PEER_TO_PEER) != mDot::MDOT_OK) { |
Maciej Pijanowski |
0:f3d9c16b08d7 | 84 | logError("failed to set network join mode to PEER_TO_PEER"); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 85 | } |
Maciej Pijanowski |
0:f3d9c16b08d7 | 86 | } |
Maciej Pijanowski |
0:f3d9c16b08d7 | 87 | frequency_band = dot->getFrequencyBand(); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 88 | switch (frequency_band) { |
Maciej Pijanowski |
0:f3d9c16b08d7 | 89 | case lora::ChannelPlan::EU868_OLD: |
Maciej Pijanowski |
0:f3d9c16b08d7 | 90 | case lora::ChannelPlan::EU868: |
Maciej Pijanowski |
0:f3d9c16b08d7 | 91 | // 250kHz channels achieve higher throughput |
Maciej Pijanowski |
0:f3d9c16b08d7 | 92 | // DR_6 : SF7 @ 250kHz |
Maciej Pijanowski |
0:f3d9c16b08d7 | 93 | // DR_0 - DR_5 (125kHz channels) available but much slower |
Maciej Pijanowski |
0:f3d9c16b08d7 | 94 | tx_frequency = 869850000; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 95 | tx_datarate = lora::DR_6; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 96 | // the 869850000 frequency is 100% duty cycle if the total power is under 7 dBm - tx power 4 + antenna gain 3 = 7 |
Maciej Pijanowski |
0:f3d9c16b08d7 | 97 | tx_power = 4; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 98 | break; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 99 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 100 | case lora::ChannelPlan::US915_OLD: |
Maciej Pijanowski |
0:f3d9c16b08d7 | 101 | case lora::ChannelPlan::US915: |
Maciej Pijanowski |
0:f3d9c16b08d7 | 102 | case lora::ChannelPlan::AU915_OLD: |
Maciej Pijanowski |
0:f3d9c16b08d7 | 103 | case lora::ChannelPlan::AU915: |
Maciej Pijanowski |
0:f3d9c16b08d7 | 104 | // 500kHz channels achieve highest throughput |
Maciej Pijanowski |
0:f3d9c16b08d7 | 105 | // DR_8 : SF12 @ 500kHz |
Maciej Pijanowski |
0:f3d9c16b08d7 | 106 | // DR_9 : SF11 @ 500kHz |
Maciej Pijanowski |
0:f3d9c16b08d7 | 107 | // DR_10 : SF10 @ 500kHz |
Maciej Pijanowski |
0:f3d9c16b08d7 | 108 | // DR_11 : SF9 @ 500kHz |
Maciej Pijanowski |
0:f3d9c16b08d7 | 109 | // DR_12 : SF8 @ 500kHz |
Maciej Pijanowski |
0:f3d9c16b08d7 | 110 | // DR_13 : SF7 @ 500kHz |
Maciej Pijanowski |
0:f3d9c16b08d7 | 111 | // DR_0 - DR_3 (125kHz channels) available but much slower |
Maciej Pijanowski |
0:f3d9c16b08d7 | 112 | tx_frequency = 915500000; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 113 | tx_datarate = lora::DR_13; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 114 | // 915 bands have no duty cycle restrictions, set tx power to max |
Maciej Pijanowski |
0:f3d9c16b08d7 | 115 | tx_power = 20; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 116 | break; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 117 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 118 | case lora::ChannelPlan::AS923: |
Maciej Pijanowski |
0:f3d9c16b08d7 | 119 | case lora::ChannelPlan::AS923_JAPAN: |
Maciej Pijanowski |
0:f3d9c16b08d7 | 120 | // 250kHz channels achieve higher throughput |
Maciej Pijanowski |
0:f3d9c16b08d7 | 121 | // DR_6 : SF7 @ 250kHz |
Maciej Pijanowski |
0:f3d9c16b08d7 | 122 | // DR_0 - DR_5 (125kHz channels) available but much slower |
Maciej Pijanowski |
0:f3d9c16b08d7 | 123 | tx_frequency = 924800000; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 124 | tx_datarate = lora::DR_6; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 125 | tx_power = 16; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 126 | break; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 127 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 128 | case lora::ChannelPlan::KR920: |
Maciej Pijanowski |
0:f3d9c16b08d7 | 129 | // DR_5 : SF7 @ 125kHz |
Maciej Pijanowski |
0:f3d9c16b08d7 | 130 | tx_frequency = 922700000; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 131 | tx_datarate = lora::DR_5; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 132 | tx_power = 14; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 133 | break; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 134 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 135 | default: |
Maciej Pijanowski |
0:f3d9c16b08d7 | 136 | while (true) { |
Maciej Pijanowski |
0:f3d9c16b08d7 | 137 | logFatal("no known channel plan in use - extra configuration is needed!"); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 138 | wait(5); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 139 | } |
Maciej Pijanowski |
0:f3d9c16b08d7 | 140 | break; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 141 | } |
Maciej Pijanowski |
0:f3d9c16b08d7 | 142 | // in PEER_TO_PEER mode there is no join request/response transaction |
Maciej Pijanowski |
0:f3d9c16b08d7 | 143 | // as long as both Dots are configured correctly, they should be able to communicate |
Maciej Pijanowski |
0:f3d9c16b08d7 | 144 | update_peer_to_peer_config(network_address, network_session_key, data_session_key, tx_frequency, tx_datarate, tx_power); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 145 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 146 | // save changes to configuration |
Maciej Pijanowski |
0:f3d9c16b08d7 | 147 | logInfo("saving configuration"); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 148 | if (!dot->saveConfig()) { |
Maciej Pijanowski |
0:f3d9c16b08d7 | 149 | logError("failed to save configuration"); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 150 | } |
Maciej Pijanowski |
0:f3d9c16b08d7 | 151 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 152 | // display configuration |
Maciej Pijanowski |
0:f3d9c16b08d7 | 153 | display_config(); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 154 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 155 | #if defined(TARGET_XDOT_L151CC) |
Maciej Pijanowski |
0:f3d9c16b08d7 | 156 | // configure the ISL29011 sensor on the xDot-DK for continuous ambient light sampling, 16 bit conversion, and maximum range |
Maciej Pijanowski |
0:f3d9c16b08d7 | 157 | lux.setMode(ISL29011::ALS_CONT); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 158 | lux.setResolution(ISL29011::ADC_16BIT); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 159 | lux.setRange(ISL29011::RNG_64000); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 160 | #endif |
Maciej Pijanowski |
0:f3d9c16b08d7 | 161 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 162 | while (true) { |
Maciej Pijanowski |
0:f3d9c16b08d7 | 163 | uint16_t light; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 164 | std::vector<uint8_t> tx_data; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 165 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 166 | // join network if not joined |
Maciej Pijanowski |
0:f3d9c16b08d7 | 167 | if (!dot->getNetworkJoinStatus()) { |
Maciej Pijanowski |
0:f3d9c16b08d7 | 168 | join_network(); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 169 | } |
Maciej Pijanowski |
0:f3d9c16b08d7 | 170 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 171 | #if defined(TARGET_XDOT_L151CC) |
Maciej Pijanowski |
0:f3d9c16b08d7 | 172 | // get the latest light sample and send it to the gateway |
Maciej Pijanowski |
0:f3d9c16b08d7 | 173 | light = lux.getData(); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 174 | tx_data.push_back((light >> 8) & 0xFF); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 175 | tx_data.push_back(light & 0xFF); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 176 | logInfo("light: %lu [0x%04X]", light, light); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 177 | send_data(tx_data); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 178 | #else |
Maciej Pijanowski |
0:f3d9c16b08d7 | 179 | // get some dummy data and send it to the gateway |
Maciej Pijanowski |
0:f3d9c16b08d7 | 180 | light = lux.read_u16(); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 181 | tx_data.push_back((light >> 8) & 0xFF); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 182 | tx_data.push_back(light & 0xFF); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 183 | logInfo("light: %lu [0x%04X]", light, light); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 184 | send_data(tx_data); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 185 | #endif |
Maciej Pijanowski |
0:f3d9c16b08d7 | 186 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 187 | // the Dot can't sleep in PEER_TO_PEER mode |
Maciej Pijanowski |
0:f3d9c16b08d7 | 188 | // it must be waiting for data from the other Dot |
Maciej Pijanowski |
0:f3d9c16b08d7 | 189 | // send data every 5 seconds |
Maciej Pijanowski |
0:f3d9c16b08d7 | 190 | logInfo("waiting for 5s"); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 191 | wait(5); |
Maciej Pijanowski |
0:f3d9c16b08d7 | 192 | } |
Maciej Pijanowski |
0:f3d9c16b08d7 | 193 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 194 | return 0; |
Maciej Pijanowski |
0:f3d9c16b08d7 | 195 | } |
Maciej Pijanowski |
0:f3d9c16b08d7 | 196 | |
Maciej Pijanowski |
0:f3d9c16b08d7 | 197 | #endif |
Maciej Pijanowski |
0:f3d9c16b08d7 | 198 |