wotio library demonstrating LoRaWAN connectivity using MultiTech mDot & MultiTech Conduit Gateway

Dependencies:   libmDot mbed-rtos mbed

Committer:
tuddman
Date:
Fri Oct 30 13:35:16 2015 +0000
Revision:
1:6b84fe382e27
Parent:
0:eea6446bc510
Child:
2:8cf7270307e5
adds latest libraries

Who changed what in which revision?

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