Refactor dtostrfd

This commit is contained in:
Theo Arends 2021-01-26 16:26:00 +01:00
parent c5fea7cd0a
commit 20704ab700
15 changed files with 58 additions and 76 deletions

View File

@ -746,6 +746,7 @@ const char S_JSON_DRIVER_INDEX_SVALUE[] PROGMEM = "{\"" D_CMND_DRIVE
const char S_JSON_SVALUE_ACTION_SVALUE[] PROGMEM = "{\"%s\":{\"Action\":\"%s\"}}";
const char JSON_SNS_F_TEMP[] PROGMEM = ",\"%s\":{\"" D_JSON_TEMPERATURE "\":%*_f}";
const char JSON_SNS_TEMP[] PROGMEM = ",\"%s\":{\"" D_JSON_TEMPERATURE "\":%s}";
const char JSON_SNS_ILLUMINANCE[] PROGMEM = ",\"%s\":{\"" D_JSON_ILLUMINANCE "\":%d}";

View File

@ -83,9 +83,7 @@ void ResponseCmndNumber(int value)
void ResponseCmndFloat(float value, uint32_t decimals)
{
char stemp1[TOPSZ];
dtostrfd(value, decimals, stemp1);
Response_P(S_JSON_COMMAND_XVALUE, XdrvMailbox.command, stemp1); // Return float value without quotes
Response_P(PSTR("{\"%s\":%*_f}"), XdrvMailbox.command, decimals, &value); // Return float value without quotes
}
void ResponseCmndIdxNumber(int value)

View File

@ -783,11 +783,15 @@ void WSContentSpaceButton(uint32_t title_index)
WSContentButton(title_index);
}
void WSContentSend_Temp(const char *types, float f_temperature) {
WSContentSend_PD(HTTP_SNS_F_TEMP, types, Settings.flag2.temperature_resolution, &f_temperature, TempUnit());
}
void WSContentSend_THD(const char *types, float f_temperature, float f_humidity)
{
WSContentSend_Temp(types, f_temperature);
char parameter[FLOATSZ];
dtostrfd(f_temperature, Settings.flag2.temperature_resolution, parameter);
WSContentSend_PD(HTTP_SNS_TEMP, types, parameter, TempUnit());
dtostrfd(f_humidity, Settings.flag2.humidity_resolution, parameter);
WSContentSend_PD(HTTP_SNS_HUM, types, parameter);
dtostrfd(CalcTempHumToDew(f_temperature, f_humidity), Settings.flag2.temperature_resolution, parameter);

View File

@ -1303,7 +1303,7 @@ void TuyaSensorsShow(bool json)
if (TuyaGetDpId(sensor) != 0) {
switch (sensor) {
case 71:
WSContentSend_PD(HTTP_SNS_TEMP, "", dtostrfd(Tuya.Sensors[0], Settings.flag2.temperature_resolution, tempval), TempUnit());
WSContentSend_Temp("", Tuya.Sensors[0]);
break;
case 72:
WSContentSend_PD(PSTR("{s}" D_TEMPERATURE " Set{m}%s " D_UNIT_DEGREE "%c{e}"),

View File

@ -435,8 +435,6 @@ void solaxX1Show(bool json)
char pv2_power[33];
dtostrfd(solaxX1.dc2_power, Settings.flag2.wattage_resolution, pv2_power);
#endif
char temperature[33];
dtostrfd(solaxX1.temperature, Settings.flag2.temperature_resolution, temperature);
char runtime[33];
dtostrfd(solaxX1.runtime_total, 0, runtime);
char status[33];
@ -450,12 +448,12 @@ void solaxX1Show(bool json)
ResponseAppend_P(PSTR(",\"" D_JSON_PV2_VOLTAGE "\":%s,\"" D_JSON_PV2_CURRENT "\":%s,\"" D_JSON_PV2_POWER "\":%s"),
pv2_voltage, pv2_current, pv2_power);
#endif
ResponseAppend_P(PSTR(",\"" D_JSON_TEMPERATURE "\":%s,\"" D_JSON_RUNTIME "\":%s,\"" D_JSON_STATUS "\":\"%s\",\"" D_JSON_ERROR "\":%d"),
temperature, runtime, status, solaxX1.errorCode);
ResponseAppend_P(PSTR(",\"" D_JSON_TEMPERATURE "\":%*_f,\"" D_JSON_RUNTIME "\":%s,\"" D_JSON_STATUS "\":\"%s\",\"" D_JSON_ERROR "\":%d"),
Settings.flag2.temperature_resolution, &solaxX1.temperature, runtime, status, solaxX1.errorCode);
#ifdef USE_DOMOTICZ
// Avoid bad temperature report at beginning of the day (spikes of 1200 celsius degrees)
if (0 == TasmotaGlobal.tele_period && solaxX1.temperature < 100) { DomoticzSensor(DZ_TEMP, temperature); }
if (0 == TasmotaGlobal.tele_period && solaxX1.temperature < 100) { DomoticzFloatSensor(DZ_TEMP, solaxX1.temperature); }
#endif // USE_DOMOTICZ
#ifdef USE_WEBSERVER
@ -464,7 +462,7 @@ void solaxX1Show(bool json)
#ifdef SOLAXX1_PV2
WSContentSend_PD(HTTP_SNS_solaxX1_DATA2, pv2_voltage, pv2_current, pv2_power);
#endif
WSContentSend_PD(HTTP_SNS_TEMP, D_SOLAX_X1, temperature, TempUnit());
WSContentSend_Temp(D_SOLAX_X1, solaxX1.temperature);
char errorCodeString[33];
WSContentSend_PD(HTTP_SNS_solaxX1_DATA3, runtime, status,
GetTextIndexed(errorCodeString, sizeof(errorCodeString), solaxX1_ParseErrorCode(solaxX1.errorCode), kSolaxError));

View File

@ -268,14 +268,11 @@ bool Bl0940Command(void) {
}
void Bl0940Show(bool json) {
char temperature[33];
dtostrfd(Bl0940.temperature, Settings.flag2.temperature_resolution, temperature);
if (json) {
ResponseAppend_P(JSON_SNS_TEMP, "BL0940", temperature);
ResponseAppend_P(JSON_SNS_F_TEMP, "BL0940", Settings.flag2.temperature_resolution, &Bl0940.temperature);
if (0 == TasmotaGlobal.tele_period) {
#ifdef USE_DOMOTICZ
DomoticzSensor(DZ_TEMP, temperature);
DomoticzFloatSensor(DZ_TEMP, Bl0940.temperature);
#endif // USE_DOMOTICZ
#ifdef USE_KNX
KnxSensor(KNX_TEMPERATURE, Bl0940.temperature);
@ -283,8 +280,9 @@ void Bl0940Show(bool json) {
}
#ifdef USE_WEBSERVER
} else {
WSContentSend_PD(HTTP_SNS_TEMP, "", temperature, TempUnit());
WSContentSend_Temp("", Bl0940.temperature);
#endif // USE_WEBSERVER
}
}

View File

@ -457,15 +457,12 @@ void AdcShow(bool json) {
break;
}
case ADC_TEMP: {
char temperature[33];
dtostrfd(Adc[idx].temperature, Settings.flag2.temperature_resolution, temperature);
if (json) {
AdcShowContinuation(&jsonflg);
ResponseAppend_P(PSTR("\"" D_JSON_TEMPERATURE "%s\":%s"), adc_idx, temperature);
ResponseAppend_P(PSTR("\"" D_JSON_TEMPERATURE "%s\":%*_f"), adc_idx, Settings.flag2.temperature_resolution, &Adc[idx].temperature);
if ((0 == TasmotaGlobal.tele_period) && (!domo_flag[ADC_TEMP])) {
#ifdef USE_DOMOTICZ
DomoticzSensor(DZ_TEMP, temperature);
DomoticzFloatSensor(DZ_TEMP, Adc[idx].temperature);
domo_flag[ADC_TEMP] = true;
#endif // USE_DOMOTICZ
#ifdef USE_KNX
@ -474,7 +471,7 @@ void AdcShow(bool json) {
}
#ifdef USE_WEBSERVER
} else {
WSContentSend_PD(HTTP_SNS_TEMP, adc_name, temperature, TempUnit());
WSContentSend_Temp(adc_name, Adc[idx].temperature);
#endif // USE_WEBSERVER
}
break;

View File

@ -499,9 +499,6 @@ void Ds18x20Show(bool json)
uint8_t index = ds18x20_sensor[i].index;
if (ds18x20_sensor[index].valid) { // Check for valid temperature
char temperature[33];
dtostrfd(ds18x20_sensor[index].temperature, Settings.flag2.temperature_resolution, temperature);
Ds18x20Name(i);
if (json) {
@ -509,10 +506,11 @@ void Ds18x20Show(bool json)
for (uint32_t j = 0; j < 6; j++) {
sprintf(address+2*j, "%02X", ds18x20_sensor[index].address[6-j]); // Skip sensor type and crc
}
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_ID "\":\"%s\",\"" D_JSON_TEMPERATURE "\":%s}"), ds18x20_types, address, temperature);
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_ID "\":\"%s\",\"" D_JSON_TEMPERATURE "\":%*_f}"),
ds18x20_types, address, Settings.flag2.temperature_resolution, &ds18x20_sensor[index].temperature);
#ifdef USE_DOMOTICZ
if ((0 == TasmotaGlobal.tele_period) && (0 == i)) {
DomoticzSensor(DZ_TEMP, temperature);
DomoticzFloatSensor(DZ_TEMP, ds18x20_sensor[index].temperature);
}
#endif // USE_DOMOTICZ
#ifdef USE_KNX
@ -522,7 +520,7 @@ void Ds18x20Show(bool json)
#endif // USE_KNX
#ifdef USE_WEBSERVER
} else {
WSContentSend_PD(HTTP_SNS_TEMP, ds18x20_types, temperature, TempUnit());
WSContentSend_Temp(ds18x20_types, ds18x20_sensor[index].temperature);
#endif // USE_WEBSERVER
}
}

View File

@ -200,9 +200,6 @@ void Ds18x20Show(bool json)
uint8_t dsxflg = 0;
for (uint32_t i = 0; i < ds18x20_sensors; i++) {
if (Ds18x20Read(i, t)) { // Check if read failed
char temperature[33];
dtostrfd(t, Settings.flag2.temperature_resolution, temperature);
Ds18x20Name(i);
if (json) {
@ -210,11 +207,12 @@ void Ds18x20Show(bool json)
for (uint32_t j = 0; j < 6; j++) {
sprintf(address+2*j, "%02X", ds18x20_address[ds18x20_index[i]][6-j]); // Skip sensor type and crc
}
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_ID "\":\"%s\",\"" D_JSON_TEMPERATURE "\":%s}"), ds18x20_types, address, temperature);
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_ID "\":\"%s\",\"" D_JSON_TEMPERATURE "\":%*_f}"),
ds18x20_types, address, Settings.flag2.temperature_resolution, &t);
dsxflg++;
#ifdef USE_DOMOTICZ
if ((0 == TasmotaGlobal.tele_period) && (1 == dsxflg)) {
DomoticzSensor(DZ_TEMP, temperature);
DomoticzFloatSensor(DZ_TEMP, t);
}
#endif // USE_DOMOTICZ
#ifdef USE_KNX
@ -224,7 +222,7 @@ void Ds18x20Show(bool json)
#endif // USE_KNX
#ifdef USE_WEBSERVER
} else {
WSContentSend_PD(HTTP_SNS_TEMP, ds18x20_types, temperature, TempUnit());
WSContentSend_Temp(ds18x20_types, t);
#endif // USE_WEBSERVER
}
}

View File

@ -527,8 +527,6 @@ void BmpShow(bool json)
snprintf_P(name, sizeof(name), PSTR("%s%c%02X"), name, IndexSeparator(), bmp_sensors[bmp_idx].bmp_address); // BMXXXX-XX
}
char temperature[33];
dtostrfd(bmp_temperature, Settings.flag2.temperature_resolution, temperature);
char pressure[33];
dtostrfd(bmp_pressure, Settings.flag2.pressure_resolution, pressure);
char sea_pressure[33];
@ -554,16 +552,16 @@ void BmpShow(bool json)
char json_gas[40];
snprintf_P(json_gas, sizeof(json_gas), PSTR(",\"" D_JSON_GAS "\":%s"), gas_resistance);
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_TEMPERATURE "\":%s%s,\"" D_JSON_PRESSURE "\":%s%s%s}"),
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_TEMPERATURE "\":%*_f%s,\"" D_JSON_PRESSURE "\":%s%s%s}"),
name,
temperature,
Settings.flag2.temperature_resolution, &bmp_temperature,
(bmp_sensors[bmp_idx].bmp_model >= 2) ? json_humidity : "",
pressure,
(Settings.altitude != 0) ? json_sealevel : "",
(bmp_sensors[bmp_idx].bmp_model >= 3) ? json_gas : "");
#else
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_TEMPERATURE "\":%s%s,\"" D_JSON_PRESSURE "\":%s%s}"),
name, temperature, (bmp_sensors[bmp_idx].bmp_model >= 2) ? json_humidity : "", pressure, (Settings.altitude != 0) ? json_sealevel : "");
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_TEMPERATURE "\":%*_f%s,\"" D_JSON_PRESSURE "\":%s%s}"),
name, Settings.flag2.temperature_resolution, &bmp_temperature, (bmp_sensors[bmp_idx].bmp_model >= 2) ? json_humidity : "", pressure, (Settings.altitude != 0) ? json_sealevel : "");
#endif // USE_BME680
#ifdef USE_DOMOTICZ
@ -584,7 +582,7 @@ void BmpShow(bool json)
#ifdef USE_WEBSERVER
} else {
WSContentSend_PD(HTTP_SNS_TEMP, name, temperature, TempUnit());
WSContentSend_Temp(name, bmp_temperature);
if (bmp_sensors[bmp_idx].bmp_model >= 2) {
WSContentSend_PD(HTTP_SNS_HUM, name, humidity);
WSContentSend_PD(HTTP_SNS_DEW, name, dewpoint, TempUnit());

View File

@ -338,23 +338,22 @@ void MhzInit(void)
void MhzShow(bool json)
{
char types[7] = "MHZ19B"; // MHZ19B for legacy reasons. Prefered is MHZ19
char temperature[33];
dtostrfd(mhz_temperature, Settings.flag2.temperature_resolution, temperature);
char model[3];
GetTextIndexed(model, sizeof(model), mhz_type -1, kMhzModels);
if (json) {
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_MODEL "\":\"%s\",\"" D_JSON_CO2 "\":%d,\"" D_JSON_TEMPERATURE "\":%s}"), types, model, mhz_last_ppm, temperature);
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_MODEL "\":\"%s\",\"" D_JSON_CO2 "\":%d,\"" D_JSON_TEMPERATURE "\":%*_f}"),
types, model, mhz_last_ppm, Settings.flag2.temperature_resolution, &mhz_temperature);
#ifdef USE_DOMOTICZ
if (0 == TasmotaGlobal.tele_period) {
DomoticzSensor(DZ_AIRQUALITY, mhz_last_ppm);
DomoticzSensor(DZ_TEMP, temperature);
DomoticzFloatSensor(DZ_TEMP, mhz_temperature);
}
#endif // USE_DOMOTICZ
#ifdef USE_WEBSERVER
} else {
WSContentSend_PD(HTTP_SNS_CO2, types, mhz_last_ppm);
WSContentSend_PD(HTTP_SNS_TEMP, types, temperature, TempUnit());
WSContentSend_Temp(types, mhz_temperature);
#endif // USE_WEBSERVER
}
}

View File

@ -85,17 +85,15 @@ float LM75ADGetTemp(void)
void LM75ADShow(bool json)
{
float t = LM75ADGetTemp();
char temperature[33];
dtostrfd(t, Settings.flag2.temperature_resolution, temperature);
if (json) {
ResponseAppend_P(JSON_SNS_TEMP, "LM75AD", temperature);
ResponseAppend_P(JSON_SNS_F_TEMP, "LM75AD", Settings.flag2.temperature_resolution, &t);
#ifdef USE_DOMOTICZ
if (0 == TasmotaGlobal.tele_period) DomoticzSensor(DZ_TEMP, temperature);
if (0 == TasmotaGlobal.tele_period) DomoticzFloatSensor(DZ_TEMP, t);
#endif // USE_DOMOTICZ
#ifdef USE_WEBSERVER
} else {
WSContentSend_PD(HTTP_SNS_TEMP, "LM75AD", temperature, TempUnit());
WSContentSend_Temp("LM75AD", t);
#endif // USE_WEBSERVER
}
}

View File

@ -182,8 +182,6 @@ void MPU_6050Show(bool json)
MPU_6050PerformReading();
float tempConv = ConvertTemp(MPU_6050_temperature / 340.0 + 35.53);
char temperature[33];
dtostrfd(tempConv, Settings.flag2.temperature_resolution, temperature);
char axis_ax[33];
dtostrfd(MPU_6050_ax, Settings.flag2.axis_resolution, axis_ax);
char axis_ay[33];
@ -225,19 +223,19 @@ void MPU_6050Show(bool json)
snprintf_P(json_ypr_p, sizeof(json_ypr_p), PSTR(",\"" D_JSON_PITCH "\":%s"), axis_pitch);
char json_ypr_r[25];
snprintf_P(json_ypr_r, sizeof(json_ypr_r), PSTR(",\"" D_JSON_ROLL "\":%s"), axis_roll);
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_TEMPERATURE "\":%s%s%s%s%s%s%s%s%s%s}"),
D_SENSOR_MPU6050, temperature, json_axis_ax, json_axis_ay, json_axis_az, json_axis_gx, json_axis_gy, json_axis_gz,
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_TEMPERATURE "\":%*_f%s%s%s%s%s%s%s%s%s}"),
D_SENSOR_MPU6050, Settings.flag2.temperature_resolution, &tempConv, json_axis_ax, json_axis_ay, json_axis_az, json_axis_gx, json_axis_gy, json_axis_gz,
json_ypr_y, json_ypr_p, json_ypr_r);
#else
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_TEMPERATURE "\":%s%s%s%s%s%s%s}"),
D_SENSOR_MPU6050, temperature, json_axis_ax, json_axis_ay, json_axis_az, json_axis_gx, json_axis_gy, json_axis_gz);
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_TEMPERATURE "\":%*_f%s%s%s%s%s%s}"),
D_SENSOR_MPU6050, Settings.flag2.temperature_resolution, &tempConv, json_axis_ax, json_axis_ay, json_axis_az, json_axis_gx, json_axis_gy, json_axis_gz);
#endif // USE_MPU6050_DMP
#ifdef USE_DOMOTICZ
DomoticzSensor(DZ_TEMP, temperature);
DomoticzFloatSensor(DZ_TEMP, tempConv);
#endif // USE_DOMOTICZ
#ifdef USE_WEBSERVER
} else {
WSContentSend_PD(HTTP_SNS_TEMP, D_SENSOR_MPU6050, temperature, TempUnit());
WSContentSend_Temp(D_SENSOR_MPU6050, tempConv);
WSContentSend_PD(HTTP_SNS_AXIS, axis_ax, axis_ay, axis_az, axis_gx, axis_gy, axis_gz);
#ifdef USE_MPU6050_DMP
WSContentSend_PD(HTTP_SNS_YPR, axis_yaw, axis_pitch, axis_roll);

View File

@ -272,22 +272,21 @@ void RfSnsTheoV2Show(bool json)
sensor, GetDT(rfsns_theo_v2_t1[i].time).c_str(), voltage);
}
} else {
char temperature[33];
dtostrfd(ConvertTemp((float)rfsns_theo_v2_t1[i].temp / 100), Settings.flag2.temperature_resolution, temperature);
float temp = ConvertTemp((float)rfsns_theo_v2_t1[i].temp / 100)
if (json) {
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_TEMPERATURE "\":%s,\"" D_JSON_ILLUMINANCE "\":%d,\"" D_JSON_VOLTAGE "\":%s}"),
sensor, temperature, rfsns_theo_v2_t1[i].lux, voltage);
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_TEMPERATURE "\":%*_f,\"" D_JSON_ILLUMINANCE "\":%d,\"" D_JSON_VOLTAGE "\":%s}"),
sensor, Settings.flag2.temperature_resolution, &temp, rfsns_theo_v2_t1[i].lux, voltage);
#ifdef USE_DOMOTICZ
if ((0 == TasmotaGlobal.tele_period) && !sensor_once) {
DomoticzSensor(DZ_TEMP, temperature);
DomoticzFloatSensor(DZ_TEMP, temp);
DomoticzSensor(DZ_ILLUMINANCE, rfsns_theo_v2_t1[i].lux);
sensor_once = true;
}
#endif // USE_DOMOTICZ
#ifdef USE_WEBSERVER
} else {
WSContentSend_PD(HTTP_SNS_TEMP, sensor, temperature, TempUnit());
WSContentSend_Temp(sensor, temp);
WSContentSend_PD(HTTP_SNS_ILLUMINANCE, sensor, rfsns_theo_v2_t1[i].lux);
#endif // USE_WEBSERVER
}

View File

@ -139,20 +139,18 @@ void MAX31855_GetResult(void) {
}
void MAX31855_Show(bool Json) {
char probetemp[33];
char referencetemp[33];
dtostrfd(MAX31855_Result.ProbeTemperature, Settings.flag2.temperature_resolution, probetemp);
dtostrfd(MAX31855_Result.ReferenceTemperature, Settings.flag2.temperature_resolution, referencetemp);
char sensor_name[10];
GetTextIndexed(sensor_name, sizeof(sensor_name), Settings.flag4.max6675, kMax31855Types);
if (Json) {
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_PROBETEMPERATURE "\":%s,\"" D_JSON_REFERENCETEMPERATURE "\":%s,\"" D_JSON_ERROR "\":%d}"), \
sensor_name, probetemp, referencetemp, MAX31855_Result.ErrorCode);
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_PROBETEMPERATURE "\":%*_f,\"" D_JSON_REFERENCETEMPERATURE "\":%*_f,\"" D_JSON_ERROR "\":%d}"), \
sensor_name,
Settings.flag2.temperature_resolution, &MAX31855_Result.ProbeTemperature,
Settings.flag2.temperature_resolution, &MAX31855_Result.ReferenceTemperature,
MAX31855_Result.ErrorCode);
#ifdef USE_DOMOTICZ
if (0 == TasmotaGlobal.tele_period) {
DomoticzSensor(DZ_TEMP, probetemp);
DomoticzFloatSensor(DZ_TEMP, MAX31855_Result.ProbeTemperature);
}
#endif // USE_DOMOTICZ
#ifdef USE_KNX
@ -162,7 +160,7 @@ void MAX31855_Show(bool Json) {
#endif // USE_KNX
#ifdef USE_WEBSERVER
} else {
WSContentSend_PD(HTTP_SNS_TEMP, sensor_name, probetemp, TempUnit());
WSContentSend_Temp(sensor_name, MAX31855_Result.ProbeTemperature);
#endif // USE_WEBSERVER
}
}