Connecting to gateway and sending packets with a MultiTech mDot.

Dependencies:   libmDot mbed-rtos mbed

Fork of libmDot_sample by MultiTech

Committer:
jreiss
Date:
Tue Apr 12 23:44:25 2016 +0000
Revision:
6:f0b6fea36e28
Parent:
5:e9d78a9bafc5
comment out public network

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 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:e9d78a9bafc5 10 static std::string config_network_name = "<lora network id>";
jreiss 5:e9d78a9bafc5 11 static std::string config_network_pass = "<lora network key>";
jreiss 5:e9d78a9bafc5 12 static uint8_t config_frequency_sub_band = 1;
mfiore 0:09250cd371d2 13
mfiore 0:09250cd371d2 14 int main() {
mfiore 0:09250cd371d2 15 int32_t ret;
mfiore 0:09250cd371d2 16 mDot* dot;
mfiore 0:09250cd371d2 17 std::vector<uint8_t> data;
mfiore 4:36e214ebfa56 18 std::string data_str = "hello!";
mfiore 2:6e2c378339d9 19
mfiore 0:09250cd371d2 20 // get a mDot handle
mfiore 0:09250cd371d2 21 dot = mDot::getInstance();
mfiore 2:6e2c378339d9 22
mfiore 2:6e2c378339d9 23 // print library version information
mfiore 2:6e2c378339d9 24 logInfo("version: %s", dot->getId().c_str());
mfiore 0:09250cd371d2 25
mfiore 2:6e2c378339d9 26 //*******************************************
mfiore 2:6e2c378339d9 27 // configuration
mfiore 2:6e2c378339d9 28 //*******************************************
mfiore 0:09250cd371d2 29 // reset to default config so we know what state we're in
mfiore 0:09250cd371d2 30 dot->resetConfig();
mfiore 0:09250cd371d2 31
mfiore 4:36e214ebfa56 32 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
mfiore 0:09250cd371d2 33
mfiore 2:6e2c378339d9 34 // set up the mDot with our network information: frequency sub band, network name, and network password
mfiore 2:6e2c378339d9 35 // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig()
mfiore 4:36e214ebfa56 36
mfiore 4:36e214ebfa56 37 // frequency sub band is only applicable in the 915 (US) frequency band
mfiore 4:36e214ebfa56 38 // 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 39 // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels
jreiss 5:e9d78a9bafc5 40
jreiss 6:f0b6fea36e28 41 // logInfo("setting to public network");
jreiss 6:f0b6fea36e28 42 // if ((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) {
jreiss 6:f0b6fea36e28 43 // logError("failed to set public network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
jreiss 6:f0b6fea36e28 44 // }
jreiss 5:e9d78a9bafc5 45
jreiss 5:e9d78a9bafc5 46 logInfo("setting frequency sub band");
mfiore 0:09250cd371d2 47 if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 48 logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 2:6e2c378339d9 49 }
mfiore 4:36e214ebfa56 50
mfiore 2:6e2c378339d9 51 logInfo("setting network name");
mfiore 2:6e2c378339d9 52 if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 53 logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:09250cd371d2 54 }
mfiore 4:36e214ebfa56 55
mfiore 2:6e2c378339d9 56 logInfo("setting network password");
mfiore 2:6e2c378339d9 57 if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 58 logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:09250cd371d2 59 }
mfiore 4:36e214ebfa56 60
mfiore 4:36e214ebfa56 61 // a higher spreading factor allows for longer range but lower throughput
mfiore 4:36e214ebfa56 62 // in the 915 (US) frequency band, spreading factors 7 - 10 are available
mfiore 4:36e214ebfa56 63 // in the 868 (EU) frequency band, spreading factors 7 - 12 are available
mfiore 4:36e214ebfa56 64 logInfo("setting TX spreading factor");
mfiore 4:36e214ebfa56 65 if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) {
mfiore 4:36e214ebfa56 66 logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 67 }
mfiore 4:36e214ebfa56 68
mfiore 4:36e214ebfa56 69 // request receive confirmation of packets from the gateway
mfiore 4:36e214ebfa56 70 logInfo("enabling ACKs");
mfiore 4:36e214ebfa56 71 if ((ret = dot->setAck(1)) != mDot::MDOT_OK) {
mfiore 4:36e214ebfa56 72 logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 73 }
mfiore 4:36e214ebfa56 74
mfiore 4:36e214ebfa56 75 // save this configuration to the mDot's NVM
mfiore 2:6e2c378339d9 76 logInfo("saving config");
mfiore 2:6e2c378339d9 77 if (! dot->saveConfig()) {
mfiore 2:6e2c378339d9 78 logError("failed to save configuration");
mfiore 0:09250cd371d2 79 }
mfiore 2:6e2c378339d9 80 //*******************************************
mfiore 2:6e2c378339d9 81 // end of configuration
mfiore 2:6e2c378339d9 82 //*******************************************
mfiore 0:09250cd371d2 83
mfiore 0:09250cd371d2 84 // attempt to join the network
mfiore 2:6e2c378339d9 85 logInfo("joining network");
mfiore 0:09250cd371d2 86 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 87 logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 88 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
mfiore 4:36e214ebfa56 89 osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs()));
mfiore 0:09250cd371d2 90 }
mfiore 0:09250cd371d2 91
mfiore 0:09250cd371d2 92 // format data for sending to the gateway
mfiore 0:09250cd371d2 93 for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++)
mfiore 0:09250cd371d2 94 data.push_back((uint8_t) *it);
mfiore 0:09250cd371d2 95
mfiore 0:09250cd371d2 96 while (true) {
mfiore 4:36e214ebfa56 97 // send the data to the gateway
mfiore 0:09250cd371d2 98 if ((ret = dot->send(data)) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 99 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:09250cd371d2 100 } else {
mfiore 2:6e2c378339d9 101 logInfo("successfully sent data to gateway");
mfiore 0:09250cd371d2 102 }
mfiore 0:09250cd371d2 103
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)5000, (uint32_t)dot->getNextTxMs()));
mfiore 0:09250cd371d2 106 }
mfiore 0:09250cd371d2 107
mfiore 0:09250cd371d2 108 return 0;
mfiore 0:09250cd371d2 109 }