Code to talk to the mdot

Dependencies:   libmDot-mbed5 ISL29011

Committer:
bhimebau
Date:
Thu Feb 08 18:45:41 2018 +0000
Revision:
0:34da43c761f8
Initial push;

Who changed what in which revision?

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