initial version / sends time + 012345689

Dependencies:   libmDot mbed-rtos mbed

Fork of wotiolora by Scott Tudd

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 = "wotioloranetwork";
00011 static std::string config_network_pass = "<PASSWORD HERE>";
00012 static uint8_t config_frequency_sub_band = 7;
00013 
00014 
00015 #define UDK2 1
00016 
00017 #ifdef UDK2
00018 DigitalOut led(LED1);
00019 #else
00020 DigitalOut led(XBEE_RSSI);
00021 #endif
00022 
00023 Ticker tick;
00024 
00025 // callback function to change LED state
00026 void blink() {
00027     led = !led;
00028 }
00029 
00030 
00031 int main() {
00032     int32_t ret;
00033     mDot* dot;
00034     
00035     std::string data_str = "1234567890";
00036 
00037    
00038     // get a mDot handle
00039     dot = mDot::getInstance();
00040     
00041     // print library version information
00042     logInfo("version: %s", dot->getId().c_str());
00043 
00044     //*******************************************
00045     // configuration
00046     //*******************************************
00047     // reset to default config so we know what state we're in
00048     dot->resetConfig();
00049     
00050     dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
00051 
00052     // set up the mDot with our network information: frequency sub band, network name, and network password
00053     // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig()
00054     
00055     // frequency sub band is only applicable in the 915 (US) frequency band
00056     // 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
00057     // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels
00058     logInfo("setting frequency sub band");
00059     if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
00060         logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
00061     }
00062     
00063     logInfo("setting network name");
00064     if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
00065         logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
00066     }
00067     
00068     logInfo("setting network password");
00069     if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
00070         logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
00071     }
00072     
00073     // a higher spreading factor allows for longer range but lower throughput
00074     // in the 915 (US) frequency band, spreading factors 7 - 10 are available
00075     // in the 868 (EU) frequency band, spreading factors 7 - 12 are available
00076     logInfo("setting TX spreading factor");
00077     if ((ret = dot->setTxDataRate(mDot::SF_8)) != mDot::MDOT_OK) {
00078         logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
00079     }
00080     
00081     // request receive confirmation of packets from the gateway
00082     logInfo("enabling ACKs");
00083     if ((ret = dot->setAck(1)) != mDot::MDOT_OK) {
00084         logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
00085     }
00086     
00087     // save this configuration to the mDot's NVM
00088     logInfo("saving config");
00089     if (! dot->saveConfig()) {
00090         logError("failed to save configuration");
00091     }
00092     //*******************************************
00093     // end of configuration
00094     //*******************************************
00095 
00096     // attempt to join the network
00097     logInfo("joining network");
00098     while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
00099         logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
00100         // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
00101         osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs()));
00102     }
00103 
00104     while (true) {
00105         // empty data array before compiling next message
00106         std::vector<uint8_t> data;
00107         
00108         time_t seconds = time(NULL);
00109         
00110         char buffer[32];
00111         strftime(buffer, 32, "%Y-%m-%d %H:%M:%S: ", localtime(&seconds));
00112         for (int i = 0; i < strlen(buffer); i++) {
00113             data.push_back((uint8_t) buffer[i]);
00114         }      
00115 
00116         // format data for sending to the gateway
00117         for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++)
00118             data.push_back((uint8_t) *it);
00119 
00120         // send the data to the gateway
00121         if ((ret = dot->send(data)) != mDot::MDOT_OK) {
00122             logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
00123         } else {
00124             // configure the Ticker to blink the LED on 1sec interval
00125             tick.attach(&blink, 1.0);
00126             logInfo("successfully sent data to gateway");
00127         }
00128 
00129         // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
00130         osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
00131     }
00132  
00133     return 0;
00134 }