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 == MANUAL_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 the settings on your gateway //
nguyenhoang9x5555 0:6a6c127cf398 20 // * edit their values to match your configuration //
nguyenhoang9x5555 0:6a6c127cf398 21 // * frequency sub band is only relevant for the 915 bands //
nguyenhoang9x5555 0:6a6c127cf398 22 /////////////////////////////////////////////////////////////
nguyenhoang9x5555 0:6a6c127cf398 23 static uint8_t network_address[] = { 0x01, 0x02, 0x03, 0x04 };
nguyenhoang9x5555 0:6a6c127cf398 24 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 25 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 26 static uint8_t frequency_sub_band = 6;
nguyenhoang9x5555 0:6a6c127cf398 27 static bool public_network = true;
nguyenhoang9x5555 0:6a6c127cf398 28 static uint8_t join_delay = 5;
nguyenhoang9x5555 0:6a6c127cf398 29 static uint8_t ack = 1;
nguyenhoang9x5555 0:6a6c127cf398 30 static bool adr = true;
nguyenhoang9x5555 0:6a6c127cf398 31
nguyenhoang9x5555 0:6a6c127cf398 32 // deepsleep consumes slightly less current than sleep
nguyenhoang9x5555 0:6a6c127cf398 33 // in sleep mode, IO state is maintained, RAM is retained, and application will resume after waking up
nguyenhoang9x5555 0:6a6c127cf398 34 // in deepsleep mode, IOs float, RAM is lost, and application will start from beginning after waking up
nguyenhoang9x5555 0:6a6c127cf398 35 // if deep_sleep == true, device will enter deepsleep mode
nguyenhoang9x5555 0:6a6c127cf398 36 static bool deep_sleep = true;
nguyenhoang9x5555 0:6a6c127cf398 37
nguyenhoang9x5555 0:6a6c127cf398 38 mDot* dot = NULL;
nguyenhoang9x5555 0:6a6c127cf398 39 lora::ChannelPlan* plan = NULL;
nguyenhoang9x5555 0:6a6c127cf398 40
nguyenhoang9x5555 0:6a6c127cf398 41 Serial pc(USBTX, USBRX);
nguyenhoang9x5555 0:6a6c127cf398 42
nguyenhoang9x5555 0:6a6c127cf398 43 #if defined(TARGET_XDOT_L151CC)
nguyenhoang9x5555 0:6a6c127cf398 44 I2C i2c(I2C_SDA, I2C_SCL);
nguyenhoang9x5555 0:6a6c127cf398 45 ISL29011 lux(i2c);
nguyenhoang9x5555 0:6a6c127cf398 46 #else
nguyenhoang9x5555 0:6a6c127cf398 47 AnalogIn lux(XBEE_AD0);
nguyenhoang9x5555 0:6a6c127cf398 48 #endif
nguyenhoang9x5555 0:6a6c127cf398 49
nguyenhoang9x5555 0:6a6c127cf398 50 int main() {
nguyenhoang9x5555 0:6a6c127cf398 51 // Custom event handler for automatically displaying RX data
nguyenhoang9x5555 0:6a6c127cf398 52 RadioEvent events;
nguyenhoang9x5555 0:6a6c127cf398 53
nguyenhoang9x5555 0:6a6c127cf398 54 pc.baud(115200);
nguyenhoang9x5555 0:6a6c127cf398 55
nguyenhoang9x5555 0:6a6c127cf398 56 #if defined(TARGET_XDOT_L151CC)
nguyenhoang9x5555 0:6a6c127cf398 57 i2c.frequency(400000);
nguyenhoang9x5555 0:6a6c127cf398 58 #endif
nguyenhoang9x5555 0:6a6c127cf398 59
nguyenhoang9x5555 0:6a6c127cf398 60 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
nguyenhoang9x5555 0:6a6c127cf398 61
nguyenhoang9x5555 0:6a6c127cf398 62 #if CHANNEL_PLAN == CP_US915
nguyenhoang9x5555 0:6a6c127cf398 63 plan = new lora::ChannelPlan_US915();
nguyenhoang9x5555 0:6a6c127cf398 64 #elif CHANNEL_PLAN == CP_AU915
nguyenhoang9x5555 0:6a6c127cf398 65 plan = new lora::ChannelPlan_AU915();
nguyenhoang9x5555 0:6a6c127cf398 66 #elif CHANNEL_PLAN == CP_EU868
nguyenhoang9x5555 0:6a6c127cf398 67 plan = new lora::ChannelPlan_EU868();
nguyenhoang9x5555 0:6a6c127cf398 68 #elif CHANNEL_PLAN == CP_KR920
nguyenhoang9x5555 0:6a6c127cf398 69 plan = new lora::ChannelPlan_KR920();
nguyenhoang9x5555 0:6a6c127cf398 70 #elif CHANNEL_PLAN == CP_AS923
nguyenhoang9x5555 0:6a6c127cf398 71 plan = new lora::ChannelPlan_AS923();
nguyenhoang9x5555 0:6a6c127cf398 72 #elif CHANNEL_PLAN == CP_AS923_JAPAN
nguyenhoang9x5555 0:6a6c127cf398 73 plan = new lora::ChannelPlan_AS923_Japan();
nguyenhoang9x5555 0:6a6c127cf398 74 #elif CHANNEL_PLAN == CP_IN865
nguyenhoang9x5555 0:6a6c127cf398 75 plan = new lora::ChannelPlan_IN865();
nguyenhoang9x5555 0:6a6c127cf398 76 #endif
nguyenhoang9x5555 0:6a6c127cf398 77 assert(plan);
nguyenhoang9x5555 0:6a6c127cf398 78
nguyenhoang9x5555 0:6a6c127cf398 79 dot = mDot::getInstance(plan);
nguyenhoang9x5555 0:6a6c127cf398 80 assert(dot);
nguyenhoang9x5555 0:6a6c127cf398 81
nguyenhoang9x5555 0:6a6c127cf398 82 // attach the custom events handler
nguyenhoang9x5555 0:6a6c127cf398 83 dot->setEvents(&events);
nguyenhoang9x5555 0:6a6c127cf398 84
nguyenhoang9x5555 0:6a6c127cf398 85 if (!dot->getStandbyFlag()) {
nguyenhoang9x5555 0:6a6c127cf398 86 logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
nguyenhoang9x5555 0:6a6c127cf398 87
nguyenhoang9x5555 0:6a6c127cf398 88 // start from a well-known state
nguyenhoang9x5555 0:6a6c127cf398 89 logInfo("defaulting Dot configuration");
nguyenhoang9x5555 0:6a6c127cf398 90 dot->resetConfig();
nguyenhoang9x5555 0:6a6c127cf398 91 dot->resetNetworkSession();
nguyenhoang9x5555 0:6a6c127cf398 92
nguyenhoang9x5555 0:6a6c127cf398 93 // make sure library logging is turned on
nguyenhoang9x5555 0:6a6c127cf398 94 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
nguyenhoang9x5555 0:6a6c127cf398 95
nguyenhoang9x5555 0:6a6c127cf398 96 // update configuration if necessary
nguyenhoang9x5555 0:6a6c127cf398 97 if (dot->getJoinMode() != mDot::MANUAL) {
nguyenhoang9x5555 0:6a6c127cf398 98 logInfo("changing network join mode to MANUAL");
nguyenhoang9x5555 0:6a6c127cf398 99 if (dot->setJoinMode(mDot::MANUAL) != mDot::MDOT_OK) {
nguyenhoang9x5555 0:6a6c127cf398 100 logError("failed to set network join mode to MANUAL");
nguyenhoang9x5555 0:6a6c127cf398 101 }
nguyenhoang9x5555 0:6a6c127cf398 102 }
nguyenhoang9x5555 0:6a6c127cf398 103 // in MANUAL join mode there is no join request/response transaction
nguyenhoang9x5555 0:6a6c127cf398 104 // as long as the Dot is configured correctly and provisioned correctly on the gateway, it should be able to communicate
nguyenhoang9x5555 0:6a6c127cf398 105 // network address - 4 bytes (00000001 - FFFFFFFE)
nguyenhoang9x5555 0:6a6c127cf398 106 // network session key - 16 bytes
nguyenhoang9x5555 0:6a6c127cf398 107 // data session key - 16 bytes
nguyenhoang9x5555 0:6a6c127cf398 108 // to provision your Dot with a Conduit gateway, follow the following steps
nguyenhoang9x5555 0:6a6c127cf398 109 // * ssh into the Conduit
nguyenhoang9x5555 0:6a6c127cf398 110 // * provision the Dot using the lora-query application: http://www.multitech.net/developer/software/lora/lora-network-server/
nguyenhoang9x5555 0:6a6c127cf398 111 // lora-query -a 01020304 A 0102030401020304 <your Dot's device ID> 01020304010203040102030401020304 01020304010203040102030401020304
nguyenhoang9x5555 0:6a6c127cf398 112 // * if you change the network address, network session key, or data session key, make sure you update them on the gateway
nguyenhoang9x5555 0:6a6c127cf398 113 // to provision your Dot with a 3rd party gateway, see the gateway or network provider documentation
nguyenhoang9x5555 0:6a6c127cf398 114 update_manual_config(network_address, network_session_key, data_session_key, frequency_sub_band, public_network, ack);
nguyenhoang9x5555 0:6a6c127cf398 115
nguyenhoang9x5555 0:6a6c127cf398 116 // enable or disable Adaptive Data Rate
nguyenhoang9x5555 0:6a6c127cf398 117 dot->setAdr(adr);
nguyenhoang9x5555 0:6a6c127cf398 118
nguyenhoang9x5555 0:6a6c127cf398 119 // Configure the join delay
nguyenhoang9x5555 0:6a6c127cf398 120 dot->setJoinDelay(join_delay);
nguyenhoang9x5555 0:6a6c127cf398 121
nguyenhoang9x5555 0:6a6c127cf398 122 // save changes to configuration
nguyenhoang9x5555 0:6a6c127cf398 123 logInfo("saving configuration");
nguyenhoang9x5555 0:6a6c127cf398 124 if (!dot->saveConfig()) {
nguyenhoang9x5555 0:6a6c127cf398 125 logError("failed to save configuration");
nguyenhoang9x5555 0:6a6c127cf398 126 }
nguyenhoang9x5555 0:6a6c127cf398 127
nguyenhoang9x5555 0:6a6c127cf398 128 // display configuration
nguyenhoang9x5555 0:6a6c127cf398 129 display_config();
nguyenhoang9x5555 0:6a6c127cf398 130 } else {
nguyenhoang9x5555 0:6a6c127cf398 131 // restore the saved session if the dot woke from deepsleep mode
nguyenhoang9x5555 0:6a6c127cf398 132 // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep
nguyenhoang9x5555 0:6a6c127cf398 133 logInfo("restoring network session from NVM");
nguyenhoang9x5555 0:6a6c127cf398 134 dot->restoreNetworkSession();
nguyenhoang9x5555 0:6a6c127cf398 135 }
nguyenhoang9x5555 0:6a6c127cf398 136
nguyenhoang9x5555 0:6a6c127cf398 137 while (true) {
nguyenhoang9x5555 0:6a6c127cf398 138 uint16_t light;
nguyenhoang9x5555 0:6a6c127cf398 139 std::vector<uint8_t> tx_data;
nguyenhoang9x5555 0:6a6c127cf398 140
nguyenhoang9x5555 0:6a6c127cf398 141 #if defined(TARGET_XDOT_L151CC)
nguyenhoang9x5555 0:6a6c127cf398 142 // configure the ISL29011 sensor on the xDot-DK for continuous ambient light sampling, 16 bit conversion, and maximum range
nguyenhoang9x5555 0:6a6c127cf398 143 lux.setMode(ISL29011::ALS_CONT);
nguyenhoang9x5555 0:6a6c127cf398 144 lux.setResolution(ISL29011::ADC_16BIT);
nguyenhoang9x5555 0:6a6c127cf398 145 lux.setRange(ISL29011::RNG_64000);
nguyenhoang9x5555 0:6a6c127cf398 146
nguyenhoang9x5555 0:6a6c127cf398 147 // get the latest light sample and send it to the gateway
nguyenhoang9x5555 0:6a6c127cf398 148 light = lux.getData();
nguyenhoang9x5555 0:6a6c127cf398 149 tx_data.push_back((light >> 8) & 0xFF);
nguyenhoang9x5555 0:6a6c127cf398 150 tx_data.push_back(light & 0xFF);
nguyenhoang9x5555 0:6a6c127cf398 151 logInfo("light: %lu [0x%04X]", light, light);
nguyenhoang9x5555 0:6a6c127cf398 152 send_data(tx_data);
nguyenhoang9x5555 0:6a6c127cf398 153
nguyenhoang9x5555 0:6a6c127cf398 154 // put the LSL29011 ambient light sensor into a low power state
nguyenhoang9x5555 0:6a6c127cf398 155 lux.setMode(ISL29011::PWR_DOWN);
nguyenhoang9x5555 0:6a6c127cf398 156 #else
nguyenhoang9x5555 0:6a6c127cf398 157 // get some dummy data and send it to the gateway
nguyenhoang9x5555 0:6a6c127cf398 158 light = lux.read_u16();
nguyenhoang9x5555 0:6a6c127cf398 159 tx_data.push_back((light >> 8) & 0xFF);
nguyenhoang9x5555 0:6a6c127cf398 160 tx_data.push_back(light & 0xFF);
nguyenhoang9x5555 0:6a6c127cf398 161 logInfo("light: %lu [0x%04X]", light, light);
nguyenhoang9x5555 0:6a6c127cf398 162 send_data(tx_data);
nguyenhoang9x5555 0:6a6c127cf398 163 #endif
nguyenhoang9x5555 0:6a6c127cf398 164
nguyenhoang9x5555 0:6a6c127cf398 165 // if going into deepsleep mode, save the session so we don't need to join again after waking up
nguyenhoang9x5555 0:6a6c127cf398 166 // not necessary if going into sleep mode since RAM is retained
nguyenhoang9x5555 0:6a6c127cf398 167 if (deep_sleep) {
nguyenhoang9x5555 0:6a6c127cf398 168 logInfo("saving network session to NVM");
nguyenhoang9x5555 0:6a6c127cf398 169 dot->saveNetworkSession();
nguyenhoang9x5555 0:6a6c127cf398 170 }
nguyenhoang9x5555 0:6a6c127cf398 171
nguyenhoang9x5555 0:6a6c127cf398 172 // ONLY ONE of the three functions below should be uncommented depending on the desired wakeup method
nguyenhoang9x5555 0:6a6c127cf398 173 //sleep_wake_rtc_only(deep_sleep);
nguyenhoang9x5555 0:6a6c127cf398 174 //sleep_wake_interrupt_only(deep_sleep);
nguyenhoang9x5555 0:6a6c127cf398 175 sleep_wake_rtc_or_interrupt(deep_sleep);
nguyenhoang9x5555 0:6a6c127cf398 176 }
nguyenhoang9x5555 0:6a6c127cf398 177
nguyenhoang9x5555 0:6a6c127cf398 178 return 0;
nguyenhoang9x5555 0:6a6c127cf398 179 }
nguyenhoang9x5555 0:6a6c127cf398 180
nguyenhoang9x5555 0:6a6c127cf398 181 #endif