2020-03-08 20:54:28 +00:00
|
|
|
/*
|
2020-03-14 11:43:02 +00:00
|
|
|
xsns_65_hdc1080.ino - Texas Instruments HDC1080 temperature and humidity sensor support for Tasmota
|
2020-03-08 20:54:28 +00:00
|
|
|
|
|
|
|
Copyright (C) 2020 Luis Teixeira
|
|
|
|
|
|
|
|
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_HDC1080
|
2020-03-13 00:46:25 +00:00
|
|
|
|
2020-03-08 20:54:28 +00:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* HDC1080 - Temperature and Humidy sensor
|
|
|
|
*
|
|
|
|
* Source: Luis Teixeira
|
|
|
|
*
|
|
|
|
* I2C Address: 0x40
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2020-03-13 00:46:25 +00:00
|
|
|
#define XSNS_65 65
|
|
|
|
#define XI2C_45 45 // See I2CDEVICES.md
|
2020-03-08 20:54:28 +00:00
|
|
|
|
|
|
|
#define HDC1080_ADDR 0x40
|
|
|
|
|
|
|
|
// Registers:
|
|
|
|
|
|
|
|
#define HDC_REG_TEMP 0x00 // Temperature register
|
|
|
|
#define HDC_REG_RH 0x01 // Humidity register
|
|
|
|
#define HDC_REG_CONFIG 0x02 // Configuration register
|
|
|
|
#define HDC_REG_SERIAL1 0xFB // First 2 bytes of the serial ID
|
|
|
|
#define HDC_REG_SERIAL2 0xFC // Mid 2 bytes of the serial ID
|
|
|
|
#define HDC_REG_SERIAL3 0xFD // Last bits of the serial ID
|
|
|
|
#define HDC_REG_MAN_ID 0xFE // Manufacturer ID
|
|
|
|
#define HDC_REG_DEV_ID 0xFF // Device ID
|
|
|
|
|
|
|
|
// Expected constant values of some of the registers:
|
|
|
|
|
|
|
|
#define HDC1080_MAN_ID 0x5449 // Manufacturer ID (Texas Instruments)
|
|
|
|
#define HDC1080_DEV_ID 0x1050 // Device ID (valid for the HDC1080)
|
|
|
|
|
|
|
|
// Possible values for the configuration register fields:
|
|
|
|
|
2020-03-09 23:02:03 +00:00
|
|
|
#define HDC1080_RST_ON 0x8000
|
|
|
|
#define HDC1080_HEAT_ON 0x2000
|
|
|
|
#define HDC1080_MODE_ON 0x1000 // acquision mode (temperature + humidity)
|
|
|
|
#define HDC1080_TRES_11 0x400
|
|
|
|
#define HDC1080_HRES_11 0x100
|
|
|
|
#define HDC1080_HRES_8 0x80
|
|
|
|
|
|
|
|
// Constants:
|
|
|
|
|
2020-03-13 22:40:33 +00:00
|
|
|
#define HDC1080_CONV_TIME 15 // Assume 6.50 + 6.35 ms + x of conversion delay for this device
|
2020-03-08 20:54:28 +00:00
|
|
|
#define HDC1080_TEMP_MULT 0.0025177
|
2020-04-27 00:49:46 +01:00
|
|
|
#define HDC1080_RH_MULT 0.0015258
|
2020-03-09 23:02:03 +00:00
|
|
|
#define HDC1080_TEMP_OFFSET 40.0
|
2020-03-08 20:54:28 +00:00
|
|
|
|
2020-03-10 22:53:49 +00:00
|
|
|
const char* hdc_type_name = "HDC1080";
|
2020-03-09 23:02:03 +00:00
|
|
|
uint16_t hdc_manufacturer_id = 0;
|
|
|
|
uint16_t hdc_device_id = 0;
|
|
|
|
|
|
|
|
float hdc_temperature = 0.0;
|
|
|
|
float hdc_humidity = 0.0;
|
2020-03-08 20:54:28 +00:00
|
|
|
|
|
|
|
uint8_t hdc_valid = 0;
|
|
|
|
|
2020-03-13 00:46:25 +00:00
|
|
|
bool is_reading = false;
|
2020-03-13 22:40:33 +00:00
|
|
|
uint32_t hdc_next_read;
|
2020-03-13 00:46:25 +00:00
|
|
|
|
2020-03-08 20:54:28 +00:00
|
|
|
/**
|
|
|
|
* Reads the device ID register.
|
2020-03-14 11:43:02 +00:00
|
|
|
*
|
2020-03-08 20:54:28 +00:00
|
|
|
*/
|
|
|
|
uint16_t HdcReadDeviceId(void) {
|
2020-03-09 23:02:03 +00:00
|
|
|
return I2cRead16(HDC1080_ADDR, HDC_REG_DEV_ID);
|
|
|
|
}
|
2020-03-08 20:54:28 +00:00
|
|
|
|
2020-03-09 23:02:03 +00:00
|
|
|
/**
|
|
|
|
* Reads the manufacturer ID register.
|
2020-03-14 11:43:02 +00:00
|
|
|
*
|
2020-03-09 23:02:03 +00:00
|
|
|
*/
|
|
|
|
uint16_t HdcReadManufacturerId(void) {
|
|
|
|
return I2cRead16(HDC1080_ADDR, HDC_REG_MAN_ID);
|
2020-03-08 20:54:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 23:02:03 +00:00
|
|
|
/**
|
|
|
|
* Overwrites the configuration register with the provided config
|
|
|
|
*/
|
|
|
|
void HdcConfig(uint16_t config) {
|
|
|
|
I2cWrite16(HDC1080_ADDR, HDC_REG_CONFIG, config);
|
2020-03-08 20:54:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-03-09 23:02:03 +00:00
|
|
|
* Performs a soft reset on the device.
|
2020-03-14 11:43:02 +00:00
|
|
|
*
|
2020-03-09 23:02:03 +00:00
|
|
|
* RST = 1 -> software reset
|
2020-03-14 11:43:02 +00:00
|
|
|
*
|
2020-03-08 20:54:28 +00:00
|
|
|
*/
|
2020-03-09 23:02:03 +00:00
|
|
|
void HdcReset(void) {
|
2020-03-08 20:54:28 +00:00
|
|
|
uint16_t current = I2cRead16(HDC1080_ADDR, HDC_REG_CONFIG);
|
2020-03-14 11:43:02 +00:00
|
|
|
|
2020-03-09 23:02:03 +00:00
|
|
|
// bit 15 of the configuration register contains the RST flag
|
|
|
|
// so we set it to 1:
|
2020-03-08 20:54:28 +00:00
|
|
|
|
2020-03-09 23:02:03 +00:00
|
|
|
current |= 0x8000;
|
2020-03-08 20:54:28 +00:00
|
|
|
|
|
|
|
I2cWrite16(HDC1080_ADDR, HDC_REG_CONFIG, current);
|
2020-03-09 23:02:03 +00:00
|
|
|
|
2020-03-10 00:15:42 +00:00
|
|
|
delay(HDC1080_CONV_TIME); // Not sure how long it takes to reset. Assuming this value.
|
2020-03-08 20:54:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-10 00:15:42 +00:00
|
|
|
/**
|
2020-03-14 11:43:02 +00:00
|
|
|
* Performs the write portion of the HDC1080 sensor transaction. This
|
2020-03-13 00:46:25 +00:00
|
|
|
* action of writing to a register signals the beginning of the operation
|
|
|
|
* (e.g. data acquisition).
|
2020-03-14 11:43:02 +00:00
|
|
|
*
|
2020-03-13 00:46:25 +00:00
|
|
|
* addr: the address of the I2C device we are talking to.
|
|
|
|
* reg: the register where we are writing to.
|
2020-03-14 11:43:02 +00:00
|
|
|
*
|
2020-03-13 00:46:25 +00:00
|
|
|
* returns: 0 if the transmission was successfully completed, != 0 otherwise.
|
2020-03-10 00:15:42 +00:00
|
|
|
*/
|
2020-03-13 00:46:25 +00:00
|
|
|
int8_t HdcTransactionOpen(uint8_t addr, uint8_t reg) {
|
|
|
|
Wire.beginTransmission((uint8_t) addr);
|
|
|
|
Wire.write((uint8_t) reg);
|
|
|
|
return Wire.endTransmission();
|
|
|
|
}
|
2020-03-10 00:15:42 +00:00
|
|
|
|
2020-03-13 00:46:25 +00:00
|
|
|
/**
|
|
|
|
* Performs the read portion of the HDC1080 sensor transaction.
|
2020-03-14 11:43:02 +00:00
|
|
|
*
|
2020-03-13 00:46:25 +00:00
|
|
|
* addr: the address of the I2C device we are talking to.
|
|
|
|
* reg_data: the pointer to the memory location where we will place the bytes that were read from the device
|
|
|
|
* len: the number of bytes we expect to read
|
2020-03-14 11:43:02 +00:00
|
|
|
*
|
2020-03-13 00:46:25 +00:00
|
|
|
* returns: if the read operation was successful. != 0 otherwise.
|
|
|
|
*/
|
|
|
|
int8_t HdcTransactionClose(uint8_t addr, uint8_t *reg_data, uint16_t len) {
|
|
|
|
if (len != Wire.requestFrom((uint8_t) addr, (uint8_t) len)) {
|
2020-03-10 00:15:42 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (len--) {
|
|
|
|
*reg_data = (uint8_t)Wire.read();
|
|
|
|
reg_data++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-03-09 23:02:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The various initialization steps for this sensor.
|
2020-03-14 11:43:02 +00:00
|
|
|
*
|
2020-03-09 23:02:03 +00:00
|
|
|
*/
|
|
|
|
void HdcInit(void) {
|
2020-03-08 20:54:28 +00:00
|
|
|
HdcReset();
|
2020-03-10 00:15:42 +00:00
|
|
|
HdcConfig(HDC1080_MODE_ON);
|
2020-03-08 20:54:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-13 00:46:25 +00:00
|
|
|
/**
|
|
|
|
* Triggers the single transaction read of the T/RH sensor.
|
2020-03-14 11:43:02 +00:00
|
|
|
*
|
2020-03-13 00:46:25 +00:00
|
|
|
*/
|
|
|
|
bool HdcTriggerRead(void) {
|
|
|
|
int8_t status = HdcTransactionOpen(HDC1080_ADDR, HDC_REG_TEMP);
|
|
|
|
|
2020-03-13 22:40:33 +00:00
|
|
|
hdc_next_read = millis() + HDC1080_CONV_TIME;
|
|
|
|
|
2020-03-13 00:46:25 +00:00
|
|
|
if(status) {
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG, PSTR("HdcTriggerRead: failed to open the transaction for HDC_REG_TEMP. Status = %d"), status);
|
2020-03-13 00:46:25 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
is_reading = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:54:28 +00:00
|
|
|
/**
|
|
|
|
* Performs a temperature and humidity measurement, and calls
|
|
|
|
* the conversion function providing the results in the correct
|
|
|
|
* unit according to the device settings.
|
2020-03-14 11:43:02 +00:00
|
|
|
*
|
2020-03-13 00:46:25 +00:00
|
|
|
* returns: false if something failed during the read process.
|
2020-03-14 11:43:02 +00:00
|
|
|
*
|
2020-03-08 20:54:28 +00:00
|
|
|
*/
|
|
|
|
bool HdcRead(void) {
|
|
|
|
int8_t status = 0;
|
2020-03-10 00:15:42 +00:00
|
|
|
uint8_t sensor_data[4];
|
|
|
|
uint16_t temp_data = 0;
|
|
|
|
uint16_t rh_data = 0;
|
2020-03-08 20:54:28 +00:00
|
|
|
|
2020-03-13 00:46:25 +00:00
|
|
|
is_reading = false;
|
2020-03-08 20:54:28 +00:00
|
|
|
|
2020-03-13 00:46:25 +00:00
|
|
|
status = HdcTransactionClose(HDC1080_ADDR, sensor_data, 4);
|
2020-03-08 20:54:28 +00:00
|
|
|
|
2020-03-13 00:46:25 +00:00
|
|
|
if(status) {
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG, PSTR("HdcRead: failed to read HDC_REG_TEMP. Status = %d"), status);
|
2020-03-08 20:54:28 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-13 00:46:25 +00:00
|
|
|
temp_data = (uint16_t) ((sensor_data[0] << 8) | sensor_data[1]);
|
|
|
|
rh_data = (uint16_t) ((sensor_data[2] << 8) | sensor_data[3]);
|
|
|
|
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG, PSTR("HdcRead: temperature raw data: 0x%04x; humidity raw data: 0x%04x"), temp_data, rh_data);
|
2020-03-13 00:46:25 +00:00
|
|
|
|
2020-03-10 00:15:42 +00:00
|
|
|
// read the temperature from the first 16 bits of the result
|
2020-03-08 20:54:28 +00:00
|
|
|
|
2020-03-10 00:15:42 +00:00
|
|
|
hdc_temperature = ConvertTemp(HDC1080_TEMP_MULT * (float) (temp_data) - HDC1080_TEMP_OFFSET);
|
2020-03-09 23:02:03 +00:00
|
|
|
|
2020-03-10 00:15:42 +00:00
|
|
|
hdc_humidity = HDC1080_RH_MULT * (float) (rh_data);
|
2020-03-09 23:02:03 +00:00
|
|
|
|
|
|
|
if (hdc_humidity > 100) { hdc_humidity = 100.0; }
|
2020-03-08 20:54:28 +00:00
|
|
|
if (hdc_humidity < 0) { hdc_humidity = 0.01; }
|
2020-03-18 10:23:00 +00:00
|
|
|
hdc_humidity = ConvertHumidity(hdc_humidity);
|
2020-03-08 20:54:28 +00:00
|
|
|
|
|
|
|
hdc_valid = SENSOR_MAX_MISS;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-13 00:46:25 +00:00
|
|
|
/**
|
|
|
|
* Performs the detection of the HTC1080 sensor.
|
2020-03-14 11:43:02 +00:00
|
|
|
*
|
2020-03-13 00:46:25 +00:00
|
|
|
*/
|
2020-03-08 20:54:28 +00:00
|
|
|
|
2020-03-09 23:02:03 +00:00
|
|
|
void HdcDetect(void) {
|
2020-03-14 11:43:02 +00:00
|
|
|
if (I2cActive(HDC1080_ADDR)) {
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG, PSTR("HdcDetect: Address = 0x%02X already in use."), HDC1080_ADDR);
|
2020-03-08 20:54:28 +00:00
|
|
|
|
2020-03-14 11:43:02 +00:00
|
|
|
return;
|
2020-03-08 20:54:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 23:02:03 +00:00
|
|
|
hdc_manufacturer_id = HdcReadManufacturerId();
|
|
|
|
hdc_device_id = HdcReadDeviceId();
|
2020-03-08 20:54:28 +00:00
|
|
|
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG, PSTR("HdcDetect: detected device with manufacturerId = 0x%04X and deviceId = 0x%04X"), hdc_manufacturer_id, hdc_device_id);
|
2020-03-08 20:54:28 +00:00
|
|
|
|
2020-03-09 23:02:03 +00:00
|
|
|
if (hdc_device_id == HDC1080_DEV_ID) {
|
2020-03-08 20:54:28 +00:00
|
|
|
HdcInit();
|
2020-03-10 00:15:42 +00:00
|
|
|
I2cSetActiveFound(HDC1080_ADDR, hdc_type_name);
|
2020-03-08 20:54:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 00:46:25 +00:00
|
|
|
/**
|
|
|
|
* As the name suggests, this function is called every second
|
|
|
|
* for performing driver related logic.
|
2020-03-14 11:43:02 +00:00
|
|
|
*
|
2020-03-13 00:46:25 +00:00
|
|
|
*/
|
2020-03-08 20:54:28 +00:00
|
|
|
void HdcEverySecond(void) {
|
2020-10-28 16:32:07 +00:00
|
|
|
if (TasmotaGlobal.uptime &1) { // Every 2 seconds
|
2020-03-13 00:46:25 +00:00
|
|
|
if (!HdcTriggerRead()) {
|
2020-03-10 22:53:49 +00:00
|
|
|
AddLogMissed((char*) hdc_type_name, hdc_valid);
|
2020-03-08 20:54:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 00:46:25 +00:00
|
|
|
/**
|
2020-03-14 11:43:02 +00:00
|
|
|
* Tasmota boilerplate for presenting the sensor data in the web UI, JSON for
|
2020-03-13 00:46:25 +00:00
|
|
|
* the MQTT messages, and so on.
|
2020-03-14 11:43:02 +00:00
|
|
|
*
|
2020-03-13 00:46:25 +00:00
|
|
|
*/
|
2020-03-16 16:09:15 +00:00
|
|
|
void HdcShow(bool json) {
|
|
|
|
if (hdc_valid) {
|
2020-10-29 12:37:09 +00:00
|
|
|
TempHumDewShow(json, (0 == TasmotaGlobal.tele_period), hdc_type_name, hdc_temperature, hdc_humidity);
|
2020-03-16 16:09:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-08 20:54:28 +00:00
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2020-03-13 00:46:25 +00:00
|
|
|
bool Xsns65(uint8_t function)
|
2020-03-08 20:54:28 +00:00
|
|
|
{
|
2020-03-14 11:43:02 +00:00
|
|
|
if (!I2cEnabled(XI2C_45)) {
|
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG, PSTR("Xsns65: I2C driver not enabled for this device."));
|
2020-03-09 23:02:03 +00:00
|
|
|
|
2020-03-14 11:43:02 +00:00
|
|
|
return false;
|
2020-03-09 23:02:03 +00:00
|
|
|
}
|
2020-03-08 20:54:28 +00:00
|
|
|
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
if (FUNC_INIT == function) {
|
|
|
|
HdcDetect();
|
|
|
|
}
|
2020-03-09 23:02:03 +00:00
|
|
|
else if (hdc_device_id) {
|
2020-03-08 20:54:28 +00:00
|
|
|
switch (function) {
|
2020-03-13 00:46:25 +00:00
|
|
|
case FUNC_EVERY_50_MSECOND:
|
2020-03-13 22:40:33 +00:00
|
|
|
if(is_reading && TimeReached(hdc_next_read)) {
|
2020-03-13 00:46:25 +00:00
|
|
|
if(!HdcRead()) {
|
|
|
|
AddLogMissed((char*) hdc_type_name, hdc_valid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2020-03-08 20:54:28 +00:00
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
HdcEverySecond();
|
|
|
|
break;
|
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
HdcShow(1);
|
|
|
|
break;
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
case FUNC_WEB_SENSOR:
|
|
|
|
HdcShow(0);
|
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_HDC1080
|
|
|
|
#endif // USE_I2C
|
|
|
|
|