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 == AUTO_OTA_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 // * either the network name and passphrase can be used or //
nguyenhoang9x5555 0:6a6c127cf398 23 // the network ID (8 bytes) and KEY (16 bytes) //
nguyenhoang9x5555 0:6a6c127cf398 24 /////////////////////////////////////////////////////////////
nguyenhoang9x5555 0:6a6c127cf398 25 static std::string network_name = "MultiTech";
nguyenhoang9x5555 0:6a6c127cf398 26 static std::string network_passphrase = "MultiTech";
nguyenhoang9x5555 0:6a6c127cf398 27 static uint8_t network_id[] = { 0x6C, 0x4E, 0xEF, 0x66, 0xF4, 0x79, 0x86, 0xA6 };
nguyenhoang9x5555 0:6a6c127cf398 28 static uint8_t network_key[] = { 0x1F, 0x33, 0xA1, 0x70, 0xA5, 0xF1, 0xFD, 0xA0, 0xAB, 0x69, 0x7A, 0xAE, 0x2B, 0x95, 0x91, 0x6B };
nguyenhoang9x5555 0:6a6c127cf398 29 static uint8_t frequency_sub_band = 0;
nguyenhoang9x5555 0:6a6c127cf398 30 static bool public_network = true;
nguyenhoang9x5555 0:6a6c127cf398 31 static uint8_t join_delay = 5;
nguyenhoang9x5555 0:6a6c127cf398 32 static uint8_t ack = 0;
nguyenhoang9x5555 0:6a6c127cf398 33 static bool adr = true;
nguyenhoang9x5555 0:6a6c127cf398 34
nguyenhoang9x5555 0:6a6c127cf398 35 // deepsleep consumes slightly less current than sleep
nguyenhoang9x5555 0:6a6c127cf398 36 // in sleep mode, IO state is maintained, RAM is retained, and application will resume after waking up
nguyenhoang9x5555 0:6a6c127cf398 37 // in deepsleep mode, IOs float, RAM is lost, and application will start from beginning after waking up
nguyenhoang9x5555 0:6a6c127cf398 38 // if deep_sleep == true, device will enter deepsleep mode
nguyenhoang9x5555 0:6a6c127cf398 39 static bool deep_sleep = true;
nguyenhoang9x5555 0:6a6c127cf398 40
nguyenhoang9x5555 0:6a6c127cf398 41 mDot* dot = NULL;
nguyenhoang9x5555 0:6a6c127cf398 42 lora::ChannelPlan* plan = NULL;
nguyenhoang9x5555 0:6a6c127cf398 43
nguyenhoang9x5555 0:6a6c127cf398 44 Serial pc(USBTX, USBRX);
nguyenhoang9x5555 0:6a6c127cf398 45
nguyenhoang9x5555 0:6a6c127cf398 46 #if defined(TARGET_XDOT_L151CC)
nguyenhoang9x5555 0:6a6c127cf398 47 I2C i2c(I2C_SDA, I2C_SCL);
nguyenhoang9x5555 0:6a6c127cf398 48 ISL29011 lux(i2c);
nguyenhoang9x5555 0:6a6c127cf398 49 #else
nguyenhoang9x5555 0:6a6c127cf398 50 AnalogIn lux(XBEE_AD0);
nguyenhoang9x5555 0:6a6c127cf398 51 #endif
nguyenhoang9x5555 0:6a6c127cf398 52
nguyenhoang9x5555 0:6a6c127cf398 53 int main() {
nguyenhoang9x5555 0:6a6c127cf398 54 // Custom event handler for automatically displaying RX data
nguyenhoang9x5555 0:6a6c127cf398 55 RadioEvent events;
nguyenhoang9x5555 0:6a6c127cf398 56
nguyenhoang9x5555 0:6a6c127cf398 57 pc.baud(115200);
nguyenhoang9x5555 0:6a6c127cf398 58
nguyenhoang9x5555 0:6a6c127cf398 59 #if defined(TARGET_XDOT_L151CC)
nguyenhoang9x5555 0:6a6c127cf398 60 i2c.frequency(400000);
nguyenhoang9x5555 0:6a6c127cf398 61 #endif
nguyenhoang9x5555 0:6a6c127cf398 62
nguyenhoang9x5555 0:6a6c127cf398 63 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
nguyenhoang9x5555 0:6a6c127cf398 64
nguyenhoang9x5555 0:6a6c127cf398 65 #if CHANNEL_PLAN == CP_US915
nguyenhoang9x5555 0:6a6c127cf398 66 plan = new lora::ChannelPlan_US915();
nguyenhoang9x5555 0:6a6c127cf398 67 #elif CHANNEL_PLAN == CP_AU915
nguyenhoang9x5555 0:6a6c127cf398 68 plan = new lora::ChannelPlan_AU915();
nguyenhoang9x5555 0:6a6c127cf398 69 #elif CHANNEL_PLAN == CP_EU868
nguyenhoang9x5555 0:6a6c127cf398 70 plan = new lora::ChannelPlan_EU868();
nguyenhoang9x5555 0:6a6c127cf398 71 #elif CHANNEL_PLAN == CP_KR920
nguyenhoang9x5555 0:6a6c127cf398 72 plan = new lora::ChannelPlan_KR920();
nguyenhoang9x5555 0:6a6c127cf398 73 #elif CHANNEL_PLAN == CP_AS923
nguyenhoang9x5555 0:6a6c127cf398 74 plan = new lora::ChannelPlan_AS923();
nguyenhoang9x5555 0:6a6c127cf398 75 #elif CHANNEL_PLAN == CP_AS923_JAPAN
nguyenhoang9x5555 0:6a6c127cf398 76 plan = new lora::ChannelPlan_AS923_Japan();
nguyenhoang9x5555 0:6a6c127cf398 77 #elif CHANNEL_PLAN == CP_IN865
nguyenhoang9x5555 0:6a6c127cf398 78 plan = new lora::ChannelPlan_IN865();
nguyenhoang9x5555 0:6a6c127cf398 79 #endif
nguyenhoang9x5555 0:6a6c127cf398 80 assert(plan);
nguyenhoang9x5555 0:6a6c127cf398 81
nguyenhoang9x5555 0:6a6c127cf398 82 dot = mDot::getInstance(plan);
nguyenhoang9x5555 0:6a6c127cf398 83 assert(dot);
nguyenhoang9x5555 0:6a6c127cf398 84
nguyenhoang9x5555 0:6a6c127cf398 85 // attach the custom events handler
nguyenhoang9x5555 0:6a6c127cf398 86 dot->setEvents(&events);
nguyenhoang9x5555 0:6a6c127cf398 87
nguyenhoang9x5555 0:6a6c127cf398 88 if (!dot->getStandbyFlag()) {
nguyenhoang9x5555 0:6a6c127cf398 89 logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
nguyenhoang9x5555 0:6a6c127cf398 90
nguyenhoang9x5555 0:6a6c127cf398 91 // start from a well-known state
nguyenhoang9x5555 0:6a6c127cf398 92 logInfo("defaulting Dot configuration");
nguyenhoang9x5555 0:6a6c127cf398 93 dot->resetConfig();
nguyenhoang9x5555 0:6a6c127cf398 94 dot->resetNetworkSession();
nguyenhoang9x5555 0:6a6c127cf398 95
nguyenhoang9x5555 0:6a6c127cf398 96 // make sure library logging is turned on
nguyenhoang9x5555 0:6a6c127cf398 97 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
nguyenhoang9x5555 0:6a6c127cf398 98
nguyenhoang9x5555 0:6a6c127cf398 99 // update configuration if necessary
nguyenhoang9x5555 0:6a6c127cf398 100 // in AUTO_OTA mode the session is automatically saved, so saveNetworkSession and restoreNetworkSession are not needed
nguyenhoang9x5555 0:6a6c127cf398 101 if (dot->getJoinMode() != mDot::AUTO_OTA) {
nguyenhoang9x5555 0:6a6c127cf398 102 logInfo("changing network join mode to AUTO_OTA");
nguyenhoang9x5555 0:6a6c127cf398 103 if (dot->setJoinMode(mDot::AUTO_OTA) != mDot::MDOT_OK) {
nguyenhoang9x5555 0:6a6c127cf398 104 logError("failed to set network join mode to AUTO_OTA");
nguyenhoang9x5555 0:6a6c127cf398 105 }
nguyenhoang9x5555 0:6a6c127cf398 106 }
nguyenhoang9x5555 0:6a6c127cf398 107 // in OTA and AUTO_OTA join modes, the credentials can be passed to the library as a name and passphrase or an ID and KEY
nguyenhoang9x5555 0:6a6c127cf398 108 // only one method or the other should be used!
nguyenhoang9x5555 0:6a6c127cf398 109 // network ID = crc64(network name)
nguyenhoang9x5555 0:6a6c127cf398 110 // network KEY = cmac(network passphrase)
nguyenhoang9x5555 0:6a6c127cf398 111 update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, public_network, ack);
nguyenhoang9x5555 0:6a6c127cf398 112 //update_ota_config_id_key(network_id, network_key, frequency_sub_band, public_network, ack);
nguyenhoang9x5555 0:6a6c127cf398 113
nguyenhoang9x5555 0:6a6c127cf398 114 // configure network link checks
nguyenhoang9x5555 0:6a6c127cf398 115 // network link checks are a good alternative to requiring the gateway to ACK every packet and should allow a single gateway to handle more Dots
nguyenhoang9x5555 0:6a6c127cf398 116 // check the link every count packets
nguyenhoang9x5555 0:6a6c127cf398 117 // declare the Dot disconnected after threshold failed link checks
nguyenhoang9x5555 0:6a6c127cf398 118 // for count = 3 and threshold = 5, the Dot will ask for a link check response every 5 packets and will consider the connection lost if it fails to receive 3 responses in a row
nguyenhoang9x5555 0:6a6c127cf398 119 update_network_link_check_config(3, 5);
nguyenhoang9x5555 0:6a6c127cf398 120
nguyenhoang9x5555 0:6a6c127cf398 121 // enable or disable Adaptive Data Rate
nguyenhoang9x5555 0:6a6c127cf398 122 dot->setAdr(adr);
nguyenhoang9x5555 0:6a6c127cf398 123
nguyenhoang9x5555 0:6a6c127cf398 124 // Configure the join delay
nguyenhoang9x5555 0:6a6c127cf398 125 dot->setJoinDelay(join_delay);
nguyenhoang9x5555 0:6a6c127cf398 126
nguyenhoang9x5555 0:6a6c127cf398 127 // save changes to configuration
nguyenhoang9x5555 0:6a6c127cf398 128 logInfo("saving configuration");
nguyenhoang9x5555 0:6a6c127cf398 129 if (!dot->saveConfig()) {
nguyenhoang9x5555 0:6a6c127cf398 130 logError("failed to save configuration");
nguyenhoang9x5555 0:6a6c127cf398 131 }
nguyenhoang9x5555 0:6a6c127cf398 132
nguyenhoang9x5555 0:6a6c127cf398 133 // display configuration
nguyenhoang9x5555 0:6a6c127cf398 134 display_config();
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 // join network if not joined
nguyenhoang9x5555 0:6a6c127cf398 142 if (!dot->getNetworkJoinStatus()) {
nguyenhoang9x5555 0:6a6c127cf398 143 join_network();
nguyenhoang9x5555 0:6a6c127cf398 144 }
nguyenhoang9x5555 0:6a6c127cf398 145
nguyenhoang9x5555 0:6a6c127cf398 146 #if defined(TARGET_XDOT_L151CC)
nguyenhoang9x5555 0:6a6c127cf398 147 // configure the ISL29011 sensor on the xDot-DK for continuous ambient light sampling, 16 bit conversion, and maximum range
nguyenhoang9x5555 0:6a6c127cf398 148 lux.setMode(ISL29011::ALS_CONT);
nguyenhoang9x5555 0:6a6c127cf398 149 lux.setResolution(ISL29011::ADC_16BIT);
nguyenhoang9x5555 0:6a6c127cf398 150 lux.setRange(ISL29011::RNG_64000);
nguyenhoang9x5555 0:6a6c127cf398 151
nguyenhoang9x5555 0:6a6c127cf398 152 // get the latest light sample and send it to the gateway
nguyenhoang9x5555 0:6a6c127cf398 153 light = lux.getData();
nguyenhoang9x5555 0:6a6c127cf398 154 tx_data.push_back((light >> 8) & 0xFF);
nguyenhoang9x5555 0:6a6c127cf398 155 tx_data.push_back(light & 0xFF);
nguyenhoang9x5555 0:6a6c127cf398 156 logInfo("light: %lu [0x%04X]", light, light);
nguyenhoang9x5555 0:6a6c127cf398 157 send_data(tx_data);
nguyenhoang9x5555 0:6a6c127cf398 158
nguyenhoang9x5555 0:6a6c127cf398 159 // put the LSL29011 ambient light sensor into a low power state
nguyenhoang9x5555 0:6a6c127cf398 160 lux.setMode(ISL29011::PWR_DOWN);
nguyenhoang9x5555 0:6a6c127cf398 161 #else
nguyenhoang9x5555 0:6a6c127cf398 162 // get some dummy data and send it to the gateway
nguyenhoang9x5555 0:6a6c127cf398 163 light = lux.read_u16();
nguyenhoang9x5555 0:6a6c127cf398 164 tx_data.push_back((light >> 8) & 0xFF);
nguyenhoang9x5555 0:6a6c127cf398 165 tx_data.push_back(light & 0xFF);
nguyenhoang9x5555 0:6a6c127cf398 166 logInfo("light: %lu [0x%04X]", light, light);
nguyenhoang9x5555 0:6a6c127cf398 167 send_data(tx_data);
nguyenhoang9x5555 0:6a6c127cf398 168 #endif
nguyenhoang9x5555 0:6a6c127cf398 169
nguyenhoang9x5555 0:6a6c127cf398 170 // ONLY ONE of the three functions below should be uncommented depending on the desired wakeup method
nguyenhoang9x5555 0:6a6c127cf398 171 //sleep_wake_rtc_only(deep_sleep);
nguyenhoang9x5555 0:6a6c127cf398 172 //sleep_wake_interrupt_only(deep_sleep);
nguyenhoang9x5555 0:6a6c127cf398 173 sleep_wake_rtc_or_interrupt(deep_sleep);
nguyenhoang9x5555 0:6a6c127cf398 174 }
nguyenhoang9x5555 0:6a6c127cf398 175
nguyenhoang9x5555 0:6a6c127cf398 176 return 0;
nguyenhoang9x5555 0:6a6c127cf398 177 }
nguyenhoang9x5555 0:6a6c127cf398 178
nguyenhoang9x5555 0:6a6c127cf398 179 #endif