From 64ed79debc29246e681cf54794bcbc7e2ee7d94e Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Tue, 8 Nov 2022 16:16:15 +0100 Subject: [PATCH] Make distance floats with one decimal (#17021) --- tasmota/include/i18n.h | 1 + tasmota/tasmota_xsns_sensor/xsns_22_sr04.ino | 9 +++------ tasmota/tasmota_xsns_sensor/xsns_64_hrxl.ino | 9 +++++---- tasmota/tasmota_xsns_sensor/xsns_76_dyp.ino | 9 +++++---- tasmota/tasmota_xsns_sensor/xsns_86_tfminiplus.ino | 14 ++++++-------- 5 files changed, 20 insertions(+), 22 deletions(-) diff --git a/tasmota/include/i18n.h b/tasmota/include/i18n.h index 1c46f3df4..5fe199aaa 100644 --- a/tasmota/include/i18n.h +++ b/tasmota/include/i18n.h @@ -901,6 +901,7 @@ const char HTTP_SNS_RANGE_CHR[] PROGMEM = "{s}%s " D_RANGE "{ const char HTTP_SNS_RANGE[] PROGMEM = "{s}%s " D_RANGE "{m}%d" "{e}"; const char HTTP_SNS_DISTANCE[] PROGMEM = "{s}%s " D_DISTANCE "{m}%d " D_UNIT_MILLIMETER "{e}"; const char HTTP_SNS_DISTANCE_CM[] PROGMEM = "{s}%s " D_DISTANCE "{m}%s " D_UNIT_CENTIMETER "{e}"; +const char HTTP_SNS_F_DISTANCE_CM[] PROGMEM = "{s}%s " D_DISTANCE "{m}%1_f " D_UNIT_CENTIMETER "{e}"; const char HTTP_SNS_HALL_EFFECT[] PROGMEM = "{s}%s " D_HALL_EFFECT "{m}%d" "{e}"; const char HTTP_SNS_VOLTAGE[] PROGMEM = "{s}" D_VOLTAGE "{m}%s " D_UNIT_VOLT "{e}"; const char HTTP_SNS_CURRENT[] PROGMEM = "{s}" D_CURRENT "{m}%s " D_UNIT_AMPERE "{e}"; diff --git a/tasmota/tasmota_xsns_sensor/xsns_22_sr04.ino b/tasmota/tasmota_xsns_sensor/xsns_22_sr04.ino index 85c13a97b..df1764430 100644 --- a/tasmota/tasmota_xsns_sensor/xsns_22_sr04.ino +++ b/tasmota/tasmota_xsns_sensor/xsns_22_sr04.ino @@ -190,19 +190,16 @@ void Sr04TReading(void) { void Sr04Show(bool json) { if (SR04.valid) { // Check if read failed - char distance_chr[33]; - dtostrfd(SR04.distance, 3, distance_chr); - if(json) { - ResponseAppend_P(PSTR(",\"SR04\":{\"" D_JSON_DISTANCE "\":%s}"), distance_chr); + ResponseAppend_P(PSTR(",\"SR04\":{\"" D_JSON_DISTANCE "\":%1_f}"), &SR04.distance); #ifdef USE_DOMOTICZ if (0 == TasmotaGlobal.tele_period) { - DomoticzSensor(DZ_COUNT, distance_chr); // Send distance as Domoticz Counter value + DomoticzFloatSensor(DZ_COUNT, SR04.distance); // Send distance as Domoticz Counter value } #endif // USE_DOMOTICZ #ifdef USE_WEBSERVER } else { - WSContentSend_PD(HTTP_SNS_DISTANCE_CM, "SR04", distance_chr); + WSContentSend_PD(HTTP_SNS_F_DISTANCE_CM, "SR04", &SR04.distance); #endif // USE_WEBSERVER } } diff --git a/tasmota/tasmota_xsns_sensor/xsns_64_hrxl.ino b/tasmota/tasmota_xsns_sensor/xsns_64_hrxl.ino index 9d2a60682..422279cfe 100644 --- a/tasmota/tasmota_xsns_sensor/xsns_64_hrxl.ino +++ b/tasmota/tasmota_xsns_sensor/xsns_64_hrxl.ino @@ -32,7 +32,7 @@ #include TasmotaSerial *HRXLSerial = nullptr; -uint32_t hrxl_distance_cm = 0; // distance, cm +uint32_t hrxl_distance_mm = 0; // distance, mm /*********************************************************************************************/ @@ -62,17 +62,18 @@ void HRXLEverySecond(void) { } } if (num_read > 1) { - hrxl_distance_cm = int(sum / num_read) / 10; // cm + hrxl_distance_mm = int(sum / num_read); // mm } } void HRXLShow(bool json) { char types[5] = "HRXL"; + float distance = (float)hrxl_distance_mm / 10; // cm if (json) { - ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_DISTANCE "\":%d}"), types, hrxl_distance_cm); + ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_DISTANCE "\":%1_f}"), types, &distance); #ifdef USE_WEBSERVER } else { - WSContentSend_PD(HTTP_SNS_DISTANCE_CM, types, hrxl_distance_cm); + WSContentSend_PD(HTTP_SNS_F_DISTANCE_CM, types, &distance); #endif // USE_WEBSERVER } } diff --git a/tasmota/tasmota_xsns_sensor/xsns_76_dyp.ino b/tasmota/tasmota_xsns_sensor/xsns_76_dyp.ino index 3956e42d8..9c4124fe8 100644 --- a/tasmota/tasmota_xsns_sensor/xsns_76_dyp.ino +++ b/tasmota/tasmota_xsns_sensor/xsns_76_dyp.ino @@ -41,7 +41,7 @@ TasmotaSerial *DYPSerial = nullptr; #define DYP_ABOVEMAX 4999 #define DYP_NOSENSOR 5999 -uint16_t DYPDistance = 0; // distance in centimeters +uint16_t DYPDistance = 0; // distance in millimeters /*********************************************************************************************/ @@ -87,7 +87,7 @@ void DYPEverySecond(void) { if (data > DYP_MAX) { data = DYP_ABOVEMAX; } - DYPDistance = data / 10; // cm + DYPDistance = data; // mm } else { DYPDistance = DYP_CRCERROR; } @@ -97,11 +97,12 @@ void DYPEverySecond(void) { void DYPShow(bool json) { char types[4] = "DYP"; + float distance = (float)DYPDistance / 10; // cm if (json) { - ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_DISTANCE "\":%d}"), types, DYPDistance); + ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_DISTANCE "\":%1_f}"), types, &distance); #ifdef USE_WEBSERVER } else { - WSContentSend_PD(HTTP_SNS_DISTANCE_CM, types, DYPDistance); + WSContentSend_PD(HTTP_SNS_F_DISTANCE_CM, types, &distance); #endif // USE_WEBSERVER } } diff --git a/tasmota/tasmota_xsns_sensor/xsns_86_tfminiplus.ino b/tasmota/tasmota_xsns_sensor/xsns_86_tfminiplus.ino index 0f31032e0..6b7963f90 100644 --- a/tasmota/tasmota_xsns_sensor/xsns_86_tfminiplus.ino +++ b/tasmota/tasmota_xsns_sensor/xsns_86_tfminiplus.ino @@ -185,24 +185,22 @@ const char HTTP_SNS_SIGNALSTRENGTH[] PROGMEM = "{s}%s " D_SIGNALSTRENGTH "{m}%d{ const char HTTP_SNS_CHIPTEMPERATURE[] PROGMEM = "{s}%s " D_CHIPTEMPERATURE "{m}%d " D_UNIT_DEGREE "%c{e}"; #endif // USE_WEBSERVER -void TfmpShow(bool json) -{ +void TfmpShow(bool json) { char sensor_name[12]; strcpy_P(sensor_name, "TFminiPlus"); - char distance_chr[FLOATSZ]; - dtostrfd(tfminiplus_sensor.distance, 3, distance_chr); + float distance = (float)tfminiplus_sensor.distance; // cm if (json) { - ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_DISTANCE "\":\"%s\",\"" D_JSON_SIGNALSTRENGTH "\":\"%d\",\"" D_JSON_CHIPTEMPERATURE "\":%d}"), - sensor_name, distance_chr, tfminiplus_sensor.sigstrength, tfminiplus_sensor.chiptemp); + ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_DISTANCE "\":\"%1_f\",\"" D_JSON_SIGNALSTRENGTH "\":\"%d\",\"" D_JSON_CHIPTEMPERATURE "\":%d}"), + sensor_name, &distance, tfminiplus_sensor.sigstrength, tfminiplus_sensor.chiptemp); #ifdef USE_DOMOTICZ if (0 == TasmotaGlobal.tele_period) { - DomoticzSensor(DZ_COUNT, distance_chr); + DomoticzFloatSensor(DZ_COUNT, distance); } #endif // USE_DOMOTICZ #ifdef USE_WEBSERVER } else { - WSContentSend_P(HTTP_SNS_DISTANCE_CM, sensor_name, distance_chr); + WSContentSend_P(HTTP_SNS_F_DISTANCE_CM, sensor_name, &distance); WSContentSend_P(HTTP_SNS_SIGNALSTRENGTH, sensor_name, tfminiplus_sensor.sigstrength); WSContentSend_P(HTTP_SNS_CHIPTEMPERATURE, sensor_name, tfminiplus_sensor.chiptemp, TempUnit()); #endif // USE_WEBSERVER