Da ka xi tong.

Dependencies:   libmDot mbed-rtos mbed

Fork of mDot_Connect_IoTClub_one_wire_4ch_8_24 by wireless sensor

Committer:
wang1tao
Date:
Fri Jul 29 09:13:29 2016 +0000
Revision:
7:3a4df8dbbb2e
Parent:
5:6c7d2f4d7377
Child:
8:dfcee7a96019
Added PH sensor analog channel read

Who changed what in which revision?

UserRevisionLine numberNew 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"
wang1tao 7:3a4df8dbbb2e 8 #include "LinearTemp.h"
JCheng 5:6c7d2f4d7377 9
JCheng 5:6c7d2f4d7377 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 char * PhStrBuf;
wang1tao 7:3a4df8dbbb2e 25 float PhValue;
JCheng 5:6c7d2f4d7377 26 std::string data_str = "T&H Sensors";
JCheng 5:6c7d2f4d7377 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;
JCheng 5:6c7d2f4d7377 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);
mfiore 0:09250cd371d2 102
JCheng 5:6c7d2f4d7377 103 char dataBuf[11];
JCheng 5:6c7d2f4d7377 104 int thflag=0;
JCheng 5:6c7d2f4d7377 105
mfiore 0:09250cd371d2 106 while (true) {
mfiore 4:36e214ebfa56 107 // send the data to the gateway
mfiore 0:09250cd371d2 108 if ((ret = dot->send(data)) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 109 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:09250cd371d2 110 } else {
mfiore 2:6e2c378339d9 111 logInfo("successfully sent data to gateway");
mfiore 0:09250cd371d2 112 }
wang1tao 7:3a4df8dbbb2e 113
mfiore 0:09250cd371d2 114
mfiore 4:36e214ebfa56 115 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
mfiore 4:36e214ebfa56 116 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
JCheng 5:6c7d2f4d7377 117
JCheng 5:6c7d2f4d7377 118 wait(2.0f);
JCheng 5:6c7d2f4d7377 119 error = sensor.readData();
JCheng 5:6c7d2f4d7377 120 if (0 == error) {
JCheng 5:6c7d2f4d7377 121 c = sensor.ReadTemperature(CELCIUS);
JCheng 5:6c7d2f4d7377 122 // f = sensor.ReadTemperature(FARENHEIT);
JCheng 5:6c7d2f4d7377 123 // k = sensor.ReadTemperature(KELVIN);
JCheng 5:6c7d2f4d7377 124 h = sensor.ReadHumidity();
JCheng 5:6c7d2f4d7377 125 // dp = sensor.CalcdewPoint(c, h);
JCheng 5:6c7d2f4d7377 126 // dpf = sensor.CalcdewPointFast(c, h);
JCheng 5:6c7d2f4d7377 127 printf("Temperature: %4.2f Humidity: %4.2f\n\n", c, h);
JCheng 5:6c7d2f4d7377 128
JCheng 5:6c7d2f4d7377 129 if (thflag==0) {
JCheng 5:6c7d2f4d7377 130 sprintf(dataBuf, "T:%4.2f", c);
JCheng 5:6c7d2f4d7377 131 thflag=1;
JCheng 5:6c7d2f4d7377 132 } else {
JCheng 5:6c7d2f4d7377 133 sprintf(dataBuf, "H:%4.2f", h);
JCheng 5:6c7d2f4d7377 134 thflag=0;
JCheng 5:6c7d2f4d7377 135 }
JCheng 5:6c7d2f4d7377 136
JCheng 5:6c7d2f4d7377 137 data.clear();
wang1tao 7:3a4df8dbbb2e 138
wang1tao 7:3a4df8dbbb2e 139 // Read the PH value
wang1tao 7:3a4df8dbbb2e 140 PhValue = myPHSensor;
wang1tao 7:3a4df8dbbb2e 141
wang1tao 7:3a4df8dbbb2e 142 sprintf(PhStrBuf, "PH:%5.4f", PhValue);
wang1tao 7:3a4df8dbbb2e 143 logInfo("PH: %5.4f", PhValue);
wang1tao 7:3a4df8dbbb2e 144
JCheng 5:6c7d2f4d7377 145 // probably not the most efficent way to do this
JCheng 5:6c7d2f4d7377 146 for( int i=0; i< strlen(dataBuf); i++ )
JCheng 5:6c7d2f4d7377 147 data.push_back( dataBuf[i] );
JCheng 5:6c7d2f4d7377 148
JCheng 5:6c7d2f4d7377 149 } else {
JCheng 5:6c7d2f4d7377 150 printf("Error: %d\n", error);
JCheng 5:6c7d2f4d7377 151 }
JCheng 5:6c7d2f4d7377 152
mfiore 0:09250cd371d2 153 }
mfiore 0:09250cd371d2 154
JCheng 5:6c7d2f4d7377 155 // return 0;
mfiore 0:09250cd371d2 156 }