mirror of
https://github.com/arendst/Tasmota.git
synced 2025-07-27 12:46:34 +00:00
Fix HTU21 sensor loss after ESP32 restart
This commit is contained in:
parent
684b7dc3a4
commit
48dd608da9
@ -59,14 +59,18 @@
|
|||||||
|
|
||||||
const char kHtuTypes[] PROGMEM = "HTU21|SI7013|SI7020|SI7021|T/RH?";
|
const char kHtuTypes[] PROGMEM = "HTU21|SI7013|SI7020|SI7021|T/RH?";
|
||||||
|
|
||||||
uint8_t htu_address;
|
struct {
|
||||||
uint8_t htu_type = 0;
|
float temperature = 0;
|
||||||
uint8_t htu_delay_temp;
|
float humidity = 0;
|
||||||
uint8_t htu_delay_humidity = 50;
|
uint8_t address;
|
||||||
uint8_t htu_valid = 0;
|
uint8_t type = 0;
|
||||||
float htu_temperature = 0;
|
uint8_t delay_temp;
|
||||||
float htu_humidity = 0;
|
uint8_t delay_humidity = 50;
|
||||||
char htu_types[7];
|
uint8_t valid = 0;
|
||||||
|
char types[7];
|
||||||
|
} Htu;
|
||||||
|
|
||||||
|
/*********************************************************************************************/
|
||||||
|
|
||||||
uint8_t HtuCheckCrc8(uint16_t data)
|
uint8_t HtuCheckCrc8(uint16_t data)
|
||||||
{
|
{
|
||||||
@ -82,6 +86,8 @@ uint8_t HtuCheckCrc8(uint16_t data)
|
|||||||
|
|
||||||
uint8_t HtuReadDeviceId(void)
|
uint8_t HtuReadDeviceId(void)
|
||||||
{
|
{
|
||||||
|
HtuReset(); // Fixes ESP32 sensor loss at restart
|
||||||
|
|
||||||
uint16_t deviceID = 0;
|
uint16_t deviceID = 0;
|
||||||
uint8_t checksum = 0;
|
uint8_t checksum = 0;
|
||||||
|
|
||||||
@ -146,12 +152,12 @@ bool HtuRead(void)
|
|||||||
uint8_t checksum = 0;
|
uint8_t checksum = 0;
|
||||||
uint16_t sensorval = 0;
|
uint16_t sensorval = 0;
|
||||||
|
|
||||||
if (htu_valid) { htu_valid--; }
|
if (Htu.valid) { Htu.valid--; }
|
||||||
|
|
||||||
Wire.beginTransmission(HTU21_ADDR);
|
Wire.beginTransmission(HTU21_ADDR);
|
||||||
Wire.write(HTU21_READTEMP);
|
Wire.write(HTU21_READTEMP);
|
||||||
if (Wire.endTransmission() != 0) { return false; } // In case of error
|
if (Wire.endTransmission() != 0) { return false; } // In case of error
|
||||||
delay(htu_delay_temp); // Sensor time at max resolution
|
delay(Htu.delay_temp); // Sensor time at max resolution
|
||||||
|
|
||||||
Wire.requestFrom(HTU21_ADDR, 3);
|
Wire.requestFrom(HTU21_ADDR, 3);
|
||||||
if (3 == Wire.available()) {
|
if (3 == Wire.available()) {
|
||||||
@ -161,12 +167,12 @@ bool HtuRead(void)
|
|||||||
}
|
}
|
||||||
if (HtuCheckCrc8(sensorval) != checksum) { return false; } // Checksum mismatch
|
if (HtuCheckCrc8(sensorval) != checksum) { return false; } // Checksum mismatch
|
||||||
|
|
||||||
htu_temperature = ConvertTemp(0.002681 * (float)sensorval - 46.85);
|
Htu.temperature = ConvertTemp(0.002681 * (float)sensorval - 46.85);
|
||||||
|
|
||||||
Wire.beginTransmission(HTU21_ADDR);
|
Wire.beginTransmission(HTU21_ADDR);
|
||||||
Wire.write(HTU21_READHUM);
|
Wire.write(HTU21_READHUM);
|
||||||
if (Wire.endTransmission() != 0) { return false; } // In case of error
|
if (Wire.endTransmission() != 0) { return false; } // In case of error
|
||||||
delay(htu_delay_humidity); // Sensor time at max resolution
|
delay(Htu.delay_humidity); // Sensor time at max resolution
|
||||||
|
|
||||||
Wire.requestFrom(HTU21_ADDR, 3);
|
Wire.requestFrom(HTU21_ADDR, 3);
|
||||||
if (3 <= Wire.available()) {
|
if (3 <= Wire.available()) {
|
||||||
@ -177,19 +183,19 @@ bool HtuRead(void)
|
|||||||
if (HtuCheckCrc8(sensorval) != checksum) { return false; } // Checksum mismatch
|
if (HtuCheckCrc8(sensorval) != checksum) { return false; } // Checksum mismatch
|
||||||
|
|
||||||
sensorval ^= 0x02; // clear status bits
|
sensorval ^= 0x02; // clear status bits
|
||||||
htu_humidity = 0.001907 * (float)sensorval - 6;
|
Htu.humidity = 0.001907 * (float)sensorval - 6;
|
||||||
if (htu_humidity > 100) { htu_humidity = 100.0; }
|
if (Htu.humidity > 100) { Htu.humidity = 100.0; }
|
||||||
if (htu_humidity < 0) { htu_humidity = 0.01; }
|
if (Htu.humidity < 0) { Htu.humidity = 0.01; }
|
||||||
|
|
||||||
if ((0.00 == htu_humidity) && (0.00 == htu_temperature)) {
|
if ((0.00 == Htu.humidity) && (0.00 == Htu.temperature)) {
|
||||||
htu_humidity = 0.0;
|
Htu.humidity = 0.0;
|
||||||
}
|
}
|
||||||
if ((htu_temperature > 0.00) && (htu_temperature < 80.00)) {
|
if ((Htu.temperature > 0.00) && (Htu.temperature < 80.00)) {
|
||||||
htu_humidity = (-0.15) * (25 - htu_temperature) + htu_humidity;
|
Htu.humidity = (-0.15) * (25 - Htu.temperature) + Htu.humidity;
|
||||||
}
|
}
|
||||||
htu_humidity = ConvertHumidity(htu_humidity);
|
Htu.humidity = ConvertHumidity(Htu.humidity);
|
||||||
|
|
||||||
htu_valid = SENSOR_MAX_MISS;
|
Htu.valid = SENSOR_MAX_MISS;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,17 +203,17 @@ bool HtuRead(void)
|
|||||||
|
|
||||||
void HtuDetect(void)
|
void HtuDetect(void)
|
||||||
{
|
{
|
||||||
htu_address = HTU21_ADDR;
|
Htu.address = HTU21_ADDR;
|
||||||
if (I2cActive(htu_address)) { return; }
|
if (I2cActive(Htu.address)) { return; }
|
||||||
|
|
||||||
htu_type = HtuReadDeviceId();
|
Htu.type = HtuReadDeviceId();
|
||||||
if (htu_type) {
|
if (Htu.type) {
|
||||||
uint8_t index = 0;
|
uint8_t index = 0;
|
||||||
HtuInit();
|
HtuInit();
|
||||||
switch (htu_type) {
|
switch (Htu.type) {
|
||||||
case HTU21_CHIPID:
|
case HTU21_CHIPID:
|
||||||
htu_delay_temp = 50;
|
Htu.delay_temp = 50;
|
||||||
htu_delay_humidity = 16;
|
Htu.delay_humidity = 16;
|
||||||
break;
|
break;
|
||||||
case SI7021_CHIPID:
|
case SI7021_CHIPID:
|
||||||
index++; // 3
|
index++; // 3
|
||||||
@ -215,16 +221,16 @@ void HtuDetect(void)
|
|||||||
index++; // 2
|
index++; // 2
|
||||||
case SI7013_CHIPID:
|
case SI7013_CHIPID:
|
||||||
index++; // 1
|
index++; // 1
|
||||||
htu_delay_temp = 12;
|
Htu.delay_temp = 12;
|
||||||
htu_delay_humidity = 23;
|
Htu.delay_humidity = 23;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
index = 4;
|
index = 4;
|
||||||
htu_delay_temp = 50;
|
Htu.delay_temp = 50;
|
||||||
htu_delay_humidity = 23;
|
Htu.delay_humidity = 23;
|
||||||
}
|
}
|
||||||
GetTextIndexed(htu_types, sizeof(htu_types), index, kHtuTypes);
|
GetTextIndexed(Htu.types, sizeof(Htu.types), index, kHtuTypes);
|
||||||
I2cSetActiveFound(htu_address, htu_types);
|
I2cSetActiveFound(Htu.address, Htu.types);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -233,15 +239,15 @@ void HtuEverySecond(void)
|
|||||||
if (uptime &1) { // Every 2 seconds
|
if (uptime &1) { // Every 2 seconds
|
||||||
// HTU21: 68mS, SI70xx: 37mS
|
// HTU21: 68mS, SI70xx: 37mS
|
||||||
if (!HtuRead()) {
|
if (!HtuRead()) {
|
||||||
AddLogMissed(htu_types, htu_valid);
|
AddLogMissed(Htu.types, Htu.valid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void HtuShow(bool json)
|
void HtuShow(bool json)
|
||||||
{
|
{
|
||||||
if (htu_valid) {
|
if (Htu.valid) {
|
||||||
TempHumDewShow(json, (0 == tele_period), htu_types, htu_temperature, htu_humidity);
|
TempHumDewShow(json, (0 == tele_period), Htu.types, Htu.temperature, Htu.humidity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -258,7 +264,7 @@ bool Xsns08(uint8_t function)
|
|||||||
if (FUNC_INIT == function) {
|
if (FUNC_INIT == function) {
|
||||||
HtuDetect();
|
HtuDetect();
|
||||||
}
|
}
|
||||||
else if (htu_type) {
|
else if (Htu.type) {
|
||||||
switch (function) {
|
switch (function) {
|
||||||
case FUNC_EVERY_SECOND:
|
case FUNC_EVERY_SECOND:
|
||||||
HtuEverySecond();
|
HtuEverySecond();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user