Tasmota/tasmota/xsns_73_hp303b.ino

174 lines
5.4 KiB
Arduino
Raw Normal View History

2020-06-06 21:35:41 +01:00
/*
xsns_72_hp303b.ino - HP303B digital barometric air pressure sensor support for Tasmota
2021-01-01 12:44:04 +00:00
Copyright (C) 2021 Theo Arends
2020-06-06 21:35:41 +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.
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/>.
*/
2020-06-06 21:35:41 +01:00
#ifdef USE_I2C
#ifdef USE_HP303B
/*********************************************************************************************\
* HP303B - Pressure and temperature sensor
2020-06-06 21:35:41 +01:00
*
* Source: Lolin LOLIN_HP303B_Library
*
* I2C Address: 0x77 or 0x76
\*********************************************************************************************/
#define XSNS_73 73
#define XI2C_52 52 // See I2CDEVICES.md
#define HP303B_MAX_SENSORS 2
#define HP303B_START_ADDRESS 0x76
2020-06-06 21:35:41 +01:00
#include <LOLIN_HP303B.h>
// HP303B Object
2020-06-06 21:35:41 +01:00
LOLIN_HP303B HP303BSensor = LOLIN_HP303B();
struct {
int16_t oversampling = 7;
char types[7] = "HP303B";
uint8_t count = 0;
} hp303b_cfg;
2020-06-08 08:13:01 +01:00
struct BHP303B {
float temperature = NAN;
float pressure = NAN;
uint8_t address;
uint8_t valid = 0;
} hp303b_sensor[HP303B_MAX_SENSORS];
2020-06-06 21:35:41 +01:00
/*********************************************************************************************/
bool HP303B_Read(uint32_t hp303b_idx) {
if (hp303b_sensor[hp303b_idx].valid) { hp303b_sensor[hp303b_idx].valid--; }
2020-06-08 09:19:51 +01:00
2020-06-07 15:33:28 +01:00
float t;
if (HP303BSensor.measureTempOnce(t, hp303b_sensor[hp303b_idx].address, hp303b_cfg.oversampling) != 0) {
2020-06-06 21:35:41 +01:00
return false;
}
2020-06-06 21:35:41 +01:00
float p;
if (HP303BSensor.measurePressureOnce(p, hp303b_sensor[hp303b_idx].address, hp303b_cfg.oversampling) != 0) {
2020-06-06 21:35:41 +01:00
return false;
}
2020-06-06 21:35:41 +01:00
hp303b_sensor[hp303b_idx].temperature = (float)ConvertTemp(t);
hp303b_sensor[hp303b_idx].pressure = (float)ConvertPressure(p / 100); // Conversion to hPa
2020-06-06 21:35:41 +01:00
hp303b_sensor[hp303b_idx].valid = SENSOR_MAX_MISS;
2020-06-06 21:35:41 +01:00
return true;
}
/********************************************************************************************/
void HP303B_Detect(void) {
for (uint32_t i = 0; i < HP303B_MAX_SENSORS; i++) {
if (!I2cSetDevice(HP303B_START_ADDRESS + i)) { continue; }
2020-06-06 21:35:41 +01:00
if (HP303BSensor.begin(HP303B_START_ADDRESS + i)) {
hp303b_sensor[hp303b_cfg.count].address = HP303B_START_ADDRESS + i;
I2cSetActiveFound(hp303b_sensor[hp303b_cfg.count].address, hp303b_cfg.types);
hp303b_cfg.count++;
2020-06-06 21:35:41 +01:00
}
}
}
void HP303B_EverySecond(void) {
for (uint32_t i = 0; i < hp303b_cfg.count; i++) {
2020-10-28 16:32:07 +00:00
if (TasmotaGlobal.uptime &1) {
if (!HP303B_Read(i)) {
AddLogMissed(hp303b_cfg.types, hp303b_sensor[i].valid);
}
2020-06-08 09:19:51 +01:00
}
}
}
2020-06-06 21:35:41 +01:00
void HP303B_Show(bool json) {
for (uint32_t i = 0; i < hp303b_cfg.count; i++) {
if (hp303b_sensor[i].valid) {
char sensor_name[12];
strlcpy(sensor_name, hp303b_cfg.types, sizeof(sensor_name));
if (hp303b_cfg.count > 1) {
snprintf_P(sensor_name, sizeof(sensor_name), PSTR("%s%c%02X"), sensor_name, IndexSeparator(), hp303b_sensor[i].address); // HP303B-76, HP303B-77
}
float sealevel = ConvertPressureForSeaLevel(hp303b_sensor[i].pressure);
2020-06-07 15:33:28 +01:00
char str_pressure[33];
2021-06-11 17:14:12 +01:00
dtostrfd(hp303b_sensor[i].pressure, Settings->flag2.pressure_resolution, str_pressure);
char sea_pressure[33];
2021-06-11 17:14:12 +01:00
dtostrfd(sealevel, Settings->flag2.pressure_resolution, sea_pressure);
if (json) {
2021-01-26 16:12:08 +00:00
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_TEMPERATURE "\":%*_f,\"" D_JSON_PRESSURE "\":%s"),
2021-06-11 17:14:12 +01:00
sensor_name, Settings->flag2.temperature_resolution, &hp303b_sensor[i].temperature, str_pressure);
if (Settings->altitude != 0) {
ResponseAppend_P(PSTR(",\"" D_JSON_PRESSUREATSEALEVEL "\":%s"), sea_pressure);
}
ResponseJsonEnd();
#ifdef USE_DOMOTICZ
2020-06-09 08:14:44 +01:00
// Domoticz and knx only support one temp sensor
2020-10-29 12:37:09 +00:00
if ((0 == TasmotaGlobal.tele_period) && (0 == i)) {
2021-01-26 16:12:08 +00:00
DomoticzFloatSensor(DZ_TEMP, hp303b_sensor[i].temperature);
}
#endif // USE_DOMOTICZ
#ifdef USE_WEBSERVER
} else {
2021-01-26 16:12:08 +00:00
WSContentSend_Temp(sensor_name, hp303b_sensor[i].temperature);
WSContentSend_PD(HTTP_SNS_PRESSURE, sensor_name, str_pressure, PressureUnit().c_str());
2021-06-11 17:14:12 +01:00
if (Settings->altitude != 0) {
WSContentSend_PD(HTTP_SNS_SEAPRESSURE, sensor_name, sea_pressure, PressureUnit().c_str());
}
#endif // USE_WEBSERVER
2020-06-06 21:35:41 +01:00
}
}
}
}
/*********************************************************************************************\
* Interface
\*********************************************************************************************/
bool Xsns73(uint8_t function)
{
2020-06-08 09:19:51 +01:00
if (!I2cEnabled(XI2C_52)) { return false; }
2020-06-06 21:35:41 +01:00
bool result = false;
2020-06-08 09:19:51 +01:00
if (FUNC_INIT == function) {
2020-06-06 21:35:41 +01:00
HP303B_Detect();
}
else if (hp303b_cfg.count) {
switch (function) {
2020-06-08 09:19:51 +01:00
case FUNC_EVERY_SECOND:
HP303B_EverySecond();
break;
2020-06-06 21:35:41 +01:00
case FUNC_JSON_APPEND:
HP303B_Show(1);
break;
#ifdef USE_WEBSERVER
case FUNC_WEB_SENSOR:
HP303B_Show(0);
break;
#endif // USE_WEBSERVER
}
}
return result;
}
#endif // USE_HP303B
#endif // USE_I2C