SOC-1
Dependencies: libmDot mbed-rtos mbed
Fork of mDot_LoRa_Sleep_Example by
main.cpp@1:f2e840f754c8, 2015-08-25 (annotated)
- Committer:
- mfiore
- Date:
- Tue Aug 25 21:36:47 2015 +0000
- Revision:
- 1:f2e840f754c8
- Parent:
- 0:5ec8b39fcf53
- Child:
- 2:4f4f5307d9e4
clean up code, add more comments about configuration
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 | 1:f2e840f754c8 | 8 | // uncomment the following lines and edit their values to match your configuration |
mfiore | 1:f2e840f754c8 | 9 | //static std::string config_network_name = "<lora network id>"; |
mfiore | 1:f2e840f754c8 | 10 | //static std::string config_network_pass = "<lora network key>"; |
mfiore | 1:f2e840f754c8 | 11 | //static uint8_t config_frequency_sub_band = 1; |
mfiore | 0:5ec8b39fcf53 | 12 | |
mfiore | 0:5ec8b39fcf53 | 13 | int main() { |
mfiore | 0:5ec8b39fcf53 | 14 | int32_t ret; |
mfiore | 0:5ec8b39fcf53 | 15 | mDot* dot; |
mfiore | 0:5ec8b39fcf53 | 16 | std::vector<uint8_t> data; |
mfiore | 0:5ec8b39fcf53 | 17 | std::string data_str = "hello world!"; |
mfiore | 0:5ec8b39fcf53 | 18 | |
mfiore | 0:5ec8b39fcf53 | 19 | // get a mDot handle |
mfiore | 0:5ec8b39fcf53 | 20 | dot = mDot::getInstance(); |
mfiore | 0:5ec8b39fcf53 | 21 | |
mfiore | 0:5ec8b39fcf53 | 22 | // print library version information |
mfiore | 0:5ec8b39fcf53 | 23 | logInfo("version: %s", dot->getId().c_str()); |
mfiore | 0:5ec8b39fcf53 | 24 | |
mfiore | 1:f2e840f754c8 | 25 | //******************************************* |
mfiore | 1:f2e840f754c8 | 26 | // configuration |
mfiore | 1:f2e840f754c8 | 27 | //******************************************* |
mfiore | 1:f2e840f754c8 | 28 | // reset to default config so we know what state we're in |
mfiore | 1:f2e840f754c8 | 29 | dot->resetConfig(); |
mfiore | 1:f2e840f754c8 | 30 | |
mfiore | 1:f2e840f754c8 | 31 | dot->setLogLevel(mts::MTSLog::TRACE_LEVEL); |
mfiore | 1:f2e840f754c8 | 32 | |
mfiore | 1:f2e840f754c8 | 33 | // set up the mDot with our network information: frequency sub band, network name, and network password |
mfiore | 1:f2e840f754c8 | 34 | // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig() |
mfiore | 1:f2e840f754c8 | 35 | logInfo("setting frequency sub band"); |
mfiore | 0:5ec8b39fcf53 | 36 | if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
mfiore | 0:5ec8b39fcf53 | 37 | logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:5ec8b39fcf53 | 38 | } |
mfiore | 1:f2e840f754c8 | 39 | logInfo("setting network name"); |
mfiore | 0:5ec8b39fcf53 | 40 | if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { |
mfiore | 0:5ec8b39fcf53 | 41 | logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:5ec8b39fcf53 | 42 | } |
mfiore | 1:f2e840f754c8 | 43 | logInfo("setting network password"); |
mfiore | 0:5ec8b39fcf53 | 44 | if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { |
mfiore | 0:5ec8b39fcf53 | 45 | logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:5ec8b39fcf53 | 46 | } |
mfiore | 1:f2e840f754c8 | 47 | logInfo("saving config"); |
mfiore | 1:f2e840f754c8 | 48 | if (! dot->saveConfig()) { |
mfiore | 1:f2e840f754c8 | 49 | logError("failed to save configuration"); |
mfiore | 0:5ec8b39fcf53 | 50 | } |
mfiore | 1:f2e840f754c8 | 51 | //******************************************* |
mfiore | 1:f2e840f754c8 | 52 | // end of configuration |
mfiore | 1:f2e840f754c8 | 53 | //******************************************* |
mfiore | 1:f2e840f754c8 | 54 | |
mfiore | 0:5ec8b39fcf53 | 55 | // attempt to join the network |
mfiore | 1:f2e840f754c8 | 56 | logInfo("joining network"); |
mfiore | 0:5ec8b39fcf53 | 57 | if ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
mfiore | 0:5ec8b39fcf53 | 58 | logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:5ec8b39fcf53 | 59 | } |
mfiore | 0:5ec8b39fcf53 | 60 | |
mfiore | 0:5ec8b39fcf53 | 61 | // format data for sending to the gateway |
mfiore | 0:5ec8b39fcf53 | 62 | for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++) |
mfiore | 0:5ec8b39fcf53 | 63 | data.push_back((uint8_t) *it); |
mfiore | 0:5ec8b39fcf53 | 64 | |
mfiore | 0:5ec8b39fcf53 | 65 | if (!dot->getNetworkJoinStatus()) { |
mfiore | 1:f2e840f754c8 | 66 | logInfo("network not joined, joining network"); |
mfiore | 0:5ec8b39fcf53 | 67 | if ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
mfiore | 0:5ec8b39fcf53 | 68 | logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:5ec8b39fcf53 | 69 | } |
mfiore | 0:5ec8b39fcf53 | 70 | } |
mfiore | 0:5ec8b39fcf53 | 71 | if (dot->getNetworkJoinStatus()) { |
mfiore | 0:5ec8b39fcf53 | 72 | // send the data |
mfiore | 0:5ec8b39fcf53 | 73 | // ACKs are enabled by default, so we're expecting to get one back |
mfiore | 0:5ec8b39fcf53 | 74 | if ((ret = dot->send(data)) != mDot::MDOT_OK) { |
mfiore | 0:5ec8b39fcf53 | 75 | logError("failed to send %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:5ec8b39fcf53 | 76 | } else { |
mfiore | 0:5ec8b39fcf53 | 77 | logInfo("successfully sent data to gateway"); |
mfiore | 0:5ec8b39fcf53 | 78 | } |
mfiore | 0:5ec8b39fcf53 | 79 | } |
mfiore | 0:5ec8b39fcf53 | 80 | |
mfiore | 0:5ec8b39fcf53 | 81 | uint32_t sleep_time = (dot->getNextTxMs() / 1000) + 10; |
mfiore | 0:5ec8b39fcf53 | 82 | logInfo("going to sleep..."); |
mfiore | 0:5ec8b39fcf53 | 83 | |
mfiore | 0:5ec8b39fcf53 | 84 | // go to sleep and wake up automatically sleep_time seconds later |
mfiore | 0:5ec8b39fcf53 | 85 | dot->sleep(sleep_time, mDot::RTC_ALARM, false); |
mfiore | 0:5ec8b39fcf53 | 86 | |
mfiore | 0:5ec8b39fcf53 | 87 | // go to sleep and wake up on rising edge of WKUP pin (PA0/XBEE_CTS/XBEE_DIO7) |
mfiore | 0:5ec8b39fcf53 | 88 | // dot->sleep(0, mDot::INTERRUPT, false); |
mfiore | 0:5ec8b39fcf53 | 89 | |
mfiore | 0:5ec8b39fcf53 | 90 | return 0; |
mfiore | 0:5ec8b39fcf53 | 91 | } |