MultiTech / Mbed OS mDot_LoRa_Connect_Example_CUSTOM_AS923

Dependencies:   libmDot-Custom

Fork of mDot_LoRa_Connect_Example by MultiTech

The project is a starting point for the AS923 channel plan.

Default frequencies are 923.2 and 923.4 Join Datarate is set to DR2:SF10BW125 Additional channels can be added in JoinAccept message or through new channel mac commands

New MAC commands in LoRaWAN 1.02 have NOT been included to set the DwellTime and MaxEIRP Overriding this function is available to someone who would like to do this. virtual uint8_t HandleMacCommands(uint8_t* payload, uint8_t index, uint8_t end_index);

Committer:
jreiss
Date:
Mon Sep 19 17:03:31 2016 +0000
Revision:
8:945bf15931ee
Parent:
7:6aac90ea2240
Child:
10:f5ea829bf9ca
Use DR2 instead of SF

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