Bryce Himebaugh / Mbed OS xdot_reference

Dependencies:   libxDot-mbed5 TSL2561

Fork of Dot-Examples by MultiTech

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers auto_ota_example.cpp Source File

auto_ota_example.cpp

00001 #include "dot_util.h"
00002 #include "RadioEvent.h"
00003  
00004 #if ACTIVE_EXAMPLE == AUTO_OTA_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 // * either the network name and passphrase can be used or //
00023 //     the network ID (8 bytes) and KEY (16 bytes)         //
00024 /////////////////////////////////////////////////////////////
00025 static std::string network_name = "MultiTech";
00026 static std::string network_passphrase = "MultiTech";
00027 static uint8_t network_id[] = { 0x6C, 0x4E, 0xEF, 0x66, 0xF4, 0x79, 0x86, 0xA6 };
00028 static uint8_t network_key[] = { 0x1F, 0x33, 0xA1, 0x70, 0xA5, 0xF1, 0xFD, 0xA0, 0xAB, 0x69, 0x7A, 0xAE, 0x2B, 0x95, 0x91, 0x6B };
00029 static uint8_t frequency_sub_band = 0;
00030 static lora::NetworkType public_network = lora::PUBLIC_LORAWAN;
00031 static uint8_t join_delay = 5;
00032 static uint8_t ack = 0;
00033 static bool adr = true;
00034 
00035 // deepsleep consumes slightly less current than sleep
00036 // in sleep mode, IO state is maintained, RAM is retained, and application will resume after waking up
00037 // in deepsleep mode, IOs float, RAM is lost, and application will start from beginning after waking up
00038 // if deep_sleep == true, device will enter deepsleep mode
00039 static bool deep_sleep = true;
00040 
00041 mDot* dot = NULL;
00042 lora::ChannelPlan* plan = NULL;
00043 
00044 Serial pc(USBTX, USBRX);
00045 
00046 int main() {
00047     // Custom event handler for automatically displaying RX data
00048     RadioEvent events;
00049 
00050     pc.baud(115200);
00051 
00052     mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
00053     
00054 #if CHANNEL_PLAN == CP_US915
00055     plan = new lora::ChannelPlan_US915();
00056 #elif CHANNEL_PLAN == CP_AU915
00057     plan = new lora::ChannelPlan_AU915();
00058 #elif CHANNEL_PLAN == CP_EU868
00059     plan = new lora::ChannelPlan_EU868();
00060 #elif CHANNEL_PLAN == CP_KR920
00061     plan = new lora::ChannelPlan_KR920();
00062 #elif CHANNEL_PLAN == CP_AS923
00063     plan = new lora::ChannelPlan_AS923();
00064 #elif CHANNEL_PLAN == CP_AS923_JAPAN
00065     plan = new lora::ChannelPlan_AS923_Japan();
00066 #elif CHANNEL_PLAN == CP_IN865
00067     plan = new lora::ChannelPlan_IN865();
00068 #endif
00069     assert(plan);
00070 
00071     dot = mDot::getInstance(plan);
00072     assert(dot);
00073 
00074     // attach the custom events handler
00075     dot->setEvents(&events);
00076 
00077     if (!dot->getStandbyFlag()) {
00078         logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
00079 
00080         // start from a well-known state
00081         logInfo("defaulting Dot configuration");
00082         dot->resetConfig();
00083         dot->resetNetworkSession();
00084 
00085         // make sure library logging is turned on
00086         dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
00087 
00088         // update configuration if necessary
00089         // in AUTO_OTA mode the session is automatically saved, so saveNetworkSession and restoreNetworkSession are not needed
00090         if (dot->getJoinMode() != mDot::AUTO_OTA) {
00091             logInfo("changing network join mode to AUTO_OTA");
00092             if (dot->setJoinMode(mDot::AUTO_OTA) != mDot::MDOT_OK) {
00093                 logError("failed to set network join mode to AUTO_OTA");
00094             }
00095         }
00096         // 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
00097         // only one method or the other should be used!
00098         // network ID = crc64(network name)
00099         // network KEY = cmac(network passphrase)
00100         update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, public_network, ack);
00101         //update_ota_config_id_key(network_id, network_key, frequency_sub_band, public_network, ack);
00102     
00103         // configure network link checks
00104         // 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
00105         // check the link every count packets
00106         // declare the Dot disconnected after threshold failed link checks
00107         // 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
00108         update_network_link_check_config(3, 5);
00109 
00110         // enable or disable Adaptive Data Rate
00111         dot->setAdr(adr);
00112 
00113         // Configure the join delay
00114         dot->setJoinDelay(join_delay);
00115     
00116         // save changes to configuration
00117         logInfo("saving configuration");
00118         if (!dot->saveConfig()) {
00119             logError("failed to save configuration");
00120         }
00121     
00122         // display configuration
00123         display_config();
00124     }
00125 
00126     uint8_t counter = 0;
00127     while (true) {
00128         std::vector<uint8_t> tx_data;
00129 
00130         // join network if not joined
00131         if (!dot->getNetworkJoinStatus()) {
00132             join_network();
00133         }
00134 
00135         tx_data.push_back(++counter);
00136         logInfo("sending uplink with data = %d", counter);
00137         send_data(tx_data);
00138 
00139         // ONLY ONE of the three functions below should be uncommented depending on the desired wakeup method
00140         //sleep_wake_rtc_only(deep_sleep);
00141         //sleep_wake_interrupt_only(deep_sleep);
00142         sleep_wake_rtc_or_interrupt(deep_sleep);
00143     }
00144  
00145     return 0;
00146 }
00147 
00148 #endif