Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: libxDot-mbed5 TSL2561
Fork of Dot-Examples by
ota_example.cpp
00001 #include "dot_util.h" 00002 #include "RadioEvent.h" 00003 #include "TSL2561.h" 00004 00005 00006 #if ACTIVE_EXAMPLE == OTA_EXAMPLE 00007 00008 ///////////////////////////////////////////////////////////////////////////// 00009 // -------------------- DOT LIBRARY REQUIRED ------------------------------// 00010 // * Because these example programs can be used for both mDot and xDot // 00011 // devices, the LoRa stack is not included. The libmDot library should // 00012 // be imported if building for mDot devices. The libxDot library // 00013 // should be imported if building for xDot devices. // 00014 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-dev-mbed5/ // 00015 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-mbed5/ // 00016 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-dev-mbed5/ // 00017 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-mbed5/ // 00018 ///////////////////////////////////////////////////////////////////////////// 00019 00020 ///////////////////////////////////////////////////////////// 00021 // * these options must match the settings on your gateway // 00022 // * edit their values to match your configuration // 00023 // * frequency sub band is only relevant for the 915 bands // 00024 // * either the network name and passphrase can be used or // 00025 // the network ID (8 bytes) and KEY (16 bytes) // 00026 ///////////////////////////////////////////////////////////// 00027 00028 // wifi gateway 00029 static std::string network_name = "MTCDT-19400691"; 00030 static std::string network_passphrase = "MTCDT-19400691"; 00031 00032 // cellular gateway 00033 //static std::string network_name = "iuiot-gw1"; 00034 //static std::string network_passphrase = "pivotiot"; 00035 00036 00037 static uint8_t network_id[] = { 0x6C, 0x4E, 0xEF, 0x66, 0xF4, 0x79, 0x86, 0xA6 }; 00038 static uint8_t network_key[] = { 0x1F, 0x33, 0xA1, 0x70, 0xA5, 0xF1, 0xFD, 0xA0, 0xAB, 0x69, 0x7A, 0xAE, 0x2B, 0x95, 0x91, 0x6B }; 00039 static uint8_t frequency_sub_band = 1; 00040 static lora::NetworkType public_network;// = lora::PUBLIC_LORAWAN; 00041 static uint8_t join_delay = 1; 00042 static uint8_t ack = 0; 00043 static bool adr = true; 00044 00045 // deepsleep consumes slightly less current than sleep 00046 // in sleep mode, IO state is maintained, RAM is retained, and application will resume after waking up 00047 // in deepsleep mode, IOs float, RAM is lost, and application will start from beginning after waking up 00048 // if deep_sleep == true, device will enter deepsleep mode 00049 static bool deep_sleep = false; 00050 static const uint16_t HOURLY_SLEEP_INTERVAL = 5; // 1 hour *** changed for demo 1 min 00051 static const uint16_t DAILY_SLEEP_INTERVAL = 43200; // 12 hours 00052 00053 mDot* dot = NULL; 00054 lora::ChannelPlan* plan = NULL; 00055 00056 DigitalOut led1(LED1); 00057 Serial pc(PA_9, PA_10); 00058 TSL2561 lightSensor(PB_9, PB_8); 00059 00060 int main() { 00061 pc.baud(115200); 00062 pc.printf("main started \r\n"); 00063 // Custom event handler for automatically displaying RX data 00064 RadioEvent events; 00065 00066 00067 ///////////////////////////// 00068 // visual display 00069 led1 = 0; 00070 for(int i = 0; i < 5; i++) { 00071 led1 = 1; 00072 wait(1); 00073 led1 = 0; 00074 wait(1); 00075 } 00076 00077 // set gain on light sensor 00078 lightSensor.set_timing_reg(TIMING_GAIN_16); 00079 00080 00081 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); 00082 00083 #if CHANNEL_PLAN == CP_US915 00084 plan = new lora::ChannelPlan_US915(); 00085 #elif CHANNEL_PLAN == CP_AU915 00086 plan = new lora::ChannelPlan_AU915(); 00087 #elif CHANNEL_PLAN == CP_EU868 00088 plan = new lora::ChannelPlan_EU868(); 00089 #elif CHANNEL_PLAN == CP_KR920 00090 plan = new lora::ChannelPlan_KR920(); 00091 #elif CHANNEL_PLAN == CP_AS923 00092 plan = new lora::ChannelPlan_AS923(); 00093 #elif CHANNEL_PLAN == CP_AS923_JAPAN 00094 plan = new lora::ChannelPlan_AS923_Japan(); 00095 #elif CHANNEL_PLAN == CP_IN865 00096 plan = new lora::ChannelPlan_IN865(); 00097 #endif 00098 assert(plan); 00099 00100 dot = mDot::getInstance(plan); 00101 assert(dot); 00102 00103 00104 // attach the custom events handler 00105 dot->setEvents(&events); 00106 00107 if (!dot->getStandbyFlag()) { 00108 logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION); 00109 00110 // start from a well-known state 00111 logInfo("defaulting Dot configuration"); 00112 dot->resetConfig(); 00113 dot->resetNetworkSession(); 00114 00115 // set the data_rate 00116 dot->setTxDataRate(lora::DR_1); 00117 00118 // make sure library logging is turned on 00119 dot->setLogLevel(mts::MTSLog::INFO_LEVEL); 00120 00121 // update configuration if necessary 00122 if (dot->getJoinMode() != mDot::OTA) { 00123 logInfo("changing network join mode to OTA"); 00124 if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) { 00125 logError("failed to set network join mode to OTA"); 00126 } 00127 } 00128 // 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 00129 // only one method or the other should be used! 00130 // network ID = crc64(network name) 00131 // network KEY = cmac(network passphrase) 00132 update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, public_network, ack); 00133 //update_ota_config_id_key(network_id, network_key, frequency_sub_band, public_network, ack); 00134 00135 // configure network link checks 00136 // 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 00137 // check the link every count packets 00138 // declare the Dot disconnected after threshold failed link checks 00139 // 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 00140 update_network_link_check_config(3, 5); 00141 00142 // enable or disable Adaptive Data Rate 00143 dot->setAdr(adr); 00144 00145 // Configure the join delay 00146 dot->setJoinDelay(join_delay); 00147 00148 pc.printf("network information set\r\n"); 00149 00150 // save changes to configuration 00151 logInfo("saving configuration"); 00152 if (!dot->saveConfig()) { 00153 logError("failed to save configuration"); 00154 } 00155 00156 // display configuration 00157 display_config(); 00158 } else { 00159 // restore the saved session if the dot woke from deepsleep mode 00160 // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep 00161 logInfo("restoring network session from NVM"); 00162 dot->restoreNetworkSession(); 00163 } 00164 00165 uint8_t counter = 0; 00166 std::vector<uint8_t> tx_data; 00167 char buffer[5]; 00168 00169 while (true) { 00170 00171 // join network if not joined 00172 if (!dot->getNetworkJoinStatus()) { 00173 pc.printf("joining lora network.\r\n"); 00174 join_network(&pc); 00175 } 00176 00177 00178 // hourly loop 00179 while( counter < 12) { 00180 00181 uint8_t light_data = get_light_data(&lightSensor, &pc); 00182 //pc.printf("pseudo-light data: %d\r\n", light_data); 00183 pc.printf("\r\nhour: %d\r\n", ++counter); 00184 00185 00186 // clean out char buffer 00187 strcpy(buffer, "XXXX"); 00188 00189 // convert to ascii char && push to vector 00190 ascii_converter(buffer, light_data, &tx_data, &pc); 00191 pc.printf("char buffer: %s\r\n", buffer); 00192 00193 if( counter < 12) 00194 tx_data.push_back(' '); 00195 00196 00197 // if going into deepsleep mode, save the session so we don't need to join again after waking up 00198 // not necessary if going into sleep mode since RAM is retained 00199 if (deep_sleep) { 00200 logInfo("saving network session to NVM"); 00201 dot->saveNetworkSession(); 00202 } 00203 00204 // ONLY ONE of the three functions below should be uncommented depending on the desired wakeup method 00205 sleep_wake_rtc_only(deep_sleep, HOURLY_SLEEP_INTERVAL, &pc); 00206 //sleep_wake_interrupt_only(deep_sleep); 00207 //sleep_wake_rtc_or_interrupt(deep_sleep); 00208 } 00209 00210 //print vector data 00211 pc.printf("vector data:"); 00212 00213 for(std::vector<uint8_t>::const_iterator iter = tx_data.begin(); iter != tx_data.end(); ++iter) { 00214 pc.printf(" %c",*iter); 00215 } 00216 pc.printf("\r\n"); 00217 00218 pc.printf("capacity tx_data: %d\r\n", tx_data.capacity()); 00219 pc.printf("sized tx_data: %d\r\n", tx_data.size()); 00220 //actually send the data 00221 pc.printf("sending data\r\n"); 00222 00223 00224 // set the data_rate (from forum) 00225 dot->setTxDataRate(lora::DR_1); 00226 int retVal = send_data(tx_data, &pc); 00227 //while(retVal == 1) 00228 //retVal = send_data(tx_data, &pc); 00229 00230 // erase vector data & reset counter 00231 tx_data.erase(tx_data.begin(), tx_data.end()); 00232 counter = 0; 00233 pc.printf("deleted tx_data and reset counter\r\ncap: %d size: %d\r\n", tx_data.capacity(), tx_data.size()); 00234 00235 // sleep for the day *** don't want to sleep for 12 hours for the demo 00236 //sleep_wake_rtc_only(deep_sleep, DAILY_SLEEP_INTERVAL, &pc); 00237 00238 } 00239 00240 return 0; 00241 } 00242 00243 #endif
Generated on Tue Jul 12 2022 18:10:22 by
