Start project of The Things Network workshop with the MultiTech mDot
Dependencies: libmDot mbed-rtos mbed
Fork of mDot_LoRa_Connect_Example_APP_EUI_KEY by
main.cpp@13:6ce90b84a3fe, 2016-04-06 (annotated)
- Committer:
- johanstokking
- Date:
- Wed Apr 06 00:10:30 2016 +0000
- Revision:
- 13:6ce90b84a3fe
- Parent:
- 12:343229a12653
Switched to ABP
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 | |
| johanstokking | 13:6ce90b84a3fe | 8 | static uint8_t config_network_addr[] = { 0x01, 0x02, 0x03, 0x04 }; |
| johanstokking | 13:6ce90b84a3fe | 9 | static uint8_t config_network_nskey[] = {0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C}; |
| johanstokking | 13:6ce90b84a3fe | 10 | static uint8_t config_network_dskey[] = {0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C}; |
| johanstokking | 8:308a67b71c86 | 11 | static uint8_t config_frequency_sub_band = 7; |
| mfiore | 0:09250cd371d2 | 12 | |
| johanstokking | 9:d589fb5e68a4 | 13 | mDot* dot; |
| johanstokking | 9:d589fb5e68a4 | 14 | |
| johanstokking | 10:5332d0939ebe | 15 | void setupNetwork(); |
| johanstokking | 10:5332d0939ebe | 16 | |
| mfiore | 0:09250cd371d2 | 17 | int main() { |
| johanstokking | 12:343229a12653 | 18 | // Set up the network |
| johanstokking | 9:d589fb5e68a4 | 19 | setupNetwork(); |
| johanstokking | 9:d589fb5e68a4 | 20 | |
| johanstokking | 12:343229a12653 | 21 | // Message you want to send |
| johanstokking | 9:d589fb5e68a4 | 22 | std::string data_str = "Hello!"; |
| johanstokking | 9:d589fb5e68a4 | 23 | |
| johanstokking | 12:343229a12653 | 24 | // Copy the message in an array of bytes |
| johanstokking | 9:d589fb5e68a4 | 25 | std::vector<uint8_t> data; |
| johanstokking | 9:d589fb5e68a4 | 26 | for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++) |
| johanstokking | 9:d589fb5e68a4 | 27 | data.push_back((uint8_t) *it); |
| johanstokking | 10:5332d0939ebe | 28 | |
| johanstokking | 12:343229a12653 | 29 | // Start the loop |
| johanstokking | 10:5332d0939ebe | 30 | int32_t ret; |
| johanstokking | 9:d589fb5e68a4 | 31 | while (true) { |
| johanstokking | 12:343229a12653 | 32 | // Send the data |
| johanstokking | 9:d589fb5e68a4 | 33 | if ((ret = dot->send(data)) != mDot::MDOT_OK) { |
| johanstokking | 12:343229a12653 | 34 | // Oops, there was an error, check the debug screen |
| johanstokking | 10:5332d0939ebe | 35 | logError("Failed to send", ret, mDot::getReturnCodeString(ret).c_str()); |
| johanstokking | 9:d589fb5e68a4 | 36 | } else { |
| johanstokking | 12:343229a12653 | 37 | // Sent the data |
| johanstokking | 10:5332d0939ebe | 38 | logInfo("Successfully sent data"); |
| johanstokking | 9:d589fb5e68a4 | 39 | } |
| johanstokking | 10:5332d0939ebe | 40 | |
| johanstokking | 12:343229a12653 | 41 | // Wait 5 seconds |
| johanstokking | 12:343229a12653 | 42 | osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs())); |
| johanstokking | 9:d589fb5e68a4 | 43 | } |
| johanstokking | 9:d589fb5e68a4 | 44 | |
| johanstokking | 9:d589fb5e68a4 | 45 | return 0; |
| johanstokking | 9:d589fb5e68a4 | 46 | } |
| johanstokking | 9:d589fb5e68a4 | 47 | |
| johanstokking | 9:d589fb5e68a4 | 48 | void setupNetwork() { |
| mfiore | 0:09250cd371d2 | 49 | int32_t ret; |
| johanstokking | 9:d589fb5e68a4 | 50 | |
| mfiore | 0:09250cd371d2 | 51 | // get a mDot handle |
| mfiore | 0:09250cd371d2 | 52 | dot = mDot::getInstance(); |
| mfiore | 2:6e2c378339d9 | 53 | |
| mfiore | 2:6e2c378339d9 | 54 | // print library version information |
| johanstokking | 10:5332d0939ebe | 55 | logInfo("Version: %s", dot->getId().c_str()); |
| mfiore | 0:09250cd371d2 | 56 | |
| mfiore | 0:09250cd371d2 | 57 | // reset to default config so we know what state we're in |
| mfiore | 0:09250cd371d2 | 58 | dot->resetConfig(); |
| mfiore | 0:09250cd371d2 | 59 | |
| mfiore | 4:36e214ebfa56 | 60 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
| mfiore | 0:09250cd371d2 | 61 | |
| johanstokking | 13:6ce90b84a3fe | 62 | dot->setJoinMode(mDot::MANUAL); |
| johanstokking | 13:6ce90b84a3fe | 63 | |
| mfiore | 2:6e2c378339d9 | 64 | // set up the mDot with our network information: frequency sub band, network name, and network password |
| mfiore | 2:6e2c378339d9 | 65 | // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig() |
| mfiore | 4:36e214ebfa56 | 66 | |
| mfiore | 4:36e214ebfa56 | 67 | // frequency sub band is only applicable in the 915 (US) frequency band |
| johanstokking | 10:5332d0939ebe | 68 | logInfo("Setting frequency sub band"); |
| mfiore | 0:09250cd371d2 | 69 | if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
| johanstokking | 10:5332d0939ebe | 70 | logError("Failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
| mfiore | 2:6e2c378339d9 | 71 | } |
| mfiore | 4:36e214ebfa56 | 72 | |
| jreiss | 5:6b988a804fcb | 73 | std::vector<uint8_t> temp; |
| jreiss | 5:6b988a804fcb | 74 | |
| johanstokking | 8:308a67b71c86 | 75 | if ((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) { |
| johanstokking | 10:5332d0939ebe | 76 | logError("Failed to enable public network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
| johanstokking | 8:308a67b71c86 | 77 | } |
| johanstokking | 13:6ce90b84a3fe | 78 | |
| johanstokking | 13:6ce90b84a3fe | 79 | for (int i = 0; i < 4; i++) { |
| johanstokking | 13:6ce90b84a3fe | 80 | temp.push_back(config_network_addr[i]); |
| johanstokking | 13:6ce90b84a3fe | 81 | } |
| johanstokking | 13:6ce90b84a3fe | 82 | |
| johanstokking | 13:6ce90b84a3fe | 83 | logInfo("setting network addr"); |
| johanstokking | 13:6ce90b84a3fe | 84 | if ((ret = dot->setNetworkAddress(temp)) != mDot::MDOT_OK) { |
| johanstokking | 13:6ce90b84a3fe | 85 | logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
| mfiore | 0:09250cd371d2 | 86 | } |
| mfiore | 4:36e214ebfa56 | 87 | |
| jreiss | 5:6b988a804fcb | 88 | temp.clear(); |
| jreiss | 5:6b988a804fcb | 89 | for (int i = 0; i < 16; i++) { |
| johanstokking | 13:6ce90b84a3fe | 90 | temp.push_back(config_network_nskey[i]); |
| johanstokking | 13:6ce90b84a3fe | 91 | } |
| johanstokking | 13:6ce90b84a3fe | 92 | |
| johanstokking | 13:6ce90b84a3fe | 93 | logInfo("setting network password"); |
| johanstokking | 13:6ce90b84a3fe | 94 | if ((ret = dot->setNetworkSessionKey(temp)) != mDot::MDOT_OK) { |
| johanstokking | 13:6ce90b84a3fe | 95 | logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
| jreiss | 5:6b988a804fcb | 96 | } |
| jreiss | 5:6b988a804fcb | 97 | |
| johanstokking | 13:6ce90b84a3fe | 98 | temp.clear(); |
| johanstokking | 13:6ce90b84a3fe | 99 | for (int i = 0; i < 16; i++) { |
| johanstokking | 13:6ce90b84a3fe | 100 | temp.push_back(config_network_dskey[i]); |
| johanstokking | 13:6ce90b84a3fe | 101 | } |
| johanstokking | 13:6ce90b84a3fe | 102 | |
| johanstokking | 13:6ce90b84a3fe | 103 | logInfo("setting network password"); |
| johanstokking | 13:6ce90b84a3fe | 104 | if ((ret = dot->setDataSessionKey(temp)) != mDot::MDOT_OK) { |
| johanstokking | 13:6ce90b84a3fe | 105 | logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
| mfiore | 0:09250cd371d2 | 106 | } |
| mfiore | 4:36e214ebfa56 | 107 | |
| mfiore | 4:36e214ebfa56 | 108 | // a higher spreading factor allows for longer range but lower throughput |
| mfiore | 4:36e214ebfa56 | 109 | // in the 915 (US) frequency band, spreading factors 7 - 10 are available |
| mfiore | 4:36e214ebfa56 | 110 | // in the 868 (EU) frequency band, spreading factors 7 - 12 are available |
| johanstokking | 10:5332d0939ebe | 111 | logInfo("Setting TX spreading factor"); |
| johanstokking | 9:d589fb5e68a4 | 112 | if ((ret = dot->setTxDataRate(mDot::SF_8)) != mDot::MDOT_OK) { |
| johanstokking | 10:5332d0939ebe | 113 | logError("Failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
| mfiore | 4:36e214ebfa56 | 114 | } |
| mfiore | 4:36e214ebfa56 | 115 | |
| mfiore | 4:36e214ebfa56 | 116 | // request receive confirmation of packets from the gateway |
| johanstokking | 13:6ce90b84a3fe | 117 | logInfo("Disabling ACKs"); |
| johanstokking | 13:6ce90b84a3fe | 118 | if ((ret = dot->setAck(0)) != mDot::MDOT_OK) { |
| johanstokking | 10:5332d0939ebe | 119 | logError("Failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
| mfiore | 4:36e214ebfa56 | 120 | } |
| mfiore | 4:36e214ebfa56 | 121 | |
| mfiore | 4:36e214ebfa56 | 122 | // save this configuration to the mDot's NVM |
| johanstokking | 10:5332d0939ebe | 123 | logInfo("Saving config"); |
| mfiore | 2:6e2c378339d9 | 124 | if (! dot->saveConfig()) { |
| johanstokking | 10:5332d0939ebe | 125 | logError("Failed to save configuration"); |
| mfiore | 0:09250cd371d2 | 126 | } |
| mfiore | 0:09250cd371d2 | 127 | |
| mfiore | 0:09250cd371d2 | 128 | // attempt to join the network |
| johanstokking | 13:6ce90b84a3fe | 129 | //logInfo("Joining network"); |
| johanstokking | 13:6ce90b84a3fe | 130 | // while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
| johanstokking | 13:6ce90b84a3fe | 131 | // logError("Failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
| johanstokking | 13:6ce90b84a3fe | 132 | // // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
| johanstokking | 13:6ce90b84a3fe | 133 | // osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs())); |
| johanstokking | 13:6ce90b84a3fe | 134 | // } |
| johanstokking | 9:d589fb5e68a4 | 135 | } |
The Things Network
