1 step for smart green house

Dependencies:   mbed

Committer:
belloula
Date:
Wed May 22 22:28:10 2019 +0000
Revision:
0:fcce3b1e1dec
for smart green house

Who changed what in which revision?

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