hw3_21300515

Fork of coap-example by sandbox

Committer:
DongHeon
Date:
Tue Jan 02 12:36:15 2018 +0000
Revision:
1:4d65f05c7d2f
a

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DongHeon 1:4d65f05c7d2f 1 /*
DongHeon 1:4d65f05c7d2f 2 * DHT Library for Digital-output Humidity and Temperature sensors
DongHeon 1:4d65f05c7d2f 3 *
DongHeon 1:4d65f05c7d2f 4 * Works with DHT11, DHT22
DongHeon 1:4d65f05c7d2f 5 * SEN11301P, Grove - Temperature&Humidity Sensor (Seeed Studio)
DongHeon 1:4d65f05c7d2f 6 * SEN51035P, Grove - Temperature&Humidity Sensor Pro (Seeed Studio)
DongHeon 1:4d65f05c7d2f 7 * AM2302 , temperature-humidity sensor
DongHeon 1:4d65f05c7d2f 8 * HM2303 , Digital-output humidity and temperature sensor
DongHeon 1:4d65f05c7d2f 9 *
DongHeon 1:4d65f05c7d2f 10 * Copyright (C) Wim De Roeve
DongHeon 1:4d65f05c7d2f 11 * based on DHT22 sensor library by HO WING KIT
DongHeon 1:4d65f05c7d2f 12 * Arduino DHT11 library
DongHeon 1:4d65f05c7d2f 13 *
DongHeon 1:4d65f05c7d2f 14 * Permission is hereby granted, free of charge, to any person obtaining a copy
DongHeon 1:4d65f05c7d2f 15 * of this software and associated documnetation files (the "Software"), to deal
DongHeon 1:4d65f05c7d2f 16 * in the Software without restriction, including without limitation the rights
DongHeon 1:4d65f05c7d2f 17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
DongHeon 1:4d65f05c7d2f 18 * copies of the Software, and to permit persons to whom the Software is
DongHeon 1:4d65f05c7d2f 19 * furished to do so, subject to the following conditions:
DongHeon 1:4d65f05c7d2f 20 *
DongHeon 1:4d65f05c7d2f 21 * The above copyright notice and this permission notice shall be included in
DongHeon 1:4d65f05c7d2f 22 * all copies or substantial portions of the Software.
DongHeon 1:4d65f05c7d2f 23 *
DongHeon 1:4d65f05c7d2f 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
DongHeon 1:4d65f05c7d2f 25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
DongHeon 1:4d65f05c7d2f 26 * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
DongHeon 1:4d65f05c7d2f 27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
DongHeon 1:4d65f05c7d2f 28 * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
DongHeon 1:4d65f05c7d2f 29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
DongHeon 1:4d65f05c7d2f 30 * THE SOFTWARE.
DongHeon 1:4d65f05c7d2f 31 */
DongHeon 1:4d65f05c7d2f 32
DongHeon 1:4d65f05c7d2f 33 #include "DHT.h"
DongHeon 1:4d65f05c7d2f 34
DongHeon 1:4d65f05c7d2f 35 #define DHT_DATA_BIT_COUNT 40
DongHeon 1:4d65f05c7d2f 36
DongHeon 1:4d65f05c7d2f 37 DHT::DHT(PinName pin, eType DHTtype)
DongHeon 1:4d65f05c7d2f 38 {
DongHeon 1:4d65f05c7d2f 39 _pin = pin;
DongHeon 1:4d65f05c7d2f 40 _DHTtype = DHTtype;
DongHeon 1:4d65f05c7d2f 41 _firsttime = true;
DongHeon 1:4d65f05c7d2f 42 }
DongHeon 1:4d65f05c7d2f 43
DongHeon 1:4d65f05c7d2f 44 DHT::~DHT()
DongHeon 1:4d65f05c7d2f 45 {
DongHeon 1:4d65f05c7d2f 46
DongHeon 1:4d65f05c7d2f 47 }
DongHeon 1:4d65f05c7d2f 48
DongHeon 1:4d65f05c7d2f 49 eError DHT::stall(DigitalInOut &io, int const level, int const max_time)
DongHeon 1:4d65f05c7d2f 50 {
DongHeon 1:4d65f05c7d2f 51 int cnt = 0;
DongHeon 1:4d65f05c7d2f 52 while (level == io) {
DongHeon 1:4d65f05c7d2f 53 if (cnt > max_time) {
DongHeon 1:4d65f05c7d2f 54 return ERROR_NO_PATIENCE;
DongHeon 1:4d65f05c7d2f 55 }
DongHeon 1:4d65f05c7d2f 56 cnt++;
DongHeon 1:4d65f05c7d2f 57 wait_us(1);
DongHeon 1:4d65f05c7d2f 58 }
DongHeon 1:4d65f05c7d2f 59 return ERROR_NONE;
DongHeon 1:4d65f05c7d2f 60 }
DongHeon 1:4d65f05c7d2f 61
DongHeon 1:4d65f05c7d2f 62 eError DHT::readData()
DongHeon 1:4d65f05c7d2f 63 {
DongHeon 1:4d65f05c7d2f 64 uint8_t i = 0, j = 0, b = 0, data_valid = 0;
DongHeon 1:4d65f05c7d2f 65 uint32_t bit_value[DHT_DATA_BIT_COUNT] = {0};
DongHeon 1:4d65f05c7d2f 66
DongHeon 1:4d65f05c7d2f 67 eError err = ERROR_NONE;
DongHeon 1:4d65f05c7d2f 68 time_t currentTime = time(NULL);
DongHeon 1:4d65f05c7d2f 69
DongHeon 1:4d65f05c7d2f 70 DigitalInOut DHT_io(_pin);
DongHeon 1:4d65f05c7d2f 71
DongHeon 1:4d65f05c7d2f 72 // IO must be in hi state to start
DongHeon 1:4d65f05c7d2f 73 if (ERROR_NONE != stall(DHT_io, 0, 250)) {
DongHeon 1:4d65f05c7d2f 74 return BUS_BUSY;
DongHeon 1:4d65f05c7d2f 75 }
DongHeon 1:4d65f05c7d2f 76
DongHeon 1:4d65f05c7d2f 77 // start the transfer
DongHeon 1:4d65f05c7d2f 78 DHT_io.output();
DongHeon 1:4d65f05c7d2f 79 DHT_io = 0;
DongHeon 1:4d65f05c7d2f 80 // only 500uS for DHT22 but 18ms for DHT11
DongHeon 1:4d65f05c7d2f 81 (_DHTtype == 11) ? wait_ms(18) : wait(0.0005);
DongHeon 1:4d65f05c7d2f 82 DHT_io = 1;
DongHeon 1:4d65f05c7d2f 83 wait_us(30);
DongHeon 1:4d65f05c7d2f 84 DHT_io.input();
DongHeon 1:4d65f05c7d2f 85 // wait till the sensor grabs the bus
DongHeon 1:4d65f05c7d2f 86 if (ERROR_NONE != stall(DHT_io, 1, 40)) {
DongHeon 1:4d65f05c7d2f 87 return ERROR_NOT_PRESENT;
DongHeon 1:4d65f05c7d2f 88 }
DongHeon 1:4d65f05c7d2f 89 // sensor should signal low 80us and then hi 80us
DongHeon 1:4d65f05c7d2f 90 if (ERROR_NONE != stall(DHT_io, 0, 100)) {
DongHeon 1:4d65f05c7d2f 91 return ERROR_SYNC_TIMEOUT;
DongHeon 1:4d65f05c7d2f 92 }
DongHeon 1:4d65f05c7d2f 93 if (ERROR_NONE != stall(DHT_io, 1, 100)) {
DongHeon 1:4d65f05c7d2f 94 return ERROR_NO_PATIENCE;
DongHeon 1:4d65f05c7d2f 95 }
DongHeon 1:4d65f05c7d2f 96 // capture the data
DongHeon 1:4d65f05c7d2f 97 for (i = 0; i < 5; i++) {
DongHeon 1:4d65f05c7d2f 98 for (j = 0; j < 8; j++) {
DongHeon 1:4d65f05c7d2f 99 if (ERROR_NONE != stall(DHT_io, 0, 75)) {
DongHeon 1:4d65f05c7d2f 100 return ERROR_DATA_TIMEOUT;
DongHeon 1:4d65f05c7d2f 101 }
DongHeon 1:4d65f05c7d2f 102 // logic 0 is 28us max, 1 is 70us
DongHeon 1:4d65f05c7d2f 103 wait_us(40);
DongHeon 1:4d65f05c7d2f 104 bit_value[i*8+j] = DHT_io;
DongHeon 1:4d65f05c7d2f 105 if (ERROR_NONE != stall(DHT_io, 1, 50)) {
DongHeon 1:4d65f05c7d2f 106 return ERROR_DATA_TIMEOUT;
DongHeon 1:4d65f05c7d2f 107 }
DongHeon 1:4d65f05c7d2f 108 }
DongHeon 1:4d65f05c7d2f 109 }
DongHeon 1:4d65f05c7d2f 110 // store the data
DongHeon 1:4d65f05c7d2f 111 for (i = 0; i < 5; i++) {
DongHeon 1:4d65f05c7d2f 112 b=0;
DongHeon 1:4d65f05c7d2f 113 for (j=0; j<8; j++) {
DongHeon 1:4d65f05c7d2f 114 if (bit_value[i*8+j] == 1) {
DongHeon 1:4d65f05c7d2f 115 b |= (1 << (7-j));
DongHeon 1:4d65f05c7d2f 116 }
DongHeon 1:4d65f05c7d2f 117 }
DongHeon 1:4d65f05c7d2f 118 DHT_data[i]=b;
DongHeon 1:4d65f05c7d2f 119 }
DongHeon 1:4d65f05c7d2f 120
DongHeon 1:4d65f05c7d2f 121 // uncomment to see the checksum error if it exists
DongHeon 1:4d65f05c7d2f 122 //printf(" 0x%02x + 0x%02x + 0x%02x + 0x%02x = 0x%02x \n", DHT_data[0], DHT_data[1], DHT_data[2], DHT_data[3], DHT_data[4]);
DongHeon 1:4d65f05c7d2f 123 data_valid = DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3];
DongHeon 1:4d65f05c7d2f 124 if (DHT_data[4] == data_valid) {
DongHeon 1:4d65f05c7d2f 125 _lastReadTime = currentTime;
DongHeon 1:4d65f05c7d2f 126 _lastTemperature = CalcTemperature();
DongHeon 1:4d65f05c7d2f 127 _lastHumidity = CalcHumidity();
DongHeon 1:4d65f05c7d2f 128
DongHeon 1:4d65f05c7d2f 129 } else {
DongHeon 1:4d65f05c7d2f 130 err = ERROR_CHECKSUM;
DongHeon 1:4d65f05c7d2f 131 }
DongHeon 1:4d65f05c7d2f 132
DongHeon 1:4d65f05c7d2f 133 return err;
DongHeon 1:4d65f05c7d2f 134
DongHeon 1:4d65f05c7d2f 135 }
DongHeon 1:4d65f05c7d2f 136
DongHeon 1:4d65f05c7d2f 137 float DHT::CalcTemperature()
DongHeon 1:4d65f05c7d2f 138 {
DongHeon 1:4d65f05c7d2f 139 int v;
DongHeon 1:4d65f05c7d2f 140
DongHeon 1:4d65f05c7d2f 141 switch (_DHTtype) {
DongHeon 1:4d65f05c7d2f 142 case DHT11:
DongHeon 1:4d65f05c7d2f 143 v = DHT_data[2];
DongHeon 1:4d65f05c7d2f 144 return float(v);
DongHeon 1:4d65f05c7d2f 145 case DHT22:
DongHeon 1:4d65f05c7d2f 146 v = DHT_data[2] & 0x7F;
DongHeon 1:4d65f05c7d2f 147 v *= 256;
DongHeon 1:4d65f05c7d2f 148 v += DHT_data[3];
DongHeon 1:4d65f05c7d2f 149 v /= 10;
DongHeon 1:4d65f05c7d2f 150 if (DHT_data[2] & 0x80)
DongHeon 1:4d65f05c7d2f 151 v *= -1;
DongHeon 1:4d65f05c7d2f 152 return float(v);
DongHeon 1:4d65f05c7d2f 153 }
DongHeon 1:4d65f05c7d2f 154 return 0;
DongHeon 1:4d65f05c7d2f 155 }
DongHeon 1:4d65f05c7d2f 156
DongHeon 1:4d65f05c7d2f 157 float DHT::ReadHumidity()
DongHeon 1:4d65f05c7d2f 158 {
DongHeon 1:4d65f05c7d2f 159 return _lastHumidity+30;
DongHeon 1:4d65f05c7d2f 160 }
DongHeon 1:4d65f05c7d2f 161
DongHeon 1:4d65f05c7d2f 162 float DHT::ConvertCelciustoFarenheit(float const celsius)
DongHeon 1:4d65f05c7d2f 163 {
DongHeon 1:4d65f05c7d2f 164 return celsius * 9 / 5 + 32;
DongHeon 1:4d65f05c7d2f 165 }
DongHeon 1:4d65f05c7d2f 166
DongHeon 1:4d65f05c7d2f 167 float DHT::ConvertCelciustoKelvin(float const celsius)
DongHeon 1:4d65f05c7d2f 168 {
DongHeon 1:4d65f05c7d2f 169 return celsius + 273.15;
DongHeon 1:4d65f05c7d2f 170 }
DongHeon 1:4d65f05c7d2f 171
DongHeon 1:4d65f05c7d2f 172 // dewPoint function NOAA
DongHeon 1:4d65f05c7d2f 173 // reference: http://wahiduddin.net/calc/density_algorithms.htm
DongHeon 1:4d65f05c7d2f 174 float DHT::CalcdewPoint(float const celsius, float const humidity)
DongHeon 1:4d65f05c7d2f 175 {
DongHeon 1:4d65f05c7d2f 176 float A0= 373.15/(273.15 + celsius);
DongHeon 1:4d65f05c7d2f 177 float SUM = -7.90298 * (A0-1);
DongHeon 1:4d65f05c7d2f 178 SUM += 5.02808 * log10(A0);
DongHeon 1:4d65f05c7d2f 179 SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
DongHeon 1:4d65f05c7d2f 180 SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
DongHeon 1:4d65f05c7d2f 181 SUM += log10(1013.246);
DongHeon 1:4d65f05c7d2f 182 float VP = pow(10, SUM-3) * (humidity);
DongHeon 1:4d65f05c7d2f 183 float T = log(VP/0.61078); // temp var
DongHeon 1:4d65f05c7d2f 184 return (241.88 * T) / (17.558-T);
DongHeon 1:4d65f05c7d2f 185 }
DongHeon 1:4d65f05c7d2f 186
DongHeon 1:4d65f05c7d2f 187 // delta max = 0.6544 wrt dewPoint()
DongHeon 1:4d65f05c7d2f 188 // 5x faster than dewPoint()
DongHeon 1:4d65f05c7d2f 189 // reference: http://en.wikipedia.org/wiki/Dew_point
DongHeon 1:4d65f05c7d2f 190 /*float DHT::CalcdewPointFast(float const celsius, float const humidity)
DongHeon 1:4d65f05c7d2f 191 {
DongHeon 1:4d65f05c7d2f 192 float a = 17.271;
DongHeon 1:4d65f05c7d2f 193 float b = 237.7;
DongHeon 1:4d65f05c7d2f 194 float temp = (a * celsius) / (b + celsius) + log(humidity/100);
DongHeon 1:4d65f05c7d2f 195 float Td = (b * temp) / (a - temp);
DongHeon 1:4d65f05c7d2f 196 return Td;
DongHeon 1:4d65f05c7d2f 197 }*/
DongHeon 1:4d65f05c7d2f 198
DongHeon 1:4d65f05c7d2f 199 float DHT::ReadTemperature(eScale Scale)
DongHeon 1:4d65f05c7d2f 200 {
DongHeon 1:4d65f05c7d2f 201 if (Scale == FARENHEIT)
DongHeon 1:4d65f05c7d2f 202 return ConvertCelciustoFarenheit(_lastTemperature);
DongHeon 1:4d65f05c7d2f 203 else if (Scale == KELVIN)
DongHeon 1:4d65f05c7d2f 204 return ConvertCelciustoKelvin(_lastTemperature);
DongHeon 1:4d65f05c7d2f 205 else
DongHeon 1:4d65f05c7d2f 206 return _lastTemperature;
DongHeon 1:4d65f05c7d2f 207 }
DongHeon 1:4d65f05c7d2f 208
DongHeon 1:4d65f05c7d2f 209 float DHT::CalcHumidity()
DongHeon 1:4d65f05c7d2f 210 {
DongHeon 1:4d65f05c7d2f 211 float v;
DongHeon 1:4d65f05c7d2f 212
DongHeon 1:4d65f05c7d2f 213 switch (_DHTtype) {
DongHeon 1:4d65f05c7d2f 214 case DHT11:
DongHeon 1:4d65f05c7d2f 215 v = DHT_data[0];
DongHeon 1:4d65f05c7d2f 216 return float(v);
DongHeon 1:4d65f05c7d2f 217 case DHT22:
DongHeon 1:4d65f05c7d2f 218 v = DHT_data[0];
DongHeon 1:4d65f05c7d2f 219 v *= 256;
DongHeon 1:4d65f05c7d2f 220 v += DHT_data[1];
DongHeon 1:4d65f05c7d2f 221 v /= 10;
DongHeon 1:4d65f05c7d2f 222 return float(v);
DongHeon 1:4d65f05c7d2f 223 }
DongHeon 1:4d65f05c7d2f 224 return 0;
DongHeon 1:4d65f05c7d2f 225 }
DongHeon 1:4d65f05c7d2f 226
DongHeon 1:4d65f05c7d2f 227
DongHeon 1:4d65f05c7d2f 228