mDot ABP base
Dependencies: libmDot mbed-rtos mbed
Fork of mDot_LoRa_Connect_Example_ABPA by
main.cpp@8:d2c85894f26c, 2016-06-16 (annotated)
- Committer:
- ludot
- Date:
- Thu Jun 16 21:40:59 2016 +0000
- Revision:
- 8:d2c85894f26c
- Parent:
- 7:978cdd1cede8
modified sketch
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 | |
ludot | 8:d2c85894f26c | 8 | static uint8_t devAddr[] = { <insert DevAddr> }; //for example: { 0x44, 0x08, 0xCC, 0x9A }; |
ludot | 8:d2c85894f26c | 9 | static uint8_t nwkSKey[] = { <insert nwkSKey> }; //for example: { 0x99, 0xC8, 0xA0, 0x4C, 0x50, 0x25, 0x94, 0xD2, 0xEB, 0xFD, 0xD9, 0x37, 0x56, 0x3C, 0x83, 0x32 }; |
ludot | 8:d2c85894f26c | 10 | static uint8_t appSKey[] = { <insert appSKey> }; //for example: { 0x07, 0xA5, 0x9B, 0xAD, 0x11, 0x0B, 0x94, 0x6E, 0xEE, 0xE5, 0xF0, 0x87, 0xD0, 0x10, 0x35, 0x07 }; |
ludot | 8:d2c85894f26c | 11 | |
ludot | 7:978cdd1cede8 | 12 | static uint8_t config_frequency_sub_band = 7; |
mfiore | 0:09250cd371d2 | 13 | |
ludot | 8:d2c85894f26c | 14 | mDot* dot; |
ludot | 8:d2c85894f26c | 15 | |
ludot | 8:d2c85894f26c | 16 | void setupNetwork(); |
ludot | 8:d2c85894f26c | 17 | |
mfiore | 0:09250cd371d2 | 18 | int main() { |
ludot | 8:d2c85894f26c | 19 | |
ludot | 8:d2c85894f26c | 20 | Serial pc(USBTX, USBRX); |
ludot | 8:d2c85894f26c | 21 | // Set up the network |
ludot | 8:d2c85894f26c | 22 | setupNetwork(); |
ludot | 8:d2c85894f26c | 23 | |
ludot | 8:d2c85894f26c | 24 | // Message you want to send |
ludot | 8:d2c85894f26c | 25 | std::string data_str = "Hello!"; |
ludot | 8:d2c85894f26c | 26 | |
ludot | 8:d2c85894f26c | 27 | // Copy the message in an array of bytes |
ludot | 8:d2c85894f26c | 28 | std::vector<uint8_t> data; |
ludot | 8:d2c85894f26c | 29 | for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++) |
ludot | 8:d2c85894f26c | 30 | data.push_back((uint8_t) *it); |
ludot | 8:d2c85894f26c | 31 | |
ludot | 8:d2c85894f26c | 32 | // Start the loop |
mfiore | 0:09250cd371d2 | 33 | int32_t ret; |
ludot | 8:d2c85894f26c | 34 | while (true) { |
ludot | 8:d2c85894f26c | 35 | // Send the data |
ludot | 8:d2c85894f26c | 36 | if ((ret = dot->send(data)) != mDot::MDOT_OK) { |
ludot | 8:d2c85894f26c | 37 | // Oops, there was an error, check the debug screen |
ludot | 8:d2c85894f26c | 38 | logError("Failed to send", ret, mDot::getReturnCodeString(ret).c_str()); |
ludot | 8:d2c85894f26c | 39 | //pc.printf("Failed to send"); |
ludot | 8:d2c85894f26c | 40 | } else { |
ludot | 8:d2c85894f26c | 41 | // Sent the data |
ludot | 8:d2c85894f26c | 42 | logInfo("Successfully sent data"); |
ludot | 8:d2c85894f26c | 43 | //pc.printf("Successfully sent data"); |
ludot | 8:d2c85894f26c | 44 | } |
mfiore | 2:6e2c378339d9 | 45 | |
ludot | 8:d2c85894f26c | 46 | // Wait 5 seconds |
ludot | 8:d2c85894f26c | 47 | osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs())); |
ludot | 8:d2c85894f26c | 48 | } |
ludot | 8:d2c85894f26c | 49 | |
ludot | 8:d2c85894f26c | 50 | return 0; |
ludot | 8:d2c85894f26c | 51 | } |
ludot | 8:d2c85894f26c | 52 | |
ludot | 8:d2c85894f26c | 53 | void setupNetwork() { |
ludot | 8:d2c85894f26c | 54 | |
ludot | 8:d2c85894f26c | 55 | //Serial pc(USBTX, USBRX); |
ludot | 8:d2c85894f26c | 56 | |
ludot | 8:d2c85894f26c | 57 | int32_t ret; |
ludot | 8:d2c85894f26c | 58 | |
mfiore | 0:09250cd371d2 | 59 | // get a mDot handle |
mfiore | 0:09250cd371d2 | 60 | dot = mDot::getInstance(); |
mfiore | 2:6e2c378339d9 | 61 | |
mfiore | 2:6e2c378339d9 | 62 | // print library version information |
ludot | 8:d2c85894f26c | 63 | logInfo("Version: %s", dot->getId().c_str()); |
mfiore | 0:09250cd371d2 | 64 | |
mfiore | 0:09250cd371d2 | 65 | // reset to default config so we know what state we're in |
mfiore | 0:09250cd371d2 | 66 | dot->resetConfig(); |
mfiore | 0:09250cd371d2 | 67 | |
mfiore | 4:36e214ebfa56 | 68 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
ludot | 8:d2c85894f26c | 69 | |
ludot | 8:d2c85894f26c | 70 | //joinmode: [MANUAL = ABP]/[OTA]/[AUTO_OTA] |
ludot | 8:d2c85894f26c | 71 | dot->setJoinMode(mDot::MANUAL); |
ludot | 8:d2c85894f26c | 72 | |
mfiore | 2:6e2c378339d9 | 73 | // set up the mDot with our network information: frequency sub band, network name, and network password |
mfiore | 2:6e2c378339d9 | 74 | // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig() |
mfiore | 4:36e214ebfa56 | 75 | |
mfiore | 4:36e214ebfa56 | 76 | // frequency sub band is only applicable in the 915 (US) frequency band |
ludot | 8:d2c85894f26c | 77 | logInfo("Setting frequency sub band"); |
mfiore | 0:09250cd371d2 | 78 | if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
ludot | 8:d2c85894f26c | 79 | logError("Failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 2:6e2c378339d9 | 80 | } |
mfiore | 4:36e214ebfa56 | 81 | |
jreiss | 5:6b988a804fcb | 82 | std::vector<uint8_t> temp; |
jreiss | 5:6b988a804fcb | 83 | |
ludot | 8:d2c85894f26c | 84 | |
ludot | 8:d2c85894f26c | 85 | if ((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) { |
ludot | 8:d2c85894f26c | 86 | logError("Failed to enable public network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
jreiss | 5:6b988a804fcb | 87 | } |
jreiss | 5:6b988a804fcb | 88 | |
ludot | 8:d2c85894f26c | 89 | |
ludot | 8:d2c85894f26c | 90 | for (int i = 0; i < 4; i++) { |
ludot | 8:d2c85894f26c | 91 | temp.push_back(devAddr[i]); |
ludot | 8:d2c85894f26c | 92 | } |
ludot | 8:d2c85894f26c | 93 | |
ludot | 8:d2c85894f26c | 94 | logInfo("setting Device Address"); |
jreiss | 5:6b988a804fcb | 95 | if ((ret = dot->setNetworkAddress(temp)) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 96 | logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:09250cd371d2 | 97 | } |
mfiore | 4:36e214ebfa56 | 98 | |
jreiss | 5:6b988a804fcb | 99 | temp.clear(); |
jreiss | 5:6b988a804fcb | 100 | for (int i = 0; i < 16; i++) { |
ludot | 8:d2c85894f26c | 101 | temp.push_back(nwkSKey[i]); |
jreiss | 5:6b988a804fcb | 102 | } |
jreiss | 5:6b988a804fcb | 103 | |
ludot | 8:d2c85894f26c | 104 | logInfo("setting Network Session Key "); |
jreiss | 5:6b988a804fcb | 105 | if ((ret = dot->setNetworkSessionKey(temp)) != mDot::MDOT_OK) { |
jreiss | 5:6b988a804fcb | 106 | logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
jreiss | 5:6b988a804fcb | 107 | } |
jreiss | 5:6b988a804fcb | 108 | |
jreiss | 5:6b988a804fcb | 109 | temp.clear(); |
jreiss | 5:6b988a804fcb | 110 | for (int i = 0; i < 16; i++) { |
ludot | 8:d2c85894f26c | 111 | temp.push_back(appSKey[i]); |
jreiss | 5:6b988a804fcb | 112 | } |
jreiss | 5:6b988a804fcb | 113 | |
ludot | 8:d2c85894f26c | 114 | logInfo("setting App Session Key"); |
jreiss | 5:6b988a804fcb | 115 | if ((ret = dot->setDataSessionKey(temp)) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 116 | logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
ludot | 8:d2c85894f26c | 117 | } |
ludot | 8:d2c85894f26c | 118 | |
mfiore | 4:36e214ebfa56 | 119 | // a higher spreading factor allows for longer range but lower throughput |
mfiore | 4:36e214ebfa56 | 120 | // in the 915 (US) frequency band, spreading factors 7 - 10 are available |
mfiore | 4:36e214ebfa56 | 121 | // in the 868 (EU) frequency band, spreading factors 7 - 12 are available |
ludot | 8:d2c85894f26c | 122 | logInfo("Setting TX spreading factor"); |
ludot | 8:d2c85894f26c | 123 | if ((ret = dot->setTxDataRate(mDot::SF_8)) != mDot::MDOT_OK) { |
ludot | 8:d2c85894f26c | 124 | logError("Failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 4:36e214ebfa56 | 125 | } |
mfiore | 4:36e214ebfa56 | 126 | |
mfiore | 4:36e214ebfa56 | 127 | // request receive confirmation of packets from the gateway |
ludot | 8:d2c85894f26c | 128 | logInfo("Disabling ACKs"); |
ludot | 8:d2c85894f26c | 129 | if ((ret = dot->setAck(0)) != mDot::MDOT_OK) { |
ludot | 8:d2c85894f26c | 130 | logError("Failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 4:36e214ebfa56 | 131 | } |
mfiore | 4:36e214ebfa56 | 132 | |
mfiore | 4:36e214ebfa56 | 133 | // save this configuration to the mDot's NVM |
ludot | 8:d2c85894f26c | 134 | logInfo("Saving config"); |
mfiore | 2:6e2c378339d9 | 135 | if (! dot->saveConfig()) { |
ludot | 8:d2c85894f26c | 136 | logError("Failed to save configuration"); |
mfiore | 0:09250cd371d2 | 137 | } |
ludot | 8:d2c85894f26c | 138 | } |