2017-03-16 15:23:11 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2017 Theo Arends. All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
- Redistributions of source code must retain the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer.
|
|
|
|
- Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer in the documentation
|
|
|
|
and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_I2C
|
|
|
|
#ifdef USE_SHT
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* SHT1x - Temperature and Humidy
|
|
|
|
*
|
|
|
|
* Reading temperature and humidity takes about 320 milliseconds!
|
|
|
|
* Source: Marinus vd Broek https://github.com/ESP8266nu/ESPEasy
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
enum {
|
|
|
|
SHT1X_CMD_MEASURE_TEMP = B00000011,
|
|
|
|
SHT1X_CMD_MEASURE_RH = B00000101,
|
|
|
|
SHT1X_CMD_SOFT_RESET = B00011110
|
|
|
|
};
|
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
uint8_t sht_sda_pin;
|
|
|
|
uint8_t sht_scl_pin;
|
|
|
|
uint8_t shttype = 0;
|
2017-03-16 15:23:11 +00:00
|
|
|
|
|
|
|
boolean sht_reset()
|
|
|
|
{
|
|
|
|
pinMode(sht_sda_pin, INPUT_PULLUP);
|
|
|
|
pinMode(sht_scl_pin, OUTPUT);
|
|
|
|
delay(11);
|
|
|
|
for (byte i = 0; i < 9; i++) {
|
|
|
|
digitalWrite(sht_scl_pin, HIGH);
|
|
|
|
digitalWrite(sht_scl_pin, LOW);
|
|
|
|
}
|
|
|
|
boolean success = sht_sendCommand(SHT1X_CMD_SOFT_RESET);
|
|
|
|
delay(11);
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean sht_sendCommand(const byte cmd)
|
|
|
|
{
|
|
|
|
pinMode(sht_sda_pin, OUTPUT);
|
|
|
|
// Transmission Start sequence
|
|
|
|
digitalWrite(sht_sda_pin, HIGH);
|
|
|
|
digitalWrite(sht_scl_pin, HIGH);
|
|
|
|
digitalWrite(sht_sda_pin, LOW);
|
|
|
|
digitalWrite(sht_scl_pin, LOW);
|
|
|
|
digitalWrite(sht_scl_pin, HIGH);
|
|
|
|
digitalWrite(sht_sda_pin, HIGH);
|
|
|
|
digitalWrite(sht_scl_pin, LOW);
|
|
|
|
// Send the command (address must be 000b)
|
|
|
|
shiftOut(sht_sda_pin, sht_scl_pin, MSBFIRST, cmd);
|
|
|
|
// Wait for ACK
|
|
|
|
boolean ackerror = false;
|
|
|
|
digitalWrite(sht_scl_pin, HIGH);
|
|
|
|
pinMode(sht_sda_pin, INPUT_PULLUP);
|
2017-04-25 17:24:42 +01:00
|
|
|
if (digitalRead(sht_sda_pin) != LOW) {
|
|
|
|
ackerror = true;
|
|
|
|
}
|
2017-03-16 15:23:11 +00:00
|
|
|
digitalWrite(sht_scl_pin, LOW);
|
|
|
|
delayMicroseconds(1); // Give the sensor time to release the data line
|
2017-04-25 17:24:42 +01:00
|
|
|
if (digitalRead(sht_sda_pin) != HIGH) {
|
|
|
|
ackerror = true;
|
|
|
|
}
|
2017-03-16 15:23:11 +00:00
|
|
|
if (ackerror) {
|
|
|
|
shttype = 0;
|
|
|
|
addLog_P(LOG_LEVEL_DEBUG, PSTR("SHT1X: Sensor did not ACK command"));
|
|
|
|
}
|
|
|
|
return (!ackerror);
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean sht_awaitResult()
|
|
|
|
{
|
|
|
|
// Maximum 320ms for 14 bit measurement
|
|
|
|
for (byte i = 0; i < 16; i++) {
|
2017-04-25 17:24:42 +01:00
|
|
|
if (LOW == digitalRead(sht_sda_pin)) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-03-16 15:23:11 +00:00
|
|
|
delay(20);
|
|
|
|
}
|
|
|
|
addLog_P(LOG_LEVEL_DEBUG, PSTR("SHT1X: Data not ready"));
|
|
|
|
shttype = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sht_readData()
|
|
|
|
{
|
|
|
|
int val = 0;
|
|
|
|
|
|
|
|
// Read most significant byte
|
|
|
|
val = shiftIn(sht_sda_pin, sht_scl_pin, 8);
|
|
|
|
val <<= 8;
|
|
|
|
// Send ACK
|
|
|
|
pinMode(sht_sda_pin, OUTPUT);
|
|
|
|
digitalWrite(sht_sda_pin, LOW);
|
|
|
|
digitalWrite(sht_scl_pin, HIGH);
|
|
|
|
digitalWrite(sht_scl_pin, LOW);
|
|
|
|
pinMode(sht_sda_pin, INPUT_PULLUP);
|
|
|
|
// Read least significant byte
|
|
|
|
val |= shiftIn(sht_sda_pin, sht_scl_pin, 8);
|
|
|
|
// Keep DATA pin high to skip CRC
|
|
|
|
digitalWrite(sht_scl_pin, HIGH);
|
|
|
|
digitalWrite(sht_scl_pin, LOW);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean sht_readTempHum(float &t, float &h)
|
|
|
|
{
|
2017-04-25 17:24:42 +01:00
|
|
|
float tempRaw;
|
|
|
|
float humRaw;
|
|
|
|
float rhLinear;
|
2017-03-16 15:23:11 +00:00
|
|
|
|
|
|
|
t = NAN;
|
|
|
|
h = NAN;
|
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
if (!sht_reset()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!sht_sendCommand(SHT1X_CMD_MEASURE_TEMP)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!sht_awaitResult()) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-16 15:23:11 +00:00
|
|
|
tempRaw = sht_readData();
|
|
|
|
// Temperature conversion coefficients from SHT1X datasheet for version 4
|
|
|
|
const float d1 = -39.7; // 3.5V
|
|
|
|
const float d2 = 0.01; // 14-bit
|
|
|
|
t = d1 + (tempRaw * d2);
|
2017-04-25 17:24:42 +01:00
|
|
|
if (!sht_sendCommand(SHT1X_CMD_MEASURE_RH)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!sht_awaitResult()) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-16 15:23:11 +00:00
|
|
|
humRaw = sht_readData();
|
|
|
|
// Temperature conversion coefficients from SHT1X datasheet for version 4
|
|
|
|
const float c1 = -2.0468;
|
|
|
|
const float c2 = 0.0367;
|
|
|
|
const float c3 = -1.5955E-6;
|
|
|
|
const float t1 = 0.01;
|
|
|
|
const float t2 = 0.00008;
|
|
|
|
rhLinear = c1 + c2 * humRaw + c3 * humRaw * humRaw;
|
|
|
|
h = (t - 25) * (t1 + t2 * humRaw) + rhLinear;
|
2017-04-25 17:24:42 +01:00
|
|
|
if (!isnan(t) && TEMP_CONVERSION) {
|
|
|
|
t = t * 1.8 + 32;
|
|
|
|
}
|
2017-03-16 15:23:11 +00:00
|
|
|
return (!isnan(t) && !isnan(h));
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean sht_readCharTempHum(char* temp, char* hum)
|
|
|
|
{
|
2017-04-25 17:24:42 +01:00
|
|
|
float t;
|
|
|
|
float h;
|
2017-03-16 15:23:11 +00:00
|
|
|
|
|
|
|
boolean success = sht_readTempHum(t, h);
|
|
|
|
dtostrf(t, 1, TEMP_RESOLUTION &3, temp);
|
|
|
|
dtostrf(h, 1, HUMIDITY_RESOLUTION &3, hum);
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean sht_detect()
|
|
|
|
{
|
2017-04-25 17:24:42 +01:00
|
|
|
if (shttype) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-03-16 15:23:11 +00:00
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
float t;
|
|
|
|
float h;
|
2017-03-16 15:23:11 +00:00
|
|
|
|
|
|
|
sht_sda_pin = pin[GPIO_I2C_SDA];
|
|
|
|
sht_scl_pin = pin[GPIO_I2C_SCL];
|
|
|
|
if (sht_readTempHum(t, h)) {
|
|
|
|
shttype = 1;
|
|
|
|
addLog_P(LOG_LEVEL_DEBUG, PSTR("I2C: SHT1X found"));
|
|
|
|
} else {
|
|
|
|
Wire.begin(sht_sda_pin, sht_scl_pin);
|
|
|
|
shttype = 0;
|
|
|
|
}
|
|
|
|
return shttype;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Presentation
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
void sht_mqttPresent(char* svalue, uint16_t ssvalue, uint8_t* djson)
|
|
|
|
{
|
2017-04-25 17:24:42 +01:00
|
|
|
if (!shttype) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-16 15:23:11 +00:00
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
char stemp[10];
|
|
|
|
char shum[10];
|
|
|
|
|
2017-03-16 15:23:11 +00:00
|
|
|
if (sht_readCharTempHum(stemp, shum)) {
|
2017-04-03 15:38:15 +01:00
|
|
|
snprintf_P(svalue, ssvalue, JSON_SNS_TEMPHUM, svalue, "SHT1X", stemp, shum);
|
2017-03-16 15:23:11 +00:00
|
|
|
*djson = 1;
|
|
|
|
#ifdef USE_DOMOTICZ
|
|
|
|
domoticz_sensor2(stemp, shum);
|
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
String sht_webPresent()
|
|
|
|
{
|
|
|
|
String page = "";
|
|
|
|
if (shttype) {
|
2017-04-25 17:24:42 +01:00
|
|
|
char stemp[10];
|
|
|
|
char shum[10];
|
|
|
|
|
2017-03-16 15:23:11 +00:00
|
|
|
if (sht_readCharTempHum(stemp, shum)) {
|
|
|
|
char sensor[80];
|
|
|
|
snprintf_P(sensor, sizeof(sensor), HTTP_SNS_TEMP, "SHT1X", stemp, (TEMP_CONVERSION) ? 'F' : 'C');
|
|
|
|
page += sensor;
|
|
|
|
snprintf_P(sensor, sizeof(sensor), HTTP_SNS_HUM, "SHT1X", shum);
|
|
|
|
page += sensor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
#endif // USE_SHT
|
|
|
|
#endif // USE_I2C
|
|
|
|
|