2017-12-01 14:59:53 +00:00
|
|
|
/*
|
2019-10-27 10:13:24 +00:00
|
|
|
xsns_14_sht3x.ino - SHT3X temperature and humidity sensor support for Tasmota
|
2017-12-01 14:59:53 +00:00
|
|
|
|
2019-12-31 13:23:34 +00:00
|
|
|
Copyright (C) 2020 Theo Arends
|
2017-12-01 14:59:53 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_I2C
|
|
|
|
#ifdef USE_SHT3X
|
|
|
|
/*********************************************************************************************\
|
2018-03-09 11:13:40 +00:00
|
|
|
* SHT3X and SHTC3 - Temperature and Humidity
|
2017-12-01 14:59:53 +00:00
|
|
|
*
|
2018-02-19 14:33:06 +00:00
|
|
|
* I2C Address: 0x44, 0x45 or 0x70 (SHTC3)
|
2017-12-01 14:59:53 +00:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-11-06 16:33:51 +00:00
|
|
|
#define XSNS_14 14
|
2019-11-03 16:54:39 +00:00
|
|
|
#define XI2C_15 15 // See I2CDEVICES.md
|
2018-11-06 16:33:51 +00:00
|
|
|
|
2017-12-28 16:08:32 +00:00
|
|
|
#define SHT3X_ADDR_GND 0x44 // address pin low (GND)
|
|
|
|
#define SHT3X_ADDR_VDD 0x45 // address pin high (VDD)
|
2018-02-19 14:33:06 +00:00
|
|
|
#define SHTC3_ADDR 0x70 // address for shtc3 sensor
|
|
|
|
|
2018-03-09 11:13:40 +00:00
|
|
|
#define SHT3X_MAX_SENSORS 3
|
2017-12-01 14:59:53 +00:00
|
|
|
|
2018-03-09 11:13:40 +00:00
|
|
|
const char kShtTypes[] PROGMEM = "SHT3X|SHT3X|SHTC3";
|
2018-02-19 14:33:06 +00:00
|
|
|
uint8_t sht3x_addresses[] = { SHT3X_ADDR_GND, SHT3X_ADDR_VDD, SHTC3_ADDR };
|
2017-12-01 14:59:53 +00:00
|
|
|
|
2018-03-09 11:13:40 +00:00
|
|
|
uint8_t sht3x_count = 0;
|
|
|
|
struct SHT3XSTRUCT {
|
|
|
|
uint8_t address; // I2C bus address
|
|
|
|
char types[6]; // Sensor type name and address - "SHT3X-0xXX"
|
|
|
|
} sht3x_sensors[SHT3X_MAX_SENSORS];
|
|
|
|
|
|
|
|
bool Sht3xRead(float &t, float &h, uint8_t sht3x_address)
|
2017-12-01 14:59:53 +00:00
|
|
|
{
|
|
|
|
unsigned int data[6];
|
|
|
|
|
|
|
|
t = NAN;
|
|
|
|
h = NAN;
|
|
|
|
|
2017-12-28 16:08:32 +00:00
|
|
|
Wire.beginTransmission(sht3x_address);
|
2018-02-19 14:33:06 +00:00
|
|
|
if (SHTC3_ADDR == sht3x_address) {
|
|
|
|
Wire.write(0x35); // Wake from
|
|
|
|
Wire.write(0x17); // sleep
|
|
|
|
Wire.endTransmission();
|
|
|
|
Wire.beginTransmission(sht3x_address);
|
2018-03-09 11:13:40 +00:00
|
|
|
Wire.write(0x78); // Disable clock stretching ( I don't think that wire library support clock stretching )
|
2018-02-19 14:33:06 +00:00
|
|
|
Wire.write(0x66); // High resolution
|
|
|
|
} else {
|
|
|
|
Wire.write(0x2C); // Enable clock stretching
|
|
|
|
Wire.write(0x06); // High repeatability
|
|
|
|
}
|
2017-12-28 16:08:32 +00:00
|
|
|
if (Wire.endTransmission() != 0) { // Stop I2C transmission
|
2017-12-01 14:59:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-12-28 16:08:32 +00:00
|
|
|
delay(30); // Timing verified with logic analyzer (10 is to short)
|
|
|
|
Wire.requestFrom(sht3x_address, (uint8_t)6); // Request 6 bytes of data
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < 6; i++) {
|
2017-12-28 16:08:32 +00:00
|
|
|
data[i] = Wire.read(); // cTemp msb, cTemp lsb, cTemp crc, humidity msb, humidity lsb, humidity crc
|
|
|
|
};
|
2017-12-01 14:59:53 +00:00
|
|
|
t = ConvertTemp((float)((((data[0] << 8) | data[1]) * 175) / 65535.0) - 45);
|
2020-03-18 10:23:00 +00:00
|
|
|
h = ConvertHumidity((float)((((data[3] << 8) | data[4]) * 100) / 65535.0));
|
2019-07-11 09:56:12 +01:00
|
|
|
return (!isnan(t) && !isnan(h) && (h != 0));
|
2017-12-01 14:59:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void Sht3xDetect(void)
|
2017-12-01 14:59:53 +00:00
|
|
|
{
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < SHT3X_MAX_SENSORS; i++) {
|
2019-11-07 15:56:05 +00:00
|
|
|
if (I2cActive(sht3x_addresses[i])) { continue; }
|
2019-11-11 16:32:44 +00:00
|
|
|
float t;
|
|
|
|
float h;
|
2018-03-09 11:13:40 +00:00
|
|
|
if (Sht3xRead(t, h, sht3x_addresses[i])) {
|
|
|
|
sht3x_sensors[sht3x_count].address = sht3x_addresses[i];
|
|
|
|
GetTextIndexed(sht3x_sensors[sht3x_count].types, sizeof(sht3x_sensors[sht3x_count].types), i, kShtTypes);
|
2019-11-09 17:34:22 +00:00
|
|
|
I2cSetActiveFound(sht3x_sensors[sht3x_count].address, sht3x_sensors[sht3x_count].types);
|
2018-03-09 11:13:40 +00:00
|
|
|
sht3x_count++;
|
2017-12-01 14:59:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-17 15:29:59 +00:00
|
|
|
|
2020-03-16 15:52:22 +00:00
|
|
|
void Sht3xShow(bool json)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < sht3x_count; i++) {
|
|
|
|
float t;
|
|
|
|
float h;
|
|
|
|
if (Sht3xRead(t, h, sht3x_sensors[i].address)) {
|
|
|
|
char types[11];
|
|
|
|
snprintf_P(types, sizeof(types), PSTR("%s%c0x%02X"), sht3x_sensors[i].types, IndexSeparator(), sht3x_sensors[i].address); // "SHT3X-0xXX"
|
2020-10-29 12:37:09 +00:00
|
|
|
TempHumDewShow(json, ((0 == TasmotaGlobal.tele_period) && (0 == i)), types, t, h);
|
2020-03-16 15:52:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-01 14:59:53 +00:00
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool Xsns14(uint8_t function)
|
2017-12-01 14:59:53 +00:00
|
|
|
{
|
2019-11-04 09:38:05 +00:00
|
|
|
if (!I2cEnabled(XI2C_15)) { return false; }
|
2019-11-03 16:54:39 +00:00
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2017-12-01 14:59:53 +00:00
|
|
|
|
2019-11-11 16:32:44 +00:00
|
|
|
if (FUNC_INIT == function) {
|
|
|
|
Sht3xDetect();
|
|
|
|
}
|
|
|
|
else if (sht3x_count) {
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
Sht3xShow(1);
|
|
|
|
break;
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
case FUNC_WEB_SENSOR:
|
|
|
|
Sht3xShow(0);
|
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
2017-12-01 14:59:53 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_SHT3X
|
2018-07-23 15:27:15 +01:00
|
|
|
#endif // USE_I2C
|