
wotio library demonstrating LoRaWAN connectivity using MultiTech mDot & MultiTech Conduit Gateway
Dependencies: libmDot mbed-rtos mbed
main.cpp@3:689fbc447181, 2015-11-09 (annotated)
- Committer:
- tuddman
- Date:
- Mon Nov 09 23:33:43 2015 +0000
- Revision:
- 3:689fbc447181
- Parent:
- 2:8cf7270307e5
initial commit of wotio lora connector
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:689fbc447181 | 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 | 2:8cf7270307e5 | 35 | std::string data_str = "LoRa->wotio"; |
tuddman | 0:eea6446bc510 | 36 | |
tuddman | 0:eea6446bc510 | 37 | |
tuddman | 0:eea6446bc510 | 38 | // get a mDot handle |
tuddman | 0:eea6446bc510 | 39 | dot = mDot::getInstance(); |
tuddman | 0:eea6446bc510 | 40 | |
tuddman | 0:eea6446bc510 | 41 | // print library version information |
tuddman | 0:eea6446bc510 | 42 | logInfo("version: %s", dot->getId().c_str()); |
tuddman | 0:eea6446bc510 | 43 | |
tuddman | 0:eea6446bc510 | 44 | //******************************************* |
tuddman | 0:eea6446bc510 | 45 | // configuration |
tuddman | 0:eea6446bc510 | 46 | //******************************************* |
tuddman | 0:eea6446bc510 | 47 | // reset to default config so we know what state we're in |
tuddman | 0:eea6446bc510 | 48 | dot->resetConfig(); |
tuddman | 0:eea6446bc510 | 49 | |
tuddman | 0:eea6446bc510 | 50 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
tuddman | 0:eea6446bc510 | 51 | |
tuddman | 0:eea6446bc510 | 52 | // set up the mDot with our network information: frequency sub band, network name, and network password |
tuddman | 0:eea6446bc510 | 53 | // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig() |
tuddman | 0:eea6446bc510 | 54 | |
tuddman | 0:eea6446bc510 | 55 | // frequency sub band is only applicable in the 915 (US) frequency band |
tuddman | 0:eea6446bc510 | 56 | // 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 | 57 | // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels |
tuddman | 0:eea6446bc510 | 58 | logInfo("setting frequency sub band"); |
tuddman | 0:eea6446bc510 | 59 | if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
tuddman | 0:eea6446bc510 | 60 | logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
tuddman | 0:eea6446bc510 | 61 | } |
tuddman | 0:eea6446bc510 | 62 | |
tuddman | 0:eea6446bc510 | 63 | logInfo("setting network name"); |
tuddman | 0:eea6446bc510 | 64 | if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { |
tuddman | 0:eea6446bc510 | 65 | logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
tuddman | 0:eea6446bc510 | 66 | } |
tuddman | 0:eea6446bc510 | 67 | |
tuddman | 0:eea6446bc510 | 68 | logInfo("setting network password"); |
tuddman | 0:eea6446bc510 | 69 | if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { |
tuddman | 0:eea6446bc510 | 70 | logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
tuddman | 0:eea6446bc510 | 71 | } |
tuddman | 0:eea6446bc510 | 72 | |
tuddman | 0:eea6446bc510 | 73 | // a higher spreading factor allows for longer range but lower throughput |
tuddman | 0:eea6446bc510 | 74 | // in the 915 (US) frequency band, spreading factors 7 - 10 are available |
tuddman | 0:eea6446bc510 | 75 | // in the 868 (EU) frequency band, spreading factors 7 - 12 are available |
tuddman | 0:eea6446bc510 | 76 | logInfo("setting TX spreading factor"); |
tuddman | 0:eea6446bc510 | 77 | if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) { |
tuddman | 0:eea6446bc510 | 78 | logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
tuddman | 0:eea6446bc510 | 79 | } |
tuddman | 0:eea6446bc510 | 80 | |
tuddman | 0:eea6446bc510 | 81 | // request receive confirmation of packets from the gateway |
tuddman | 0:eea6446bc510 | 82 | logInfo("enabling ACKs"); |
tuddman | 0:eea6446bc510 | 83 | if ((ret = dot->setAck(1)) != mDot::MDOT_OK) { |
tuddman | 0:eea6446bc510 | 84 | logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
tuddman | 0:eea6446bc510 | 85 | } |
tuddman | 0:eea6446bc510 | 86 | |
tuddman | 0:eea6446bc510 | 87 | // save this configuration to the mDot's NVM |
tuddman | 0:eea6446bc510 | 88 | logInfo("saving config"); |
tuddman | 0:eea6446bc510 | 89 | if (! dot->saveConfig()) { |
tuddman | 0:eea6446bc510 | 90 | logError("failed to save configuration"); |
tuddman | 0:eea6446bc510 | 91 | } |
tuddman | 0:eea6446bc510 | 92 | //******************************************* |
tuddman | 0:eea6446bc510 | 93 | // end of configuration |
tuddman | 0:eea6446bc510 | 94 | //******************************************* |
tuddman | 0:eea6446bc510 | 95 | |
tuddman | 1:6b84fe382e27 | 96 | // attempt to join the network |
tuddman | 1:6b84fe382e27 | 97 | logInfo("joining network"); |
tuddman | 1:6b84fe382e27 | 98 | while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
tuddman | 1:6b84fe382e27 | 99 | logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
tuddman | 1:6b84fe382e27 | 100 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
tuddman | 1:6b84fe382e27 | 101 | osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs())); |
tuddman | 1:6b84fe382e27 | 102 | } |
tuddman | 1:6b84fe382e27 | 103 | |
tuddman | 0:eea6446bc510 | 104 | // format data for sending to the gateway |
tuddman | 0:eea6446bc510 | 105 | for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++) |
tuddman | 0:eea6446bc510 | 106 | data.push_back((uint8_t) *it); |
tuddman | 0:eea6446bc510 | 107 | |
tuddman | 1:6b84fe382e27 | 108 | while (true) { |
tuddman | 1:6b84fe382e27 | 109 | // send the data to the gateway |
tuddman | 0:eea6446bc510 | 110 | if ((ret = dot->send(data)) != mDot::MDOT_OK) { |
tuddman | 1:6b84fe382e27 | 111 | logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str()); |
tuddman | 0:eea6446bc510 | 112 | } else { |
tuddman | 2:8cf7270307e5 | 113 | // configure the Ticker to blink the LED on 1sec interval |
tuddman | 2:8cf7270307e5 | 114 | tick.attach(&blink, 1.0); |
tuddman | 0:eea6446bc510 | 115 | logInfo("successfully sent data to gateway"); |
tuddman | 0:eea6446bc510 | 116 | } |
tuddman | 1:6b84fe382e27 | 117 | |
tuddman | 1:6b84fe382e27 | 118 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
tuddman | 1:6b84fe382e27 | 119 | osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs())); |
tuddman | 0:eea6446bc510 | 120 | } |
tuddman | 0:eea6446bc510 | 121 | |
tuddman | 0:eea6446bc510 | 122 | return 0; |
tuddman | 0:eea6446bc510 | 123 | } |