zcbvsDG

Dependencies:   ISL29011

Committer:
deepak15
Date:
Mon Sep 25 09:41:00 2017 +0000
Revision:
0:aa8df9825311
test _1;

Who changed what in which revision?

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