Frederick Huang / Mbed OS LD100-Examples-201907

Dependencies:   libmDot-mbed5 ISL29011

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers class_b_example.cpp Source File

class_b_example.cpp

00001 #include "dot_util.h"
00002 #include "RadioEvent.h"
00003  
00004 #if ACTIVE_EXAMPLE == CLASS_B_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 = 1;
00030 static lora::NetworkType public_network = lora::PUBLIC_LORAWAN;
00031 static uint8_t join_delay = 5;
00032 static uint8_t ack = 3;
00033 static bool adr = true;
00034 
00035 // Number of ping slots to open per beacon interval - see mDot.h
00036 static uint8_t ping_periodicity = 4;
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     logInfo("mbed-os library version: %d.%d.%d", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
00083 
00084     // start from a well-known state
00085     logInfo("defaulting Dot configuration");
00086     dot->resetConfig();
00087     dot->resetNetworkSession();
00088 
00089     // make sure library logging is turned on
00090     dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
00091 
00092     // attach the custom events handler
00093     dot->setEvents(&events);
00094 
00095     // update configuration if necessary
00096     if (dot->getJoinMode() != mDot::OTA) {
00097         logInfo("changing network join mode to OTA");
00098         if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) {
00099             logError("failed to set network join mode to OTA");
00100         }
00101     }
00102     // 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
00103     // only one method or the other should be used!
00104     // network ID = crc64(network name)
00105     // network KEY = cmac(network passphrase)
00106     update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, public_network, ack);
00107     //update_ota_config_id_key(network_id, network_key, frequency_sub_band, public_network, ack);
00108 
00109     // enable or disable Adaptive Data Rate
00110     dot->setAdr(adr);
00111 
00112     // Configure the join delay
00113     dot->setJoinDelay(join_delay);
00114 
00115     // Configure the class B ping periodicity
00116     dot->setPingPeriodicity(ping_periodicity);
00117 
00118     // save changes to configuration
00119     logInfo("saving configuration");
00120     if (!dot->saveConfig()) {
00121         logError("failed to save configuration");
00122     }
00123 
00124     // display configuration
00125     display_config();
00126 
00127     // join the network - must do this before attempting to switch to class B
00128     join_network();
00129 
00130     // configure the Dot for class B operation
00131     // the Dot must also be configured on the gateway for class B
00132     // use the lora-query application to do this on a Conduit: http://www.multitech.net/developer/software/lora/lora-network-server/
00133     // to provision your Dot for class B operation with a 3rd party gateway, see the gateway or network provider documentation
00134     // Note: we won't actually switch to class B until we receive a beacon (mDotEvent::BeaconRx fires)
00135     logInfo("changing network mode to class B");
00136 
00137     if (dot->setClass("B") != mDot::MDOT_OK) {
00138         logError("Failed to set network mode to class B");
00139         logInfo("Reset the MCU to try again");
00140         return 0;
00141     }
00142 
00143     // Start a timer to check the beacon was acquired
00144     LowPowerTimer bcn_timer;
00145     bcn_timer.start();
00146 
00147     while (true) {
00148         std::vector<uint8_t> tx_data;
00149         static bool send_uplink = true;
00150 
00151         // Check if we locked the beacon yet and send an uplink to notify the network server
00152         // To receive data from the gateway in class B ping slots, we must have received a beacon
00153         // already, and sent one uplink to signal to the network server that we are in class B mode
00154         if (events.BeaconLocked && send_uplink) {
00155             logInfo("Acquired a beacon lock");
00156 
00157             // Add a random delay before trying the uplink to avoid collisions w/ other motes
00158             srand(dot->getRadioRandom());
00159             uint32_t rand_delay = rand() % 5000;
00160             logInfo("Applying a random delay of %d ms before class notification uplink", rand_delay);
00161             osDelay(rand_delay);
00162 
00163             // Ensure the link is idle before trying to transmit
00164             while (!dot->getIsIdle()) {
00165                 osDelay(10);
00166             }
00167 
00168             if (send_data(tx_data) != mDot::MDOT_OK) {
00169                 logError("Failed to inform the network server we are in class B");
00170                 logInfo("Reset the MCU to try again");
00171                 return 0;
00172             }
00173 
00174             logInfo("Enqueued packets may now be scheduled on class B ping slots");
00175             send_uplink = false;
00176             bcn_timer.stop();
00177         } else if (!events.BeaconLocked) {
00178             logInfo("Waiting to receive a beacon..");
00179 
00180             if (bcn_timer.read() > lora::DEFAULT_BEACON_PERIOD) {
00181                 if (dot->setClass("B") != mDot::MDOT_OK) {
00182                     logError("Failed to set network mode to class B");
00183                     logInfo("Reset the MCU to try again");
00184                     return 0;
00185                 }
00186 
00187                 bcn_timer.reset();
00188             }
00189         }
00190 
00191         Thread::wait(10000);
00192     }
00193 
00194     return 0;
00195 }
00196 
00197 #endif