diff --git a/interface/src/project/EMSESPtypes.ts b/interface/src/project/EMSESPtypes.ts index 80ce48159..748a01bee 100644 --- a/interface/src/project/EMSESPtypes.ts +++ b/interface/src/project/EMSESPtypes.ts @@ -45,7 +45,7 @@ export interface Device { export interface Sensor { no: number; id: string; - temp: string; + temp: number; } export interface EMSESPDevices { diff --git a/src/EMSESPDevicesService.cpp b/src/EMSESPDevicesService.cpp index 7b42926d9..541550b52 100644 --- a/src/EMSESPDevicesService.cpp +++ b/src/EMSESPDevicesService.cpp @@ -64,14 +64,12 @@ void EMSESPDevicesService::all_devices(AsyncWebServerRequest * request) { JsonArray sensors = root.createNestedArray("sensors"); if (!EMSESP::sensor_devices().empty()) { - char s[7]; uint8_t i = 1; for (const auto & sensor : EMSESP::sensor_devices()) { JsonObject obj = sensors.createNestedObject(); - obj["no"] = i; + obj["no"] = i++; obj["id"] = sensor.to_string(); - obj["temp"] = Helpers::render_value(s, sensor.temperature_c, 10); - i++; + obj["temp"] = (float)(sensor.temperature_c) / 10; } } diff --git a/src/helpers.cpp b/src/helpers.cpp index 58afc0204..ebf9b1422 100644 --- a/src/helpers.cpp +++ b/src/helpers.cpp @@ -23,13 +23,9 @@ namespace emsesp { uint8_t Helpers::bool_format_ = BOOL_FORMAT_ONOFF; // on/off // like itoa but for hex, and quicker -// only for single byte hex values -char * Helpers::hextoa(char * result, const uint8_t value, bool prefix) { - char * p = result; - if (prefix) { - *p++ = '0'; - *p++ = 'x'; - } +// note: only for single byte hex values +char * Helpers::hextoa(char * result, const uint8_t value) { + char * p = result; uint8_t nib1 = (value >> 4) & 0x0F; uint8_t nib2 = (value >> 0) & 0x0F; *p++ = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA; @@ -79,7 +75,7 @@ char * Helpers::ultostr(char * ptr, uint32_t value, const uint8_t base) { * itoa for 2 byte signed (short) integers * written by Lukás Chmela, Released under GPLv3. http://www.strudel.org.uk/itoa/ version 0.4 */ -char * Helpers::itoa(char * result, int16_t value, const uint8_t base) { +char * Helpers::itoa(char * result, int32_t value, const uint8_t base) { // check that the base if valid if (base < 2 || base > 36) { *result = '\0'; @@ -171,7 +167,7 @@ char * Helpers::render_value(char * result, uint8_t value, uint8_t format) { if (value == EMS_VALUE_BOOL_OFF) { render_boolean(result, false); } else if (value == EMS_VALUE_BOOL_NOTSET) { - result[0] = '\0'; + return nullptr; } else { render_boolean(result, true); // assume on. could have value 0x01 or 0xFF } @@ -179,8 +175,7 @@ char * Helpers::render_value(char * result, uint8_t value, uint8_t format) { } if (!hasValue(value)) { - result[0] = '\0'; - return result; + return nullptr; } if (!format) { @@ -188,7 +183,7 @@ char * Helpers::render_value(char * result, uint8_t value, uint8_t format) { return result; } - char s2[5]; + char s2[10]; // special case for / 2 if (format == 2) { @@ -208,24 +203,23 @@ char * Helpers::render_value(char * result, uint8_t value, uint8_t format) { // float: convert float to char // format is the precision, 0 to 8 char * Helpers::render_value(char * result, const float value, const uint8_t format) { - long p[] = {0, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000}; - - result[0] = '\0'; - if (value == NAN || format > 8) { - return result; + if (format > 8) { + return nullptr; } - char * ret = result; - long whole = (long)value; - ltoa(whole, result, 10); + uint32_t p[] = {0, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000}; + + char * ret = result; + int32_t whole = (int32_t)value; + itoa(result, whole, 10); while (*result != '\0') { result++; } - *result++ = '.'; - long decimal = abs((long)((value - whole) * p[format])); - ltoa(decimal, result, 10); + *result++ = '.'; + int32_t decimal = abs((int32_t)((value - whole) * p[format])); + itoa(result, decimal, 10); return ret; } @@ -233,9 +227,8 @@ char * Helpers::render_value(char * result, const float value, const uint8_t for // int16: convert short (two bytes) to text string and returns string // format: 0=no division, other divide by the value given and render with a decimal point char * Helpers::render_value(char * result, const int16_t value, const uint8_t format) { - result[0] = '\0'; if (!hasValue(value)) { - return result; + return nullptr; } // just print it if no conversion required (format = 0) @@ -245,6 +238,7 @@ char * Helpers::render_value(char * result, const int16_t value, const uint8_t f } int16_t new_value = value; + result[0] = '\0'; // check for negative values if (new_value < 0) { @@ -273,8 +267,7 @@ char * Helpers::render_value(char * result, const int16_t value, const uint8_t f // uint16: convert unsigned short (two bytes) to text string and prints it char * Helpers::render_value(char * result, const uint16_t value, const uint8_t format) { if (!hasValue(value)) { - result[0] = '\0'; - return result; + return nullptr; } return (render_value(result, (int16_t)value, format)); // use same code, force it to a signed int } @@ -282,8 +275,7 @@ char * Helpers::render_value(char * result, const uint16_t value, const uint8_t // int8: convert signed byte to text string and prints it char * Helpers::render_value(char * result, const int8_t value, const uint8_t format) { if (!hasValue(value)) { - result[0] = '\0'; - return result; + return nullptr; } return (render_value(result, (int16_t)value, format)); // use same code, force it to a signed int } @@ -291,8 +283,7 @@ char * Helpers::render_value(char * result, const int8_t value, const uint8_t fo // uint32: render long (4 byte) unsigned values char * Helpers::render_value(char * result, const uint32_t value, const uint8_t format) { if (!hasValue(value)) { - result[0] = '\0'; - return result; + return nullptr; } char s[20]; @@ -480,5 +471,4 @@ bool Helpers::value2enum(const char * v, uint8_t & value, const std::vector> 48) & 0xFF, (unsigned int)(id_ >> 32) & 0xFFFF, (unsigned int)(id_ >> 16) & 0xFFFF, - (unsigned int)(id_) & 0xFFFF); + (unsigned int)(id_)&0xFFFF); return str; } @@ -328,27 +331,31 @@ void Sensor::publish_values() { return; } */ + DynamicJsonDocument doc(100 * num_devices); - uint8_t i = 1; // sensor count + uint8_t sensor_no = 1; // sensor count for (const auto & device : devices_) { - char s[7]; // for rendering temperature char sensorID[10]; // sensor{1-n} - snprintf_P(sensorID, 10, PSTR("sensor%d"), i); + snprintf_P(sensorID, 10, PSTR("sensor%d"), sensor_no); if (mqtt_format_ == Mqtt::Format::SINGLE) { // e.g. sensor_data = {"sensor1":23.3,"sensor2":24.0} - doc[sensorID] = Helpers::render_value(s, device.temperature_c, 10); + // doc[sensorID] = Helpers::render_value(s, device.temperature_c, 10); + doc[sensorID] = (float)(device.temperature_c) / 10; } else if (mqtt_format_ == Mqtt::Format::NESTED) { // e.g. sensor_data = {"sensor1":{"id":"28-EA41-9497-0E03","temp":"23.3"},"sensor2":{"id":"28-233D-9497-0C03","temp":"24.0"}} JsonObject dataSensor = doc.createNestedObject(sensorID); dataSensor["id"] = device.to_string(); - dataSensor["temp"] = Helpers::render_value(s, device.temperature_c, 10); + // dataSensor["temp"] = Helpers::render_value(s, device.temperature_c, 10); + dataSensor["temp"] = (float)(device.temperature_c) / 10; } else if (mqtt_format_ == Mqtt::Format::HA) { // e.g. sensor_data = {"28-EA41-9497-0E03":23.3,"28-233D-9497-0C03":24.0} - doc[device.to_string()] = Helpers::render_value(s, device.temperature_c, 10); - + // doc[device.to_string()] = Helpers::render_value(s, device.temperature_c, 10); + JsonObject dataSensor = doc.createNestedObject(sensorID); + dataSensor["id"] = device.to_string(); + dataSensor["temp"] = (float)(device.temperature_c) / 10; // create the config if this hasn't already been done - // to e.g. homeassistant/sensor/ems-esp/dallas_sensor1/config - if (!(registered_ha_[i])) { + // to e.g. homeassistant/sensor/ems-esp/dallas_28-233D-9497-0C03/config + if (!(registered_ha_[sensor_no - 1])) { StaticJsonDocument config; config["dev_cla"] = F("temperature"); @@ -359,11 +366,11 @@ void Sensor::publish_values() { config["unit_of_meas"] = F("°C"); char str[50]; - snprintf_P(str, sizeof(str), PSTR("{{value_json.%s}}"), device.to_string().c_str()); + snprintf_P(str, sizeof(str), PSTR("{{value_json.sensor%d.temp}}"), sensor_no); config["val_tpl"] = str; // name as sensor number not the long unique ID - snprintf_P(str, sizeof(str), PSTR("Dallas Sensor %d"), i); + snprintf_P(str, sizeof(str), PSTR("Dallas Sensor %d"), sensor_no); config["name"] = str; snprintf_P(str, sizeof(str), PSTR("dallas_%s"), device.to_string().c_str()); @@ -377,10 +384,10 @@ void Sensor::publish_values() { snprintf_P(&topic[0], 100, PSTR("homeassistant/sensor/ems-esp/dallas_%s/config"), device.to_string().c_str()); Mqtt::publish_retain(topic, config.as(), true); // publish the config payload with retain flag - registered_ha_[i] = true; + registered_ha_[sensor_no - 1] = true; } } - i++; // increment sensor count + sensor_no++; // increment sensor count } Mqtt::publish(F("sensor_data"), doc.as()); diff --git a/src/test/test.cpp b/src/test/test.cpp index 8b7e4fc78..cfbda7164 100644 --- a/src/test/test.cpp +++ b/src/test/test.cpp @@ -27,7 +27,7 @@ namespace emsesp { // used with the 'test' command, under su/admin void Test::run_test(uuid::console::Shell & shell, const std::string & command) { if (command == "default") { - run_test(shell, "mixing"); // add the default test case here + run_test(shell, "render"); // add the default test case here } if (command.empty()) { @@ -122,6 +122,11 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & command) { uint8bitb = EMS_VALUE_UINT_NOTSET; telegram->read_bitvalue(uint8bitb, 0, 0); // value is 0x01 = 0000 0001 shell.printfln("uint8 bit read: expecting 1, got:%d", uint8bitb); + + float test_float = 20.56; + char result[100]; + Helpers::render_value(result, test_float, 2); + shell.printfln("Float test from %f to %s", test_float, result); } if (command == "devices") {