diff --git a/interface/src/project/EMSESPDevicesForm.tsx b/interface/src/project/EMSESPDevicesForm.tsx
index 551bebda2..94c4c1ac8 100644
--- a/interface/src/project/EMSESPDevicesForm.tsx
+++ b/interface/src/project/EMSESPDevicesForm.tsx
@@ -179,18 +179,22 @@ class EMSESPDevicesForm extends Component<
- ID
+ Sensor no.
+ ID
Temperature
{data.sensors.map((sensorData) => (
-
+
+ Sensor {sensorData.no}
+
+
{sensorData.id}
- {sensorData.temp.toFixed(1)} °C
+ {sensorData.temp} °C
))}
diff --git a/interface/src/project/EMSESPtypes.ts b/interface/src/project/EMSESPtypes.ts
index 93f921671..80ce48159 100644
--- a/interface/src/project/EMSESPtypes.ts
+++ b/interface/src/project/EMSESPtypes.ts
@@ -43,8 +43,9 @@ export interface Device {
}
export interface Sensor {
+ no: number;
id: string;
- temp: number;
+ temp: string;
}
export interface EMSESPDevices {
diff --git a/src/EMSESPDevicesService.cpp b/src/EMSESPDevicesService.cpp
index 8bfd0cb66..7b42926d9 100644
--- a/src/EMSESPDevicesService.cpp
+++ b/src/EMSESPDevicesService.cpp
@@ -64,10 +64,14 @@ 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["id"] = sensor.to_string();
- obj["temp"] = sensor.temperature_c;
+ obj["temp"] = Helpers::render_value(s, sensor.temperature_c, 10);
+ i++;
}
}
diff --git a/src/devices/boiler.cpp b/src/devices/boiler.cpp
index f11c96e54..bedac8237 100644
--- a/src/devices/boiler.cpp
+++ b/src/devices/boiler.cpp
@@ -256,11 +256,11 @@ bool Boiler::export_values(JsonObject & output) {
// Warm Water comfort setting
if (Helpers::hasValue(wWComfort_)) {
if (wWComfort_ == 0x00) {
- output["wWComfort"] = "Hot";
+ output["wWComfort"] = F("Hot");
} else if (wWComfort_ == 0xD8) {
- output["wWComfort"] = "Eco";
+ output["wWComfort"] = F("Eco");
} else if (wWComfort_ == 0xEC) {
- output["wWComfort"] = "Intelligent";
+ output["wWComfort"] = F("Intelligent");
}
}
@@ -319,7 +319,7 @@ bool Boiler::export_values(JsonObject & output) {
// Warm Water charging type
if (Helpers::hasValue(wWChargeType_, EMS_VALUE_BOOL)) {
- output["wWChargeType"] = wWChargeType_ ? F("3way valve") : F("charge pump");
+ output["wWChargeType"] = wWChargeType_ ? F("3way_valve") : F("chargepump");
}
// Warm Water circulation pump available bool
diff --git a/src/emsesp.cpp b/src/emsesp.cpp
index 6294e9cf3..aac5c4814 100644
--- a/src/emsesp.cpp
+++ b/src/emsesp.cpp
@@ -279,8 +279,10 @@ void EMSESP::show_sensor_values(uuid::console::Shell & shell) {
char valuestr[8] = {0}; // for formatting temp
shell.printfln(F("Dallas temperature sensors:"));
+ uint8_t i = 1;
for (const auto & device : sensor_devices()) {
- shell.printfln(F(" ID: %s, Temperature: %s°C"), device.to_string().c_str(), Helpers::render_value(valuestr, device.temperature_c, 1));
+ shell.printfln(F(" Sensor %d, ID: %s, Temperature: %s °C"), i, device.to_string().c_str(), Helpers::render_value(valuestr, device.temperature_c, 10));
+ i++;
}
shell.println();
}
diff --git a/src/helpers.cpp b/src/helpers.cpp
index eadd33414..58afc0204 100644
--- a/src/helpers.cpp
+++ b/src/helpers.cpp
@@ -171,7 +171,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) {
- return nullptr;
+ result[0] = '\0';
} else {
render_boolean(result, true); // assume on. could have value 0x01 or 0xFF
}
@@ -179,7 +179,8 @@ char * Helpers::render_value(char * result, uint8_t value, uint8_t format) {
}
if (!hasValue(value)) {
- return nullptr;
+ result[0] = '\0';
+ return result;
}
if (!format) {
@@ -209,9 +210,14 @@ char * Helpers::render_value(char * result, uint8_t value, uint8_t format) {
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;
+ }
+
char * ret = result;
long whole = (long)value;
- Helpers::itoa(result, whole, 10);
+ ltoa(whole, result, 10);
while (*result != '\0') {
result++;
@@ -219,7 +225,7 @@ char * Helpers::render_value(char * result, const float value, const uint8_t for
*result++ = '.';
long decimal = abs((long)((value - whole) * p[format]));
- itoa(result, decimal, 10);
+ ltoa(decimal, result, 10);
return ret;
}
@@ -227,8 +233,9 @@ 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 nullptr;
+ return result;
}
// just print it if no conversion required (format = 0)
@@ -238,7 +245,6 @@ 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) {
@@ -267,7 +273,8 @@ 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)) {
- return nullptr;
+ result[0] = '\0';
+ return result;
}
return (render_value(result, (int16_t)value, format)); // use same code, force it to a signed int
}
@@ -275,7 +282,8 @@ 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)) {
- return nullptr;
+ result[0] = '\0';
+ return result;
}
return (render_value(result, (int16_t)value, format)); // use same code, force it to a signed int
}
@@ -283,7 +291,8 @@ 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)) {
- return nullptr;
+ result[0] = '\0';
+ return result;
}
char s[20];
diff --git a/src/sensor.cpp b/src/sensor.cpp
index 0845bb88a..8ed89b745 100644
--- a/src/sensor.cpp
+++ b/src/sensor.cpp
@@ -75,7 +75,6 @@ void Sensor::loop() {
} else {
// no sensors found
// LOG_ERROR(F("Bus reset failed")); // uncomment for debug
- devices_.clear(); // remove all know devices in case we have a disconnect
}
last_activity_ = time_now;
}
@@ -83,7 +82,6 @@ void Sensor::loop() {
if (temperature_convert_complete() && (time_now - last_activity_ > CONVERSION_MS)) {
// LOG_DEBUG(F("Scanning for sensors")); // uncomment for debug
bus_.reset_search();
- found_.clear();
state_ = State::SCANNING;
} else if (time_now - last_activity_ > READ_TIMEOUT_MS) {
LOG_ERROR(F("Sensor read timeout"));
@@ -106,20 +104,28 @@ void Sensor::loop() {
case TYPE_DS18S20:
case TYPE_DS1822:
case TYPE_DS1825:
- float f;
- f = get_temperature_c(addr);
- if ((f != NAN) && (f >= -55) && (f <= 125)) {
- found_.emplace_back(addr);
- found_.back().temperature_c = f;
+ int16_t t;
+ t = get_temperature_c(addr);
+ if ((t >= -550) && (t <= 1250)) {
+ // check if we have this sensor already
+ bool found = false;
+ for (auto & device : devices_) {
+ if (device.id() == get_id(addr)) {
+ changed_ |= (t != device.temperature_c);
+ device.temperature_c = t;
+ device.read = true;
+ found = true;
+ break;
+ }
+ }
+ // add new sensor
+ if (!found && (devices_.size() < (MAX_SENSORS - 1))) {
+ devices_.emplace_back(addr);
+ devices_.back().temperature_c = t;
+ devices_.back().read = true;
+ changed_ = true;
+ }
}
-
- /*
- // comment out for debugging
- char result[10];
- LOG_DEBUG(F("Temp of %s = %s"),
- found_.back().to_string().c_str(),
- Helpers::render_value(result, found_.back().temperature_c_, 2));
- */
break;
default:
@@ -133,22 +139,22 @@ void Sensor::loop() {
if (!parasite_) {
bus_.depower();
}
- if ((found_.size() >= devices_.size()) || (retrycnt_ > 5)) {
- if (found_.size() == devices_.size()) {
- for (uint8_t i = 0; i < devices_.size(); i++) {
- if (found_[i].temperature_c != devices_[i].temperature_c) {
- changed_ = true;
- }
+ // check for missing sensors after some samples
+ if (++scancnt_ > 5) {
+ for (auto & device : devices_) {
+ if (!device.read) {
+ device.temperature_c = EMS_VALUE_SHORT_NOTSET;
+ changed_ = true;
}
- } else {
- changed_ = true;
+ device.read = false;
}
- devices_ = std::move(found_);
- retrycnt_ = 0;
- } else {
- retrycnt_++;
+ scancnt_ = 0;
+ } else if (scancnt_ == -2) { // startup
+ firstscan_ = devices_.size();
+ } else if ((scancnt_ <= 0) && (firstscan_ != devices_.size())) { // check 2 times for no change of sensorno.
+ scancnt_ = -3;
+ devices_.clear(); // restart scaning and clear to get correct numbering
}
- found_.clear();
// LOG_DEBUG(F("Found %zu sensor(s). Adding them."), devices_.size()); // uncomment for debug
state_ = State::IDLE;
}
@@ -171,11 +177,11 @@ bool Sensor::temperature_convert_complete() {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
-float Sensor::get_temperature_c(const uint8_t addr[]) {
+int16_t Sensor::get_temperature_c(const uint8_t addr[]) {
#ifndef EMSESP_STANDALONE
if (!bus_.reset()) {
LOG_ERROR(F("Bus reset failed before reading scratchpad from %s"), Device(addr).to_string().c_str());
- return NAN;
+ return EMS_VALUE_SHORT_NOTSET;
}
YIELD;
uint8_t scratchpad[SCRATCHPAD_LEN] = {0};
@@ -185,7 +191,7 @@ float Sensor::get_temperature_c(const uint8_t addr[]) {
YIELD;
if (!bus_.reset()) {
LOG_ERROR(F("Bus reset failed after reading scratchpad from %s"), Device(addr).to_string().c_str());
- return NAN;
+ return EMS_VALUE_SHORT_NOTSET;
}
YIELD;
if (bus_.crc8(scratchpad, SCRATCHPAD_LEN - 1) != scratchpad[SCRATCHPAD_LEN - 1]) {
@@ -200,7 +206,7 @@ float Sensor::get_temperature_c(const uint8_t addr[]) {
scratchpad[7],
scratchpad[8],
Device(addr).to_string().c_str());
- return NAN;
+ return EMS_VALUE_SHORT_NOTSET;
}
int16_t raw_value = ((int16_t)scratchpad[SCRATCHPAD_TEMP_MSB] << 8) | scratchpad[SCRATCHPAD_TEMP_LSB];
@@ -224,10 +230,10 @@ float Sensor::get_temperature_c(const uint8_t addr[]) {
break;
}
}
- uint32_t raw = ((uint32_t)raw_value * 625 + 500) / 1000; // round to 0.1
- return (float)raw / 10;
+ raw_value = ((int32_t)raw_value * 625 + 500) / 1000; // round to 0.1
+ return raw_value;
#else
- return NAN;
+ return EMS_VALUE_SHORT_NOTSET;
#endif
}
@@ -237,9 +243,15 @@ const std::vector Sensor::devices() const {
return devices_;
}
+// skip crc from id.
Sensor::Device::Device(const uint8_t addr[])
- : id_(((uint64_t)addr[0] << 56) | ((uint64_t)addr[1] << 48) | ((uint64_t)addr[2] << 40) | ((uint64_t)addr[3] << 32) | ((uint64_t)addr[4] << 24)
- | ((uint64_t)addr[5] << 16) | ((uint64_t)addr[6] << 8) | (uint64_t)addr[7]) {
+ : id_(((uint64_t)addr[0] << 48) | ((uint64_t)addr[1] << 40) | ((uint64_t)addr[2] << 32) | ((uint64_t)addr[3] << 24) | ((uint64_t)addr[4] << 16)
+ | ((uint64_t)addr[5] << 8) | ((uint64_t)addr[6])) {
+}
+
+uint64_t Sensor::get_id(const uint8_t addr[]) {
+ return (((uint64_t)addr[0] << 48) | ((uint64_t)addr[1] << 40) | ((uint64_t)addr[2] << 32) | ((uint64_t)addr[3] << 24) | ((uint64_t)addr[4] << 16)
+ | ((uint64_t)addr[5] << 8) | ((uint64_t)addr[6]));
}
uint64_t Sensor::Device::id() const {
@@ -250,12 +262,11 @@ std::string Sensor::Device::to_string() const {
std::string str(20, '\0');
snprintf_P(&str[0],
str.capacity() + 1,
- PSTR("%02X-%04X-%04X-%04X-%02X"),
- (unsigned int)(id_ >> 56) & 0xFF,
- (unsigned int)(id_ >> 40) & 0xFFFF,
- (unsigned int)(id_ >> 24) & 0xFFFF,
- (unsigned int)(id_ >> 8) & 0xFFFF,
- (unsigned int)(id_)&0xFF);
+ PSTR("%02X-%04X-%04X-%04X"),
+ (unsigned int)(id_ >> 48) & 0xFF,
+ (unsigned int)(id_ >> 32) & 0xFFFF,
+ (unsigned int)(id_ >> 16) & 0xFFFF,
+ (unsigned int)(id_) & 0xFFFF);
return str;
}
@@ -282,12 +293,11 @@ bool Sensor::export_values(JsonObject & output) {
uint8_t i = 1; // sensor count
for (const auto & device : devices_) {
char s[7];
- char sensorID[20]; // sensor{1-n}
- strlcpy(sensorID, "sensor", 20);
- strlcat(sensorID, Helpers::itoa(s, i), 20);
+ char sensorID[10]; // sensor{1-n}
+ snprintf_P(sensorID, 10, PSTR("sensor%d"), i);
JsonObject dataSensor = output.createNestedObject(sensorID);
dataSensor["id"] = device.to_string();
- dataSensor["temp"] = Helpers::render_value(s, device.temperature_c, 1);
+ dataSensor["temp"] = Helpers::render_value(s, device.temperature_c, 10);
i++;
}
@@ -304,39 +314,38 @@ void Sensor::publish_values() {
}
uint8_t mqtt_format_ = Mqtt::mqtt_format();
-
- // single mode as e.g. ems-esp/sensor_28-EA41-9497-0E03-5F = {"temp":20.2}
+ /*
+ // single mode as one topic per sensor e.g. ems-esp/sensor_data1 = 20.2
if (mqtt_format_ == Mqtt::Format::SINGLE) {
- StaticJsonDocument<100> doc;
+ uint8_t i = 1;
for (const auto & device : devices_) {
- char topic[60];
- strlcpy(topic, "sensor_", 50);
- strlcat(topic, device.to_string().c_str(), 60);
+ char topic[20];
+ snprintf_P(topic, 20, PSTR("sensor_data%d"), i);
char s[7]; // to support -55.00 to 125.00
- doc["temp"] = Helpers::render_value(s, device.temperature_c, 1);
- Mqtt::publish(topic, doc.as());
- doc.clear(); // clear json doc so we can reuse the buffer again
+ Mqtt::publish(topic, Helpers::render_value(s, device.temperature_c, 10));
+ i++;
}
return;
}
-
+ */
DynamicJsonDocument doc(100 * num_devices);
uint8_t i = 1; // sensor count
for (const auto & device : devices_) {
- char s[7];
-
- if ((mqtt_format_ == Mqtt::Format::NESTED) || (mqtt_format_ == Mqtt::Format::HA)) {
- // e.g. sensor_data = {"sensor1":{"id":"28-EA41-9497-0E03-5F","temp":"23.30"},"sensor2":{"id":"28-233D-9497-0C03-8B","temp":"24.0"}}
- char sensorID[20]; // sensor{1-n}
- strlcpy(sensorID, "sensor", 20);
- strlcat(sensorID, Helpers::itoa(s, i), 20);
+ char s[7]; // for rendering temperature
+ char sensorID[10]; // sensor{1-n}
+ snprintf_P(sensorID, 10, PSTR("sensor%d"), i);
+ 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);
+ } 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, 1);
- }
+ dataSensor["temp"] = Helpers::render_value(s, 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);
- // special for HA
- if (mqtt_format_ == Mqtt::Format::HA) {
// create the config if this hasn't already been done
// to e.g. homeassistant/sensor/ems-esp/dallas_sensor1/config
if (!(registered_ha_[i])) {
@@ -350,13 +359,14 @@ void Sensor::publish_values() {
config["unit_of_meas"] = F("°C");
char str[50];
- snprintf_P(str, sizeof(str), PSTR("{{value_json.sensor%d.temp}}"), i);
+ snprintf_P(str, sizeof(str), PSTR("{{value_json.%s}}"), device.to_string().c_str());
config["val_tpl"] = str;
- snprintf_P(str, sizeof(str), PSTR("Dallas sensor%d"), i);
+ // name as sensor number not the long unique ID
+ snprintf_P(str, sizeof(str), PSTR("Dallas Sensor %d"), i);
config["name"] = str;
- snprintf_P(str, sizeof(str), PSTR("dalas_sensor%d"), i);
+ snprintf_P(str, sizeof(str), PSTR("dallas_%s"), device.to_string().c_str());
config["uniq_id"] = str;
JsonObject dev = config.createNestedObject("dev");
@@ -364,7 +374,7 @@ void Sensor::publish_values() {
ids.add("ems-esp");
std::string topic(100, '\0');
- snprintf_P(&topic[0], 100, PSTR("homeassistant/sensor/ems-esp/dallas_sensor%d/config"), i);
+ 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;
diff --git a/src/sensor.h b/src/sensor.h
index 3e231a4e3..feb1ffcb4 100644
--- a/src/sensor.h
+++ b/src/sensor.h
@@ -46,11 +46,11 @@ class Sensor {
uint64_t id() const;
std::string to_string() const;
- float temperature_c = NAN;
+ int16_t temperature_c = EMS_VALUE_SHORT_NOTSET;
+ bool read = false;
private:
const uint64_t id_;
- bool registered_ = false;
};
Sensor() = default;
@@ -100,18 +100,19 @@ class Sensor {
OneWire bus_;
#endif
- bool temperature_convert_complete();
- float get_temperature_c(const uint8_t addr[]);
+ bool temperature_convert_complete();
+ int16_t get_temperature_c(const uint8_t addr[]);
+ uint64_t get_id(const uint8_t addr[]);
uint32_t last_activity_ = uuid::get_uptime();
uint32_t last_publish_ = uuid::get_uptime();
State state_ = State::IDLE;
- std::vector found_;
std::vector devices_;
bool registered_ha_[MAX_SENSORS];
- uint8_t retrycnt_ = 0;
+ int8_t scancnt_ = -3;
+ uint8_t firstscan_ = 0;
uint8_t dallas_gpio_ = 0;
bool parasite_ = false;
bool changed_ = false;