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@5:6b988a804fcb, 2016-03-29 (annotated)
- Committer:
- jreiss
- Date:
- Tue Mar 29 13:41:12 2016 +0000
- Revision:
- 5:6b988a804fcb
- Parent:
- 4:36e214ebfa56
- Child:
- 6:ac306e26e4cc
Modified mDot connect example for ABPA join
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 | 4:36e214ebfa56 | 3 | #include "MTSLog.h" |
mfiore | 0:09250cd371d2 | 4 | #include <string> |
mfiore | 0:09250cd371d2 | 5 | #include <vector> |
mfiore | 4:36e214ebfa56 | 6 | #include <algorithm> |
mfiore | 0:09250cd371d2 | 7 | |
mfiore | 2:6e2c378339d9 | 8 | // these options must match the settings on your Conduit |
mfiore | 2:6e2c378339d9 | 9 | // uncomment the following lines and edit their values to match your configuration |
jreiss | 5:6b988a804fcb | 10 | static uint8_t config_network_addr[] = { 0x01, 0x02, 0x03, 0x04 }; |
jreiss | 5:6b988a804fcb | 11 | static uint8_t config_network_nskey[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 }; |
jreiss | 5:6b988a804fcb | 12 | static uint8_t config_network_dskey[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 }; |
jreiss | 5:6b988a804fcb | 13 | static uint8_t config_frequency_sub_band = 1; |
mfiore | 0:09250cd371d2 | 14 | |
mfiore | 0:09250cd371d2 | 15 | int main() { |
mfiore | 0:09250cd371d2 | 16 | int32_t ret; |
mfiore | 0:09250cd371d2 | 17 | mDot* dot; |
mfiore | 0:09250cd371d2 | 18 | std::vector<uint8_t> data; |
mfiore | 4:36e214ebfa56 | 19 | std::string data_str = "hello!"; |
mfiore | 2:6e2c378339d9 | 20 | |
mfiore | 0:09250cd371d2 | 21 | // get a mDot handle |
mfiore | 0:09250cd371d2 | 22 | dot = mDot::getInstance(); |
mfiore | 2:6e2c378339d9 | 23 | |
mfiore | 2:6e2c378339d9 | 24 | // print library version information |
mfiore | 2:6e2c378339d9 | 25 | logInfo("version: %s", dot->getId().c_str()); |
mfiore | 0:09250cd371d2 | 26 | |
mfiore | 2:6e2c378339d9 | 27 | //******************************************* |
mfiore | 2:6e2c378339d9 | 28 | // configuration |
mfiore | 2:6e2c378339d9 | 29 | //******************************************* |
mfiore | 0:09250cd371d2 | 30 | // reset to default config so we know what state we're in |
mfiore | 0:09250cd371d2 | 31 | dot->resetConfig(); |
mfiore | 0:09250cd371d2 | 32 | |
mfiore | 4:36e214ebfa56 | 33 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
mfiore | 0:09250cd371d2 | 34 | |
mfiore | 2:6e2c378339d9 | 35 | // set up the mDot with our network information: frequency sub band, network name, and network password |
mfiore | 2:6e2c378339d9 | 36 | // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig() |
mfiore | 4:36e214ebfa56 | 37 | |
mfiore | 4:36e214ebfa56 | 38 | // frequency sub band is only applicable in the 915 (US) frequency band |
mfiore | 4:36e214ebfa56 | 39 | // 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 |
mfiore | 4:36e214ebfa56 | 40 | // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels |
mfiore | 2:6e2c378339d9 | 41 | logInfo("setting frequency sub band"); |
mfiore | 0:09250cd371d2 | 42 | if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 43 | logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 2:6e2c378339d9 | 44 | } |
mfiore | 4:36e214ebfa56 | 45 | |
jreiss | 5:6b988a804fcb | 46 | std::vector<uint8_t> temp; |
jreiss | 5:6b988a804fcb | 47 | |
jreiss | 5:6b988a804fcb | 48 | for (int i = 0; i < 4; i++) { |
jreiss | 5:6b988a804fcb | 49 | temp.push_back(config_network_addr[i]); |
jreiss | 5:6b988a804fcb | 50 | } |
jreiss | 5:6b988a804fcb | 51 | |
jreiss | 5:6b988a804fcb | 52 | logInfo("setting network addr"); |
jreiss | 5:6b988a804fcb | 53 | if ((ret = dot->setNetworkAddress(temp)) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 54 | logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:09250cd371d2 | 55 | } |
mfiore | 4:36e214ebfa56 | 56 | |
jreiss | 5:6b988a804fcb | 57 | temp.clear(); |
jreiss | 5:6b988a804fcb | 58 | for (int i = 0; i < 16; i++) { |
jreiss | 5:6b988a804fcb | 59 | temp.push_back(config_network_nskey[i]); |
jreiss | 5:6b988a804fcb | 60 | } |
jreiss | 5:6b988a804fcb | 61 | |
mfiore | 2:6e2c378339d9 | 62 | logInfo("setting network password"); |
jreiss | 5:6b988a804fcb | 63 | if ((ret = dot->setNetworkSessionKey(temp)) != mDot::MDOT_OK) { |
jreiss | 5:6b988a804fcb | 64 | logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
jreiss | 5:6b988a804fcb | 65 | } |
jreiss | 5:6b988a804fcb | 66 | |
jreiss | 5:6b988a804fcb | 67 | temp.clear(); |
jreiss | 5:6b988a804fcb | 68 | for (int i = 0; i < 16; i++) { |
jreiss | 5:6b988a804fcb | 69 | temp.push_back(config_network_dskey[i]); |
jreiss | 5:6b988a804fcb | 70 | } |
jreiss | 5:6b988a804fcb | 71 | |
jreiss | 5:6b988a804fcb | 72 | logInfo("setting network password"); |
jreiss | 5:6b988a804fcb | 73 | if ((ret = dot->setDataSessionKey(temp)) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 74 | logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:09250cd371d2 | 75 | } |
mfiore | 4:36e214ebfa56 | 76 | |
mfiore | 4:36e214ebfa56 | 77 | // a higher spreading factor allows for longer range but lower throughput |
mfiore | 4:36e214ebfa56 | 78 | // in the 915 (US) frequency band, spreading factors 7 - 10 are available |
mfiore | 4:36e214ebfa56 | 79 | // in the 868 (EU) frequency band, spreading factors 7 - 12 are available |
mfiore | 4:36e214ebfa56 | 80 | logInfo("setting TX spreading factor"); |
mfiore | 4:36e214ebfa56 | 81 | if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) { |
mfiore | 4:36e214ebfa56 | 82 | logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 4:36e214ebfa56 | 83 | } |
mfiore | 4:36e214ebfa56 | 84 | |
mfiore | 4:36e214ebfa56 | 85 | // request receive confirmation of packets from the gateway |
mfiore | 4:36e214ebfa56 | 86 | logInfo("enabling ACKs"); |
mfiore | 4:36e214ebfa56 | 87 | if ((ret = dot->setAck(1)) != mDot::MDOT_OK) { |
mfiore | 4:36e214ebfa56 | 88 | logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 4:36e214ebfa56 | 89 | } |
mfiore | 4:36e214ebfa56 | 90 | |
mfiore | 4:36e214ebfa56 | 91 | // save this configuration to the mDot's NVM |
mfiore | 2:6e2c378339d9 | 92 | logInfo("saving config"); |
mfiore | 2:6e2c378339d9 | 93 | if (! dot->saveConfig()) { |
mfiore | 2:6e2c378339d9 | 94 | logError("failed to save configuration"); |
mfiore | 0:09250cd371d2 | 95 | } |
mfiore | 2:6e2c378339d9 | 96 | //******************************************* |
mfiore | 2:6e2c378339d9 | 97 | // end of configuration |
mfiore | 2:6e2c378339d9 | 98 | //******************************************* |
mfiore | 0:09250cd371d2 | 99 | |
mfiore | 0:09250cd371d2 | 100 | // attempt to join the network |
mfiore | 2:6e2c378339d9 | 101 | logInfo("joining network"); |
mfiore | 0:09250cd371d2 | 102 | while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 103 | logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 4:36e214ebfa56 | 104 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
mfiore | 4:36e214ebfa56 | 105 | osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs())); |
mfiore | 0:09250cd371d2 | 106 | } |
mfiore | 0:09250cd371d2 | 107 | |
mfiore | 0:09250cd371d2 | 108 | // format data for sending to the gateway |
mfiore | 0:09250cd371d2 | 109 | for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++) |
mfiore | 0:09250cd371d2 | 110 | data.push_back((uint8_t) *it); |
mfiore | 0:09250cd371d2 | 111 | |
mfiore | 0:09250cd371d2 | 112 | while (true) { |
mfiore | 4:36e214ebfa56 | 113 | // send the data to the gateway |
mfiore | 0:09250cd371d2 | 114 | if ((ret = dot->send(data)) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 115 | logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:09250cd371d2 | 116 | } else { |
mfiore | 2:6e2c378339d9 | 117 | logInfo("successfully sent data to gateway"); |
mfiore | 0:09250cd371d2 | 118 | } |
mfiore | 0:09250cd371d2 | 119 | |
mfiore | 4:36e214ebfa56 | 120 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
mfiore | 4:36e214ebfa56 | 121 | osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs())); |
mfiore | 0:09250cd371d2 | 122 | } |
mfiore | 0:09250cd371d2 | 123 | |
mfiore | 0:09250cd371d2 | 124 | return 0; |
mfiore | 0:09250cd371d2 | 125 | } |