/
Thinary_AHT10.cpp
162 lines (138 loc) · 4.7 KB
/
Thinary_AHT10.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
AHT10 - A Humidity Library for Arduino.
Supported Sensor modules:
AHT10-Breakout Module - https://www.aliexpress.com/item/33002710848.html
Created by Thinary Eletronic at Modern Device on April 2019.
* This file is part of Thinary_AHT10.
*
* Thinary_AHT10 is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or(at your option) any later version.
*
* Thinary_AHT10 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Thinary_AHT10. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <math.h>
#include <Arduino.h>
#include <Wire.h>
#include "Thinary_AHT10.h"
// Specify the constants for water vapor and barometric pressure.
#define WATER_VAPOR 17.62f
#define BAROMETRIC_PRESSURE 243.5f
Sensor_CMD eSensorCalibrateCmd[3] = {0xE1, 0x08, 0x00};
Sensor_CMD eSensorNormalCmd[3] = {0xA8, 0x00, 0x00};
Sensor_CMD eSensorMeasureCmd[3] = {0xAC, 0x33, 0x00};
Sensor_CMD eSensorResetCmd = 0xBA;
boolean GetRHumidityCmd = true;
boolean GetTempCmd = false;
/******************************************************************************
* Global Functions
******************************************************************************/
AHT10Class::AHT10Class() {
}
boolean AHT10Class::begin(unsigned char _AHT10_address)
{
AHT10_address = _AHT10_address;
Serial.begin(9600);
Serial.println("\x54\x68\x69\x6E\x61\x72\x79\x20\x45\x6C\x65\x74\x72\x6F\x6E\x69\x63\x20\x41\x48\x54\x31\x30\x20\x4D\x6F\x64\x75\x6C\x65\x2E");
Wire.begin(AHT10_address);
Wire.beginTransmission(AHT10_address);
Wire.write(eSensorCalibrateCmd, 3);
Wire.endTransmission();
Serial.println("https://thinaryelectronic.aliexpress.com");
delay(500);
if((readStatus()&0x68) == 0x08)
return true;
else
{
return false;
}
}
/**********************************************************
* GetHumidity
* Gets the current humidity from the sensor.
*
* @return float - The relative humidity in %RH
**********************************************************/
float AHT10Class::GetHumidity(void)
{
float value = readSensor(GetRHumidityCmd);
if (value == 0) {
return 0; // Some unrealistic value
}
return value * 100 / 1048576;
}
/**********************************************************
* GetTemperature
* Gets the current temperature from the sensor.
*
* @return float - The temperature in Deg C
**********************************************************/
float AHT10Class::GetTemperature(void)
{
float value = readSensor(GetTempCmd);
return ((200 * value) / 1048576) - 50;
}
/**********************************************************
* GetDewPoint
* Gets the current dew point based on the current humidity and temperature
*
* @return float - The dew point in Deg C
**********************************************************/
float AHT10Class::GetDewPoint(void)
{
float humidity = GetHumidity();
float temperature = GetTemperature();
// Calculate the intermediate value 'gamma'
float gamma = log(humidity / 100) + WATER_VAPOR * temperature / (BAROMETRIC_PRESSURE + temperature);
// Calculate dew point in Celsius
float dewPoint = BAROMETRIC_PRESSURE * gamma / (WATER_VAPOR - gamma);
return dewPoint;
}
/******************************************************************************
* Private Functions
******************************************************************************/
unsigned long AHT10Class::readSensor(boolean GetDataCmd)
{
unsigned long result, temp[6];
Wire.beginTransmission(AHT10_address);
Wire.write(eSensorMeasureCmd, 3);
Wire.endTransmission();
delay(100);
Wire.requestFrom(AHT10_address, 6);
for(unsigned char i = 0; Wire.available() > 0; i++)
{
temp[i] = Wire.read();
}
if(GetDataCmd)
{
result = ((temp[1] << 16) | (temp[2] << 8) | temp[3]) >> 4;
}
else
{
result = ((temp[3] & 0x0F) << 16) | (temp[4] << 8) | temp[5];
}
return result;
}
unsigned char AHT10Class::readStatus(void)
{
unsigned char result = 0;
Wire.requestFrom(AHT10_address, 1);
result = Wire.read();
return result;
}
void AHT10Class::Reset(void)
{
Wire.beginTransmission(AHT10_address);
Wire.write(eSensorResetCmd);
Wire.endTransmission();
delay(20);
}