Da ka xi tong.
Dependencies: libmDot mbed-rtos mbed
Fork of mDot_Connect_IoTClub_one_wire_4ch_8_24 by
main.cpp@8:dfcee7a96019, 2016-08-12 (annotated)
- Committer:
- wang1tao
- Date:
- Fri Aug 12 03:33:55 2016 +0000
- Revision:
- 8:dfcee7a96019
- Parent:
- 7:3a4df8dbbb2e
- Child:
- 9:cf45820af9b9
Optimized
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfiore | 0:09250cd371d2 | 1 | #include "mbed.h" |
mfiore | 0:09250cd371d2 | 2 | #include "mDot.h" |
mfiore | 4:36e214ebfa56 | 3 | #include "MTSLog.h" |
mfiore | 0:09250cd371d2 | 4 | #include <string> |
mfiore | 0:09250cd371d2 | 5 | #include <vector> |
mfiore | 4:36e214ebfa56 | 6 | #include <algorithm> |
wang1tao | 8:dfcee7a96019 | 7 | //#include "DHT.h" |
wang1tao | 7:3a4df8dbbb2e | 8 | #include "LinearTemp.h" |
JCheng | 5:6c7d2f4d7377 | 9 | |
wang1tao | 8:dfcee7a96019 | 10 | //DHT sensor(PB_1, DHT11); |
wang1tao | 7:3a4df8dbbb2e | 11 | LinearTemp myPHSensor(PB_0, 0.005, -273.15); |
mfiore | 0:09250cd371d2 | 12 | |
mfiore | 2:6e2c378339d9 | 13 | // these options must match the settings on your Conduit |
mfiore | 2:6e2c378339d9 | 14 | // uncomment the following lines and edit their values to match your configuration |
JCheng | 5:6c7d2f4d7377 | 15 | static std::string config_network_name = "chinaiot"; |
JCheng | 5:6c7d2f4d7377 | 16 | static std::string config_network_pass = "password"; |
JCheng | 5:6c7d2f4d7377 | 17 | static uint8_t config_frequency_sub_band = 2; |
mfiore | 0:09250cd371d2 | 18 | |
mfiore | 0:09250cd371d2 | 19 | int main() { |
mfiore | 0:09250cd371d2 | 20 | int32_t ret; |
mfiore | 0:09250cd371d2 | 21 | mDot* dot; |
mfiore | 0:09250cd371d2 | 22 | std::vector<uint8_t> data; |
JCheng | 5:6c7d2f4d7377 | 23 | std::vector<uint8_t> send_data; |
wang1tao | 7:3a4df8dbbb2e | 24 | float PhValue; |
JCheng | 5:6c7d2f4d7377 | 25 | std::string data_str = "T&H Sensors"; |
wang1tao | 8:dfcee7a96019 | 26 | char dataBuf[11]; |
wang1tao | 8:dfcee7a96019 | 27 | // int error = 0; |
JCheng | 5:6c7d2f4d7377 | 28 | // float h = 0.0f, c = 0.0f, f = 0.0f, k = 0.0f, dp = 0.0f, dpf = 0.0f; |
wang1tao | 8:dfcee7a96019 | 29 | // float h = 0.0f, c = 0.0f; |
JCheng | 5:6c7d2f4d7377 | 30 | |
JCheng | 5:6c7d2f4d7377 | 31 | |
mfiore | 0:09250cd371d2 | 32 | // get a mDot handle |
mfiore | 0:09250cd371d2 | 33 | dot = mDot::getInstance(); |
JCheng | 5:6c7d2f4d7377 | 34 | |
JCheng | 5:6c7d2f4d7377 | 35 | |
mfiore | 2:6e2c378339d9 | 36 | // print library version information |
mfiore | 2:6e2c378339d9 | 37 | logInfo("version: %s", dot->getId().c_str()); |
mfiore | 0:09250cd371d2 | 38 | |
mfiore | 2:6e2c378339d9 | 39 | //******************************************* |
mfiore | 2:6e2c378339d9 | 40 | // configuration |
mfiore | 2:6e2c378339d9 | 41 | //******************************************* |
mfiore | 0:09250cd371d2 | 42 | // reset to default config so we know what state we're in |
mfiore | 0:09250cd371d2 | 43 | dot->resetConfig(); |
JCheng | 5:6c7d2f4d7377 | 44 | |
mfiore | 4:36e214ebfa56 | 45 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
mfiore | 0:09250cd371d2 | 46 | |
mfiore | 2:6e2c378339d9 | 47 | // set up the mDot with our network information: frequency sub band, network name, and network password |
mfiore | 2:6e2c378339d9 | 48 | // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig() |
JCheng | 5:6c7d2f4d7377 | 49 | |
mfiore | 4:36e214ebfa56 | 50 | // frequency sub band is only applicable in the 915 (US) frequency band |
mfiore | 4:36e214ebfa56 | 51 | // 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:36e214ebfa56 | 52 | // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels |
mfiore | 2:6e2c378339d9 | 53 | logInfo("setting frequency sub band"); |
mfiore | 0:09250cd371d2 | 54 | if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 55 | logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 2:6e2c378339d9 | 56 | } |
JCheng | 5:6c7d2f4d7377 | 57 | |
mfiore | 2:6e2c378339d9 | 58 | logInfo("setting network name"); |
mfiore | 2:6e2c378339d9 | 59 | if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 60 | logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:09250cd371d2 | 61 | } |
JCheng | 5:6c7d2f4d7377 | 62 | |
mfiore | 2:6e2c378339d9 | 63 | logInfo("setting network password"); |
mfiore | 2:6e2c378339d9 | 64 | if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 65 | logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:09250cd371d2 | 66 | } |
JCheng | 5:6c7d2f4d7377 | 67 | |
mfiore | 4:36e214ebfa56 | 68 | // a higher spreading factor allows for longer range but lower throughput |
mfiore | 4:36e214ebfa56 | 69 | // in the 915 (US) frequency band, spreading factors 7 - 10 are available |
mfiore | 4:36e214ebfa56 | 70 | // in the 868 (EU) frequency band, spreading factors 7 - 12 are available |
mfiore | 4:36e214ebfa56 | 71 | logInfo("setting TX spreading factor"); |
mfiore | 4:36e214ebfa56 | 72 | if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) { |
mfiore | 4:36e214ebfa56 | 73 | logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 4:36e214ebfa56 | 74 | } |
JCheng | 5:6c7d2f4d7377 | 75 | |
mfiore | 4:36e214ebfa56 | 76 | // request receive confirmation of packets from the gateway |
mfiore | 4:36e214ebfa56 | 77 | logInfo("enabling ACKs"); |
mfiore | 4:36e214ebfa56 | 78 | if ((ret = dot->setAck(1)) != mDot::MDOT_OK) { |
mfiore | 4:36e214ebfa56 | 79 | logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 4:36e214ebfa56 | 80 | } |
JCheng | 5:6c7d2f4d7377 | 81 | |
mfiore | 4:36e214ebfa56 | 82 | // save this configuration to the mDot's NVM |
mfiore | 2:6e2c378339d9 | 83 | logInfo("saving config"); |
mfiore | 2:6e2c378339d9 | 84 | if (! dot->saveConfig()) { |
mfiore | 2:6e2c378339d9 | 85 | logError("failed to save configuration"); |
mfiore | 0:09250cd371d2 | 86 | } |
mfiore | 2:6e2c378339d9 | 87 | //******************************************* |
mfiore | 2:6e2c378339d9 | 88 | // end of configuration |
mfiore | 2:6e2c378339d9 | 89 | //******************************************* |
mfiore | 0:09250cd371d2 | 90 | |
mfiore | 0:09250cd371d2 | 91 | // attempt to join the network |
mfiore | 2:6e2c378339d9 | 92 | logInfo("joining network"); |
mfiore | 0:09250cd371d2 | 93 | while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 94 | logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 4:36e214ebfa56 | 95 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
mfiore | 4:36e214ebfa56 | 96 | osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs())); |
mfiore | 0:09250cd371d2 | 97 | } |
mfiore | 0:09250cd371d2 | 98 | |
mfiore | 0:09250cd371d2 | 99 | // format data for sending to the gateway |
mfiore | 0:09250cd371d2 | 100 | for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++) |
mfiore | 0:09250cd371d2 | 101 | data.push_back((uint8_t) *it); |
JCheng | 5:6c7d2f4d7377 | 102 | |
mfiore | 0:09250cd371d2 | 103 | while (true) { |
mfiore | 4:36e214ebfa56 | 104 | // send the data to the gateway |
mfiore | 0:09250cd371d2 | 105 | if ((ret = dot->send(data)) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 106 | logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:09250cd371d2 | 107 | } else { |
mfiore | 2:6e2c378339d9 | 108 | logInfo("successfully sent data to gateway"); |
mfiore | 0:09250cd371d2 | 109 | } |
wang1tao | 7:3a4df8dbbb2e | 110 | |
mfiore | 4:36e214ebfa56 | 111 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
mfiore | 4:36e214ebfa56 | 112 | osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs())); |
JCheng | 5:6c7d2f4d7377 | 113 | |
JCheng | 5:6c7d2f4d7377 | 114 | data.clear(); |
wang1tao | 7:3a4df8dbbb2e | 115 | |
wang1tao | 7:3a4df8dbbb2e | 116 | // Read the PH value |
wang1tao | 7:3a4df8dbbb2e | 117 | PhValue = myPHSensor; |
wang1tao | 7:3a4df8dbbb2e | 118 | |
wang1tao | 8:dfcee7a96019 | 119 | sprintf(dataBuf, "CH0:%5.4f", PhValue); |
wang1tao | 8:dfcee7a96019 | 120 | logInfo("%s", dataBuf); |
wang1tao | 7:3a4df8dbbb2e | 121 | |
JCheng | 5:6c7d2f4d7377 | 122 | // probably not the most efficent way to do this |
JCheng | 5:6c7d2f4d7377 | 123 | for( int i=0; i< strlen(dataBuf); i++ ) |
JCheng | 5:6c7d2f4d7377 | 124 | data.push_back( dataBuf[i] ); |
JCheng | 5:6c7d2f4d7377 | 125 | |
mfiore | 0:09250cd371d2 | 126 | } |
mfiore | 0:09250cd371d2 | 127 | |
mfiore | 0:09250cd371d2 | 128 | } |