53 lines
1023 B
C++
53 lines
1023 B
C++
#ifndef DS18B20_SENSOR_H
|
|
#define DS18B20_SENSOR_H
|
|
|
|
#include "DS18B20.h"
|
|
#include "IValueSensor.h"
|
|
|
|
class DS18B20Sensor final : public IValueSensor {
|
|
|
|
DS18B20& bus;
|
|
|
|
String name;
|
|
|
|
const int index;
|
|
|
|
const uint8_t* address;
|
|
|
|
float temperature = NAN;
|
|
|
|
public:
|
|
|
|
DS18B20Sensor(DS18B20& bus, const int index, String name) : bus(bus), name(std::move(name)), index(index), address{} {
|
|
//
|
|
}
|
|
|
|
DS18B20Sensor(DS18B20& bus, const uint8_t address[8], String name) : bus(bus), name(std::move(name)), index(-1), address(address) {
|
|
//
|
|
}
|
|
|
|
[[nodiscard]] float getValue() const override {
|
|
return temperature;
|
|
}
|
|
|
|
void loop() {
|
|
if (bus.isComplete()) {
|
|
if (index >= 0) {
|
|
temperature = bus.getTemperatureByIndex(index);
|
|
}
|
|
else {
|
|
temperature = bus.getTemperatureByAddress(address);
|
|
}
|
|
if (!name.isEmpty()) {
|
|
mqttPublishValue(name, temperature, UNIT_TEMPERATURE_C);
|
|
}
|
|
}
|
|
else if (bus.isError()) {
|
|
temperature = NAN;
|
|
}
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|