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