Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sen5x floats and units #17961

Merged
merged 1 commit into from
Feb 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 35 additions & 39 deletions tasmota/tasmota_xsns_sensor/xsns_103_sen5x.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
#ifdef USE_I2C
#ifdef USE_SEN5X
/*********************************************************************************************\
* SEN5X - Gas (VOC - Volatile Organic Compounds / NOx - Nitrous Oxides) and Particulates (PPM)
* SEN5X - Gas (VOC - Volatile Organic Compounds / NOx - Nitrous Oxides) and Particulates (PM)
*
* Source: Sensirion SEN5X Driver + Example, and Tasmota Driver 98 by Jean-Pierre Deschamps
* Adaption for TASMOTA: Tyeth Gundry
*
* I2C Address: 0x59
* I2C Address: 0x69
\*********************************************************************************************/

#define XSNS_103 103
Expand All @@ -39,7 +39,6 @@ SensirionI2CSen5x *sen5x = nullptr;

struct SEN5XDATA_s {
bool sen5x_ready;
float abshum;
float massConcentrationPm1p0;
float massConcentrationPm2p5;
float massConcentrationPm4p0;
Expand Down Expand Up @@ -165,17 +164,17 @@ void SEN5XUpdate(void) // Perform every second to ensure proper operation of the
{
#ifdef DEBUG_TASMOTA_SENSOR
DEBUG_SENSOR_LOG(PSTR("SEN5x readings:-"));
DEBUG_SENSOR_LOG(PSTR("MassConcentrationPm1p0: %f\n"), SEN5XDATA->massConcentrationPm1p0);
DEBUG_SENSOR_LOG(PSTR("MassConcentrationPm2p5: %f\n"), SEN5XDATA->massConcentrationPm2p5);
DEBUG_SENSOR_LOG(PSTR("MassConcentrationPm4p0: %f\n"), SEN5XDATA->massConcentrationPm4p0);
DEBUG_SENSOR_LOG(PSTR("MassConcentrationPm10p0: %f\n"), SEN5XDATA->massConcentrationPm10p0);
DEBUG_SENSOR_LOG(PSTR("MassConcentrationPm1p0: %1_f\n"), &SEN5XDATA->massConcentrationPm1p0);
DEBUG_SENSOR_LOG(PSTR("MassConcentrationPm2p5: %1_f\n"), &SEN5XDATA->massConcentrationPm2p5);
DEBUG_SENSOR_LOG(PSTR("MassConcentrationPm4p0: %1_f\n"), &SEN5XDATA->massConcentrationPm4p0);
DEBUG_SENSOR_LOG(PSTR("MassConcentrationPm10p0: %1_f\n"), &SEN5XDATA->massConcentrationPm10p0);
if (isnan(SEN5XDATA->ambientHumidity))
{
DEBUG_SENSOR_LOG(PSTR("AmbientHumidity: n/a\n"));
}
else
{
DEBUG_SENSOR_LOG(PSTR("AmbientHumidity: %f\n"), SEN5XDATA->ambientHumidity);
DEBUG_SENSOR_LOG(PSTR("AmbientHumidity: %*_f\n"), 2, &SEN5XDATA->ambientHumidity);
}

if (isnan(SEN5XDATA->ambientTemperature))
Expand All @@ -184,7 +183,7 @@ void SEN5XUpdate(void) // Perform every second to ensure proper operation of the
}
else
{
DEBUG_SENSOR_LOG(PSTR("AmbientTemperature: %f\n"), SEN5XDATA->ambientTemperature);
DEBUG_SENSOR_LOG(PSTR("AmbientTemperature: %*_f\n"), 2, &SEN5XDATA->ambientTemperature);
}

if (isnan(SEN5XDATA->vocIndex))
Expand All @@ -193,7 +192,7 @@ void SEN5XUpdate(void) // Perform every second to ensure proper operation of the
}
else
{
DEBUG_SENSOR_LOG(PSTR("VocIndex: %f\n"), SEN5XDATA->vocIndex);
DEBUG_SENSOR_LOG(PSTR("VocIndex: %*_f\n"), 0, &SEN5XDATA->vocIndex);
}

if (isnan(SEN5XDATA->noxIndex))
Expand All @@ -202,19 +201,15 @@ void SEN5XUpdate(void) // Perform every second to ensure proper operation of the
}
else
{
DEBUG_SENSOR_LOG(PSTR("NoxIndex: %f\n"), SEN5XDATA->noxIndex);
DEBUG_SENSOR_LOG(PSTR("NoxIndex: %*_f\n"), 0, &SEN5XDATA->noxIndex);
}
#endif
}
if (!isnan(SEN5XDATA->ambientTemperature) && SEN5XDATA->ambientHumidity > 0) {
SEN5XDATA->abshum = sen5x_AbsoluteHumidity(SEN5XDATA->ambientTemperature, SEN5XDATA->ambientHumidity);
DEBUG_SENSOR_LOG(PSTR("AbsoluteHumidity: %f\n"), SEN5XDATA->abshum);
}
}

#ifdef USE_WEBSERVER
const char HTTP_SNS_SEN5X_UNITS[] PROGMEM = "{s}SEN5X %s{m}%.*f %s{e}";
const char HTTP_SNS_SEN5X_UNITLESS[] PROGMEM = "{s}SEN5X %s{m}%.*f{e}";
const char HTTP_SNS_SEN5X_UNITS[] PROGMEM = "{s}SEN5X %s{m}%*_f %s{e}";
const char HTTP_SNS_SEN5X_UNITLESS[] PROGMEM = "{s}SEN5X %s{m}%*_f{e}";
// {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
const char HTTP_SNS_AHUMSEN5X[] PROGMEM = "{s}SEN5X Abs Humidity{m}%s g/m³{e}";
#endif
Expand All @@ -223,48 +218,49 @@ const char HTTP_SNS_AHUMSEN5X[] PROGMEM = "{s}SEN5X Abs Humidity{m}%s g/m³{e}";

void SEN5XShow(bool json)
{
float convertedTemp = 0;
float convertedHum = 0;
if (SEN5XDATA->sen5x_ready)
{
char sen5x_abs_hum[33];
bool ahum_available = !isnan(SEN5XDATA->ambientTemperature) && (SEN5XDATA->ambientHumidity > 0);
char sen5x_abs_hum[13];
bool ahum_available = !isnan(SEN5XDATA->ambientTemperature) &&
!isnan(SEN5XDATA->ambientHumidity) &&
SEN5XDATA->ambientHumidity > 0;
if (ahum_available)
{
// has humidity + temperature
dtostrfd(SEN5XDATA->abshum, 4, sen5x_abs_hum);
convertedTemp = ConvertTempToFahrenheit(SEN5XDATA->ambientTemperature);
convertedHum = ConvertHumidity(SEN5XDATA->ambientHumidity);
dtostrfd(sen5x_AbsoluteHumidity(convertedTemp,convertedHum), 4, sen5x_abs_hum);
}
if (json)
{
ResponseAppend_P(PSTR(",\"SEN5X\":{"));
ResponseAppend_P(PSTR("\"PM1\":%.1f,"), SEN5XDATA->massConcentrationPm1p0);
ResponseAppend_P(PSTR("\"PM2.5\":%.1f,"), SEN5XDATA->massConcentrationPm2p5);
ResponseAppend_P(PSTR("\"PM4\":%.1f,"), SEN5XDATA->massConcentrationPm4p0);
ResponseAppend_P(PSTR("\"PM10\":%.1f,"), SEN5XDATA->massConcentrationPm10p0);
ResponseAppend_P(PSTR("\"" D_ENVIRONMENTAL_CONCENTRATION "1\":%1_f,"), &SEN5XDATA->massConcentrationPm1p0);
ResponseAppend_P(PSTR("\"" D_ENVIRONMENTAL_CONCENTRATION "2.5\":%1_f,"), &SEN5XDATA->massConcentrationPm2p5);
ResponseAppend_P(PSTR("\"" D_ENVIRONMENTAL_CONCENTRATION "4\":%1_f,"), &SEN5XDATA->massConcentrationPm4p0);
ResponseAppend_P(PSTR("\"" D_ENVIRONMENTAL_CONCENTRATION "10\":%1_f,"), &SEN5XDATA->massConcentrationPm10p0);
if (!isnan(SEN5XDATA->noxIndex))
ResponseAppend_P(PSTR("\"NOx\":%.0f,"), SEN5XDATA->noxIndex);
ResponseAppend_P(PSTR("\"NOx\":%0_f,"), &SEN5XDATA->noxIndex);
if (!isnan(SEN5XDATA->vocIndex))
ResponseAppend_P(PSTR("\"VOC\":%.0f,"), SEN5XDATA->vocIndex);
if (!isnan(SEN5XDATA->ambientTemperature))
ResponseAppendTHD(SEN5XDATA->ambientTemperature, SEN5XDATA->ambientHumidity);
ResponseAppend_P(PSTR("\"VOC\":%0_f,"), &SEN5XDATA->vocIndex);
if (ahum_available)
ResponseAppendTHD(convertedTemp, convertedHum);
ResponseAppend_P(PSTR(",\"" D_JSON_AHUM "\":%s"), sen5x_abs_hum);
ResponseJsonEnd();
}

#ifdef USE_WEBSERVER

WSContentSend_PD(HTTP_SNS_SEN5X_UNITS, "PM1", 1, SEN5XDATA->massConcentrationPm1p0, "μg/m³");
WSContentSend_PD(HTTP_SNS_SEN5X_UNITS, "PM2.5", 1, SEN5XDATA->massConcentrationPm2p5, "μg/m³");
WSContentSend_PD(HTTP_SNS_SEN5X_UNITS, "PM4", 1, SEN5XDATA->massConcentrationPm4p0, "μg/m³");
WSContentSend_PD(HTTP_SNS_SEN5X_UNITS, "PM10", 1, SEN5XDATA->massConcentrationPm10p0, "μg/m³");
WSContentSend_PD(HTTP_SNS_SEN5X_UNITS, PSTR(D_ENVIRONMENTAL_CONCENTRATION "1"), 1, &SEN5XDATA->massConcentrationPm1p0, D_UNIT_MICROGRAM_PER_CUBIC_METER);
WSContentSend_PD(HTTP_SNS_SEN5X_UNITS, PSTR(D_ENVIRONMENTAL_CONCENTRATION "2.5"), 1, &SEN5XDATA->massConcentrationPm2p5, D_UNIT_MICROGRAM_PER_CUBIC_METER);
WSContentSend_PD(HTTP_SNS_SEN5X_UNITS, PSTR(D_ENVIRONMENTAL_CONCENTRATION "4"), 1, &SEN5XDATA->massConcentrationPm4p0, D_UNIT_MICROGRAM_PER_CUBIC_METER);
WSContentSend_PD(HTTP_SNS_SEN5X_UNITS, PSTR(D_ENVIRONMENTAL_CONCENTRATION "10"), 1, &SEN5XDATA->massConcentrationPm10p0, D_UNIT_MICROGRAM_PER_CUBIC_METER);
if (!isnan(SEN5XDATA->noxIndex))
WSContentSend_PD(HTTP_SNS_SEN5X_UNITLESS, "NOx", 0, SEN5XDATA->noxIndex);
WSContentSend_PD(HTTP_SNS_SEN5X_UNITLESS, "NOx", 0, &SEN5XDATA->noxIndex);
if (!isnan(SEN5XDATA->vocIndex))
WSContentSend_PD(HTTP_SNS_SEN5X_UNITLESS, "VOC", 0, SEN5XDATA->vocIndex);
if (!isnan(SEN5XDATA->ambientTemperature))
WSContentSend_PD(HTTP_SNS_SEN5X_UNITS, "Temperature", 2, SEN5XDATA->ambientTemperature, "°C");
if (!isnan(SEN5XDATA->ambientHumidity))
WSContentSend_PD(HTTP_SNS_SEN5X_UNITS, "Humidity", 2, SEN5XDATA->ambientHumidity, "%RH");
WSContentSend_PD(HTTP_SNS_SEN5X_UNITLESS, "VOC", 0, &SEN5XDATA->vocIndex);
if (ahum_available)
WSContentSend_THD(PSTR("SEN5X"), convertedTemp, convertedHum);
WSContentSend_PD(HTTP_SNS_AHUMSEN5X, sen5x_abs_hum);

#endif
Expand Down