initial version / sends time + 012345689

Dependencies:   libmDot mbed-rtos mbed

Fork of wotiolora by Scott Tudd

Committer:
tuddman
Date:
Wed Oct 28 22:04:20 2015 +0000
Revision:
0:eea6446bc510
Child:
1:6b84fe382e27
initial commit of wotio lora application using MultiTech mDot

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 0:eea6446bc510 11 static std::string config_network_pass = "iotandbigdata";
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
tuddman 0:eea6446bc510 32 int main() {
tuddman 0:eea6446bc510 33 int32_t ret;
tuddman 0:eea6446bc510 34 mDot* dot;
tuddman 0:eea6446bc510 35 std::vector<uint8_t> data;
tuddman 0:eea6446bc510 36 std::string data_str = "hello!";
tuddman 0:eea6446bc510 37
tuddman 0:eea6446bc510 38
tuddman 0:eea6446bc510 39 // configure the Ticker to blink the LED on 500ms interval
tuddman 0:eea6446bc510 40 tick.attach(&blink, 0.5);
tuddman 0:eea6446bc510 41
tuddman 0:eea6446bc510 42 // print the message on 2s interval
tuddman 0:eea6446bc510 43 while (true) {
tuddman 0:eea6446bc510 44 printf("hello world");
tuddman 0:eea6446bc510 45 wait(2);
tuddman 0:eea6446bc510 46 }
tuddman 0:eea6446bc510 47
tuddman 0:eea6446bc510 48
tuddman 0:eea6446bc510 49 // get a mDot handle
tuddman 0:eea6446bc510 50 dot = mDot::getInstance();
tuddman 0:eea6446bc510 51
tuddman 0:eea6446bc510 52 // print library version information
tuddman 0:eea6446bc510 53 logInfo("version: %s", dot->getId().c_str());
tuddman 0:eea6446bc510 54
tuddman 0:eea6446bc510 55 //*******************************************
tuddman 0:eea6446bc510 56 // configuration
tuddman 0:eea6446bc510 57 //*******************************************
tuddman 0:eea6446bc510 58 // reset to default config so we know what state we're in
tuddman 0:eea6446bc510 59 dot->resetConfig();
tuddman 0:eea6446bc510 60
tuddman 0:eea6446bc510 61 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
tuddman 0:eea6446bc510 62
tuddman 0:eea6446bc510 63 // set up the mDot with our network information: frequency sub band, network name, and network password
tuddman 0:eea6446bc510 64 // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig()
tuddman 0:eea6446bc510 65
tuddman 0:eea6446bc510 66 // frequency sub band is only applicable in the 915 (US) frequency band
tuddman 0:eea6446bc510 67 // 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 68 // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels
tuddman 0:eea6446bc510 69 logInfo("setting frequency sub band");
tuddman 0:eea6446bc510 70 if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
tuddman 0:eea6446bc510 71 logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
tuddman 0:eea6446bc510 72 }
tuddman 0:eea6446bc510 73
tuddman 0:eea6446bc510 74 logInfo("setting network name");
tuddman 0:eea6446bc510 75 if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
tuddman 0:eea6446bc510 76 logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
tuddman 0:eea6446bc510 77 }
tuddman 0:eea6446bc510 78
tuddman 0:eea6446bc510 79 logInfo("setting network password");
tuddman 0:eea6446bc510 80 if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
tuddman 0:eea6446bc510 81 logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
tuddman 0:eea6446bc510 82 }
tuddman 0:eea6446bc510 83
tuddman 0:eea6446bc510 84 // a higher spreading factor allows for longer range but lower throughput
tuddman 0:eea6446bc510 85 // in the 915 (US) frequency band, spreading factors 7 - 10 are available
tuddman 0:eea6446bc510 86 // in the 868 (EU) frequency band, spreading factors 7 - 12 are available
tuddman 0:eea6446bc510 87 logInfo("setting TX spreading factor");
tuddman 0:eea6446bc510 88 if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) {
tuddman 0:eea6446bc510 89 logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
tuddman 0:eea6446bc510 90 }
tuddman 0:eea6446bc510 91
tuddman 0:eea6446bc510 92 // request receive confirmation of packets from the gateway
tuddman 0:eea6446bc510 93 logInfo("enabling ACKs");
tuddman 0:eea6446bc510 94 if ((ret = dot->setAck(1)) != mDot::MDOT_OK) {
tuddman 0:eea6446bc510 95 logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
tuddman 0:eea6446bc510 96 }
tuddman 0:eea6446bc510 97
tuddman 0:eea6446bc510 98 // set join mode to AUTO_OTA so the mDot doesn't have to rejoin after sleeping
tuddman 0:eea6446bc510 99 logInfo("setting join mode to AUTO_OTA");
tuddman 0:eea6446bc510 100 if ((ret = dot->setJoinMode(mDot::AUTO_OTA)) != mDot::MDOT_OK) {
tuddman 0:eea6446bc510 101 logError("failed to set join mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
tuddman 0:eea6446bc510 102 }
tuddman 0:eea6446bc510 103
tuddman 0:eea6446bc510 104 // save this configuration to the mDot's NVM
tuddman 0:eea6446bc510 105 logInfo("saving config");
tuddman 0:eea6446bc510 106 if (! dot->saveConfig()) {
tuddman 0:eea6446bc510 107 logError("failed to save configuration");
tuddman 0:eea6446bc510 108 }
tuddman 0:eea6446bc510 109 //*******************************************
tuddman 0:eea6446bc510 110 // end of configuration
tuddman 0:eea6446bc510 111 //*******************************************
tuddman 0:eea6446bc510 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 0:eea6446bc510 117 // join the network if not joined
tuddman 0:eea6446bc510 118 if (!dot->getNetworkJoinStatus()) {
tuddman 0:eea6446bc510 119 logInfo("network not joined, joining network");
tuddman 0:eea6446bc510 120 if ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
tuddman 0:eea6446bc510 121 logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
tuddman 0:eea6446bc510 122 }
tuddman 0:eea6446bc510 123 }
tuddman 0:eea6446bc510 124 if (dot->getNetworkJoinStatus()) {
tuddman 0:eea6446bc510 125 // send the data
tuddman 0:eea6446bc510 126 // ACKs are enabled by default, so we're expecting to get one back
tuddman 0:eea6446bc510 127 if ((ret = dot->send(data)) != mDot::MDOT_OK) {
tuddman 0:eea6446bc510 128 logError("failed to send %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
tuddman 0:eea6446bc510 129 } else {
tuddman 0:eea6446bc510 130 logInfo("successfully sent data to gateway");
tuddman 0:eea6446bc510 131 }
tuddman 0:eea6446bc510 132 }
tuddman 0:eea6446bc510 133
tuddman 0:eea6446bc510 134 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
tuddman 0:eea6446bc510 135 uint32_t sleep_time = std::max((uint32_t)10000, (uint32_t)dot->getNextTxMs()) / 1000;
tuddman 0:eea6446bc510 136 logInfo("going to sleep...");
tuddman 0:eea6446bc510 137
tuddman 0:eea6446bc510 138 // go to deepsleep and wake up automatically sleep_time seconds later
tuddman 0:eea6446bc510 139 dot->sleep(sleep_time, mDot::RTC_ALARM);
tuddman 0:eea6446bc510 140
tuddman 0:eea6446bc510 141 // go to deepsleep and wake up on rising edge of WKUP pin (PA0/XBEE_CTS/XBEE_DIO7)
tuddman 0:eea6446bc510 142 // dot->sleep(0, mDot::INTERRUPT);
tuddman 0:eea6446bc510 143
tuddman 0:eea6446bc510 144 return 0;
tuddman 0:eea6446bc510 145 }