a Hello world to connect to The Things Network
Dependencies: libmDot mbed-rtos mbed
Fork of mDot_LoRa_Connect_Example by
Diff: main.cpp
- Revision:
- 2:6e2c378339d9
- Parent:
- 0:09250cd371d2
- Child:
- 4:36e214ebfa56
--- a/main.cpp Tue Aug 18 15:37:05 2015 +0000 +++ b/main.cpp Tue Aug 25 21:35:39 2015 +0000 @@ -2,48 +2,60 @@ #include "mDot.h" #include <string> #include <vector> +#include "MTSLog.h" -// these options must match the settings on your Conduit in -// /var/config/lora/lora-network-server.conf -static std::string config_network_name = "<lora network id>"; -static std::string config_network_pass = "<lora network key>"; -static uint8_t config_frequency_sub_band = 1; - -void log_error(mDot* dot, const char* msg, int32_t retval); +// these options must match the settings on your Conduit +// uncomment the following lines and edit their values to match your configuration +//static std::string config_network_name = "<lora network id>"; +//static std::string config_network_pass = "<lora network key>"; +//static uint8_t config_frequency_sub_band = 1; int main() { int32_t ret; mDot* dot; std::vector<uint8_t> data; std::string data_str = "hello world!"; - + // get a mDot handle dot = mDot::getInstance(); + + // print library version information + logInfo("version: %s", dot->getId().c_str()); + //******************************************* + // configuration + //******************************************* // reset to default config so we know what state we're in dot->resetConfig(); - // print library version information - printf("version: %s\r\n", dot->getId().c_str()); + dot->setLogLevel(mts::MTSLog::TRACE_LEVEL); - // set up the mDot with our network information - printf("setting frequency sub band\r\n"); + // set up the mDot with our network information: frequency sub band, network name, and network password + // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig() + logInfo("setting frequency sub band"); if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { - log_error(dot, "failed to set frequency sub band", ret); + logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); + } + logInfo("setting network name"); + if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { + logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); } - printf("setting network name\r\n"); - if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { - log_error(dot, "failed to set network name", ret); + logInfo("setting network password"); + if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { + logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); } - printf("setting network password\r\n"); - if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { - log_error(dot, "failed to set network password", ret); + logInfo("saving config"); + if (! dot->saveConfig()) { + logError("failed to save configuration"); } + //******************************************* + // end of configuration + //******************************************* // attempt to join the network - printf("joining network\r\n"); + logInfo("joining network"); while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { - log_error(dot, "failed to join network", ret); + logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); wait(2); } @@ -55,9 +67,9 @@ // send the data // ACKs are enabled by default, so we're expecting to get one back if ((ret = dot->send(data)) != mDot::MDOT_OK) { - log_error(dot, "failed to send", ret); + logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str()); } else { - printf("successfully sent data to gateway\r\n"); + logInfo("successfully sent data to gateway"); } wait(5); @@ -65,7 +77,3 @@ return 0; } - -void log_error(mDot* dot, const char* msg, int32_t retval) { - printf("%s - %ld:%s, %s\r\n", msg, retval, mDot::getReturnCodeString(retval).c_str(), dot->getLastError().c_str()); -} \ No newline at end of file