2017-01-28 13:41:01 +00:00
|
|
|
/*
|
2019-10-27 10:13:24 +00:00
|
|
|
xsns_08_htu21.ino - HTU21 temperature and humidity sensor support for Tasmota
|
2017-05-13 12:02:10 +01:00
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
Copyright (C) 2021 Heiko Krupp and Theo Arends
|
2017-05-13 12:02:10 +01: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/>.
|
2017-01-28 13:41:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_I2C
|
|
|
|
#ifdef USE_HTU
|
|
|
|
/*********************************************************************************************\
|
2020-11-29 20:08:08 +00:00
|
|
|
* HTU21 - Temperature and Humidity
|
2017-01-28 13:41:01 +00:00
|
|
|
*
|
|
|
|
* Source: Heiko Krupp
|
2017-11-11 11:33:30 +00:00
|
|
|
*
|
|
|
|
* I2C Address: 0x40
|
2017-01-28 13:41:01 +00:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-11-06 16:33:51 +00:00
|
|
|
#define XSNS_08 8
|
2019-11-03 16:54:39 +00:00
|
|
|
#define XI2C_09 9 // See I2CDEVICES.md
|
2018-11-06 16:33:51 +00:00
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
#define HTU21_ADDR 0x40
|
|
|
|
|
2017-02-28 15:01:48 +00:00
|
|
|
#define SI7013_CHIPID 0x0D
|
|
|
|
#define SI7020_CHIPID 0x14
|
|
|
|
#define SI7021_CHIPID 0x15
|
2017-01-28 13:41:01 +00:00
|
|
|
#define HTU21_CHIPID 0x32
|
|
|
|
|
|
|
|
#define HTU21_READTEMP 0xE3
|
|
|
|
#define HTU21_READHUM 0xE5
|
|
|
|
#define HTU21_WRITEREG 0xE6
|
|
|
|
#define HTU21_READREG 0xE7
|
|
|
|
#define HTU21_RESET 0xFE
|
|
|
|
#define HTU21_HEATER_WRITE 0x51
|
|
|
|
#define HTU21_HEATER_READ 0x11
|
|
|
|
#define HTU21_SERIAL2_READ1 0xFC /* Read 3rd two Serial bytes */
|
|
|
|
#define HTU21_SERIAL2_READ2 0xC9 /* Read 4th two Serial bytes */
|
|
|
|
|
|
|
|
#define HTU21_HEATER_ON 0x04
|
|
|
|
#define HTU21_HEATER_OFF 0xFB
|
|
|
|
|
2017-02-28 15:01:48 +00:00
|
|
|
#define HTU21_RES_RH12_T14 0x00 // Default
|
2017-01-28 13:41:01 +00:00
|
|
|
#define HTU21_RES_RH8_T12 0x01
|
|
|
|
#define HTU21_RES_RH10_T13 0x80
|
|
|
|
#define HTU21_RES_RH11_T11 0x81
|
|
|
|
|
|
|
|
#define HTU21_CRC8_POLYNOM 0x13100
|
|
|
|
|
2017-11-11 11:33:30 +00:00
|
|
|
const char kHtuTypes[] PROGMEM = "HTU21|SI7013|SI7020|SI7021|T/RH?";
|
|
|
|
|
2020-04-18 16:04:00 +01:00
|
|
|
struct {
|
|
|
|
float temperature = 0;
|
|
|
|
float humidity = 0;
|
|
|
|
uint8_t address;
|
2023-10-19 16:37:19 +01:00
|
|
|
uint8_t bus;
|
2020-04-18 16:04:00 +01:00
|
|
|
uint8_t type = 0;
|
|
|
|
uint8_t delay_temp;
|
|
|
|
uint8_t delay_humidity = 50;
|
|
|
|
uint8_t valid = 0;
|
|
|
|
char types[7];
|
|
|
|
} Htu;
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2023-10-19 16:37:19 +01:00
|
|
|
uint8_t HtuCheckCrc8(uint16_t data) {
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t bit = 0; bit < 16; bit++) {
|
2017-04-25 17:24:42 +01:00
|
|
|
if (data & 0x8000) {
|
2017-01-28 13:41:01 +00:00
|
|
|
data = (data << 1) ^ HTU21_CRC8_POLYNOM;
|
2017-04-25 17:24:42 +01:00
|
|
|
} else {
|
2017-01-28 13:41:01 +00:00
|
|
|
data <<= 1;
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
return data >>= 8;
|
|
|
|
}
|
|
|
|
|
2023-10-19 16:37:19 +01:00
|
|
|
bool HtuReset(void) {
|
|
|
|
TwoWire& myWire = I2cGetWire(Htu.bus);
|
|
|
|
if (&myWire == nullptr) { return false; } // No valid I2c bus
|
|
|
|
|
|
|
|
myWire.beginTransmission(HTU21_ADDR);
|
|
|
|
myWire.write(HTU21_RESET);
|
|
|
|
myWire.endTransmission();
|
|
|
|
delay(15); // Reset takes 15ms
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t HtuReadDeviceId(void) {
|
|
|
|
if (!HtuReset()) { return 0; }; // Fixes ESP32 sensor loss at restart
|
2020-04-18 16:04:00 +01:00
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
uint16_t deviceID = 0;
|
|
|
|
uint8_t checksum = 0;
|
|
|
|
|
2023-10-19 16:37:19 +01:00
|
|
|
TwoWire& myWire = I2cGetWire(Htu.bus);
|
|
|
|
myWire.beginTransmission(HTU21_ADDR);
|
|
|
|
myWire.write(HTU21_SERIAL2_READ1);
|
|
|
|
myWire.write(HTU21_SERIAL2_READ2);
|
|
|
|
myWire.endTransmission();
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2023-10-19 16:37:19 +01:00
|
|
|
myWire.requestFrom(HTU21_ADDR, 3);
|
|
|
|
deviceID = myWire.read() << 8;
|
|
|
|
deviceID |= myWire.read();
|
|
|
|
checksum = myWire.read();
|
2017-10-18 17:22:34 +01:00
|
|
|
if (HtuCheckCrc8(deviceID) == checksum) {
|
2017-01-28 13:41:01 +00:00
|
|
|
deviceID = deviceID >> 8;
|
|
|
|
} else {
|
|
|
|
deviceID = 0;
|
|
|
|
}
|
|
|
|
return (uint8_t)deviceID;
|
|
|
|
}
|
|
|
|
|
2023-10-19 16:37:19 +01:00
|
|
|
void HtuSetResolution(uint8_t resolution) {
|
|
|
|
uint8_t current = I2cRead8(HTU21_ADDR, HTU21_READREG, Htu.bus);
|
2017-11-11 11:33:30 +00:00
|
|
|
current &= 0x7E; // Replace current resolution bits with 0
|
|
|
|
current |= resolution; // Add new resolution bits to register
|
2023-10-19 16:37:19 +01:00
|
|
|
I2cWrite8(HTU21_ADDR, HTU21_WRITEREG, current, Htu.bus);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 16:37:19 +01:00
|
|
|
void HtuHeater(uint8_t heater) {
|
|
|
|
uint8_t current = I2cRead8(HTU21_ADDR, HTU21_READREG, Htu.bus);
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2023-10-19 16:37:19 +01:00
|
|
|
switch(heater) {
|
2017-01-28 13:41:01 +00:00
|
|
|
case HTU21_HEATER_ON : current |= heater;
|
|
|
|
break;
|
|
|
|
case HTU21_HEATER_OFF : current &= heater;
|
|
|
|
break;
|
|
|
|
default : current &= heater;
|
|
|
|
break;
|
|
|
|
}
|
2023-10-19 16:37:19 +01:00
|
|
|
I2cWrite8(HTU21_ADDR, HTU21_WRITEREG, current, Htu.bus);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 16:37:19 +01:00
|
|
|
void HtuInit(void) {
|
2017-10-18 17:22:34 +01:00
|
|
|
HtuReset();
|
|
|
|
HtuHeater(HTU21_HEATER_OFF);
|
|
|
|
HtuSetResolution(HTU21_RES_RH12_T14);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 16:37:19 +01:00
|
|
|
bool HtuRead(void) {
|
2017-04-25 17:24:42 +01:00
|
|
|
uint8_t checksum = 0;
|
|
|
|
uint16_t sensorval = 0;
|
2018-07-10 21:12:16 +01:00
|
|
|
|
2020-04-18 16:04:00 +01:00
|
|
|
if (Htu.valid) { Htu.valid--; }
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2023-10-19 16:37:19 +01:00
|
|
|
TwoWire& myWire = I2cGetWire(Htu.bus);
|
|
|
|
myWire.beginTransmission(HTU21_ADDR);
|
|
|
|
myWire.write(HTU21_READTEMP);
|
|
|
|
if (myWire.endTransmission() != 0) { return false; } // In case of error
|
2020-04-18 16:04:00 +01:00
|
|
|
delay(Htu.delay_temp); // Sensor time at max resolution
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2023-10-19 16:37:19 +01:00
|
|
|
myWire.requestFrom(HTU21_ADDR, 3);
|
|
|
|
if (3 == myWire.available()) {
|
|
|
|
sensorval = myWire.read() << 8; // MSB
|
|
|
|
sensorval |= myWire.read(); // LSB
|
|
|
|
checksum = myWire.read();
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2018-07-10 21:12:16 +01:00
|
|
|
if (HtuCheckCrc8(sensorval) != checksum) { return false; } // Checksum mismatch
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2022-04-19 14:44:53 +01:00
|
|
|
Htu.temperature = ConvertTemp(0.002681f * (float)sensorval - 46.85f);
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2023-10-19 16:37:19 +01:00
|
|
|
myWire.beginTransmission(HTU21_ADDR);
|
|
|
|
myWire.write(HTU21_READHUM);
|
|
|
|
if (myWire.endTransmission() != 0) { return false; } // In case of error
|
2020-04-18 16:04:00 +01:00
|
|
|
delay(Htu.delay_humidity); // Sensor time at max resolution
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2023-10-19 16:37:19 +01:00
|
|
|
myWire.requestFrom(HTU21_ADDR, 3);
|
|
|
|
if (3 <= myWire.available()) {
|
|
|
|
sensorval = myWire.read() << 8; // MSB
|
|
|
|
sensorval |= myWire.read(); // LSB
|
|
|
|
checksum = myWire.read();
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2018-07-10 21:12:16 +01:00
|
|
|
if (HtuCheckCrc8(sensorval) != checksum) { return false; } // Checksum mismatch
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2018-07-10 21:12:16 +01:00
|
|
|
sensorval ^= 0x02; // clear status bits
|
2022-04-19 14:44:53 +01:00
|
|
|
Htu.humidity = 0.001907f * (float)sensorval - 6;
|
|
|
|
if (Htu.humidity > 100) { Htu.humidity = 100.0f; }
|
|
|
|
if (Htu.humidity < 0) { Htu.humidity = 0.01f; }
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2022-04-19 14:44:53 +01:00
|
|
|
if ((0.00f == Htu.humidity) && (0.00f == Htu.temperature)) {
|
|
|
|
Htu.humidity = 0.0f;
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2022-04-19 14:44:53 +01:00
|
|
|
if ((Htu.temperature > 0.00f) && (Htu.temperature < 80.00f)) {
|
|
|
|
Htu.humidity = (-0.15f) * (25 - Htu.temperature) + Htu.humidity;
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2020-04-18 16:04:00 +01:00
|
|
|
Htu.humidity = ConvertHumidity(Htu.humidity);
|
2018-07-24 17:41:50 +01:00
|
|
|
|
2020-04-18 16:04:00 +01:00
|
|
|
Htu.valid = SENSOR_MAX_MISS;
|
2018-07-10 21:12:16 +01:00
|
|
|
return true;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-11-04 15:36:51 +00:00
|
|
|
/********************************************************************************************/
|
|
|
|
|
2023-10-19 16:37:19 +01:00
|
|
|
void HtuDetect(void) {
|
2020-04-18 16:04:00 +01:00
|
|
|
Htu.address = HTU21_ADDR;
|
2023-10-19 16:37:19 +01:00
|
|
|
for (Htu.bus = 0; Htu.bus < 2; Htu.bus++) {
|
|
|
|
if (!I2cSetDevice(Htu.address, Htu.bus)) { continue; }
|
|
|
|
|
|
|
|
Htu.type = HtuReadDeviceId();
|
|
|
|
if (Htu.type) {
|
|
|
|
uint8_t index = 0;
|
|
|
|
HtuInit();
|
|
|
|
switch (Htu.type) {
|
|
|
|
case HTU21_CHIPID:
|
|
|
|
Htu.delay_temp = 50;
|
|
|
|
Htu.delay_humidity = 16;
|
|
|
|
break;
|
|
|
|
case SI7021_CHIPID:
|
|
|
|
index++; // 3
|
|
|
|
case SI7020_CHIPID:
|
|
|
|
index++; // 2
|
|
|
|
case SI7013_CHIPID:
|
|
|
|
index++; // 1
|
|
|
|
Htu.delay_temp = 12;
|
|
|
|
Htu.delay_humidity = 23;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
index = 4;
|
|
|
|
Htu.delay_temp = 50;
|
|
|
|
Htu.delay_humidity = 23;
|
|
|
|
}
|
|
|
|
GetTextIndexed(Htu.types, sizeof(Htu.types), index, kHtuTypes);
|
2023-10-19 16:44:59 +01:00
|
|
|
I2cSetActiveFound(Htu.address, Htu.types, Htu.bus);
|
2023-10-19 16:37:19 +01:00
|
|
|
break;
|
2017-11-11 11:33:30 +00:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 16:37:19 +01:00
|
|
|
void HtuEverySecond(void) {
|
2020-10-28 16:32:07 +00:00
|
|
|
if (TasmotaGlobal.uptime &1) { // Every 2 seconds
|
2018-07-12 11:19:08 +01:00
|
|
|
// HTU21: 68mS, SI70xx: 37mS
|
2019-11-11 16:32:44 +00:00
|
|
|
if (!HtuRead()) {
|
2020-04-18 16:04:00 +01:00
|
|
|
AddLogMissed(Htu.types, Htu.valid);
|
2018-07-10 21:12:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-17 15:29:59 +00:00
|
|
|
|
2023-10-19 16:37:19 +01:00
|
|
|
void HtuShow(bool json) {
|
2020-04-18 16:04:00 +01:00
|
|
|
if (Htu.valid) {
|
2020-10-29 12:37:09 +00:00
|
|
|
TempHumDewShow(json, (0 == TasmotaGlobal.tele_period), Htu.types, Htu.temperature, Htu.humidity);
|
2020-03-16 15:52:22 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-03 17:07:25 +00:00
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2022-11-11 09:44:56 +00:00
|
|
|
bool Xsns08(uint32_t function)
|
2017-11-03 17:07:25 +00:00
|
|
|
{
|
2019-11-04 09:38:05 +00:00
|
|
|
if (!I2cEnabled(XI2C_09)) { return false; }
|
2019-11-03 16:54:39 +00:00
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2017-11-03 17:07:25 +00:00
|
|
|
|
2019-11-11 16:32:44 +00:00
|
|
|
if (FUNC_INIT == function) {
|
|
|
|
HtuDetect();
|
|
|
|
}
|
2020-04-18 16:04:00 +01:00
|
|
|
else if (Htu.type) {
|
2019-11-11 16:32:44 +00:00
|
|
|
switch (function) {
|
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
HtuEverySecond();
|
|
|
|
break;
|
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
HtuShow(1);
|
|
|
|
break;
|
2017-11-03 17:07:25 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
2019-11-11 16:32:44 +00:00
|
|
|
case FUNC_WEB_SENSOR:
|
|
|
|
HtuShow(0);
|
|
|
|
break;
|
2017-11-03 17:07:25 +00:00
|
|
|
#endif // USE_WEBSERVER
|
2019-11-11 16:32:44 +00:00
|
|
|
}
|
2017-11-03 17:07:25 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
#endif // USE_HTU
|
|
|
|
#endif // USE_I2C
|
|
|
|
|