2017-03-16 15:23:11 +00:00
|
|
|
/*
|
2019-10-27 10:13:24 +00:00
|
|
|
xsns_07_sht1x.ino - SHT1x temperature and sensor support for Tasmota
|
2017-03-16 15:23:11 +00:00
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
Copyright (C) 2021 Theo Arends
|
2017-03-16 15:23:11 +00:00
|
|
|
|
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.
|
2017-03-16 15:23:11 +00:00
|
|
|
|
2017-05-13 12:02:10 +01:00
|
|
|
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-03-16 15:23:11 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_I2C
|
|
|
|
#ifdef USE_SHT
|
|
|
|
/*********************************************************************************************\
|
2020-11-29 20:08:08 +00:00
|
|
|
* SHT1x - Temperature and Humidity
|
2017-03-16 15:23:11 +00:00
|
|
|
*
|
|
|
|
* Reading temperature and humidity takes about 320 milliseconds!
|
|
|
|
* Source: Marinus vd Broek https://github.com/ESP8266nu/ESPEasy
|
2017-11-11 11:33:30 +00:00
|
|
|
*
|
2022-06-13 13:41:40 +01:00
|
|
|
* I2C Address: None and ruins I2C bus hence reinit I2C after each call
|
2017-03-16 15:23:11 +00:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-11-06 16:33:51 +00:00
|
|
|
#define XSNS_07 7
|
2019-11-03 16:54:39 +00:00
|
|
|
#define XI2C_08 8 // See I2CDEVICES.md
|
2018-11-06 16:33:51 +00:00
|
|
|
|
2017-03-16 15:23:11 +00:00
|
|
|
enum {
|
|
|
|
SHT1X_CMD_MEASURE_TEMP = B00000011,
|
|
|
|
SHT1X_CMD_MEASURE_RH = B00000101,
|
|
|
|
SHT1X_CMD_SOFT_RESET = B00011110
|
|
|
|
};
|
|
|
|
|
2022-06-13 13:41:40 +01:00
|
|
|
struct {
|
|
|
|
float temperature = 0;
|
|
|
|
float humidity = 0;
|
|
|
|
int8_t sda_pin;
|
|
|
|
int8_t scl_pin;
|
|
|
|
uint8_t type = 0;
|
|
|
|
uint8_t valid = 0;
|
|
|
|
char types[6] = "SHT1X";
|
|
|
|
} Sht1x;
|
|
|
|
|
|
|
|
bool ShtReset(void) {
|
|
|
|
pinMode(Sht1x.sda_pin, INPUT_PULLUP);
|
|
|
|
pinMode(Sht1x.scl_pin, OUTPUT);
|
2017-03-16 15:23:11 +00:00
|
|
|
delay(11);
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < 9; i++) {
|
2022-06-13 13:41:40 +01:00
|
|
|
digitalWrite(Sht1x.scl_pin, HIGH);
|
|
|
|
digitalWrite(Sht1x.scl_pin, LOW);
|
2017-03-16 15:23:11 +00:00
|
|
|
}
|
2019-01-28 13:08:33 +00:00
|
|
|
bool success = ShtSendCommand(SHT1X_CMD_SOFT_RESET);
|
2017-03-16 15:23:11 +00:00
|
|
|
delay(11);
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2022-06-13 13:41:40 +01:00
|
|
|
bool ShtSendCommand(const uint8_t cmd) {
|
|
|
|
pinMode(Sht1x.sda_pin, OUTPUT);
|
2017-03-16 15:23:11 +00:00
|
|
|
// Transmission Start sequence
|
2022-06-13 13:41:40 +01:00
|
|
|
digitalWrite(Sht1x.sda_pin, HIGH);
|
|
|
|
digitalWrite(Sht1x.scl_pin, HIGH);
|
|
|
|
digitalWrite(Sht1x.sda_pin, LOW);
|
|
|
|
digitalWrite(Sht1x.scl_pin, LOW);
|
|
|
|
digitalWrite(Sht1x.scl_pin, HIGH);
|
|
|
|
digitalWrite(Sht1x.sda_pin, HIGH);
|
|
|
|
digitalWrite(Sht1x.scl_pin, LOW);
|
2017-03-16 15:23:11 +00:00
|
|
|
// Send the command (address must be 000b)
|
2022-06-13 13:41:40 +01:00
|
|
|
TasShiftOut(Sht1x.sda_pin, Sht1x.scl_pin, MSBFIRST, cmd);
|
2017-03-16 15:23:11 +00:00
|
|
|
// Wait for ACK
|
2019-01-28 13:08:33 +00:00
|
|
|
bool ackerror = false;
|
2022-06-13 13:41:40 +01:00
|
|
|
digitalWrite(Sht1x.scl_pin, HIGH);
|
|
|
|
pinMode(Sht1x.sda_pin, INPUT_PULLUP);
|
|
|
|
if (digitalRead(Sht1x.sda_pin) != LOW) {
|
2017-04-25 17:24:42 +01:00
|
|
|
ackerror = true;
|
|
|
|
}
|
2022-06-13 13:41:40 +01:00
|
|
|
digitalWrite(Sht1x.scl_pin, LOW);
|
2017-03-16 15:23:11 +00:00
|
|
|
delayMicroseconds(1); // Give the sensor time to release the data line
|
2022-06-13 13:41:40 +01:00
|
|
|
if (digitalRead(Sht1x.sda_pin) != HIGH) {
|
2017-04-25 17:24:42 +01:00
|
|
|
ackerror = true;
|
|
|
|
}
|
2017-03-16 15:23:11 +00:00
|
|
|
if (ackerror) {
|
2022-06-13 13:41:40 +01:00
|
|
|
// Sht1x.type = 0;
|
2021-01-23 16:10:06 +00:00
|
|
|
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_SHT1 D_SENSOR_DID_NOT_ACK_COMMAND));
|
2017-03-16 15:23:11 +00:00
|
|
|
}
|
|
|
|
return (!ackerror);
|
|
|
|
}
|
|
|
|
|
2022-06-13 13:41:40 +01:00
|
|
|
bool ShtAwaitResult(void) {
|
2017-03-16 15:23:11 +00:00
|
|
|
// Maximum 320ms for 14 bit measurement
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < 16; i++) {
|
2022-06-13 13:41:40 +01:00
|
|
|
if (LOW == digitalRead(Sht1x.sda_pin)) {
|
2017-04-25 17:24:42 +01:00
|
|
|
return true;
|
|
|
|
}
|
2017-03-16 15:23:11 +00:00
|
|
|
delay(20);
|
|
|
|
}
|
2021-01-23 16:10:06 +00:00
|
|
|
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_SHT1 D_SENSOR_BUSY));
|
2022-06-13 13:41:40 +01:00
|
|
|
// Sht1x.type = 0;
|
2017-03-16 15:23:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-13 13:41:40 +01:00
|
|
|
int ShtReadData(void) {
|
2017-03-16 15:23:11 +00:00
|
|
|
int val = 0;
|
|
|
|
|
|
|
|
// Read most significant byte
|
2022-06-13 13:41:40 +01:00
|
|
|
val = TasShiftIn(Sht1x.sda_pin, Sht1x.scl_pin, 8);
|
2017-03-16 15:23:11 +00:00
|
|
|
val <<= 8;
|
|
|
|
// Send ACK
|
2022-06-13 13:41:40 +01:00
|
|
|
pinMode(Sht1x.sda_pin, OUTPUT);
|
|
|
|
digitalWrite(Sht1x.sda_pin, LOW);
|
|
|
|
digitalWrite(Sht1x.scl_pin, HIGH);
|
|
|
|
digitalWrite(Sht1x.scl_pin, LOW);
|
|
|
|
pinMode(Sht1x.sda_pin, INPUT_PULLUP);
|
2017-03-16 15:23:11 +00:00
|
|
|
// Read least significant byte
|
2022-06-13 13:41:40 +01:00
|
|
|
val |= TasShiftIn(Sht1x.sda_pin, Sht1x.scl_pin, 8);
|
2017-03-16 15:23:11 +00:00
|
|
|
// Keep DATA pin high to skip CRC
|
2022-06-13 13:41:40 +01:00
|
|
|
digitalWrite(Sht1x.scl_pin, HIGH);
|
|
|
|
digitalWrite(Sht1x.scl_pin, LOW);
|
2017-03-16 15:23:11 +00:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2022-06-13 13:41:40 +01:00
|
|
|
bool ShtRead(void) {
|
|
|
|
if (Sht1x.valid) { Sht1x.valid--; }
|
2018-07-10 14:50:07 +01:00
|
|
|
if (!ShtReset()) { return false; }
|
|
|
|
if (!ShtSendCommand(SHT1X_CMD_MEASURE_TEMP)) { return false; }
|
|
|
|
if (!ShtAwaitResult()) { return false; }
|
|
|
|
float tempRaw = ShtReadData();
|
|
|
|
if (!ShtSendCommand(SHT1X_CMD_MEASURE_RH)) { return false; }
|
|
|
|
if (!ShtAwaitResult()) { return false; }
|
|
|
|
float humRaw = ShtReadData();
|
2017-03-16 15:23:11 +00:00
|
|
|
|
|
|
|
// Temperature conversion coefficients from SHT1X datasheet for version 4
|
2022-04-19 14:44:53 +01:00
|
|
|
const float d1 = -39.7f; // 3.5V
|
|
|
|
const float d2 = 0.01f; // 14-bit
|
2022-06-13 13:41:40 +01:00
|
|
|
Sht1x.temperature = d1 + (tempRaw * d2);
|
2022-04-19 14:44:53 +01:00
|
|
|
const float c1 = -2.0468f;
|
|
|
|
const float c2 = 0.0367f;
|
|
|
|
const float c3 = -1.5955E-6f;
|
|
|
|
const float t1 = 0.01f;
|
|
|
|
const float t2 = 0.00008f;
|
2018-07-10 14:50:07 +01:00
|
|
|
float rhLinear = c1 + c2 * humRaw + c3 * humRaw * humRaw;
|
2022-06-13 13:41:40 +01:00
|
|
|
Sht1x.humidity = (Sht1x.temperature - 25) * (t1 + t2 * humRaw) + rhLinear;
|
|
|
|
Sht1x.temperature = ConvertTemp(Sht1x.temperature);
|
|
|
|
Sht1x.humidity = ConvertHumidity(Sht1x.humidity);
|
2018-07-24 17:41:50 +01:00
|
|
|
|
2022-06-13 13:41:40 +01:00
|
|
|
Sht1x.valid = SENSOR_MAX_MISS;
|
2018-07-10 14:50:07 +01:00
|
|
|
return true;
|
2017-03-16 15:23:11 +00:00
|
|
|
}
|
|
|
|
|
2017-11-04 15:36:51 +00:00
|
|
|
/********************************************************************************************/
|
|
|
|
|
2022-06-13 13:41:40 +01:00
|
|
|
void ShtDetect(void) {
|
|
|
|
Sht1x.sda_pin = Pin(GPIO_I2C_SDA);
|
|
|
|
Sht1x.scl_pin = Pin(GPIO_I2C_SCL);
|
2018-07-10 14:50:07 +01:00
|
|
|
if (ShtRead()) {
|
2022-06-13 13:41:40 +01:00
|
|
|
Sht1x.type = 1;
|
2021-01-23 16:10:06 +00:00
|
|
|
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_I2C D_SHT1X_FOUND));
|
2017-03-16 15:23:11 +00:00
|
|
|
}
|
2022-06-13 13:41:40 +01:00
|
|
|
I2cBegin(Sht1x.sda_pin, Sht1x.scl_pin); // Reinit I2C bus
|
2017-03-16 15:23:11 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 13:41:40 +01:00
|
|
|
void ShtEverySecond(void) {
|
|
|
|
if (!(TasmotaGlobal.uptime %4)) { // Every 4 seconds
|
2018-07-12 11:19:08 +01:00
|
|
|
// 344mS
|
2018-07-10 21:12:16 +01:00
|
|
|
if (!ShtRead()) {
|
2022-06-13 13:41:40 +01:00
|
|
|
AddLogMissed(Sht1x.types, Sht1x.valid);
|
2018-07-10 21:12:16 +01:00
|
|
|
}
|
2022-06-13 13:41:40 +01:00
|
|
|
I2cBegin(Sht1x.sda_pin, Sht1x.scl_pin); // Reinit I2C bus
|
2018-07-10 21:12:16 +01:00
|
|
|
}
|
2018-07-10 14:50:07 +01:00
|
|
|
}
|
2020-03-17 15:29:59 +00:00
|
|
|
|
2022-06-13 13:41:40 +01:00
|
|
|
void ShtShow(bool json) {
|
|
|
|
if (Sht1x.valid) {
|
|
|
|
TempHumDewShow(json, (0 == TasmotaGlobal.tele_period), Sht1x.types, Sht1x.temperature, Sht1x.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 Xsns07(uint32_t function) {
|
2019-11-04 09:38:05 +00:00
|
|
|
if (!I2cEnabled(XI2C_08)) { 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) {
|
|
|
|
ShtDetect();
|
|
|
|
}
|
2022-06-13 13:41:40 +01:00
|
|
|
else if (Sht1x.type) {
|
2019-11-11 16:32:44 +00:00
|
|
|
switch (function) {
|
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
ShtEverySecond();
|
|
|
|
break;
|
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
ShtShow(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:
|
|
|
|
ShtShow(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-03-16 15:23:11 +00:00
|
|
|
#endif // USE_SHT
|
|
|
|
#endif // USE_I2C
|