From 1e336f5e208d02f094e9aca1756fbbe20a824cc5 Mon Sep 17 00:00:00 2001 From: arendst Date: Thu, 28 Dec 2017 17:08:32 +0100 Subject: [PATCH] Add support for sensor SHT3x (#1314) * Add support for sensor SHT3x (#1314) --- sonoff/_releasenotes.ino | 1 + sonoff/user_config.h | 4 +- sonoff/xsns_14_sht3x.ino | 44 ++++------ sonoff/xsns_14_sht3x_v2.ino | 142 -------------------------------- sonoff/xsns_14_sht3x_v3.ino | 156 ------------------------------------ 5 files changed, 17 insertions(+), 330 deletions(-) delete mode 100644 sonoff/xsns_14_sht3x_v2.ino delete mode 100644 sonoff/xsns_14_sht3x_v3.ino diff --git a/sonoff/_releasenotes.ino b/sonoff/_releasenotes.ino index 5a7640f9e..9ec2c7e84 100644 --- a/sonoff/_releasenotes.ino +++ b/sonoff/_releasenotes.ino @@ -1,6 +1,7 @@ /* 5.10.0e * Add Italian language file (#1449) * Fix Wemo Emulation once again closest to issue (#1357) + * Add support for sensor SHT3x * * 5.10.0d * Renamed commands Color2,3,4 to Color3,4,5 diff --git a/sonoff/user_config.h b/sonoff/user_config.h index d7325d5db..78016fd9e 100644 --- a/sonoff/user_config.h +++ b/sonoff/user_config.h @@ -173,9 +173,7 @@ // -- I2C sensors --------------------------------- #define USE_I2C // I2C using library wire (+10k code, 0k2 mem, 124 iram) #define USE_SHT // Add I2C emulating code for SHT1X sensor (+1k4 code) -// #define USE_SHT3X // Add I2C code for SHT3x sensor based on Adafruit (+0k7 code) -// #define USE_SHT3X_V2 // Add I2C code for SHT3x sensor based on EspEasy (+0k7 code) -// #define USE_SHT3X_V3 // Add I2C code for SHT3x sensor based on Wemos (+0k7 code) + #define USE_SHT3X // Add I2C code for SHT3x sensor (+0k6 code) #define USE_HTU // Add I2C code for HTU21/SI7013/SI7020/SI7021 sensor (+1k5 code) #define USE_BMP // Add I2C code for BMP085/BMP180/BMP280/BME280 sensor (+4k code) // #define USE_BME680 // Add additional support for BME680 sensor using Adafruit Sensor and BME680 libraries (+6k code) diff --git a/sonoff/xsns_14_sht3x.ino b/sonoff/xsns_14_sht3x.ino index 39b24d8b4..60d35c787 100644 --- a/sonoff/xsns_14_sht3x.ino +++ b/sonoff/xsns_14_sht3x.ino @@ -22,26 +22,16 @@ /*********************************************************************************************\ * SHT3X - Temperature and Humidy * - * Required library: none but based on Adafruit Industries SHT31 library - * * I2C Address: 0x44 or 0x45 \*********************************************************************************************/ -#define SHT3X_ADDR_GND 0x44 // address pin low (GND) -#define SHT3X_ADDR_VDD 0x45 // address pin high (VDD) +#define SHT3X_ADDR_GND 0x44 // address pin low (GND) +#define SHT3X_ADDR_VDD 0x45 // address pin high (VDD) uint8_t sht3x_type = 0; uint8_t sht3x_address; uint8_t sht3x_addresses[] = { SHT3X_ADDR_GND, SHT3X_ADDR_VDD }; -bool Sht3xConvert() -{ - if (sht3x_type) { - return I2cWrite8(sht3x_address, 0x2C, 0x06); - } - return false; -} - bool Sht3xRead(float &t, float &h) { unsigned int data[6]; @@ -49,22 +39,20 @@ bool Sht3xRead(float &t, float &h) t = NAN; h = NAN; - Wire.requestFrom(sht3x_address, (uint8_t)6); - if (Wire.available() != 6) { + Wire.beginTransmission(sht3x_address); + Wire.write(0x2C); // Enable clock stretching + Wire.write(0x06); // High repeatability + if (Wire.endTransmission() != 0) { // Stop I2C transmission return false; } - // Read 6 bytes of data - // cTemp msb, cTemp lsb, cTemp crc, humidity msb, humidity lsb, humidity crc - for (uint8_t i = 0; i < 6; i++) { - data[i] = Wire.read(); - } -// delay(50); -// if (Wire.available() != 0) { -// return false; -// } + delay(30); // Timing verified with logic analyzer (10 is to short) + Wire.requestFrom(sht3x_address, (uint8_t)6); // Request 6 bytes of data + for (int i = 0; i < 6; i++) { + data[i] = Wire.read(); // cTemp msb, cTemp lsb, cTemp crc, humidity msb, humidity lsb, humidity crc + }; t = ConvertTemp((float)((((data[0] << 8) | data[1]) * 175) / 65535.0) - 45); h = (float)((((data[3] << 8) | data[4]) * 100) / 65535.0); - return true; + return (!isnan(t) && !isnan(h)); } /********************************************************************************************/ @@ -75,10 +63,12 @@ void Sht3xDetect() return; } + float t; + float h; sht3x_type = 1; for (byte i = 0; i < sizeof(sht3x_addresses); i++) { sht3x_address = sht3x_addresses[i]; - if (Sht3xConvert()) { + if (Sht3xRead(t, h)) { snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, "SHT3X", sht3x_address); AddLog(LOG_LEVEL_DEBUG); return; @@ -128,16 +118,12 @@ boolean Xsns14(byte function) case FUNC_INIT: Sht3xDetect(); break; - case FUNC_PREP_BEFORE_TELEPERIOD: - Sht3xConvert(); - break; case FUNC_JSON_APPEND: Sht3xShow(1); break; #ifdef USE_WEBSERVER case FUNC_WEB_APPEND: Sht3xShow(0); - Sht3xConvert(); break; #endif // USE_WEBSERVER } diff --git a/sonoff/xsns_14_sht3x_v2.ino b/sonoff/xsns_14_sht3x_v2.ino deleted file mode 100644 index 85de8512a..000000000 --- a/sonoff/xsns_14_sht3x_v2.ino +++ /dev/null @@ -1,142 +0,0 @@ -/* - xsns_14_sht3x.ino - SHT3X temperature and humidity sensor support for Sonoff-Tasmota - - Copyright (C) 2018 Theo Arends - - 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 . -*/ - -#ifdef USE_I2C -#ifdef USE_SHT3X_V2 -/*********************************************************************************************\ - * SHT3X - Temperature and Humidy - * - * Required library: none but based on EspEasy plugin 68 - * - * I2C Address: 0x44 or 0x45 -\*********************************************************************************************/ - -#define SHT3X_ADDR_GND 0x44 // address pin low (GND) -#define SHT3X_ADDR_VDD 0x45 // address pin high (VDD) - -uint8_t sht3x_type = 0; -uint8_t sht3x_address; -uint8_t sht3x_addresses[] = { SHT3X_ADDR_GND, SHT3X_ADDR_VDD }; - -bool Sht3xInit() -{ - if (sht3x_type) { - return I2cWrite8(sht3x_address, 0x20, 0x32); // Periodic 0.5mps and repeatability high - } - return false; -} - -bool Sht3xRead(float &t, float &h) -{ - unsigned int data[6]; - - t = NAN; - h = NAN; - - I2cWrite8(sht3x_address, 0xE0, 0x00); // Fetch data - Wire.requestFrom(sht3x_address, (uint8_t)6); - if (Wire.available() != 6) { - return false; - } - // Read 6 bytes of data - // cTemp msb, cTemp lsb, cTemp crc, humidity msb, humidity lsb, humidity crc - for (uint8_t i = 0; i < 6; i++) { - data[i] = Wire.read(); - } - t = ConvertTemp((float)((((data[0] << 8) | data[1]) * 175) / 65535.0) - 45); - h = (float)((((data[3] << 8) | data[4]) * 100) / 65535.0); - return true; -} - -/********************************************************************************************/ - -void Sht3xDetect() -{ - if (sht3x_type) { - return; - } - - sht3x_type = 1; - for (byte i = 0; i < sizeof(sht3x_addresses); i++) { - sht3x_address = sht3x_addresses[i]; - if (Sht3xInit()) { - snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, "SHT3X", sht3x_address); - AddLog(LOG_LEVEL_DEBUG); - return; - } - } - sht3x_type = 0; -} - -void Sht3xShow(boolean json) -{ - if (sht3x_type) { - float t; - float h; - if (Sht3xRead(t, h)) { - char temperature[10]; - char humidity[10]; - dtostrfd(t, Settings.flag2.temperature_resolution, temperature); - dtostrfd(h, Settings.flag2.humidity_resolution, humidity); - - if (json) { - snprintf_P(mqtt_data, sizeof(mqtt_data), JSON_SNS_TEMPHUM, mqtt_data, "SHT3X", temperature, humidity); -#ifdef USE_DOMOTICZ - DomoticzTempHumSensor(temperature, humidity); -#endif // USE_DOMOTICZ -#ifdef USE_WEBSERVER - } else { - snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_TEMP, mqtt_data, "SHT3X", temperature, TempUnit()); - snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_HUM, mqtt_data, "SHT3X", humidity); -#endif // USE_WEBSERVER - } - } - } -} - -/*********************************************************************************************\ - * Interface -\*********************************************************************************************/ - -#define XSNS_14 - -boolean Xsns14(byte function) -{ - boolean result = false; - - if (i2c_flg) { - switch (function) { - case FUNC_INIT: - Sht3xDetect(); - break; - case FUNC_JSON_APPEND: - Sht3xShow(1); - break; -#ifdef USE_WEBSERVER - case FUNC_WEB_APPEND: - Sht3xShow(0); - break; -#endif // USE_WEBSERVER - } - } - return result; -} - -#endif // USE_SHT3X_V2 -#endif // USE_I2C \ No newline at end of file diff --git a/sonoff/xsns_14_sht3x_v3.ino b/sonoff/xsns_14_sht3x_v3.ino deleted file mode 100644 index aff368c1a..000000000 --- a/sonoff/xsns_14_sht3x_v3.ino +++ /dev/null @@ -1,156 +0,0 @@ -/* - xsns_14_sht3x.ino - SHT3X temperature and humidity sensor support for Sonoff-Tasmota - - Copyright (C) 2018 Theo Arends - - 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 . -*/ - -#ifdef USE_I2C -#ifdef USE_SHT3X_V3 -/*********************************************************************************************\ - * SHT3X - Temperature and Humidy - * - * Required library: none but based on Wemos library - * - * I2C Address: 0x44 or 0x45 -\*********************************************************************************************/ - -#define SHT3X_ADDR_GND 0x44 // address pin low (GND) -#define SHT3X_ADDR_VDD 0x45 // address pin high (VDD) - -uint8_t sht3x_type = 0; -uint8_t sht3x_address; -uint8_t sht3x_addresses[] = { SHT3X_ADDR_GND, SHT3X_ADDR_VDD }; - -bool Sht3xConvert() -{ - if (sht3x_type) { - // Start I2C Transmission - Wire.beginTransmission(sht3x_address); - // Send measurement command - Wire.write(0x2C); - Wire.write(0x06); - // Stop I2C transmission - if (Wire.endTransmission() != 0) { - return false; - } - } - return true; -} - -bool Sht3xRead(float &t, float &h) -{ - unsigned int data[6]; - - t = NAN; - h = NAN; - - // Request 6 bytes of data - Wire.requestFrom(sht3x_address, (uint8_t)6); - // Read 6 bytes of data - // cTemp msb, cTemp lsb, cTemp crc, humidity msb, humidity lsb, humidity crc - for (int i = 0; i < 6; i++) { - data[i] = Wire.read(); - }; - delay(50); - if (Wire.available() != 0) { - return false; - } - // Convert the data - t = ConvertTemp((float)((((data[0] << 8) | data[1]) * 175) / 65535.0) - 45); - h = (float)((((data[3] << 8) | data[4]) * 100) / 65535.0); - return true; -} - -/********************************************************************************************/ - -void Sht3xDetect() -{ - if (sht3x_type) { - return; - } - - sht3x_type = 1; - for (byte i = 0; i < sizeof(sht3x_addresses); i++) { - sht3x_address = sht3x_addresses[i]; - if (Sht3xConvert()) { - snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, "SHT3X", sht3x_address); - AddLog(LOG_LEVEL_DEBUG); - return; - } - } - sht3x_type = 0; -} - -void Sht3xShow(boolean json) -{ - if (sht3x_type) { - float t; - float h; - if (Sht3xRead(t, h)) { - char temperature[10]; - char humidity[10]; - dtostrfd(t, Settings.flag2.temperature_resolution, temperature); - dtostrfd(h, Settings.flag2.humidity_resolution, humidity); - - if (json) { - snprintf_P(mqtt_data, sizeof(mqtt_data), JSON_SNS_TEMPHUM, mqtt_data, "SHT3X", temperature, humidity); -#ifdef USE_DOMOTICZ - DomoticzTempHumSensor(temperature, humidity); -#endif // USE_DOMOTICZ -#ifdef USE_WEBSERVER - } else { - snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_TEMP, mqtt_data, "SHT3X", temperature, TempUnit()); - snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_HUM, mqtt_data, "SHT3X", humidity); -#endif // USE_WEBSERVER - } - } - } -} - -/*********************************************************************************************\ - * Interface -\*********************************************************************************************/ - -#define XSNS_14 - -boolean Xsns14(byte function) -{ - boolean result = false; - - if (i2c_flg) { - switch (function) { - case FUNC_INIT: - Sht3xDetect(); - break; - case FUNC_PREP_BEFORE_TELEPERIOD: - Sht3xConvert(); - break; - case FUNC_JSON_APPEND: - Sht3xShow(1); - break; -#ifdef USE_WEBSERVER - case FUNC_WEB_APPEND: - Sht3xShow(0); - Sht3xConvert(); - break; -#endif // USE_WEBSERVER - } - } - return result; -} - -#endif // USE_SHT3X_V3 -#endif // USE_I2C \ No newline at end of file