2017-01-28 13:41:01 +00:00
|
|
|
/*
|
2017-11-07 14:57:24 +00:00
|
|
|
xsns_08_htu21.ino - HTU21 temperature and humidity sensor support for Sonoff-Tasmota
|
2017-05-13 12:02:10 +01:00
|
|
|
|
2019-01-01 12:55:01 +00:00
|
|
|
Copyright (C) 2019 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
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* HTU21 - Temperature and Humidy
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
|
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?";
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
uint8_t htu_address;
|
|
|
|
uint8_t htu_type = 0;
|
2018-07-10 21:12:16 +01:00
|
|
|
uint8_t htu_delay_temp;
|
|
|
|
uint8_t htu_delay_humidity = 50;
|
|
|
|
uint8_t htu_valid = 0;
|
|
|
|
float htu_temperature = 0;
|
|
|
|
float htu_humidity = 0;
|
2017-10-18 17:22:34 +01:00
|
|
|
char htu_types[7];
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
uint8_t HtuCheckCrc8(uint16_t data)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2017-04-25 17:24:42 +01:00
|
|
|
for (uint8_t bit = 0; bit < 16; bit++) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
uint8_t HtuReadDeviceId(void)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
|
|
|
uint16_t deviceID = 0;
|
|
|
|
uint8_t checksum = 0;
|
|
|
|
|
|
|
|
Wire.beginTransmission(HTU21_ADDR);
|
|
|
|
Wire.write(HTU21_SERIAL2_READ1);
|
|
|
|
Wire.write(HTU21_SERIAL2_READ2);
|
|
|
|
Wire.endTransmission();
|
|
|
|
|
|
|
|
Wire.requestFrom(HTU21_ADDR, 3);
|
|
|
|
deviceID = Wire.read() << 8;
|
|
|
|
deviceID |= Wire.read();
|
|
|
|
checksum = Wire.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;
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void HtuSetResolution(uint8_t resolution)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
uint8_t current = I2cRead8(HTU21_ADDR, HTU21_READREG);
|
2017-11-11 11:33:30 +00:00
|
|
|
current &= 0x7E; // Replace current resolution bits with 0
|
|
|
|
current |= resolution; // Add new resolution bits to register
|
2017-10-18 17:22:34 +01:00
|
|
|
I2cWrite8(HTU21_ADDR, HTU21_WRITEREG, current);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void HtuReset(void)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
|
|
|
Wire.beginTransmission(HTU21_ADDR);
|
|
|
|
Wire.write(HTU21_RESET);
|
|
|
|
Wire.endTransmission();
|
|
|
|
delay(15); // Reset takes 15ms
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void HtuHeater(uint8_t heater)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
uint8_t current = I2cRead8(HTU21_ADDR, HTU21_READREG);
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
switch(heater)
|
|
|
|
{
|
|
|
|
case HTU21_HEATER_ON : current |= heater;
|
|
|
|
break;
|
|
|
|
case HTU21_HEATER_OFF : current &= heater;
|
|
|
|
break;
|
|
|
|
default : current &= heater;
|
|
|
|
break;
|
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
I2cWrite8(HTU21_ADDR, HTU21_WRITEREG, current);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void HtuInit(void)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
boolean HtuRead(void)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2017-04-25 17:24:42 +01:00
|
|
|
uint8_t checksum = 0;
|
|
|
|
uint16_t sensorval = 0;
|
2018-07-10 21:12:16 +01:00
|
|
|
|
|
|
|
if (htu_valid) { htu_valid--; }
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
Wire.beginTransmission(HTU21_ADDR);
|
2018-07-10 21:12:16 +01:00
|
|
|
Wire.write(HTU21_READTEMP);
|
|
|
|
if (Wire.endTransmission() != 0) { return false; } // In case of error
|
|
|
|
delay(htu_delay_temp); // Sensor time at max resolution
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
Wire.requestFrom(HTU21_ADDR, 3);
|
2018-07-10 21:12:16 +01:00
|
|
|
if (3 == Wire.available()) {
|
|
|
|
sensorval = Wire.read() << 8; // MSB
|
|
|
|
sensorval |= Wire.read(); // LSB
|
2017-01-28 13:41:01 +00:00
|
|
|
checksum = Wire.read();
|
|
|
|
}
|
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
|
|
|
htu_temperature = ConvertTemp(0.002681 * (float)sensorval - 46.85);
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
Wire.beginTransmission(HTU21_ADDR);
|
2018-07-10 21:12:16 +01:00
|
|
|
Wire.write(HTU21_READHUM);
|
|
|
|
if (Wire.endTransmission() != 0) { return false; } // In case of error
|
|
|
|
delay(htu_delay_humidity); // Sensor time at max resolution
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
Wire.requestFrom(HTU21_ADDR, 3);
|
2018-07-10 21:12:16 +01:00
|
|
|
if (3 <= Wire.available()) {
|
|
|
|
sensorval = Wire.read() << 8; // MSB
|
|
|
|
sensorval |= Wire.read(); // LSB
|
2017-01-28 13:41:01 +00:00
|
|
|
checksum = Wire.read();
|
|
|
|
}
|
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
|
|
|
|
htu_humidity = 0.001907 * (float)sensorval - 6;
|
|
|
|
if (htu_humidity > 100) { htu_humidity = 100.0; }
|
|
|
|
if (htu_humidity < 0) { htu_humidity = 0.01; }
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2018-07-10 21:12:16 +01:00
|
|
|
if ((0.00 == htu_humidity) && (0.00 == htu_temperature)) {
|
|
|
|
htu_humidity = 0.0;
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2018-07-10 21:12:16 +01:00
|
|
|
if ((htu_temperature > 0.00) && (htu_temperature < 80.00)) {
|
|
|
|
htu_humidity = (-0.15) * (25 - htu_temperature) + htu_humidity;
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2018-07-10 21:12:16 +01:00
|
|
|
|
2018-07-24 17:41:50 +01:00
|
|
|
SetGlobalValues(htu_temperature, htu_humidity);
|
|
|
|
|
2018-07-10 21:12:16 +01:00
|
|
|
htu_valid = SENSOR_MAX_MISS;
|
|
|
|
return true;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-11-04 15:36:51 +00:00
|
|
|
/********************************************************************************************/
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void HtuDetect(void)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2018-07-10 21:12:16 +01:00
|
|
|
if (htu_type) { return; }
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
htu_address = HTU21_ADDR;
|
|
|
|
htu_type = HtuReadDeviceId();
|
2017-11-11 11:33:30 +00:00
|
|
|
if (htu_type) {
|
|
|
|
uint8_t index = 0;
|
|
|
|
HtuInit();
|
|
|
|
switch (htu_type) {
|
|
|
|
case HTU21_CHIPID:
|
2018-07-10 21:12:16 +01:00
|
|
|
htu_delay_temp = 50;
|
|
|
|
htu_delay_humidity = 16;
|
2017-11-11 11:33:30 +00:00
|
|
|
break;
|
|
|
|
case SI7021_CHIPID:
|
|
|
|
index++; // 3
|
|
|
|
case SI7020_CHIPID:
|
|
|
|
index++; // 2
|
|
|
|
case SI7013_CHIPID:
|
|
|
|
index++; // 1
|
2018-07-10 21:12:16 +01:00
|
|
|
htu_delay_temp = 12;
|
|
|
|
htu_delay_humidity = 23;
|
2017-11-11 11:33:30 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
index = 4;
|
2018-07-10 21:12:16 +01:00
|
|
|
htu_delay_temp = 50;
|
|
|
|
htu_delay_humidity = 23;
|
2017-11-11 11:33:30 +00:00
|
|
|
}
|
|
|
|
GetTextIndexed(htu_types, sizeof(htu_types), index, kHtuTypes);
|
2017-11-07 14:57:24 +00:00
|
|
|
snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, htu_types, htu_address);
|
2017-10-18 17:22:34 +01:00
|
|
|
AddLog(LOG_LEVEL_DEBUG);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void HtuEverySecond(void)
|
2018-07-10 21:12:16 +01:00
|
|
|
{
|
2018-07-11 13:21:11 +01:00
|
|
|
if (92 == (uptime %100)) {
|
2018-07-12 11:19:08 +01:00
|
|
|
// 1mS
|
2018-07-10 21:12:16 +01:00
|
|
|
HtuDetect();
|
2018-07-11 13:21:11 +01:00
|
|
|
}
|
|
|
|
else if (uptime &1) {
|
2018-07-12 11:19:08 +01:00
|
|
|
// HTU21: 68mS, SI70xx: 37mS
|
2018-07-10 21:12:16 +01:00
|
|
|
if (htu_type) {
|
|
|
|
if (!HtuRead()) {
|
|
|
|
AddLogMissed(htu_types, htu_valid);
|
2018-07-11 14:35:12 +01:00
|
|
|
// if (!htu_valid) { htu_type = 0; }
|
2018-07-10 21:12:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-04 15:36:51 +00:00
|
|
|
void HtuShow(boolean json)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2018-07-11 14:35:12 +01:00
|
|
|
if (htu_valid) {
|
2018-12-21 15:17:06 +00:00
|
|
|
char temperature[33];
|
2018-07-10 21:12:16 +01:00
|
|
|
dtostrfd(htu_temperature, Settings.flag2.temperature_resolution, temperature);
|
2018-12-21 15:17:06 +00:00
|
|
|
char humidity[33];
|
2018-07-10 21:12:16 +01:00
|
|
|
dtostrfd(htu_humidity, Settings.flag2.humidity_resolution, humidity);
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-11-04 15:36:51 +00:00
|
|
|
if (json) {
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), JSON_SNS_TEMPHUM, mqtt_data, htu_types, temperature, humidity);
|
2017-01-28 13:41:01 +00:00
|
|
|
#ifdef USE_DOMOTICZ
|
2018-07-10 21:12:16 +01:00
|
|
|
if (0 == tele_period) {
|
|
|
|
DomoticzTempHumSensor(temperature, humidity);
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
#endif // USE_DOMOTICZ
|
2018-04-18 16:28:45 +01:00
|
|
|
#ifdef USE_KNX
|
|
|
|
if (0 == tele_period) {
|
2018-07-10 21:12:16 +01:00
|
|
|
KnxSensor(KNX_TEMPERATURE, htu_temperature);
|
|
|
|
KnxSensor(KNX_HUMIDITY, htu_humidity);
|
2018-04-18 16:28:45 +01:00
|
|
|
}
|
|
|
|
#endif // USE_KNX
|
2017-01-28 13:41:01 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
2017-11-04 15:36:51 +00:00
|
|
|
} else {
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_TEMP, mqtt_data, htu_types, temperature, TempUnit());
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_HUM, mqtt_data, htu_types, humidity);
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-03 17:07:25 +00:00
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
boolean Xsns08(byte function)
|
|
|
|
{
|
|
|
|
boolean result = false;
|
|
|
|
|
2017-11-04 15:36:51 +00:00
|
|
|
if (i2c_flg) {
|
|
|
|
switch (function) {
|
2018-07-11 13:21:11 +01:00
|
|
|
case FUNC_INIT:
|
|
|
|
HtuDetect();
|
|
|
|
break;
|
2018-07-10 21:12:16 +01:00
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
HtuEverySecond();
|
2017-11-04 15:36:51 +00:00
|
|
|
break;
|
2017-12-25 16:41:12 +00:00
|
|
|
case FUNC_JSON_APPEND:
|
2017-11-04 15:36:51 +00:00
|
|
|
HtuShow(1);
|
|
|
|
break;
|
2017-11-03 17:07:25 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
2017-12-25 16:41:12 +00:00
|
|
|
case FUNC_WEB_APPEND:
|
2017-11-04 15:36:51 +00:00
|
|
|
HtuShow(0);
|
|
|
|
break;
|
2017-11-03 17:07:25 +00:00
|
|
|
#endif // USE_WEBSERVER
|
2017-11-04 15:36:51 +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
|
|
|
|
|