Connecting to gateway and sending packets with a MultiTech mDot.
Dependencies: libmDot mbed-rtos mbed
Fork of libmDot_sample by
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 = "<lora network id>"; 00011 static std::string config_network_pass = "<lora network key>"; 00012 static uint8_t config_frequency_sub_band = 1; 00013 00014 int main() { 00015 int32_t ret; 00016 mDot* dot; 00017 std::vector<uint8_t> data; 00018 std::string data_str = "hello!"; 00019 00020 // get a mDot handle 00021 dot = mDot::getInstance(); 00022 00023 // print library version information 00024 logInfo("version: %s", dot->getId().c_str()); 00025 00026 //******************************************* 00027 // configuration 00028 //******************************************* 00029 // reset to default config so we know what state we're in 00030 dot->resetConfig(); 00031 00032 dot->setLogLevel(mts::MTSLog::INFO_LEVEL); 00033 00034 // set up the mDot with our network information: frequency sub band, network name, and network password 00035 // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig() 00036 00037 // frequency sub band is only applicable in the 915 (US) frequency band 00038 // 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 00039 // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels 00040 00041 // logInfo("setting to public network"); 00042 // if ((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) { 00043 // logError("failed to set public network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); 00044 // } 00045 00046 logInfo("setting frequency sub band"); 00047 if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { 00048 logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); 00049 } 00050 00051 logInfo("setting network name"); 00052 if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { 00053 logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); 00054 } 00055 00056 logInfo("setting network password"); 00057 if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { 00058 logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); 00059 } 00060 00061 // a higher spreading factor allows for longer range but lower throughput 00062 // in the 915 (US) frequency band, spreading factors 7 - 10 are available 00063 // in the 868 (EU) frequency band, spreading factors 7 - 12 are available 00064 logInfo("setting TX spreading factor"); 00065 if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) { 00066 logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); 00067 } 00068 00069 // request receive confirmation of packets from the gateway 00070 logInfo("enabling ACKs"); 00071 if ((ret = dot->setAck(1)) != mDot::MDOT_OK) { 00072 logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); 00073 } 00074 00075 // save this configuration to the mDot's NVM 00076 logInfo("saving config"); 00077 if (! dot->saveConfig()) { 00078 logError("failed to save configuration"); 00079 } 00080 //******************************************* 00081 // end of configuration 00082 //******************************************* 00083 00084 // attempt to join the network 00085 logInfo("joining network"); 00086 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { 00087 logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); 00088 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again 00089 osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs())); 00090 } 00091 00092 // format data for sending to the gateway 00093 for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++) 00094 data.push_back((uint8_t) *it); 00095 00096 while (true) { 00097 // send the data to the gateway 00098 if ((ret = dot->send(data)) != mDot::MDOT_OK) { 00099 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str()); 00100 } else { 00101 logInfo("successfully sent data to gateway"); 00102 } 00103 00104 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again 00105 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs())); 00106 } 00107 00108 return 0; 00109 }
Generated on Wed Jul 13 2022 13:05:38 by 1.7.2