Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: libmDot mbed mbed-rtos
main.cpp@5:6c7d2f4d7377, 2016-02-04 (annotated)
- Committer:
- JCheng
- Date:
- Thu Feb 04 09:52:26 2016 +0000
- Revision:
- 5:6c7d2f4d7377
- Parent:
- 4:36e214ebfa56
- Child:
- 7:3a4df8dbbb2e
initial version or collaboration.; DTH11 supported.
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> | 
| JCheng | 5:6c7d2f4d7377 | 7 | #include "DHT.h" | 
| JCheng | 5:6c7d2f4d7377 | 8 | |
| JCheng | 5:6c7d2f4d7377 | 9 | DHT sensor(PB_1, DHT11); | 
| mfiore | 0:09250cd371d2 | 10 | |
| mfiore | 2:6e2c378339d9 | 11 | // these options must match the settings on your Conduit | 
| mfiore | 2:6e2c378339d9 | 12 | // uncomment the following lines and edit their values to match your configuration | 
| JCheng | 5:6c7d2f4d7377 | 13 | static std::string config_network_name = "chinaiot"; | 
| JCheng | 5:6c7d2f4d7377 | 14 | static std::string config_network_pass = "password"; | 
| JCheng | 5:6c7d2f4d7377 | 15 | static uint8_t config_frequency_sub_band = 2; | 
| mfiore | 0:09250cd371d2 | 16 | |
| mfiore | 0:09250cd371d2 | 17 | int main() { | 
| mfiore | 0:09250cd371d2 | 18 | int32_t ret; | 
| mfiore | 0:09250cd371d2 | 19 | mDot* dot; | 
| mfiore | 0:09250cd371d2 | 20 | std::vector<uint8_t> data; | 
| JCheng | 5:6c7d2f4d7377 | 21 | std::vector<uint8_t> send_data; | 
| JCheng | 5:6c7d2f4d7377 | 22 | std::string data_str = "T&H Sensors"; | 
| JCheng | 5:6c7d2f4d7377 | 23 | int error = 0; | 
| JCheng | 5:6c7d2f4d7377 | 24 | // float h = 0.0f, c = 0.0f, f = 0.0f, k = 0.0f, dp = 0.0f, dpf = 0.0f; | 
| JCheng | 5:6c7d2f4d7377 | 25 | float h = 0.0f, c = 0.0f; | 
| JCheng | 5:6c7d2f4d7377 | 26 | |
| JCheng | 5:6c7d2f4d7377 | 27 | |
| mfiore | 0:09250cd371d2 | 28 | // get a mDot handle | 
| mfiore | 0:09250cd371d2 | 29 | dot = mDot::getInstance(); | 
| JCheng | 5:6c7d2f4d7377 | 30 | |
| JCheng | 5:6c7d2f4d7377 | 31 | |
| mfiore | 2:6e2c378339d9 | 32 | // print library version information | 
| mfiore | 2:6e2c378339d9 | 33 | logInfo("version: %s", dot->getId().c_str()); | 
| mfiore | 0:09250cd371d2 | 34 | |
| mfiore | 2:6e2c378339d9 | 35 | //******************************************* | 
| mfiore | 2:6e2c378339d9 | 36 | // configuration | 
| mfiore | 2:6e2c378339d9 | 37 | //******************************************* | 
| mfiore | 0:09250cd371d2 | 38 | // reset to default config so we know what state we're in | 
| mfiore | 0:09250cd371d2 | 39 | dot->resetConfig(); | 
| JCheng | 5:6c7d2f4d7377 | 40 | |
| mfiore | 4:36e214ebfa56 | 41 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); | 
| mfiore | 0:09250cd371d2 | 42 | |
| mfiore | 2:6e2c378339d9 | 43 | // set up the mDot with our network information: frequency sub band, network name, and network password | 
| mfiore | 2:6e2c378339d9 | 44 | // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig() | 
| JCheng | 5:6c7d2f4d7377 | 45 | |
| mfiore | 4:36e214ebfa56 | 46 | // frequency sub band is only applicable in the 915 (US) frequency band | 
| mfiore | 4:36e214ebfa56 | 47 | // 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 | 48 | // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels | 
| mfiore | 2:6e2c378339d9 | 49 | logInfo("setting frequency sub band"); | 
| mfiore | 0:09250cd371d2 | 50 | if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { | 
| mfiore | 2:6e2c378339d9 | 51 | logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); | 
| mfiore | 2:6e2c378339d9 | 52 | } | 
| JCheng | 5:6c7d2f4d7377 | 53 | |
| mfiore | 2:6e2c378339d9 | 54 | logInfo("setting network name"); | 
| mfiore | 2:6e2c378339d9 | 55 | if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { | 
| mfiore | 2:6e2c378339d9 | 56 | logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); | 
| mfiore | 0:09250cd371d2 | 57 | } | 
| JCheng | 5:6c7d2f4d7377 | 58 | |
| mfiore | 2:6e2c378339d9 | 59 | logInfo("setting network password"); | 
| mfiore | 2:6e2c378339d9 | 60 | if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { | 
| mfiore | 2:6e2c378339d9 | 61 | logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); | 
| mfiore | 0:09250cd371d2 | 62 | } | 
| JCheng | 5:6c7d2f4d7377 | 63 | |
| mfiore | 4:36e214ebfa56 | 64 | // a higher spreading factor allows for longer range but lower throughput | 
| mfiore | 4:36e214ebfa56 | 65 | // in the 915 (US) frequency band, spreading factors 7 - 10 are available | 
| mfiore | 4:36e214ebfa56 | 66 | // in the 868 (EU) frequency band, spreading factors 7 - 12 are available | 
| mfiore | 4:36e214ebfa56 | 67 | logInfo("setting TX spreading factor"); | 
| mfiore | 4:36e214ebfa56 | 68 | if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) { | 
| mfiore | 4:36e214ebfa56 | 69 | logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); | 
| mfiore | 4:36e214ebfa56 | 70 | } | 
| JCheng | 5:6c7d2f4d7377 | 71 | |
| mfiore | 4:36e214ebfa56 | 72 | // request receive confirmation of packets from the gateway | 
| mfiore | 4:36e214ebfa56 | 73 | logInfo("enabling ACKs"); | 
| mfiore | 4:36e214ebfa56 | 74 | if ((ret = dot->setAck(1)) != mDot::MDOT_OK) { | 
| mfiore | 4:36e214ebfa56 | 75 | logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); | 
| mfiore | 4:36e214ebfa56 | 76 | } | 
| JCheng | 5:6c7d2f4d7377 | 77 | |
| mfiore | 4:36e214ebfa56 | 78 | // save this configuration to the mDot's NVM | 
| mfiore | 2:6e2c378339d9 | 79 | logInfo("saving config"); | 
| mfiore | 2:6e2c378339d9 | 80 | if (! dot->saveConfig()) { | 
| mfiore | 2:6e2c378339d9 | 81 | logError("failed to save configuration"); | 
| mfiore | 0:09250cd371d2 | 82 | } | 
| mfiore | 2:6e2c378339d9 | 83 | //******************************************* | 
| mfiore | 2:6e2c378339d9 | 84 | // end of configuration | 
| mfiore | 2:6e2c378339d9 | 85 | //******************************************* | 
| mfiore | 0:09250cd371d2 | 86 | |
| mfiore | 0:09250cd371d2 | 87 | // attempt to join the network | 
| mfiore | 2:6e2c378339d9 | 88 | logInfo("joining network"); | 
| mfiore | 0:09250cd371d2 | 89 | while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { | 
| mfiore | 2:6e2c378339d9 | 90 | logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); | 
| mfiore | 4:36e214ebfa56 | 91 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again | 
| mfiore | 4:36e214ebfa56 | 92 | osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs())); | 
| mfiore | 0:09250cd371d2 | 93 | } | 
| mfiore | 0:09250cd371d2 | 94 | |
| mfiore | 0:09250cd371d2 | 95 | // format data for sending to the gateway | 
| mfiore | 0:09250cd371d2 | 96 | for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++) | 
| mfiore | 0:09250cd371d2 | 97 | data.push_back((uint8_t) *it); | 
| mfiore | 0:09250cd371d2 | 98 | |
| JCheng | 5:6c7d2f4d7377 | 99 | char dataBuf[11]; | 
| JCheng | 5:6c7d2f4d7377 | 100 | int thflag=0; | 
| JCheng | 5:6c7d2f4d7377 | 101 | |
| mfiore | 0:09250cd371d2 | 102 | while (true) { | 
| mfiore | 4:36e214ebfa56 | 103 | // send the data to the gateway | 
| mfiore | 0:09250cd371d2 | 104 | if ((ret = dot->send(data)) != mDot::MDOT_OK) { | 
| mfiore | 2:6e2c378339d9 | 105 | logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str()); | 
| mfiore | 0:09250cd371d2 | 106 | } else { | 
| mfiore | 2:6e2c378339d9 | 107 | logInfo("successfully sent data to gateway"); | 
| mfiore | 0:09250cd371d2 | 108 | } | 
| mfiore | 0:09250cd371d2 | 109 | |
| mfiore | 4:36e214ebfa56 | 110 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again | 
| mfiore | 4:36e214ebfa56 | 111 | osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs())); | 
| JCheng | 5:6c7d2f4d7377 | 112 | |
| JCheng | 5:6c7d2f4d7377 | 113 | wait(2.0f); | 
| JCheng | 5:6c7d2f4d7377 | 114 | error = sensor.readData(); | 
| JCheng | 5:6c7d2f4d7377 | 115 | if (0 == error) { | 
| JCheng | 5:6c7d2f4d7377 | 116 | c = sensor.ReadTemperature(CELCIUS); | 
| JCheng | 5:6c7d2f4d7377 | 117 | // f = sensor.ReadTemperature(FARENHEIT); | 
| JCheng | 5:6c7d2f4d7377 | 118 | // k = sensor.ReadTemperature(KELVIN); | 
| JCheng | 5:6c7d2f4d7377 | 119 | h = sensor.ReadHumidity(); | 
| JCheng | 5:6c7d2f4d7377 | 120 | // dp = sensor.CalcdewPoint(c, h); | 
| JCheng | 5:6c7d2f4d7377 | 121 | // dpf = sensor.CalcdewPointFast(c, h); | 
| JCheng | 5:6c7d2f4d7377 | 122 | printf("Temperature: %4.2f Humidity: %4.2f\n\n", c, h); | 
| JCheng | 5:6c7d2f4d7377 | 123 | |
| JCheng | 5:6c7d2f4d7377 | 124 | if (thflag==0) { | 
| JCheng | 5:6c7d2f4d7377 | 125 | sprintf(dataBuf, "T:%4.2f", c); | 
| JCheng | 5:6c7d2f4d7377 | 126 | thflag=1; | 
| JCheng | 5:6c7d2f4d7377 | 127 | } else { | 
| JCheng | 5:6c7d2f4d7377 | 128 | sprintf(dataBuf, "H:%4.2f", h); | 
| JCheng | 5:6c7d2f4d7377 | 129 | thflag=0; | 
| JCheng | 5:6c7d2f4d7377 | 130 | } | 
| JCheng | 5:6c7d2f4d7377 | 131 | |
| JCheng | 5:6c7d2f4d7377 | 132 | data.clear(); | 
| JCheng | 5:6c7d2f4d7377 | 133 | // probably not the most efficent way to do this | 
| JCheng | 5:6c7d2f4d7377 | 134 | for( int i=0; i< strlen(dataBuf); i++ ) | 
| JCheng | 5:6c7d2f4d7377 | 135 | data.push_back( dataBuf[i] ); | 
| JCheng | 5:6c7d2f4d7377 | 136 | |
| JCheng | 5:6c7d2f4d7377 | 137 | } else { | 
| JCheng | 5:6c7d2f4d7377 | 138 | printf("Error: %d\n", error); | 
| JCheng | 5:6c7d2f4d7377 | 139 | } | 
| JCheng | 5:6c7d2f4d7377 | 140 | |
| mfiore | 0:09250cd371d2 | 141 | } | 
| mfiore | 0:09250cd371d2 | 142 | |
| JCheng | 5:6c7d2f4d7377 | 143 | // return 0; | 
| mfiore | 0:09250cd371d2 | 144 | } |