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