2020-02-18 08:11:31 +00:00
|
|
|
/*
|
2020-02-19 08:37:36 +00:00
|
|
|
xsns_63_AHT1x.ino - AHT10 I2C temperature and humidity sensor support for Tasmota
|
2020-02-18 08:11:31 +00:00
|
|
|
|
2020-02-19 08:37:36 +00:00
|
|
|
Copyright (C) 2020 Martin Wagner
|
2020-02-18 08:11:31 +00:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program 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 General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2020-03-08 13:42:37 +00:00
|
|
|
|
2020-02-18 08:11:31 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_I2C
|
2020-02-19 08:37:36 +00:00
|
|
|
#ifdef USE_AHT1x
|
2020-02-18 08:11:31 +00:00
|
|
|
/*********************************************************************************************\
|
2020-02-19 08:37:36 +00:00
|
|
|
* AHT10/15 - Temperature and Humidity
|
2020-02-18 08:11:31 +00:00
|
|
|
*
|
|
|
|
* I2C Address: 0x38
|
2020-03-08 13:42:37 +00:00
|
|
|
*
|
|
|
|
* Attention: this Sensor is incompatible with other I2C devices on I2C bus.
|
2020-03-16 15:52:22 +00:00
|
|
|
*
|
2020-03-08 13:42:37 +00:00
|
|
|
* The Datasheet write:
|
2020-03-16 15:52:22 +00:00
|
|
|
* "Only a single AHT10 can be connected to the I2C bus and no other I2C
|
2020-03-08 13:42:37 +00:00
|
|
|
* devices can be connected".
|
2020-03-16 15:52:22 +00:00
|
|
|
*
|
2020-03-08 13:42:37 +00:00
|
|
|
* after lot of search and tests, now is confirmed that works only reliable with one sensor
|
|
|
|
* on I2C Bus
|
2020-02-18 08:11:31 +00:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2020-02-19 08:37:36 +00:00
|
|
|
#define XSNS_63 63
|
2020-02-18 08:11:31 +00:00
|
|
|
#define XI2C_43 43 // See I2CDEVICES.md
|
|
|
|
|
2020-03-16 15:52:22 +00:00
|
|
|
#define AHT1X_ADDR1 0x38
|
|
|
|
#define AHT1X_ADDR2 0x39
|
2020-02-18 08:11:31 +00:00
|
|
|
|
2020-03-16 15:52:22 +00:00
|
|
|
#define AHT1X_MAX_SENSORS 2
|
2020-02-18 08:11:31 +00:00
|
|
|
|
2020-03-16 15:52:22 +00:00
|
|
|
#define AHT_HUMIDITY_CONST 100
|
|
|
|
#define AHT_TEMPERATURE_CONST 200
|
|
|
|
#define AHT_TEMPERATURE_OFFSET 50
|
|
|
|
#define KILOBYTE_CONST 1048576.0f
|
2020-02-18 08:11:31 +00:00
|
|
|
|
2020-03-16 15:52:22 +00:00
|
|
|
#define AHT1X_CMD_DELAY 40
|
|
|
|
#define AHT1X_RST_DELAY 30
|
|
|
|
#define AHT1X_MEAS_DELAY 80 // over 75ms in datasheet
|
2020-02-20 12:38:03 +00:00
|
|
|
|
2020-03-16 15:52:22 +00:00
|
|
|
uint8_t AHTSetCalCmd[3] = {0xE1, 0x08, 00}; //load factory calibration coeff
|
|
|
|
uint8_t AHTSetCycleCmd[3] = {0xE1, 0x28, 00}; //enable cycle mode
|
|
|
|
uint8_t AHTMeasureCmd[3] = {0xAC, 0x33, 00}; //start measurment command
|
|
|
|
uint8_t AHTResetCmd = 0xBA; //soft reset command
|
2020-02-20 12:38:03 +00:00
|
|
|
|
2020-03-16 15:52:22 +00:00
|
|
|
const char ahtTypes[] PROGMEM = "AHT1X|AHT1X";
|
|
|
|
uint8_t aht1x_addresses[] = { AHT1X_ADDR1, AHT1X_ADDR2 };
|
|
|
|
uint8_t aht1x_count = 0;
|
|
|
|
uint8_t aht1x_Pcount = 0;
|
2020-02-19 18:49:09 +00:00
|
|
|
|
2020-03-16 15:52:22 +00:00
|
|
|
struct AHT1XSTRUCT
|
|
|
|
{
|
|
|
|
float humidity = NAN;
|
|
|
|
float temperature = NAN;
|
|
|
|
uint8_t address; // bus address
|
|
|
|
char types[6]; // Sensor type name and address -
|
|
|
|
} aht1x_sensors[AHT1X_MAX_SENSORS];
|
2020-02-19 18:49:09 +00:00
|
|
|
|
2020-03-16 15:52:22 +00:00
|
|
|
bool AHT1XWrite(uint8_t aht1x_idx)
|
|
|
|
{
|
|
|
|
Wire.beginTransmission(aht1x_sensors[aht1x_idx].address);
|
|
|
|
Wire.write(AHTMeasureCmd, 3);
|
|
|
|
if (Wire.endTransmission() != 0)
|
|
|
|
return false;
|
|
|
|
}
|
2020-02-19 18:49:09 +00:00
|
|
|
|
2020-03-16 15:52:22 +00:00
|
|
|
bool AHT1XRead(uint8_t aht1x_idx)
|
|
|
|
{
|
|
|
|
uint8_t data[6];
|
|
|
|
Wire.requestFrom(aht1x_sensors[aht1x_idx].address, (uint8_t) 6);
|
|
|
|
for(uint8_t i = 0; Wire.available() > 0; i++){
|
|
|
|
data[i] = Wire.read();
|
|
|
|
}
|
|
|
|
if ((data[0] & 0x80) == 0x80) return false; //device is busy
|
2020-02-18 08:11:31 +00:00
|
|
|
|
2020-03-16 15:52:22 +00:00
|
|
|
aht1x_sensors[aht1x_idx].humidity = (((data[1] << 12)| (data[2] << 4) | data[3] >> 4) * AHT_HUMIDITY_CONST / KILOBYTE_CONST);
|
|
|
|
aht1x_sensors[aht1x_idx].temperature = ((AHT_TEMPERATURE_CONST * (((data[3] & 0x0F) << 16) | (data[4] << 8) | data[5]) / KILOBYTE_CONST) - AHT_TEMPERATURE_OFFSET);
|
2020-02-18 08:11:31 +00:00
|
|
|
|
2020-03-16 15:52:22 +00:00
|
|
|
return (!isnan(aht1x_sensors[aht1x_idx].temperature) && !isnan(aht1x_sensors[aht1x_idx].humidity) && (aht1x_sensors[aht1x_idx].humidity != 0));
|
2020-02-18 08:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************/
|
2020-02-20 12:38:03 +00:00
|
|
|
|
2020-03-16 15:52:22 +00:00
|
|
|
// Test for Polling the device without delays
|
|
|
|
// Incompatible with other devices on I2C bus
|
|
|
|
void AHT1XPoll(void) // We have 100ms for read. Sensor needs 80-95 ms
|
2020-02-18 19:08:22 +00:00
|
|
|
{
|
2020-03-16 15:52:22 +00:00
|
|
|
aht1x_Pcount++;
|
|
|
|
switch (aht1x_Pcount) {
|
|
|
|
case 10:
|
|
|
|
AHT1XWrite(0);
|
|
|
|
break;
|
|
|
|
case 11:
|
2020-03-18 10:23:00 +00:00
|
|
|
AHT1XRead(0);
|
2020-03-16 15:52:22 +00:00
|
|
|
aht1x_Pcount = 0;
|
|
|
|
break;
|
|
|
|
}
|
2020-02-18 19:08:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-16 15:52:22 +00:00
|
|
|
unsigned char AHT1XReadStatus(uint8_t aht1x_address)
|
2020-02-18 08:11:31 +00:00
|
|
|
{
|
2020-03-16 15:52:22 +00:00
|
|
|
uint8_t result = 0;
|
|
|
|
Wire.requestFrom(aht1x_address, (uint8_t) 1);
|
|
|
|
result = Wire.read();
|
2020-02-20 12:38:03 +00:00
|
|
|
return result;
|
2020-02-18 08:11:31 +00:00
|
|
|
}
|
|
|
|
|
2020-03-16 15:52:22 +00:00
|
|
|
void AHT1XReset(uint8_t aht1x_address)
|
2020-02-18 19:08:22 +00:00
|
|
|
{
|
2020-03-16 15:52:22 +00:00
|
|
|
Wire.beginTransmission(aht1x_address);
|
|
|
|
Wire.write(AHTResetCmd);
|
2020-02-20 12:38:03 +00:00
|
|
|
Wire.endTransmission();
|
2020-03-16 15:52:22 +00:00
|
|
|
delay(AHT1X_RST_DELAY);
|
2020-02-18 19:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************/
|
2020-03-16 15:52:22 +00:00
|
|
|
bool AHT1XInit(uint8_t aht1x_address)
|
2020-02-18 08:11:31 +00:00
|
|
|
{
|
2020-03-16 15:52:22 +00:00
|
|
|
Wire.beginTransmission(aht1x_address);
|
|
|
|
Wire.write(AHTSetCalCmd, 3);
|
|
|
|
if (Wire.endTransmission() != 0) return false;
|
|
|
|
delay(AHT1X_CMD_DELAY);
|
|
|
|
if((AHT1XReadStatus(aht1x_address) & 0x68) == 0x08) // Sensor calcoef aktiv
|
|
|
|
return true;
|
|
|
|
return false;
|
2020-02-18 08:11:31 +00:00
|
|
|
}
|
|
|
|
|
2020-03-16 15:52:22 +00:00
|
|
|
void AHT1XDetect(void)
|
2020-02-18 08:11:31 +00:00
|
|
|
{
|
2020-03-16 15:52:22 +00:00
|
|
|
for (uint8_t i = 0; i < AHT1X_MAX_SENSORS; i++) {
|
|
|
|
if (I2cActive(aht1x_addresses[i])) { continue; }
|
|
|
|
if (AHT1XInit(aht1x_addresses[i]))
|
|
|
|
{
|
|
|
|
aht1x_sensors[aht1x_count].address = aht1x_addresses[i];
|
|
|
|
GetTextIndexed(aht1x_sensors[aht1x_count].types, sizeof(aht1x_sensors[aht1x_count].types), i, ahtTypes);
|
|
|
|
I2cSetActiveFound(aht1x_sensors[aht1x_count].address, aht1x_sensors[aht1x_count].types);
|
|
|
|
aht1x_count = 1;
|
|
|
|
break; // Only one Sensor allowed at the moment (I2C Sensor-Bug)
|
2020-02-18 08:11:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-17 15:29:59 +00:00
|
|
|
|
2020-03-16 15:52:22 +00:00
|
|
|
void AHT1XShow(bool json)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < aht1x_count; i++) {
|
|
|
|
float tem = ConvertTemp(aht1x_sensors[i].temperature);
|
|
|
|
float hum = ConvertHumidity(aht1x_sensors[i].humidity);
|
|
|
|
char types[11]; // AHT1X-0x38
|
|
|
|
snprintf_P(types, sizeof(types), PSTR("%s%c0x%02X"), aht1x_sensors[i].types, IndexSeparator(), aht1x_sensors[i].address); // "X-0xXX"
|
|
|
|
TempHumDewShow(json, ((0 == tele_period) && (0 == i)), types, tem, hum);
|
2020-02-18 08:11:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2020-02-19 08:37:36 +00:00
|
|
|
bool Xsns63(uint8_t function)
|
2020-02-18 08:11:31 +00:00
|
|
|
{
|
|
|
|
if (!I2cEnabled(XI2C_43)) { return false; }
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
if (FUNC_INIT == function) {
|
2020-03-16 15:52:22 +00:00
|
|
|
AHT1XDetect();
|
2020-02-18 08:11:31 +00:00
|
|
|
}
|
2020-03-16 15:52:22 +00:00
|
|
|
else if (aht1x_count){
|
2020-02-18 08:11:31 +00:00
|
|
|
switch (function) {
|
2020-03-16 15:52:22 +00:00
|
|
|
case FUNC_EVERY_100_MSECOND:
|
|
|
|
AHT1XPoll();
|
|
|
|
break;
|
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
AHT1XShow(1);
|
|
|
|
break;
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
case FUNC_WEB_SENSOR:
|
|
|
|
AHT1XShow(0);
|
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
2020-02-18 08:11:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-03-16 15:52:22 +00:00
|
|
|
#endif // USE_AHT1X
|
|
|
|
#endif // USE_I2C
|