DOT_Example_16_10_2018

Dependencies:   libmDot-mbed5 ISL29011

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?

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