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@0:5ec8b39fcf53, 2015-08-19 (annotated)
- Committer:
- mfiore
- Date:
- Wed Aug 19 17:03:30 2015 +0000
- Revision:
- 0:5ec8b39fcf53
- Child:
- 1:f2e840f754c8
initial commit
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 | 0:5ec8b39fcf53 | 3 | #include <string> |
mfiore | 0:5ec8b39fcf53 | 4 | #include <vector> |
mfiore | 0:5ec8b39fcf53 | 5 | #include "MTSLog.h" |
mfiore | 0:5ec8b39fcf53 | 6 | |
mfiore | 0:5ec8b39fcf53 | 7 | // these options must match the settings on your Conduit |
mfiore | 0:5ec8b39fcf53 | 8 | static std::string config_network_name = "<lora network id>"; |
mfiore | 0:5ec8b39fcf53 | 9 | static std::string config_network_pass = "<lora network key>"; |
mfiore | 0:5ec8b39fcf53 | 10 | static uint8_t config_frequency_sub_band = 1; |
mfiore | 0:5ec8b39fcf53 | 11 | |
mfiore | 0:5ec8b39fcf53 | 12 | int main() { |
mfiore | 0:5ec8b39fcf53 | 13 | int32_t ret; |
mfiore | 0:5ec8b39fcf53 | 14 | mDot* dot; |
mfiore | 0:5ec8b39fcf53 | 15 | std::vector<uint8_t> data; |
mfiore | 0:5ec8b39fcf53 | 16 | std::string data_str = "hello world!"; |
mfiore | 0:5ec8b39fcf53 | 17 | |
mfiore | 0:5ec8b39fcf53 | 18 | // get a mDot handle |
mfiore | 0:5ec8b39fcf53 | 19 | dot = mDot::getInstance(); |
mfiore | 0:5ec8b39fcf53 | 20 | |
mfiore | 0:5ec8b39fcf53 | 21 | // reset to default config so we know what state we're in |
mfiore | 0:5ec8b39fcf53 | 22 | dot->resetConfig(); |
mfiore | 0:5ec8b39fcf53 | 23 | |
mfiore | 0:5ec8b39fcf53 | 24 | dot->setLogLevel(mts::MTSLog::TRACE_LEVEL); |
mfiore | 0:5ec8b39fcf53 | 25 | |
mfiore | 0:5ec8b39fcf53 | 26 | // print library version information |
mfiore | 0:5ec8b39fcf53 | 27 | logInfo("version: %s", dot->getId().c_str()); |
mfiore | 0:5ec8b39fcf53 | 28 | |
mfiore | 0:5ec8b39fcf53 | 29 | // set up the mDot with our network information |
mfiore | 0:5ec8b39fcf53 | 30 | if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
mfiore | 0:5ec8b39fcf53 | 31 | logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:5ec8b39fcf53 | 32 | } |
mfiore | 0:5ec8b39fcf53 | 33 | if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { |
mfiore | 0:5ec8b39fcf53 | 34 | logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:5ec8b39fcf53 | 35 | } |
mfiore | 0:5ec8b39fcf53 | 36 | if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { |
mfiore | 0:5ec8b39fcf53 | 37 | logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:5ec8b39fcf53 | 38 | } |
mfiore | 0:5ec8b39fcf53 | 39 | if ((ret = dot->setJoinMode(mDot::AUTO_OTA)) != mDot::MDOT_OK) { |
mfiore | 0:5ec8b39fcf53 | 40 | logError("failed to set network join mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:5ec8b39fcf53 | 41 | } |
mfiore | 0:5ec8b39fcf53 | 42 | |
mfiore | 0:5ec8b39fcf53 | 43 | // attempt to join the network |
mfiore | 0:5ec8b39fcf53 | 44 | if ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
mfiore | 0:5ec8b39fcf53 | 45 | logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:5ec8b39fcf53 | 46 | } |
mfiore | 0:5ec8b39fcf53 | 47 | |
mfiore | 0:5ec8b39fcf53 | 48 | // format data for sending to the gateway |
mfiore | 0:5ec8b39fcf53 | 49 | for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++) |
mfiore | 0:5ec8b39fcf53 | 50 | data.push_back((uint8_t) *it); |
mfiore | 0:5ec8b39fcf53 | 51 | |
mfiore | 0:5ec8b39fcf53 | 52 | if (!dot->getNetworkJoinStatus()) { |
mfiore | 0:5ec8b39fcf53 | 53 | if ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
mfiore | 0:5ec8b39fcf53 | 54 | logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:5ec8b39fcf53 | 55 | } |
mfiore | 0:5ec8b39fcf53 | 56 | } |
mfiore | 0:5ec8b39fcf53 | 57 | if (dot->getNetworkJoinStatus()) { |
mfiore | 0:5ec8b39fcf53 | 58 | // send the data |
mfiore | 0:5ec8b39fcf53 | 59 | // ACKs are enabled by default, so we're expecting to get one back |
mfiore | 0:5ec8b39fcf53 | 60 | if ((ret = dot->send(data)) != mDot::MDOT_OK) { |
mfiore | 0:5ec8b39fcf53 | 61 | logError("failed to send %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:5ec8b39fcf53 | 62 | } else { |
mfiore | 0:5ec8b39fcf53 | 63 | logInfo("successfully sent data to gateway"); |
mfiore | 0:5ec8b39fcf53 | 64 | } |
mfiore | 0:5ec8b39fcf53 | 65 | } |
mfiore | 0:5ec8b39fcf53 | 66 | |
mfiore | 0:5ec8b39fcf53 | 67 | uint32_t sleep_time = (dot->getNextTxMs() / 1000) + 10; |
mfiore | 0:5ec8b39fcf53 | 68 | logInfo("going to sleep..."); |
mfiore | 0:5ec8b39fcf53 | 69 | |
mfiore | 0:5ec8b39fcf53 | 70 | // go to sleep and wake up automatically sleep_time seconds later |
mfiore | 0:5ec8b39fcf53 | 71 | dot->sleep(sleep_time, mDot::RTC_ALARM, false); |
mfiore | 0:5ec8b39fcf53 | 72 | |
mfiore | 0:5ec8b39fcf53 | 73 | // go to sleep and wake up on rising edge of WKUP pin (PA0/XBEE_CTS/XBEE_DIO7) |
mfiore | 0:5ec8b39fcf53 | 74 | // dot->sleep(0, mDot::INTERRUPT, false); |
mfiore | 0:5ec8b39fcf53 | 75 | |
mfiore | 0:5ec8b39fcf53 | 76 | return 0; |
mfiore | 0:5ec8b39fcf53 | 77 | } |