SOC-1
Dependencies: libmDot mbed-rtos mbed
Fork of mDot_LoRa_Sleep_Example by
main.cpp@5:2099d1d64f85, 2015-10-24 (annotated)
- Committer:
- socie123
- Date:
- Sat Oct 24 04:10:59 2015 +0000
- Revision:
- 5:2099d1d64f85
- Parent:
- 4:ac599fe6bc41
SOC1
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 |
socie123 | 5:2099d1d64f85 | 10 | static std::string config_network_name = "watchlink"; |
socie123 | 5:2099d1d64f85 | 11 | static std::string config_network_pass = "watchlink"; |
socie123 | 5:2099d1d64f85 | 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; |
socie123 | 5:2099d1d64f85 | 18 | //bool send(const std::string text); |
socie123 | 5:2099d1d64f85 | 19 | //std::char<uint8_t> latestdata; |
socie123 | 5:2099d1d64f85 | 20 | |
socie123 | 5:2099d1d64f85 | 21 | |
socie123 | 5:2099d1d64f85 | 22 | AnalogIn temperatureSensor(PC_1); //A2 on Arduino base shield |
mfiore | 4:ac599fe6bc41 | 23 | |
mfiore | 0:5ec8b39fcf53 | 24 | // get a mDot handle |
mfiore | 0:5ec8b39fcf53 | 25 | dot = mDot::getInstance(); |
mfiore | 0:5ec8b39fcf53 | 26 | |
mfiore | 0:5ec8b39fcf53 | 27 | // print library version information |
mfiore | 0:5ec8b39fcf53 | 28 | logInfo("version: %s", dot->getId().c_str()); |
mfiore | 0:5ec8b39fcf53 | 29 | |
mfiore | 1:f2e840f754c8 | 30 | //******************************************* |
mfiore | 1:f2e840f754c8 | 31 | // configuration |
mfiore | 1:f2e840f754c8 | 32 | //******************************************* |
mfiore | 1:f2e840f754c8 | 33 | // reset to default config so we know what state we're in |
mfiore | 1:f2e840f754c8 | 34 | dot->resetConfig(); |
mfiore | 1:f2e840f754c8 | 35 | |
mfiore | 4:ac599fe6bc41 | 36 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
mfiore | 1:f2e840f754c8 | 37 | |
mfiore | 1:f2e840f754c8 | 38 | // set up the mDot with our network information: frequency sub band, network name, and network password |
mfiore | 1:f2e840f754c8 | 39 | // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig() |
mfiore | 4:ac599fe6bc41 | 40 | |
mfiore | 4:ac599fe6bc41 | 41 | // frequency sub band is only applicable in the 915 (US) frequency band |
mfiore | 4:ac599fe6bc41 | 42 | // 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 | 43 | // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels |
mfiore | 1:f2e840f754c8 | 44 | logInfo("setting frequency sub band"); |
mfiore | 0:5ec8b39fcf53 | 45 | if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
mfiore | 0:5ec8b39fcf53 | 46 | logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:5ec8b39fcf53 | 47 | } |
mfiore | 4:ac599fe6bc41 | 48 | |
mfiore | 1:f2e840f754c8 | 49 | logInfo("setting network name"); |
mfiore | 0:5ec8b39fcf53 | 50 | if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { |
mfiore | 0:5ec8b39fcf53 | 51 | logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:5ec8b39fcf53 | 52 | } |
mfiore | 4:ac599fe6bc41 | 53 | |
mfiore | 1:f2e840f754c8 | 54 | logInfo("setting network password"); |
mfiore | 0:5ec8b39fcf53 | 55 | if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { |
mfiore | 0:5ec8b39fcf53 | 56 | logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:5ec8b39fcf53 | 57 | } |
mfiore | 4:ac599fe6bc41 | 58 | |
mfiore | 4:ac599fe6bc41 | 59 | // a higher spreading factor allows for longer range but lower throughput |
mfiore | 4:ac599fe6bc41 | 60 | // in the 915 (US) frequency band, spreading factors 7 - 10 are available |
mfiore | 4:ac599fe6bc41 | 61 | // in the 868 (EU) frequency band, spreading factors 7 - 12 are available |
mfiore | 4:ac599fe6bc41 | 62 | logInfo("setting TX spreading factor"); |
mfiore | 4:ac599fe6bc41 | 63 | if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) { |
mfiore | 4:ac599fe6bc41 | 64 | logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 4:ac599fe6bc41 | 65 | } |
mfiore | 4:ac599fe6bc41 | 66 | |
mfiore | 4:ac599fe6bc41 | 67 | // request receive confirmation of packets from the gateway |
mfiore | 4:ac599fe6bc41 | 68 | logInfo("enabling ACKs"); |
mfiore | 4:ac599fe6bc41 | 69 | if ((ret = dot->setAck(1)) != mDot::MDOT_OK) { |
mfiore | 4:ac599fe6bc41 | 70 | logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 4:ac599fe6bc41 | 71 | } |
mfiore | 4:ac599fe6bc41 | 72 | |
mfiore | 4:ac599fe6bc41 | 73 | // set join mode to AUTO_OTA so the mDot doesn't have to rejoin after sleeping |
mfiore | 4:ac599fe6bc41 | 74 | logInfo("setting join mode to AUTO_OTA"); |
mfiore | 4:ac599fe6bc41 | 75 | if ((ret = dot->setJoinMode(mDot::AUTO_OTA)) != mDot::MDOT_OK) { |
mfiore | 4:ac599fe6bc41 | 76 | logError("failed to set join mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 4:ac599fe6bc41 | 77 | } |
mfiore | 4:ac599fe6bc41 | 78 | |
mfiore | 4:ac599fe6bc41 | 79 | // save this configuration to the mDot's NVM |
mfiore | 1:f2e840f754c8 | 80 | logInfo("saving config"); |
mfiore | 1:f2e840f754c8 | 81 | if (! dot->saveConfig()) { |
mfiore | 1:f2e840f754c8 | 82 | logError("failed to save configuration"); |
mfiore | 0:5ec8b39fcf53 | 83 | } |
mfiore | 1:f2e840f754c8 | 84 | //******************************************* |
mfiore | 1:f2e840f754c8 | 85 | // end of configuration |
mfiore | 1:f2e840f754c8 | 86 | //******************************************* |
socie123 | 5:2099d1d64f85 | 87 | |
socie123 | 5:2099d1d64f85 | 88 | |
socie123 | 5:2099d1d64f85 | 89 | uint16_t analogReading; |
socie123 | 5:2099d1d64f85 | 90 | analogReading = temperatureSensor.read_u16(); |
socie123 | 5:2099d1d64f85 | 91 | |
socie123 | 5:2099d1d64f85 | 92 | char latestdata[100]; |
socie123 | 5:2099d1d64f85 | 93 | //std::vector<uint8_t> data; |
socie123 | 5:2099d1d64f85 | 94 | |
socie123 | 5:2099d1d64f85 | 95 | //data = latestdata ; |
socie123 | 5:2099d1d64f85 | 96 | sprintf(latestdata, "temp: %d", analogReading); |
socie123 | 5:2099d1d64f85 | 97 | printf("%s\r\n", latestdata); |
socie123 | 5:2099d1d64f85 | 98 | |
mfiore | 0:5ec8b39fcf53 | 99 | |
mfiore | 0:5ec8b39fcf53 | 100 | // format data for sending to the gateway |
socie123 | 5:2099d1d64f85 | 101 | //for (std::string::iterator it = data.begin(); it != data.end(); it++) |
socie123 | 5:2099d1d64f85 | 102 | //data.push_back((uint8_t) *it); |
mfiore | 0:5ec8b39fcf53 | 103 | |
mfiore | 4:ac599fe6bc41 | 104 | // join the network if not joined |
mfiore | 0:5ec8b39fcf53 | 105 | if (!dot->getNetworkJoinStatus()) { |
mfiore | 1:f2e840f754c8 | 106 | logInfo("network not joined, joining network"); |
mfiore | 0:5ec8b39fcf53 | 107 | if ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
mfiore | 0:5ec8b39fcf53 | 108 | logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:5ec8b39fcf53 | 109 | } |
mfiore | 0:5ec8b39fcf53 | 110 | } |
mfiore | 0:5ec8b39fcf53 | 111 | if (dot->getNetworkJoinStatus()) { |
mfiore | 0:5ec8b39fcf53 | 112 | // send the data |
mfiore | 0:5ec8b39fcf53 | 113 | // ACKs are enabled by default, so we're expecting to get one back |
socie123 | 5:2099d1d64f85 | 114 | std::vector<uint8_t> latestdata;//(text.begin(), text.end()); |
socie123 | 5:2099d1d64f85 | 115 | if ((ret = dot->send(latestdata)) != mDot::MDOT_OK) { |
mfiore | 0:5ec8b39fcf53 | 116 | logError("failed to send %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:5ec8b39fcf53 | 117 | } else { |
mfiore | 0:5ec8b39fcf53 | 118 | logInfo("successfully sent data to gateway"); |
mfiore | 0:5ec8b39fcf53 | 119 | } |
mfiore | 0:5ec8b39fcf53 | 120 | } |
mfiore | 0:5ec8b39fcf53 | 121 | |
mfiore | 4:ac599fe6bc41 | 122 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
mfiore | 4:ac599fe6bc41 | 123 | uint32_t sleep_time = std::max((uint32_t)10000, (uint32_t)dot->getNextTxMs()) / 1000; |
mfiore | 0:5ec8b39fcf53 | 124 | logInfo("going to sleep..."); |
mfiore | 0:5ec8b39fcf53 | 125 | |
mfiore | 2:4f4f5307d9e4 | 126 | // go to deepsleep and wake up automatically sleep_time seconds later |
mfiore | 2:4f4f5307d9e4 | 127 | dot->sleep(sleep_time, mDot::RTC_ALARM); |
mfiore | 0:5ec8b39fcf53 | 128 | |
mfiore | 2:4f4f5307d9e4 | 129 | // go to deepsleep and wake up on rising edge of WKUP pin (PA0/XBEE_CTS/XBEE_DIO7) |
mfiore | 2:4f4f5307d9e4 | 130 | // dot->sleep(0, mDot::INTERRUPT); |
mfiore | 0:5ec8b39fcf53 | 131 | |
mfiore | 0:5ec8b39fcf53 | 132 | return 0; |
mfiore | 0:5ec8b39fcf53 | 133 | } |