mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 00:09:51 +03:00
use floats for sensor values. fix HA for sensors.
This commit is contained in:
@@ -45,7 +45,7 @@ export interface Device {
|
||||
export interface Sensor {
|
||||
no: number;
|
||||
id: string;
|
||||
temp: string;
|
||||
temp: number;
|
||||
}
|
||||
|
||||
export interface EMSESPDevices {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
// note: only for single byte hex values
|
||||
char * Helpers::hextoa(char * result, const uint8_t value) {
|
||||
char * p = result;
|
||||
if (prefix) {
|
||||
*p++ = '0';
|
||||
*p++ = 'x';
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
uint32_t p[] = {0, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000};
|
||||
|
||||
char * ret = result;
|
||||
long whole = (long)value;
|
||||
ltoa(whole, result, 10);
|
||||
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);
|
||||
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<std:
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
} // namespace emsesp
|
||||
|
||||
@@ -31,7 +31,7 @@ namespace emsesp {
|
||||
|
||||
class Helpers {
|
||||
public:
|
||||
static char * hextoa(char * result, const uint8_t value, bool prefix = false);
|
||||
static char * hextoa(char * result, const uint8_t value);
|
||||
static std::string data_to_hex(const uint8_t * data, const uint8_t length);
|
||||
|
||||
static char * render_value(char * result, const float value, const uint8_t format); // format is the precision
|
||||
@@ -47,7 +47,7 @@ class Helpers {
|
||||
|
||||
static char * smallitoa(char * result, const uint8_t value);
|
||||
static char * smallitoa(char * result, const uint16_t value);
|
||||
static char * itoa(char * result, int16_t value, const uint8_t base = 10);
|
||||
static char * itoa(char * result, int32_t value, const uint8_t base = 10);
|
||||
static uint32_t hextoint(const char * hex);
|
||||
static uint16_t atoint(const char * value);
|
||||
static bool check_abs(const int32_t i);
|
||||
|
||||
@@ -184,16 +184,19 @@ int16_t Sensor::get_temperature_c(const uint8_t addr[]) {
|
||||
return EMS_VALUE_SHORT_NOTSET;
|
||||
}
|
||||
YIELD;
|
||||
|
||||
uint8_t scratchpad[SCRATCHPAD_LEN] = {0};
|
||||
bus_.select(addr);
|
||||
bus_.write(CMD_READ_SCRATCHPAD);
|
||||
bus_.read_bytes(scratchpad, SCRATCHPAD_LEN);
|
||||
YIELD;
|
||||
|
||||
if (!bus_.reset()) {
|
||||
LOG_ERROR(F("Bus reset failed after reading scratchpad from %s"), Device(addr).to_string().c_str());
|
||||
return EMS_VALUE_SHORT_NOTSET;
|
||||
}
|
||||
YIELD;
|
||||
|
||||
if (bus_.crc8(scratchpad, SCRATCHPAD_LEN - 1) != scratchpad[SCRATCHPAD_LEN - 1]) {
|
||||
LOG_WARNING(F("Invalid scratchpad CRC: %02X%02X%02X%02X%02X%02X%02X%02X%02X from device %s"),
|
||||
scratchpad[0],
|
||||
@@ -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<EMSESP_MAX_JSON_SIZE_MEDIUM> 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<JsonObject>(), 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<JsonObject>());
|
||||
|
||||
@@ -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") {
|
||||
|
||||
Reference in New Issue
Block a user