Minh NN / Mbed 2 deprecated mDot_LoRa_Sleep_Example

Dependencies:   libmDot mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "mDot.h"
00003 #include "MTSLog.h"
00004 #include <string>
00005 #include <vector>
00006 #include <algorithm>
00007 
00008 // these options must match the settings on your Conduit
00009 // uncomment the following lines and edit their values to match your configuration
00010 static std::string config_network_name = "wonjun123";
00011 static std::string config_network_pass = "123123123";
00012 static uint8_t config_frequency_sub_band = 7;
00013 
00014 //             r0   r1   r2   r3   c0   c1   c2   c3
00015 //Keypad keypad(p17, p16, p15, p14, p13, p12, p11, p10);
00016 
00017 float speed = 5.1;  // km/h
00018 bool isIncreasing = false;
00019 bool isDecreasing = false;
00020 
00021 // Callback funciton of speed increasing button pressing
00022 void cbIncreaseSpeed()
00023 {
00024     speed = speed + 0.1f;
00025     if (speed > 19) speed = 19;
00026 }
00027 // Callback funciton of speed decreasing button pressing
00028 void cbDecreaseSpeed()
00029 {
00030     speed = speed - 0.1f;
00031     if (speed < 0 ) speed = 0;
00032 }
00033 
00034 int main() {
00035     int32_t ret;
00036     mDot* dot;
00037     //std::vector<uint8_t> data;
00038     std::string data_str = "data!";
00039     
00040     InterruptIn incBtn(PA_2);    // increase speed button
00041     InterruptIn decBtn(PA_0);    // decrease speed button
00042 
00043     decBtn.rise(&cbIncreaseSpeed);
00044     //decBtn.fall(&cbDecreaseSpeed);
00045     
00046     // get a mDot handle
00047     dot = mDot::getInstance();
00048     
00049     // print library version information
00050     logError("dafdfadafdfdaf");
00051     logInfo("version: %s", dot->getId().c_str());
00052 
00053     //*******************************************
00054     // configuration
00055     //*******************************************
00056     // reset to default config so we know what state we're in
00057     dot->resetConfig();
00058     
00059     dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
00060 
00061     // set up the mDot with our network information: frequency sub band, network name, and network password
00062     // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig()
00063     
00064     // frequency sub band is only applicable in the 915 (US) frequency band
00065     // if using a MultiTech Conduit gateway, use the same sub band as your Conduit (1-8) - the mDot will use the 8 channels in that sub band
00066     // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels
00067     logInfo("setting frequency sub band");
00068     if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
00069         logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
00070     }
00071     
00072     logInfo("setting network name");
00073     if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
00074         logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
00075     }
00076     
00077     logInfo("setting network password");
00078     if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
00079         logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
00080     }
00081     
00082     // a higher spreading factor allows for longer range but lower throughput
00083     // in the 915 (US) frequency band, spreading factors 7 - 10 are available
00084     // in the 868 (EU) frequency band, spreading factors 7 - 12 are available
00085     logInfo("setting TX spreading factor");
00086     if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) {
00087         logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
00088     }
00089     
00090     // request receive confirmation of packets from the gateway
00091     logInfo("enabling ACKs");
00092     if ((ret = dot->setAck(1)) != mDot::MDOT_OK) {
00093         logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
00094     }
00095     
00096     // set join mode to AUTO_OTA so the mDot doesn't have to rejoin after sleeping
00097     logInfo("setting join mode to AUTO_OTA");
00098     if ((ret = dot->setJoinMode(mDot::AUTO_OTA)) != mDot::MDOT_OK) {
00099         logError("failed to set join mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
00100     }
00101     
00102     // save this configuration to the mDot's NVM
00103     logInfo("saving config");
00104     if (! dot->saveConfig()) {
00105         logError("failed to save configuration");
00106     }
00107     //*******************************************
00108     // end of configuration
00109     //*******************************************
00110 
00111     // format data for sending to the gateway
00112     //for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++)
00113         //data.push_back((uint8_t) *it);
00114 
00115     // join the network if not joined
00116     if (!dot->getNetworkJoinStatus()) {
00117         logInfo("network not joined, joining network");
00118         if ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
00119             logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
00120         }
00121     }
00122     
00123     while (true) {
00124         std::vector<uint8_t> data;
00125         
00126         float tmpSpeed = speed;
00127         if (tmpSpeed >= 10) {  // 10 - 19 km/h
00128             data.push_back('1');
00129             tmpSpeed -= 10;
00130         }
00131         tmpSpeed *= 10;
00132         data.push_back((int)(tmpSpeed) / 10 + '0');
00133         data.push_back('.');
00134         data.push_back((int)tmpSpeed % 10 + '0');
00135         
00136         if (dot->getNetworkJoinStatus()) {
00137             // send the data
00138             // ACKs are enabled by default, so we're expecting to get one back
00139             if ((ret = dot->send(data)) != mDot::MDOT_OK) {
00140                 logError("failed to send %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
00141             } else {
00142                 logInfo("successfully sent data to gateway");
00143             }
00144         }
00145         //osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
00146         
00147         // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
00148         uint32_t sleep_time = std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()) / 1000;
00149         //logInfo("going to sleep...");
00150     
00151         // go to deepsleep and wake up automatically sleep_time seconds later
00152         dot->sleep(sleep_time, mDot::INTERRUPT);
00153         
00154         //wait(sleep_time);
00155     }
00156 
00157     return 0;
00158 }