Echo example of class C devices receiving downlink and sending as next uplink

Dependencies:   libmDot-mbed5 ISL29011

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers manual_example.cpp Source File

manual_example.cpp

00001 #include "dot_util.h"
00002 #include "RadioEvent.h"
00003  
00004 #if ACTIVE_EXAMPLE == MANUAL_EXAMPLE
00005 
00006 /////////////////////////////////////////////////////////////////////////////
00007 // -------------------- DOT LIBRARY REQUIRED ------------------------------//
00008 // * Because these example programs can be used for both mDot and xDot     //
00009 //     devices, the LoRa stack is not included. The libmDot library should //
00010 //     be imported if building for mDot devices. The libxDot library       //
00011 //     should be imported if building for xDot devices.                    //
00012 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-dev-mbed5/    //
00013 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-mbed5/        //
00014 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-dev-mbed5/    //
00015 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-mbed5/        //
00016 /////////////////////////////////////////////////////////////////////////////
00017 
00018 /////////////////////////////////////////////////////////////
00019 // * these options must match the settings on your gateway //
00020 // * edit their values to match your configuration         //
00021 // * frequency sub band is only relevant for the 915 bands //
00022 /////////////////////////////////////////////////////////////
00023 static uint8_t network_address[] = { 0x01, 0x02, 0x03, 0x04 };
00024 static uint8_t network_session_key[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 };
00025 static uint8_t data_session_key[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 };
00026 static uint8_t frequency_sub_band = 6;
00027 static lora::NetworkType network_type = lora::PUBLIC_LORAWAN;
00028 static uint8_t join_delay = 5;
00029 static uint8_t ack = 1;
00030 static bool adr = true;
00031 
00032 // deepsleep consumes slightly less current than sleep
00033 // in sleep mode, IO state is maintained, RAM is retained, and application will resume after waking up
00034 // in deepsleep mode, IOs float, RAM is lost, and application will start from beginning after waking up
00035 // if deep_sleep == true, device will enter deepsleep mode
00036 static bool deep_sleep = true;
00037 
00038 mDot* dot = NULL;
00039 lora::ChannelPlan* plan = NULL;
00040 
00041 Serial pc(USBTX, USBRX);
00042 
00043 #if defined(TARGET_XDOT_L151CC)
00044 I2C i2c(I2C_SDA, I2C_SCL);
00045 ISL29011 lux(i2c);
00046 #else
00047 AnalogIn lux(XBEE_AD0);
00048 #endif
00049 
00050 int main() {
00051     // Custom event handler for automatically displaying RX data
00052     RadioEvent events;
00053 
00054     pc.baud(115200);
00055 
00056 #if defined(TARGET_XDOT_L151CC)
00057     i2c.frequency(400000);
00058 #endif
00059 
00060     mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
00061     
00062 #if CHANNEL_PLAN == CP_US915
00063     plan = new lora::ChannelPlan_US915();
00064 #elif CHANNEL_PLAN == CP_AU915
00065     plan = new lora::ChannelPlan_AU915();
00066 #elif CHANNEL_PLAN == CP_EU868
00067     plan = new lora::ChannelPlan_EU868();
00068 #elif CHANNEL_PLAN == CP_KR920
00069     plan = new lora::ChannelPlan_KR920();
00070 #elif CHANNEL_PLAN == CP_AS923
00071     plan = new lora::ChannelPlan_AS923();
00072 #elif CHANNEL_PLAN == CP_AS923_JAPAN
00073     plan = new lora::ChannelPlan_AS923_Japan();
00074 #elif CHANNEL_PLAN == CP_IN865
00075     plan = new lora::ChannelPlan_IN865();
00076 #endif
00077     assert(plan);
00078 
00079     dot = mDot::getInstance(plan);
00080     assert(dot);
00081 
00082     // attach the custom events handler
00083     dot->setEvents(&events);
00084 
00085     if (!dot->getStandbyFlag()) {
00086         logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
00087 
00088         // start from a well-known state
00089         logInfo("defaulting Dot configuration");
00090         dot->resetConfig();
00091         dot->resetNetworkSession();
00092 
00093         // make sure library logging is turned on
00094         dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
00095 
00096         // update configuration if necessary
00097         if (dot->getJoinMode() != mDot::MANUAL) {
00098             logInfo("changing network join mode to MANUAL");
00099             if (dot->setJoinMode(mDot::MANUAL) != mDot::MDOT_OK) {
00100                 logError("failed to set network join mode to MANUAL");
00101             }
00102         }
00103         // in MANUAL join mode there is no join request/response transaction
00104         // as long as the Dot is configured correctly and provisioned correctly on the gateway, it should be able to communicate
00105         // network address - 4 bytes (00000001 - FFFFFFFE)
00106         // network session key - 16 bytes
00107         // data session key - 16 bytes
00108         // to provision your Dot with a Conduit gateway, follow the following steps
00109         //   * ssh into the Conduit
00110         //   * provision the Dot using the lora-query application: http://www.multitech.net/developer/software/lora/lora-network-server/
00111         //      lora-query -a 01020304 A 0102030401020304 <your Dot's device ID> 01020304010203040102030401020304 01020304010203040102030401020304
00112         //   * if you change the network address, network session key, or data session key, make sure you update them on the gateway
00113         // to provision your Dot with a 3rd party gateway, see the gateway or network provider documentation
00114         update_manual_config(network_address, network_session_key, data_session_key, frequency_sub_band, network_type, ack);
00115 
00116         // enable or disable Adaptive Data Rate
00117         dot->setAdr(adr);
00118 
00119         // Configure the join delay
00120         dot->setJoinDelay(join_delay);
00121 
00122         // save changes to configuration
00123         logInfo("saving configuration");
00124         if (!dot->saveConfig()) {
00125             logError("failed to save configuration");
00126         }
00127 
00128         // display configuration
00129         display_config();
00130     } else {
00131         // restore the saved session if the dot woke from deepsleep mode
00132         // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep
00133         logInfo("restoring network session from NVM");
00134         dot->restoreNetworkSession();
00135     }
00136     
00137     while (true) {
00138         uint16_t light;
00139         std::vector<uint8_t> tx_data;
00140 
00141 #if defined(TARGET_XDOT_L151CC)
00142         // configure the ISL29011 sensor on the xDot-DK for continuous ambient light sampling, 16 bit conversion, and maximum range
00143         lux.setMode(ISL29011::ALS_CONT);
00144         lux.setResolution(ISL29011::ADC_16BIT);
00145         lux.setRange(ISL29011::RNG_64000);
00146 
00147         // get the latest light sample and send it to the gateway
00148         light = lux.getData();
00149         tx_data.push_back((light >> 8) & 0xFF);
00150         tx_data.push_back(light & 0xFF);
00151         logInfo("light: %lu [0x%04X]", light, light);
00152         send_data(tx_data);
00153 
00154         // put the LSL29011 ambient light sensor into a low power state
00155         lux.setMode(ISL29011::PWR_DOWN);
00156 #else 
00157         // get some dummy data and send it to the gateway
00158         light = lux.read_u16();
00159         tx_data.push_back((light >> 8) & 0xFF);
00160         tx_data.push_back(light & 0xFF);
00161         logInfo("light: %lu [0x%04X]", light, light);
00162         send_data(tx_data);
00163 #endif
00164 
00165         // if going into deepsleep mode, save the session so we don't need to join again after waking up
00166         // not necessary if going into sleep mode since RAM is retained
00167         if (deep_sleep) {
00168             logInfo("saving network session to NVM");
00169             dot->saveNetworkSession();
00170         }
00171 
00172         // ONLY ONE of the three functions below should be uncommented depending on the desired wakeup method
00173         //sleep_wake_rtc_only(deep_sleep);
00174         //sleep_wake_interrupt_only(deep_sleep);
00175         sleep_wake_rtc_or_interrupt(deep_sleep);
00176     }
00177  
00178     return 0;
00179 }
00180 
00181 #endif