Connecting to gateway, sending packets with a MultiTech mDot and entering deepsleep mode between transmissions.
Dependencies: libmDot mbed-rtos mbed
Fork of libmDot_sleep by
main.cpp@4:ac599fe6bc41, 2015-09-30 (annotated)
- Committer:
- mfiore
- Date:
- Wed Sep 30 20:06:52 2015 +0000
- Revision:
- 4:ac599fe6bc41
- Parent:
- 2:4f4f5307d9e4
clean up and clarify code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfiore | 0:5ec8b39fcf53 | 1 | #include "mbed.h" |
mfiore | 0:5ec8b39fcf53 | 2 | #include "mDot.h" |
mfiore | 4:ac599fe6bc41 | 3 | #include "MTSLog.h" |
mfiore | 0:5ec8b39fcf53 | 4 | #include <string> |
mfiore | 0:5ec8b39fcf53 | 5 | #include <vector> |
mfiore | 4:ac599fe6bc41 | 6 | #include <algorithm> |
mfiore | 0:5ec8b39fcf53 | 7 | |
mfiore | 0:5ec8b39fcf53 | 8 | // these options must match the settings on your Conduit |
mfiore | 1:f2e840f754c8 | 9 | // uncomment the following lines and edit their values to match your configuration |
mfiore | 1:f2e840f754c8 | 10 | //static std::string config_network_name = "<lora network id>"; |
mfiore | 1:f2e840f754c8 | 11 | //static std::string config_network_pass = "<lora network key>"; |
mfiore | 1:f2e840f754c8 | 12 | //static uint8_t config_frequency_sub_band = 1; |
mfiore | 0:5ec8b39fcf53 | 13 | |
mfiore | 0:5ec8b39fcf53 | 14 | int main() { |
mfiore | 0:5ec8b39fcf53 | 15 | int32_t ret; |
mfiore | 0:5ec8b39fcf53 | 16 | mDot* dot; |
mfiore | 0:5ec8b39fcf53 | 17 | std::vector<uint8_t> data; |
mfiore | 4:ac599fe6bc41 | 18 | std::string data_str = "hello!"; |
mfiore | 4:ac599fe6bc41 | 19 | |
mfiore | 0:5ec8b39fcf53 | 20 | // get a mDot handle |
mfiore | 0:5ec8b39fcf53 | 21 | dot = mDot::getInstance(); |
mfiore | 0:5ec8b39fcf53 | 22 | |
mfiore | 0:5ec8b39fcf53 | 23 | // print library version information |
mfiore | 0:5ec8b39fcf53 | 24 | logInfo("version: %s", dot->getId().c_str()); |
mfiore | 0:5ec8b39fcf53 | 25 | |
mfiore | 1:f2e840f754c8 | 26 | //******************************************* |
mfiore | 1:f2e840f754c8 | 27 | // configuration |
mfiore | 1:f2e840f754c8 | 28 | //******************************************* |
mfiore | 1:f2e840f754c8 | 29 | // reset to default config so we know what state we're in |
mfiore | 1:f2e840f754c8 | 30 | dot->resetConfig(); |
mfiore | 1:f2e840f754c8 | 31 | |
mfiore | 4:ac599fe6bc41 | 32 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
mfiore | 1:f2e840f754c8 | 33 | |
mfiore | 1:f2e840f754c8 | 34 | // set up the mDot with our network information: frequency sub band, network name, and network password |
mfiore | 1:f2e840f754c8 | 35 | // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig() |
mfiore | 4:ac599fe6bc41 | 36 | |
mfiore | 4:ac599fe6bc41 | 37 | // frequency sub band is only applicable in the 915 (US) frequency band |
mfiore | 4:ac599fe6bc41 | 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:ac599fe6bc41 | 39 | // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels |
mfiore | 1:f2e840f754c8 | 40 | logInfo("setting frequency sub band"); |
mfiore | 0:5ec8b39fcf53 | 41 | if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
mfiore | 0:5ec8b39fcf53 | 42 | logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:5ec8b39fcf53 | 43 | } |
mfiore | 4:ac599fe6bc41 | 44 | |
mfiore | 1:f2e840f754c8 | 45 | logInfo("setting network name"); |
mfiore | 0:5ec8b39fcf53 | 46 | if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { |
mfiore | 0:5ec8b39fcf53 | 47 | logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:5ec8b39fcf53 | 48 | } |
mfiore | 4:ac599fe6bc41 | 49 | |
mfiore | 1:f2e840f754c8 | 50 | logInfo("setting network password"); |
mfiore | 0:5ec8b39fcf53 | 51 | if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { |
mfiore | 0:5ec8b39fcf53 | 52 | logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:5ec8b39fcf53 | 53 | } |
mfiore | 4:ac599fe6bc41 | 54 | |
mfiore | 4:ac599fe6bc41 | 55 | // a higher spreading factor allows for longer range but lower throughput |
mfiore | 4:ac599fe6bc41 | 56 | // in the 915 (US) frequency band, spreading factors 7 - 10 are available |
mfiore | 4:ac599fe6bc41 | 57 | // in the 868 (EU) frequency band, spreading factors 7 - 12 are available |
mfiore | 4:ac599fe6bc41 | 58 | logInfo("setting TX spreading factor"); |
mfiore | 4:ac599fe6bc41 | 59 | if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) { |
mfiore | 4:ac599fe6bc41 | 60 | logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 4:ac599fe6bc41 | 61 | } |
mfiore | 4:ac599fe6bc41 | 62 | |
mfiore | 4:ac599fe6bc41 | 63 | // request receive confirmation of packets from the gateway |
mfiore | 4:ac599fe6bc41 | 64 | logInfo("enabling ACKs"); |
mfiore | 4:ac599fe6bc41 | 65 | if ((ret = dot->setAck(1)) != mDot::MDOT_OK) { |
mfiore | 4:ac599fe6bc41 | 66 | logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 4:ac599fe6bc41 | 67 | } |
mfiore | 4:ac599fe6bc41 | 68 | |
mfiore | 4:ac599fe6bc41 | 69 | // set join mode to AUTO_OTA so the mDot doesn't have to rejoin after sleeping |
mfiore | 4:ac599fe6bc41 | 70 | logInfo("setting join mode to AUTO_OTA"); |
mfiore | 4:ac599fe6bc41 | 71 | if ((ret = dot->setJoinMode(mDot::AUTO_OTA)) != mDot::MDOT_OK) { |
mfiore | 4:ac599fe6bc41 | 72 | logError("failed to set join mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 4:ac599fe6bc41 | 73 | } |
mfiore | 4:ac599fe6bc41 | 74 | |
mfiore | 4:ac599fe6bc41 | 75 | // save this configuration to the mDot's NVM |
mfiore | 1:f2e840f754c8 | 76 | logInfo("saving config"); |
mfiore | 1:f2e840f754c8 | 77 | if (! dot->saveConfig()) { |
mfiore | 1:f2e840f754c8 | 78 | logError("failed to save configuration"); |
mfiore | 0:5ec8b39fcf53 | 79 | } |
mfiore | 1:f2e840f754c8 | 80 | //******************************************* |
mfiore | 1:f2e840f754c8 | 81 | // end of configuration |
mfiore | 1:f2e840f754c8 | 82 | //******************************************* |
mfiore | 0:5ec8b39fcf53 | 83 | |
mfiore | 0:5ec8b39fcf53 | 84 | // format data for sending to the gateway |
mfiore | 0:5ec8b39fcf53 | 85 | for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++) |
mfiore | 0:5ec8b39fcf53 | 86 | data.push_back((uint8_t) *it); |
mfiore | 0:5ec8b39fcf53 | 87 | |
mfiore | 4:ac599fe6bc41 | 88 | // join the network if not joined |
mfiore | 0:5ec8b39fcf53 | 89 | if (!dot->getNetworkJoinStatus()) { |
mfiore | 1:f2e840f754c8 | 90 | logInfo("network not joined, joining network"); |
mfiore | 0:5ec8b39fcf53 | 91 | if ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
mfiore | 0:5ec8b39fcf53 | 92 | logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:5ec8b39fcf53 | 93 | } |
mfiore | 0:5ec8b39fcf53 | 94 | } |
mfiore | 0:5ec8b39fcf53 | 95 | if (dot->getNetworkJoinStatus()) { |
mfiore | 0:5ec8b39fcf53 | 96 | // send the data |
mfiore | 0:5ec8b39fcf53 | 97 | // ACKs are enabled by default, so we're expecting to get one back |
mfiore | 0:5ec8b39fcf53 | 98 | if ((ret = dot->send(data)) != mDot::MDOT_OK) { |
mfiore | 0:5ec8b39fcf53 | 99 | logError("failed to send %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:5ec8b39fcf53 | 100 | } else { |
mfiore | 0:5ec8b39fcf53 | 101 | logInfo("successfully sent data to gateway"); |
mfiore | 0:5ec8b39fcf53 | 102 | } |
mfiore | 0:5ec8b39fcf53 | 103 | } |
mfiore | 0:5ec8b39fcf53 | 104 | |
mfiore | 4:ac599fe6bc41 | 105 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
mfiore | 4:ac599fe6bc41 | 106 | uint32_t sleep_time = std::max((uint32_t)10000, (uint32_t)dot->getNextTxMs()) / 1000; |
mfiore | 0:5ec8b39fcf53 | 107 | logInfo("going to sleep..."); |
mfiore | 0:5ec8b39fcf53 | 108 | |
mfiore | 2:4f4f5307d9e4 | 109 | // go to deepsleep and wake up automatically sleep_time seconds later |
mfiore | 2:4f4f5307d9e4 | 110 | dot->sleep(sleep_time, mDot::RTC_ALARM); |
mfiore | 0:5ec8b39fcf53 | 111 | |
mfiore | 2:4f4f5307d9e4 | 112 | // go to deepsleep and wake up on rising edge of WKUP pin (PA0/XBEE_CTS/XBEE_DIO7) |
mfiore | 2:4f4f5307d9e4 | 113 | // dot->sleep(0, mDot::INTERRUPT); |
mfiore | 0:5ec8b39fcf53 | 114 | |
mfiore | 0:5ec8b39fcf53 | 115 | return 0; |
mfiore | 0:5ec8b39fcf53 | 116 | } |