2017-01-28 13:41:01 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2017 Heiko Krupp. 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_HTU
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* HTU21 - Temperature and Humidy
|
|
|
|
*
|
|
|
|
* Source: Heiko Krupp
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define HTU21_ADDR 0x40
|
|
|
|
|
2017-02-28 15:01:48 +00:00
|
|
|
#define SI7013_CHIPID 0x0D
|
|
|
|
#define SI7020_CHIPID 0x14
|
|
|
|
#define SI7021_CHIPID 0x15
|
2017-01-28 13:41:01 +00:00
|
|
|
#define HTU21_CHIPID 0x32
|
|
|
|
|
|
|
|
#define HTU21_READTEMP 0xE3
|
|
|
|
#define HTU21_READHUM 0xE5
|
|
|
|
#define HTU21_WRITEREG 0xE6
|
|
|
|
#define HTU21_READREG 0xE7
|
|
|
|
#define HTU21_RESET 0xFE
|
|
|
|
#define HTU21_HEATER_WRITE 0x51
|
|
|
|
#define HTU21_HEATER_READ 0x11
|
|
|
|
#define HTU21_SERIAL2_READ1 0xFC /* Read 3rd two Serial bytes */
|
|
|
|
#define HTU21_SERIAL2_READ2 0xC9 /* Read 4th two Serial bytes */
|
|
|
|
|
|
|
|
#define HTU21_HEATER_ON 0x04
|
|
|
|
#define HTU21_HEATER_OFF 0xFB
|
|
|
|
|
2017-02-28 15:01:48 +00:00
|
|
|
#define HTU21_RES_RH12_T14 0x00 // Default
|
2017-01-28 13:41:01 +00:00
|
|
|
#define HTU21_RES_RH8_T12 0x01
|
|
|
|
#define HTU21_RES_RH10_T13 0x80
|
|
|
|
#define HTU21_RES_RH11_T11 0x81
|
|
|
|
|
|
|
|
#define HTU21_CRC8_POLYNOM 0x13100
|
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
uint8_t htuaddr;
|
|
|
|
uint8_t htutype = 0;
|
|
|
|
uint8_t delayT;
|
|
|
|
uint8_t delayH = 50;
|
2017-01-28 13:41:01 +00:00
|
|
|
char htustype[7];
|
|
|
|
|
|
|
|
uint8_t check_crc8(uint16_t data)
|
|
|
|
{
|
2017-04-25 17:24:42 +01:00
|
|
|
for (uint8_t bit = 0; bit < 16; bit++) {
|
|
|
|
if (data & 0x8000) {
|
2017-01-28 13:41:01 +00:00
|
|
|
data = (data << 1) ^ HTU21_CRC8_POLYNOM;
|
2017-04-25 17:24:42 +01:00
|
|
|
} else {
|
2017-01-28 13:41:01 +00:00
|
|
|
data <<= 1;
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
return data >>= 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t htu21_readDeviceID(void)
|
|
|
|
{
|
|
|
|
uint16_t deviceID = 0;
|
|
|
|
uint8_t checksum = 0;
|
|
|
|
|
|
|
|
Wire.beginTransmission(HTU21_ADDR);
|
|
|
|
Wire.write(HTU21_SERIAL2_READ1);
|
|
|
|
Wire.write(HTU21_SERIAL2_READ2);
|
|
|
|
Wire.endTransmission();
|
|
|
|
|
|
|
|
Wire.requestFrom(HTU21_ADDR, 3);
|
|
|
|
deviceID = Wire.read() << 8;
|
|
|
|
deviceID |= Wire.read();
|
|
|
|
checksum = Wire.read();
|
|
|
|
if (check_crc8(deviceID) == checksum) {
|
|
|
|
deviceID = deviceID >> 8;
|
|
|
|
} else {
|
|
|
|
deviceID = 0;
|
|
|
|
}
|
|
|
|
return (uint8_t)deviceID;
|
|
|
|
}
|
|
|
|
|
|
|
|
void htu21_setRes(uint8_t resolution)
|
|
|
|
{
|
|
|
|
uint8_t current = i2c_read8(HTU21_ADDR, HTU21_READREG);
|
|
|
|
current &= 0x7E; // Replace current resolution bits with 0
|
|
|
|
current |= resolution; // Add new resolution bits to register
|
|
|
|
i2c_write8(HTU21_ADDR, HTU21_WRITEREG, current);
|
|
|
|
}
|
|
|
|
|
|
|
|
void htu21_reset(void)
|
|
|
|
{
|
|
|
|
Wire.beginTransmission(HTU21_ADDR);
|
|
|
|
Wire.write(HTU21_RESET);
|
|
|
|
Wire.endTransmission();
|
|
|
|
delay(15); // Reset takes 15ms
|
|
|
|
}
|
|
|
|
|
|
|
|
void htu21_heater(uint8_t heater)
|
|
|
|
{
|
|
|
|
uint8_t current = i2c_read8(HTU21_ADDR, HTU21_READREG);
|
|
|
|
|
|
|
|
switch(heater)
|
|
|
|
{
|
|
|
|
case HTU21_HEATER_ON : current |= heater;
|
|
|
|
break;
|
|
|
|
case HTU21_HEATER_OFF : current &= heater;
|
|
|
|
break;
|
|
|
|
default : current &= heater;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i2c_write8(HTU21_ADDR, HTU21_WRITEREG, current);
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean htu21_init()
|
|
|
|
{
|
|
|
|
htu21_reset();
|
|
|
|
htu21_heater(HTU21_HEATER_OFF);
|
|
|
|
htu21_setRes(HTU21_RES_RH12_T14);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
float htu21_readHumidity(void)
|
|
|
|
{
|
2017-04-25 17:24:42 +01:00
|
|
|
uint8_t checksum = 0;
|
|
|
|
uint16_t sensorval = 0;
|
|
|
|
float humidity = 0.0;
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
Wire.beginTransmission(HTU21_ADDR);
|
|
|
|
Wire.write(HTU21_READHUM);
|
2017-04-25 17:24:42 +01:00
|
|
|
if (Wire.endTransmission() != 0) {
|
|
|
|
return 0.0; // In case of error
|
|
|
|
}
|
2017-02-28 15:01:48 +00:00
|
|
|
delay(delayH); // Sensor time at max resolution
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
Wire.requestFrom(HTU21_ADDR, 3);
|
2017-04-25 17:24:42 +01:00
|
|
|
if (3 <= Wire.available()) {
|
2017-01-28 13:41:01 +00:00
|
|
|
sensorval = Wire.read() << 8; // MSB
|
|
|
|
sensorval |= Wire.read(); // LSB
|
|
|
|
checksum = Wire.read();
|
|
|
|
}
|
2017-04-25 17:24:42 +01:00
|
|
|
if (check_crc8(sensorval) != checksum) {
|
|
|
|
return 0.0; // Checksum mismatch
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
sensorval ^= 0x02; // clear status bits
|
|
|
|
humidity = 0.001907 * (float)sensorval - 6;
|
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
if (humidity > 100) {
|
|
|
|
return 100.0;
|
|
|
|
}
|
|
|
|
if (humidity < 0) {
|
|
|
|
return 0.01;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
return humidity;
|
|
|
|
}
|
|
|
|
|
2017-05-03 17:19:13 +01:00
|
|
|
float htu21_readTemperature()
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
|
|
|
uint8_t checksum=0;
|
|
|
|
uint16_t sensorval=0;
|
|
|
|
float t;
|
|
|
|
|
|
|
|
Wire.beginTransmission(HTU21_ADDR);
|
|
|
|
Wire.write(HTU21_READTEMP);
|
2017-04-25 17:24:42 +01:00
|
|
|
if (Wire.endTransmission() != 0) {
|
|
|
|
return 0.0; // In case of error
|
|
|
|
}
|
2017-02-28 15:01:48 +00:00
|
|
|
delay(delayT); // Sensor time at max resolution
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
Wire.requestFrom(HTU21_ADDR, 3);
|
2017-04-25 17:24:42 +01:00
|
|
|
if (3 == Wire.available()) {
|
2017-01-28 13:41:01 +00:00
|
|
|
sensorval = Wire.read() << 8; // MSB
|
|
|
|
sensorval |= Wire.read(); // LSB
|
|
|
|
checksum = Wire.read();
|
|
|
|
}
|
2017-04-25 17:24:42 +01:00
|
|
|
if (check_crc8(sensorval) != checksum) {
|
|
|
|
return 0.0; // Checksum mismatch
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-05-03 17:19:13 +01:00
|
|
|
t = convertTemp(0.002681 * (float)sensorval - 46.85);
|
2017-01-28 13:41:01 +00:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
float htu21_compensatedHumidity(float humidity, float temperature)
|
|
|
|
{
|
2017-04-25 17:24:42 +01:00
|
|
|
if(humidity == 0.00 && temperature == 0.00) {
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
if(temperature > 0.00 && temperature < 80.00) {
|
2017-01-28 13:41:01 +00:00
|
|
|
return (-0.15)*(25-temperature)+humidity;
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t htu_detect()
|
|
|
|
{
|
2017-04-25 17:24:42 +01:00
|
|
|
if (htutype) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
char log[LOGSZ];
|
|
|
|
boolean success = false;
|
|
|
|
|
|
|
|
htuaddr = HTU21_ADDR;
|
|
|
|
htutype = htu21_readDeviceID();
|
2017-02-28 15:01:48 +00:00
|
|
|
success = htu21_init();
|
2017-01-28 13:41:01 +00:00
|
|
|
switch (htutype) {
|
|
|
|
case HTU21_CHIPID:
|
2017-02-28 15:01:48 +00:00
|
|
|
strcpy_P(htustype, PSTR("HTU21"));
|
|
|
|
delayT=50;
|
|
|
|
delayH=16;
|
|
|
|
break;
|
|
|
|
case SI7013_CHIPID:
|
|
|
|
strcpy_P(htustype, PSTR("SI7013"));
|
|
|
|
delayT=12;
|
|
|
|
delayH=23;
|
|
|
|
break;
|
|
|
|
case SI7020_CHIPID:
|
|
|
|
strcpy_P(htustype, PSTR("SI7020"));
|
|
|
|
delayT=12;
|
|
|
|
delayH=23;
|
|
|
|
break;
|
|
|
|
case SI7021_CHIPID:
|
|
|
|
strcpy_P(htustype, PSTR("SI7021"));
|
|
|
|
delayT=12;
|
|
|
|
delayH=23;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
strcpy_P(htustype, PSTR("T/RH?"));
|
|
|
|
delayT=50;
|
|
|
|
delayH=23;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
if (success) {
|
|
|
|
snprintf_P(log, sizeof(log), PSTR("I2C: %s found at address 0x%x"), htustype, htuaddr);
|
|
|
|
addLog(LOG_LEVEL_DEBUG, log);
|
|
|
|
} else {
|
|
|
|
htutype = 0;
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Presentation
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2017-02-04 16:09:54 +00:00
|
|
|
void htu_mqttPresent(char* svalue, uint16_t ssvalue, uint8_t* djson)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2017-04-25 17:24:42 +01:00
|
|
|
if (!htutype) {
|
|
|
|
return;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
char stemp1[10];
|
|
|
|
char stemp2[10];
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-05-03 17:19:13 +01:00
|
|
|
float t = htu21_readTemperature();
|
2017-01-28 13:41:01 +00:00
|
|
|
float h = htu21_readHumidity();
|
|
|
|
h = htu21_compensatedHumidity(h, t);
|
2017-05-03 17:19:13 +01:00
|
|
|
dtostrf(t, 1, sysCfg.flag.temperature_resolution, stemp1);
|
|
|
|
dtostrf(h, 1, sysCfg.flag.humidity_resolution, stemp2);
|
2017-04-03 15:38:15 +01:00
|
|
|
snprintf_P(svalue, ssvalue, JSON_SNS_TEMPHUM, svalue, htustype, stemp1, stemp2);
|
2017-01-28 13:41:01 +00:00
|
|
|
*djson = 1;
|
|
|
|
#ifdef USE_DOMOTICZ
|
|
|
|
domoticz_sensor2(stemp1, stemp2);
|
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
String htu_webPresent()
|
|
|
|
{
|
|
|
|
String page = "";
|
|
|
|
if (htutype) {
|
2017-04-25 17:24:42 +01:00
|
|
|
char stemp[10];
|
|
|
|
char sensor[80];
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-05-03 17:19:13 +01:00
|
|
|
float t_htu21 = htu21_readTemperature();
|
2017-01-28 13:41:01 +00:00
|
|
|
float h_htu21 = htu21_readHumidity();
|
|
|
|
h_htu21 = htu21_compensatedHumidity(h_htu21, t_htu21);
|
2017-05-03 17:19:13 +01:00
|
|
|
dtostrf(t_htu21, 1, sysCfg.flag.temperature_resolution, stemp);
|
|
|
|
snprintf_P(sensor, sizeof(sensor), HTTP_SNS_TEMP, htustype, stemp, tempUnit());
|
2017-02-28 15:01:48 +00:00
|
|
|
page += sensor;
|
2017-05-03 17:19:13 +01:00
|
|
|
dtostrf(h_htu21, 1, sysCfg.flag.humidity_resolution, stemp);
|
2017-02-28 15:01:48 +00:00
|
|
|
snprintf_P(sensor, sizeof(sensor), HTTP_SNS_HUM, htustype, stemp);
|
|
|
|
page += sensor;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
#endif // USE_HTU
|
|
|
|
#endif // USE_I2C
|
|
|
|
|