Modified Connect Example showing how to set the APP-EUI and APP-KEY
Dependencies: libmDot mbed-rtos mbed
Fork of mDot_LoRa_Connect_Example_ABPA by
main.cpp@2:6e2c378339d9, 2015-08-25 (annotated)
- Committer:
- mfiore
- Date:
- Tue Aug 25 21:35:39 2015 +0000
- Revision:
- 2:6e2c378339d9
- Parent:
- 0:09250cd371d2
- Child:
- 4:36e214ebfa56
clean up code, add more comments about configuration
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfiore | 0:09250cd371d2 | 1 | #include "mbed.h" |
mfiore | 0:09250cd371d2 | 2 | #include "mDot.h" |
mfiore | 0:09250cd371d2 | 3 | #include <string> |
mfiore | 0:09250cd371d2 | 4 | #include <vector> |
mfiore | 2:6e2c378339d9 | 5 | #include "MTSLog.h" |
mfiore | 0:09250cd371d2 | 6 | |
mfiore | 2:6e2c378339d9 | 7 | // these options must match the settings on your Conduit |
mfiore | 2:6e2c378339d9 | 8 | // uncomment the following lines and edit their values to match your configuration |
mfiore | 2:6e2c378339d9 | 9 | //static std::string config_network_name = "<lora network id>"; |
mfiore | 2:6e2c378339d9 | 10 | //static std::string config_network_pass = "<lora network key>"; |
mfiore | 2:6e2c378339d9 | 11 | //static uint8_t config_frequency_sub_band = 1; |
mfiore | 0:09250cd371d2 | 12 | |
mfiore | 0:09250cd371d2 | 13 | int main() { |
mfiore | 0:09250cd371d2 | 14 | int32_t ret; |
mfiore | 0:09250cd371d2 | 15 | mDot* dot; |
mfiore | 0:09250cd371d2 | 16 | std::vector<uint8_t> data; |
mfiore | 0:09250cd371d2 | 17 | std::string data_str = "hello world!"; |
mfiore | 2:6e2c378339d9 | 18 | |
mfiore | 0:09250cd371d2 | 19 | // get a mDot handle |
mfiore | 0:09250cd371d2 | 20 | dot = mDot::getInstance(); |
mfiore | 2:6e2c378339d9 | 21 | |
mfiore | 2:6e2c378339d9 | 22 | // print library version information |
mfiore | 2:6e2c378339d9 | 23 | logInfo("version: %s", dot->getId().c_str()); |
mfiore | 0:09250cd371d2 | 24 | |
mfiore | 2:6e2c378339d9 | 25 | //******************************************* |
mfiore | 2:6e2c378339d9 | 26 | // configuration |
mfiore | 2:6e2c378339d9 | 27 | //******************************************* |
mfiore | 0:09250cd371d2 | 28 | // reset to default config so we know what state we're in |
mfiore | 0:09250cd371d2 | 29 | dot->resetConfig(); |
mfiore | 0:09250cd371d2 | 30 | |
mfiore | 2:6e2c378339d9 | 31 | dot->setLogLevel(mts::MTSLog::TRACE_LEVEL); |
mfiore | 0:09250cd371d2 | 32 | |
mfiore | 2:6e2c378339d9 | 33 | // set up the mDot with our network information: frequency sub band, network name, and network password |
mfiore | 2:6e2c378339d9 | 34 | // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig() |
mfiore | 2:6e2c378339d9 | 35 | logInfo("setting frequency sub band"); |
mfiore | 0:09250cd371d2 | 36 | if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 37 | logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 2:6e2c378339d9 | 38 | } |
mfiore | 2:6e2c378339d9 | 39 | logInfo("setting network name"); |
mfiore | 2:6e2c378339d9 | 40 | if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 41 | logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:09250cd371d2 | 42 | } |
mfiore | 2:6e2c378339d9 | 43 | logInfo("setting network password"); |
mfiore | 2:6e2c378339d9 | 44 | if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 45 | logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:09250cd371d2 | 46 | } |
mfiore | 2:6e2c378339d9 | 47 | logInfo("saving config"); |
mfiore | 2:6e2c378339d9 | 48 | if (! dot->saveConfig()) { |
mfiore | 2:6e2c378339d9 | 49 | logError("failed to save configuration"); |
mfiore | 0:09250cd371d2 | 50 | } |
mfiore | 2:6e2c378339d9 | 51 | //******************************************* |
mfiore | 2:6e2c378339d9 | 52 | // end of configuration |
mfiore | 2:6e2c378339d9 | 53 | //******************************************* |
mfiore | 0:09250cd371d2 | 54 | |
mfiore | 0:09250cd371d2 | 55 | // attempt to join the network |
mfiore | 2:6e2c378339d9 | 56 | logInfo("joining network"); |
mfiore | 0:09250cd371d2 | 57 | while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 58 | logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:09250cd371d2 | 59 | wait(2); |
mfiore | 0:09250cd371d2 | 60 | } |
mfiore | 0:09250cd371d2 | 61 | |
mfiore | 0:09250cd371d2 | 62 | // format data for sending to the gateway |
mfiore | 0:09250cd371d2 | 63 | for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++) |
mfiore | 0:09250cd371d2 | 64 | data.push_back((uint8_t) *it); |
mfiore | 0:09250cd371d2 | 65 | |
mfiore | 0:09250cd371d2 | 66 | while (true) { |
mfiore | 0:09250cd371d2 | 67 | // send the data |
mfiore | 0:09250cd371d2 | 68 | // ACKs are enabled by default, so we're expecting to get one back |
mfiore | 0:09250cd371d2 | 69 | if ((ret = dot->send(data)) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 70 | logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:09250cd371d2 | 71 | } else { |
mfiore | 2:6e2c378339d9 | 72 | logInfo("successfully sent data to gateway"); |
mfiore | 0:09250cd371d2 | 73 | } |
mfiore | 0:09250cd371d2 | 74 | |
mfiore | 0:09250cd371d2 | 75 | wait(5); |
mfiore | 0:09250cd371d2 | 76 | } |
mfiore | 0:09250cd371d2 | 77 | |
mfiore | 0:09250cd371d2 | 78 | return 0; |
mfiore | 0:09250cd371d2 | 79 | } |