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