a Hello world to connect to The Things Network

Dependencies:   libmDot mbed-rtos mbed

Fork of mDot_LoRa_Connect_Example by MultiTech

Committer:
ropu
Date:
Thu Nov 19 20:10:43 2015 +0000
Revision:
6:8f7276e7d206
Parent:
4:36e214ebfa56
Child:
7:609e7bb06486
Hello world for the TTN

Who changed what in which revision?

UserRevisionLine numberNew 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
mfiore 2:6e2c378339d9 8 // uncomment the following lines and edit their values to match your configuration
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 6:8f7276e7d206 11 static const uint8_t network_address_array[] = {0x02, 0x01, 0xBA, 0x01};
ropu 6:8f7276e7d206 12 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 13 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 14 static std::vector<uint8_t> network_address (network_address_array, network_address_array + sizeof(network_address_array) / sizeof(uint8_t));
ropu 6:8f7276e7d206 15 static uint8_t config_frequency_sub_band = 4;
mfiore 0:09250cd371d2 16
mfiore 0:09250cd371d2 17 int main() {
mfiore 0:09250cd371d2 18 int32_t ret;
mfiore 0:09250cd371d2 19 mDot* dot;
mfiore 0:09250cd371d2 20 std::vector<uint8_t> data;
ropu 6:8f7276e7d206 21 std::string data_str = "hello ropu!";
mfiore 2:6e2c378339d9 22
mfiore 0:09250cd371d2 23 // get a mDot handle
mfiore 0:09250cd371d2 24 dot = mDot::getInstance();
ropu 6:8f7276e7d206 25
ropu 6:8f7276e7d206 26 dot->resetConfig();
mfiore 0:09250cd371d2 27
ropu 6:8f7276e7d206 28 dot->setJoinMode(mDot::MANUAL);
ropu 6:8f7276e7d206 29 dot->setPublicNetwork(true);
mfiore 4:36e214ebfa56 30 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
ropu 6:8f7276e7d206 31 dot->setFrequencySubBand(config_frequency_sub_band);
ropu 6:8f7276e7d206 32 dot->setNetworkSessionKey(netowork_session_key);
ropu 6:8f7276e7d206 33 dot->setDataSessionKey(data_session_key);
ropu 6:8f7276e7d206 34 dot->setNetworkAddress(network_address);
mfiore 4:36e214ebfa56 35
mfiore 4:36e214ebfa56 36
mfiore 4:36e214ebfa56 37 // a higher spreading factor allows for longer range but lower throughput
mfiore 4:36e214ebfa56 38 // in the 915 (US) frequency band, spreading factors 7 - 10 are available
mfiore 4:36e214ebfa56 39 // in the 868 (EU) frequency band, spreading factors 7 - 12 are available
mfiore 4:36e214ebfa56 40 logInfo("setting TX spreading factor");
mfiore 4:36e214ebfa56 41 if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) {
mfiore 4:36e214ebfa56 42 logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 43 }
mfiore 4:36e214ebfa56 44
ropu 6:8f7276e7d206 45
ropu 6:8f7276e7d206 46
mfiore 4:36e214ebfa56 47 // request receive confirmation of packets from the gateway
mfiore 4:36e214ebfa56 48 logInfo("enabling ACKs");
ropu 6:8f7276e7d206 49 if ((ret = dot->setAck(0)) != mDot::MDOT_OK) {
mfiore 4:36e214ebfa56 50 logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 51 }
mfiore 4:36e214ebfa56 52
mfiore 4:36e214ebfa56 53 // save this configuration to the mDot's NVM
mfiore 2:6e2c378339d9 54 logInfo("saving config");
mfiore 2:6e2c378339d9 55 if (! dot->saveConfig()) {
mfiore 2:6e2c378339d9 56 logError("failed to save configuration");
mfiore 0:09250cd371d2 57 }
mfiore 2:6e2c378339d9 58 //*******************************************
mfiore 2:6e2c378339d9 59 // end of configuration
mfiore 2:6e2c378339d9 60 //*******************************************
mfiore 0:09250cd371d2 61
mfiore 0:09250cd371d2 62 // attempt to join the network
mfiore 2:6e2c378339d9 63 logInfo("joining network");
mfiore 0:09250cd371d2 64 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 65 logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 66 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
mfiore 4:36e214ebfa56 67 osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs()));
mfiore 0:09250cd371d2 68 }
mfiore 0:09250cd371d2 69
mfiore 0:09250cd371d2 70 // format data for sending to the gateway
mfiore 0:09250cd371d2 71 for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++)
mfiore 0:09250cd371d2 72 data.push_back((uint8_t) *it);
mfiore 0:09250cd371d2 73
mfiore 0:09250cd371d2 74 while (true) {
mfiore 4:36e214ebfa56 75 // send the data to the gateway
mfiore 0:09250cd371d2 76 if ((ret = dot->send(data)) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 77 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:09250cd371d2 78 } else {
mfiore 2:6e2c378339d9 79 logInfo("successfully sent data to gateway");
mfiore 0:09250cd371d2 80 }
mfiore 0:09250cd371d2 81
mfiore 4:36e214ebfa56 82 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
mfiore 4:36e214ebfa56 83 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
mfiore 0:09250cd371d2 84 }
mfiore 0:09250cd371d2 85
mfiore 0:09250cd371d2 86 return 0;
mfiore 0:09250cd371d2 87 }