a Hello world to connect to The Things Network
Dependencies: libmDot mbed-rtos mbed
Fork of mDot_LoRa_Connect_Example by
main.cpp@7:609e7bb06486, 2015-11-19 (annotated)
- Committer:
- ropu
- Date:
- Thu Nov 19 20:15:09 2015 +0000
- Revision:
- 7:609e7bb06486
- Parent:
- 6:8f7276e7d206
Commented network address
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 | 2:6e2c378339d9 | 7 | // these options must match the settings on your Conduit |
ropu | 7:609e7bb06486 | 8 | // TTN Keys |
ropu | 6:8f7276e7d206 | 9 | static const uint8_t netowork_session_key_array[] = {0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C}; |
ropu | 6:8f7276e7d206 | 10 | static const uint8_t data_session_key_array[] = {0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C}; |
ropu | 7:609e7bb06486 | 11 | // uncomment the following lines and edit their values to match your configuration |
ropu | 7:609e7bb06486 | 12 | //static const uint8_t network_address_array[] = {0x02, 0x01, 0xBA, 0x01}; // use yours based on http://thethingsnetwork.org/wiki/AddressSpace |
ropu | 6:8f7276e7d206 | 13 | static std::vector<uint8_t> netowork_session_key (netowork_session_key_array, netowork_session_key_array + sizeof(netowork_session_key_array) / sizeof(uint8_t)); |
ropu | 6:8f7276e7d206 | 14 | static std::vector<uint8_t> data_session_key (data_session_key_array, data_session_key_array + sizeof(data_session_key_array) / sizeof(uint8_t)); |
ropu | 6:8f7276e7d206 | 15 | static std::vector<uint8_t> network_address (network_address_array, network_address_array + sizeof(network_address_array) / sizeof(uint8_t)); |
ropu | 6:8f7276e7d206 | 16 | static uint8_t config_frequency_sub_band = 4; |
mfiore | 0:09250cd371d2 | 17 | |
mfiore | 0:09250cd371d2 | 18 | int main() { |
mfiore | 0:09250cd371d2 | 19 | int32_t ret; |
mfiore | 0:09250cd371d2 | 20 | mDot* dot; |
mfiore | 0:09250cd371d2 | 21 | std::vector<uint8_t> data; |
ropu | 6:8f7276e7d206 | 22 | std::string data_str = "hello ropu!"; |
mfiore | 2:6e2c378339d9 | 23 | |
mfiore | 0:09250cd371d2 | 24 | // get a mDot handle |
mfiore | 0:09250cd371d2 | 25 | dot = mDot::getInstance(); |
ropu | 6:8f7276e7d206 | 26 | |
ropu | 6:8f7276e7d206 | 27 | dot->resetConfig(); |
mfiore | 0:09250cd371d2 | 28 | |
ropu | 7:609e7bb06486 | 29 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
ropu | 7:609e7bb06486 | 30 | |
ropu | 7:609e7bb06486 | 31 | // too lazzy to check all errors |
ropu | 6:8f7276e7d206 | 32 | dot->setJoinMode(mDot::MANUAL); |
ropu | 6:8f7276e7d206 | 33 | dot->setPublicNetwork(true); |
ropu | 6:8f7276e7d206 | 34 | dot->setFrequencySubBand(config_frequency_sub_band); |
ropu | 6:8f7276e7d206 | 35 | dot->setNetworkSessionKey(netowork_session_key); |
ropu | 6:8f7276e7d206 | 36 | dot->setDataSessionKey(data_session_key); |
ropu | 6:8f7276e7d206 | 37 | dot->setNetworkAddress(network_address); |
mfiore | 4:36e214ebfa56 | 38 | |
mfiore | 4:36e214ebfa56 | 39 | |
mfiore | 4:36e214ebfa56 | 40 | // a higher spreading factor allows for longer range but lower throughput |
mfiore | 4:36e214ebfa56 | 41 | // in the 915 (US) frequency band, spreading factors 7 - 10 are available |
mfiore | 4:36e214ebfa56 | 42 | // in the 868 (EU) frequency band, spreading factors 7 - 12 are available |
mfiore | 4:36e214ebfa56 | 43 | logInfo("setting TX spreading factor"); |
mfiore | 4:36e214ebfa56 | 44 | if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) { |
mfiore | 4:36e214ebfa56 | 45 | logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 4:36e214ebfa56 | 46 | } |
mfiore | 4:36e214ebfa56 | 47 | |
ropu | 6:8f7276e7d206 | 48 | |
ropu | 6:8f7276e7d206 | 49 | |
mfiore | 4:36e214ebfa56 | 50 | // request receive confirmation of packets from the gateway |
mfiore | 4:36e214ebfa56 | 51 | logInfo("enabling ACKs"); |
ropu | 6:8f7276e7d206 | 52 | if ((ret = dot->setAck(0)) != mDot::MDOT_OK) { |
mfiore | 4:36e214ebfa56 | 53 | logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 4:36e214ebfa56 | 54 | } |
mfiore | 4:36e214ebfa56 | 55 | |
mfiore | 4:36e214ebfa56 | 56 | // save this configuration to the mDot's NVM |
mfiore | 2:6e2c378339d9 | 57 | logInfo("saving config"); |
mfiore | 2:6e2c378339d9 | 58 | if (! dot->saveConfig()) { |
mfiore | 2:6e2c378339d9 | 59 | logError("failed to save configuration"); |
mfiore | 0:09250cd371d2 | 60 | } |
mfiore | 2:6e2c378339d9 | 61 | //******************************************* |
mfiore | 2:6e2c378339d9 | 62 | // end of configuration |
mfiore | 2:6e2c378339d9 | 63 | //******************************************* |
mfiore | 0:09250cd371d2 | 64 | |
mfiore | 0:09250cd371d2 | 65 | // attempt to join the network |
mfiore | 2:6e2c378339d9 | 66 | logInfo("joining network"); |
mfiore | 0:09250cd371d2 | 67 | while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 68 | logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 4:36e214ebfa56 | 69 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
mfiore | 4:36e214ebfa56 | 70 | osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs())); |
mfiore | 0:09250cd371d2 | 71 | } |
mfiore | 0:09250cd371d2 | 72 | |
mfiore | 0:09250cd371d2 | 73 | // format data for sending to the gateway |
mfiore | 0:09250cd371d2 | 74 | for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++) |
mfiore | 0:09250cd371d2 | 75 | data.push_back((uint8_t) *it); |
mfiore | 0:09250cd371d2 | 76 | |
mfiore | 0:09250cd371d2 | 77 | while (true) { |
mfiore | 4:36e214ebfa56 | 78 | // send the data to the gateway |
mfiore | 0:09250cd371d2 | 79 | if ((ret = dot->send(data)) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 80 | logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:09250cd371d2 | 81 | } else { |
mfiore | 2:6e2c378339d9 | 82 | logInfo("successfully sent data to gateway"); |
mfiore | 0:09250cd371d2 | 83 | } |
mfiore | 0:09250cd371d2 | 84 | |
mfiore | 4:36e214ebfa56 | 85 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
mfiore | 4:36e214ebfa56 | 86 | osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs())); |
mfiore | 0:09250cd371d2 | 87 | } |
mfiore | 0:09250cd371d2 | 88 | |
mfiore | 0:09250cd371d2 | 89 | return 0; |
mfiore | 0:09250cd371d2 | 90 | } |