For Wonjun and Jyunghun
Dependencies: libmDot mbed-rtos mbed
main.cpp@0:f54d103ce9b6, 2017-03-24 (annotated)
- Committer:
- minhnn89
- Date:
- Fri Mar 24 06:28:11 2017 +0000
- Revision:
- 0:f54d103ce9b6
For wonjun and Jyunghun
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
minhnn89 | 0:f54d103ce9b6 | 1 | #include "mbed.h" |
minhnn89 | 0:f54d103ce9b6 | 2 | #include "mDot.h" |
minhnn89 | 0:f54d103ce9b6 | 3 | #include "MTSLog.h" |
minhnn89 | 0:f54d103ce9b6 | 4 | #include <string> |
minhnn89 | 0:f54d103ce9b6 | 5 | #include <vector> |
minhnn89 | 0:f54d103ce9b6 | 6 | #include <algorithm> |
minhnn89 | 0:f54d103ce9b6 | 7 | |
minhnn89 | 0:f54d103ce9b6 | 8 | // these options must match the settings on your Conduit |
minhnn89 | 0:f54d103ce9b6 | 9 | // uncomment the following lines and edit their values to match your configuration |
minhnn89 | 0:f54d103ce9b6 | 10 | static std::string config_network_name = "wonjun123"; |
minhnn89 | 0:f54d103ce9b6 | 11 | static std::string config_network_pass = "123123123"; |
minhnn89 | 0:f54d103ce9b6 | 12 | static uint8_t config_frequency_sub_band = 7; |
minhnn89 | 0:f54d103ce9b6 | 13 | |
minhnn89 | 0:f54d103ce9b6 | 14 | // r0 r1 r2 r3 c0 c1 c2 c3 |
minhnn89 | 0:f54d103ce9b6 | 15 | //Keypad keypad(p17, p16, p15, p14, p13, p12, p11, p10); |
minhnn89 | 0:f54d103ce9b6 | 16 | |
minhnn89 | 0:f54d103ce9b6 | 17 | float speed = 5.1; // km/h |
minhnn89 | 0:f54d103ce9b6 | 18 | bool isIncreasing = false; |
minhnn89 | 0:f54d103ce9b6 | 19 | bool isDecreasing = false; |
minhnn89 | 0:f54d103ce9b6 | 20 | |
minhnn89 | 0:f54d103ce9b6 | 21 | // Callback funciton of speed increasing button pressing |
minhnn89 | 0:f54d103ce9b6 | 22 | void cbIncreaseSpeed() |
minhnn89 | 0:f54d103ce9b6 | 23 | { |
minhnn89 | 0:f54d103ce9b6 | 24 | speed = speed + 0.1f; |
minhnn89 | 0:f54d103ce9b6 | 25 | if (speed > 19) speed = 19; |
minhnn89 | 0:f54d103ce9b6 | 26 | } |
minhnn89 | 0:f54d103ce9b6 | 27 | // Callback funciton of speed decreasing button pressing |
minhnn89 | 0:f54d103ce9b6 | 28 | void cbDecreaseSpeed() |
minhnn89 | 0:f54d103ce9b6 | 29 | { |
minhnn89 | 0:f54d103ce9b6 | 30 | speed = speed - 0.1f; |
minhnn89 | 0:f54d103ce9b6 | 31 | if (speed < 0 ) speed = 0; |
minhnn89 | 0:f54d103ce9b6 | 32 | } |
minhnn89 | 0:f54d103ce9b6 | 33 | |
minhnn89 | 0:f54d103ce9b6 | 34 | int main() { |
minhnn89 | 0:f54d103ce9b6 | 35 | int32_t ret; |
minhnn89 | 0:f54d103ce9b6 | 36 | mDot* dot; |
minhnn89 | 0:f54d103ce9b6 | 37 | //std::vector<uint8_t> data; |
minhnn89 | 0:f54d103ce9b6 | 38 | std::string data_str = "data!"; |
minhnn89 | 0:f54d103ce9b6 | 39 | |
minhnn89 | 0:f54d103ce9b6 | 40 | InterruptIn incBtn(PA_2); // increase speed button |
minhnn89 | 0:f54d103ce9b6 | 41 | InterruptIn decBtn(PA_0); // decrease speed button |
minhnn89 | 0:f54d103ce9b6 | 42 | |
minhnn89 | 0:f54d103ce9b6 | 43 | decBtn.rise(&cbIncreaseSpeed); |
minhnn89 | 0:f54d103ce9b6 | 44 | //decBtn.fall(&cbDecreaseSpeed); |
minhnn89 | 0:f54d103ce9b6 | 45 | |
minhnn89 | 0:f54d103ce9b6 | 46 | // get a mDot handle |
minhnn89 | 0:f54d103ce9b6 | 47 | dot = mDot::getInstance(); |
minhnn89 | 0:f54d103ce9b6 | 48 | |
minhnn89 | 0:f54d103ce9b6 | 49 | // print library version information |
minhnn89 | 0:f54d103ce9b6 | 50 | logError("dafdfadafdfdaf"); |
minhnn89 | 0:f54d103ce9b6 | 51 | logInfo("version: %s", dot->getId().c_str()); |
minhnn89 | 0:f54d103ce9b6 | 52 | |
minhnn89 | 0:f54d103ce9b6 | 53 | //******************************************* |
minhnn89 | 0:f54d103ce9b6 | 54 | // configuration |
minhnn89 | 0:f54d103ce9b6 | 55 | //******************************************* |
minhnn89 | 0:f54d103ce9b6 | 56 | // reset to default config so we know what state we're in |
minhnn89 | 0:f54d103ce9b6 | 57 | dot->resetConfig(); |
minhnn89 | 0:f54d103ce9b6 | 58 | |
minhnn89 | 0:f54d103ce9b6 | 59 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
minhnn89 | 0:f54d103ce9b6 | 60 | |
minhnn89 | 0:f54d103ce9b6 | 61 | // set up the mDot with our network information: frequency sub band, network name, and network password |
minhnn89 | 0:f54d103ce9b6 | 62 | // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig() |
minhnn89 | 0:f54d103ce9b6 | 63 | |
minhnn89 | 0:f54d103ce9b6 | 64 | // frequency sub band is only applicable in the 915 (US) frequency band |
minhnn89 | 0:f54d103ce9b6 | 65 | // 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 |
minhnn89 | 0:f54d103ce9b6 | 66 | // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels |
minhnn89 | 0:f54d103ce9b6 | 67 | logInfo("setting frequency sub band"); |
minhnn89 | 0:f54d103ce9b6 | 68 | if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
minhnn89 | 0:f54d103ce9b6 | 69 | logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
minhnn89 | 0:f54d103ce9b6 | 70 | } |
minhnn89 | 0:f54d103ce9b6 | 71 | |
minhnn89 | 0:f54d103ce9b6 | 72 | logInfo("setting network name"); |
minhnn89 | 0:f54d103ce9b6 | 73 | if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { |
minhnn89 | 0:f54d103ce9b6 | 74 | logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
minhnn89 | 0:f54d103ce9b6 | 75 | } |
minhnn89 | 0:f54d103ce9b6 | 76 | |
minhnn89 | 0:f54d103ce9b6 | 77 | logInfo("setting network password"); |
minhnn89 | 0:f54d103ce9b6 | 78 | if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { |
minhnn89 | 0:f54d103ce9b6 | 79 | logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
minhnn89 | 0:f54d103ce9b6 | 80 | } |
minhnn89 | 0:f54d103ce9b6 | 81 | |
minhnn89 | 0:f54d103ce9b6 | 82 | // a higher spreading factor allows for longer range but lower throughput |
minhnn89 | 0:f54d103ce9b6 | 83 | // in the 915 (US) frequency band, spreading factors 7 - 10 are available |
minhnn89 | 0:f54d103ce9b6 | 84 | // in the 868 (EU) frequency band, spreading factors 7 - 12 are available |
minhnn89 | 0:f54d103ce9b6 | 85 | logInfo("setting TX spreading factor"); |
minhnn89 | 0:f54d103ce9b6 | 86 | if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) { |
minhnn89 | 0:f54d103ce9b6 | 87 | logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
minhnn89 | 0:f54d103ce9b6 | 88 | } |
minhnn89 | 0:f54d103ce9b6 | 89 | |
minhnn89 | 0:f54d103ce9b6 | 90 | // request receive confirmation of packets from the gateway |
minhnn89 | 0:f54d103ce9b6 | 91 | logInfo("enabling ACKs"); |
minhnn89 | 0:f54d103ce9b6 | 92 | if ((ret = dot->setAck(1)) != mDot::MDOT_OK) { |
minhnn89 | 0:f54d103ce9b6 | 93 | logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
minhnn89 | 0:f54d103ce9b6 | 94 | } |
minhnn89 | 0:f54d103ce9b6 | 95 | |
minhnn89 | 0:f54d103ce9b6 | 96 | // set join mode to AUTO_OTA so the mDot doesn't have to rejoin after sleeping |
minhnn89 | 0:f54d103ce9b6 | 97 | logInfo("setting join mode to AUTO_OTA"); |
minhnn89 | 0:f54d103ce9b6 | 98 | if ((ret = dot->setJoinMode(mDot::AUTO_OTA)) != mDot::MDOT_OK) { |
minhnn89 | 0:f54d103ce9b6 | 99 | logError("failed to set join mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
minhnn89 | 0:f54d103ce9b6 | 100 | } |
minhnn89 | 0:f54d103ce9b6 | 101 | |
minhnn89 | 0:f54d103ce9b6 | 102 | // save this configuration to the mDot's NVM |
minhnn89 | 0:f54d103ce9b6 | 103 | logInfo("saving config"); |
minhnn89 | 0:f54d103ce9b6 | 104 | if (! dot->saveConfig()) { |
minhnn89 | 0:f54d103ce9b6 | 105 | logError("failed to save configuration"); |
minhnn89 | 0:f54d103ce9b6 | 106 | } |
minhnn89 | 0:f54d103ce9b6 | 107 | //******************************************* |
minhnn89 | 0:f54d103ce9b6 | 108 | // end of configuration |
minhnn89 | 0:f54d103ce9b6 | 109 | //******************************************* |
minhnn89 | 0:f54d103ce9b6 | 110 | |
minhnn89 | 0:f54d103ce9b6 | 111 | // format data for sending to the gateway |
minhnn89 | 0:f54d103ce9b6 | 112 | //for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++) |
minhnn89 | 0:f54d103ce9b6 | 113 | //data.push_back((uint8_t) *it); |
minhnn89 | 0:f54d103ce9b6 | 114 | |
minhnn89 | 0:f54d103ce9b6 | 115 | // join the network if not joined |
minhnn89 | 0:f54d103ce9b6 | 116 | if (!dot->getNetworkJoinStatus()) { |
minhnn89 | 0:f54d103ce9b6 | 117 | logInfo("network not joined, joining network"); |
minhnn89 | 0:f54d103ce9b6 | 118 | if ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
minhnn89 | 0:f54d103ce9b6 | 119 | logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
minhnn89 | 0:f54d103ce9b6 | 120 | } |
minhnn89 | 0:f54d103ce9b6 | 121 | } |
minhnn89 | 0:f54d103ce9b6 | 122 | |
minhnn89 | 0:f54d103ce9b6 | 123 | while (true) { |
minhnn89 | 0:f54d103ce9b6 | 124 | std::vector<uint8_t> data; |
minhnn89 | 0:f54d103ce9b6 | 125 | |
minhnn89 | 0:f54d103ce9b6 | 126 | float tmpSpeed = speed; |
minhnn89 | 0:f54d103ce9b6 | 127 | if (tmpSpeed >= 10) { // 10 - 19 km/h |
minhnn89 | 0:f54d103ce9b6 | 128 | data.push_back('1'); |
minhnn89 | 0:f54d103ce9b6 | 129 | tmpSpeed -= 10; |
minhnn89 | 0:f54d103ce9b6 | 130 | } |
minhnn89 | 0:f54d103ce9b6 | 131 | tmpSpeed *= 10; |
minhnn89 | 0:f54d103ce9b6 | 132 | data.push_back((int)(tmpSpeed) / 10 + '0'); |
minhnn89 | 0:f54d103ce9b6 | 133 | data.push_back('.'); |
minhnn89 | 0:f54d103ce9b6 | 134 | data.push_back((int)tmpSpeed % 10 + '0'); |
minhnn89 | 0:f54d103ce9b6 | 135 | |
minhnn89 | 0:f54d103ce9b6 | 136 | if (dot->getNetworkJoinStatus()) { |
minhnn89 | 0:f54d103ce9b6 | 137 | // send the data |
minhnn89 | 0:f54d103ce9b6 | 138 | // ACKs are enabled by default, so we're expecting to get one back |
minhnn89 | 0:f54d103ce9b6 | 139 | if ((ret = dot->send(data)) != mDot::MDOT_OK) { |
minhnn89 | 0:f54d103ce9b6 | 140 | logError("failed to send %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
minhnn89 | 0:f54d103ce9b6 | 141 | } else { |
minhnn89 | 0:f54d103ce9b6 | 142 | logInfo("successfully sent data to gateway"); |
minhnn89 | 0:f54d103ce9b6 | 143 | } |
minhnn89 | 0:f54d103ce9b6 | 144 | } |
minhnn89 | 0:f54d103ce9b6 | 145 | //osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs())); |
minhnn89 | 0:f54d103ce9b6 | 146 | |
minhnn89 | 0:f54d103ce9b6 | 147 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
minhnn89 | 0:f54d103ce9b6 | 148 | uint32_t sleep_time = std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()) / 1000; |
minhnn89 | 0:f54d103ce9b6 | 149 | //logInfo("going to sleep..."); |
minhnn89 | 0:f54d103ce9b6 | 150 | |
minhnn89 | 0:f54d103ce9b6 | 151 | // go to deepsleep and wake up automatically sleep_time seconds later |
minhnn89 | 0:f54d103ce9b6 | 152 | dot->sleep(sleep_time, mDot::INTERRUPT); |
minhnn89 | 0:f54d103ce9b6 | 153 | |
minhnn89 | 0:f54d103ce9b6 | 154 | //wait(sleep_time); |
minhnn89 | 0:f54d103ce9b6 | 155 | } |
minhnn89 | 0:f54d103ce9b6 | 156 | |
minhnn89 | 0:f54d103ce9b6 | 157 | return 0; |
minhnn89 | 0:f54d103ce9b6 | 158 | } |