Lucas Vates / Mbed OS Dot-Examples-online

Dependencies:   libmDot-mbed5 ISL29011

Fork of Dot-Examples by MultiTech

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 bool public_network = false;
00031 static uint8_t ack = 1;
00032 static bool adr = true;
00033 
00034 mDot* dot = NULL;
00035 lora::ChannelPlan* plan = NULL;
00036 
00037 Serial pc(USBTX, USBRX);
00038 
00039 #if defined(TARGET_XDOT_L151CC)
00040 I2C i2c(I2C_SDA, I2C_SCL);
00041 ISL29011 lux(i2c);
00042 #else
00043 AnalogIn lux(XBEE_AD0);
00044 #endif
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     logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
00075 
00076     // start from a well-known state
00077     logInfo("defaulting Dot configuration");
00078     dot->resetConfig();
00079     dot->resetNetworkSession();
00080 
00081         // make sure library logging is turned on
00082     dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
00083 
00084     // attach the custom events handler
00085     dot->setEvents(&events);
00086 
00087     // update configuration if necessary
00088     if (dot->getJoinMode() != mDot::OTA) {
00089         logInfo("changing network join mode to OTA");
00090         if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) {
00091             logError("failed to set network join mode to OTA");
00092         }
00093     }
00094     // 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
00095     // only one method or the other should be used!
00096     // network ID = crc64(network name)
00097     // network KEY = cmac(network passphrase)
00098     update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, public_network, ack);
00099     //update_ota_config_id_key(network_id, network_key, frequency_sub_band, public_network, ack);
00100 
00101     // configure the Dot for class C operation
00102     // the Dot must also be configured on the gateway for class C
00103     // use the lora-query application to do this on a Conduit: http://www.multitech.net/developer/software/lora/lora-network-server/
00104     // to provision your Dot for class C operation with a 3rd party gateway, see the gateway or network provider documentation
00105     logInfo("changing network mode to class C");
00106     if (dot->setClass("C") != mDot::MDOT_OK) {
00107         logError("failed to set network mode to class C");
00108     }
00109 
00110     // enable or disable Adaptive Data Rate
00111     dot->setAdr(adr);
00112     
00113     // save changes to configuration
00114     logInfo("saving configuration");
00115     if (!dot->saveConfig()) {
00116         logError("failed to save configuration");
00117     }
00118 
00119     // display configuration
00120     display_config();
00121 
00122 #if defined(TARGET_XDOT_L151CC)
00123     // configure the ISL29011 sensor on the xDot-DK for continuous ambient light sampling, 16 bit conversion, and maximum range
00124     lux.setMode(ISL29011::ALS_CONT);
00125     lux.setResolution(ISL29011::ADC_16BIT);
00126     lux.setRange(ISL29011::RNG_64000);
00127 #endif 
00128 
00129     while (true) {
00130         uint16_t light;
00131         std::vector<uint8_t> tx_data;
00132 
00133         // join network if not joined
00134         if (!dot->getNetworkJoinStatus()) {
00135             join_network();
00136         }
00137 
00138 #if defined(TARGET_XDOT_L151CC)
00139         // get the latest light sample and send it to the gateway
00140         light = lux.getData();
00141         tx_data.push_back((light >> 8) & 0xFF);
00142         tx_data.push_back(light & 0xFF);
00143         logInfo("light: %lu [0x%04X]", light, light);
00144         send_data(tx_data);
00145 #else 
00146         // get some dummy data and send it to the gateway
00147         light = lux.read_u16();
00148         tx_data.push_back((light >> 8) & 0xFF);
00149         tx_data.push_back(light & 0xFF);
00150         logInfo("light: %lu [0x%04X]", light, light);
00151         send_data(tx_data);
00152 #endif
00153 
00154         // the Dot can't sleep in class C mode
00155         // it must be waiting for data from the gateway
00156         // send data every 30s
00157         logInfo("waiting for 30s");
00158         wait(30);
00159     }
00160  
00161     return 0;
00162 }
00163 
00164 #endif
00165