Example programs for MultiTech Dot devices demonstrating how to use the Dot devices and the Dot libraries for LoRa communication.

Dependencies:   ISL29011

Dependents:   Dot-Examples-delujoc

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers class_c_example.cpp Source File

class_c_example.cpp

00001 #include "dot_util.h"
00002 #include "RadioEvent.h"
00003  
00004 #if ACTIVE_EXAMPLE == CLASS_C_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 network_type = lora::PUBLIC_LORAWAN;
00031 static uint8_t join_delay = 5;
00032 static uint8_t ack = 1;
00033 static bool adr = true;
00034 
00035 mDot* dot = NULL;
00036 lora::ChannelPlan* plan = NULL;
00037 
00038 mbed::UnbufferedSerial pc(USBTX, USBRX);
00039 
00040 #if defined(TARGET_XDOT_L151CC)
00041 I2C i2c(I2C_SDA, I2C_SCL);
00042 ISL29011 lux(i2c);
00043 #else
00044 AnalogIn lux(XBEE_AD0);
00045 #endif
00046 
00047 int main() {
00048     // Custom event handler for automatically displaying RX data
00049     RadioEvent events;
00050 
00051     pc.baud(115200);
00052 
00053 #if defined(TARGET_XDOT_L151CC)
00054     i2c.frequency(400000);
00055 #endif
00056 
00057     mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
00058 
00059     // Create channel plan
00060     plan = create_channel_plan();
00061     assert(plan);
00062 
00063     dot = mDot::getInstance(plan);
00064     assert(dot);
00065 
00066     logInfo("mbed-os library version: %d.%d.%d", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
00067 
00068     // start from a well-known state
00069     logInfo("defaulting Dot configuration");
00070     dot->resetConfig();
00071     dot->resetNetworkSession();
00072 
00073         // make sure library logging is turned on
00074     dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
00075 
00076     // attach the custom events handler
00077     dot->setEvents(&events);
00078 
00079     // update configuration if necessary
00080     if (dot->getJoinMode() != mDot::OTA) {
00081         logInfo("changing network join mode to OTA");
00082         if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) {
00083             logError("failed to set network join mode to OTA");
00084         }
00085     }
00086     // 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
00087     // only one method or the other should be used!
00088     // network ID = crc64(network name)
00089     // network KEY = cmac(network passphrase)
00090     update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, network_type, ack);
00091     //update_ota_config_id_key(network_id, network_key, frequency_sub_band, network_type, ack);
00092 
00093     // configure the Dot for class C operation
00094     // the Dot must also be configured on the gateway for class C
00095     // use the lora-query application to do this on a Conduit: http://www.multitech.net/developer/software/lora/lora-network-server/
00096     // to provision your Dot for class C operation with a 3rd party gateway, see the gateway or network provider documentation
00097     logInfo("changing network mode to class C");
00098     if (dot->setClass("C") != mDot::MDOT_OK) {
00099         logError("failed to set network mode to class C");
00100     }
00101 
00102     // enable or disable Adaptive Data Rate
00103     dot->setAdr(adr);
00104 
00105     // Configure the join delay
00106     dot->setJoinDelay(join_delay);
00107 
00108     // save changes to configuration
00109     logInfo("saving configuration");
00110     if (!dot->saveConfig()) {
00111         logError("failed to save configuration");
00112     }
00113 
00114     // display configuration
00115     display_config();
00116 
00117     while (true) {
00118         uint16_t light;
00119         std::vector<uint8_t> tx_data;
00120 
00121         // join network if not joined
00122         if (!dot->getNetworkJoinStatus()) {
00123             join_network();
00124         }
00125 
00126 #if defined(TARGET_XDOT_L151CC)
00127         // configure the ISL29011 sensor on the xDot-DK for continuous ambient light sampling, 16 bit conversion, and maximum range
00128         lux.setMode(ISL29011::ALS_CONT);
00129         lux.setResolution(ISL29011::ADC_16BIT);
00130         lux.setRange(ISL29011::RNG_64000);
00131 
00132         // get the latest light sample and send it to the gateway
00133         light = lux.getData();
00134         tx_data.push_back((light >> 8) & 0xFF);
00135         tx_data.push_back(light & 0xFF);
00136         logInfo("light: %lu [0x%04X]", light, light);
00137         send_data(tx_data);
00138 
00139         // put the LSL29011 ambient light sensor into a low power state
00140         lux.setMode(ISL29011::PWR_DOWN);
00141 #else 
00142         // get some dummy data and send it to the gateway
00143         light = lux.read_u16();
00144         tx_data.push_back((light >> 8) & 0xFF);
00145         tx_data.push_back(light & 0xFF);
00146         logInfo("light: %lu [0x%04X]", light, light);
00147         send_data(tx_data);
00148 #endif
00149 
00150         // the Dot can't sleep in class C mode
00151         // it must be waiting for data from the gateway
00152         // send data every 30s
00153         logInfo("waiting for 30s");
00154         ThisThread::sleep_for(30s);
00155     }
00156  
00157     return 0;
00158 }
00159 
00160 #endif
00161