25 bool alertPolarity =
false;
27 bool alertMode =
false;
28 double thresholdPercentage = 30.0;
31 explicit TMP102(uint8_t address,
41 explicit TMP102(uint8_t address = 0x48,
43 TwoWire *wire = &Wire)
48 initSensor(address_, wire_);
49 channel.setNewValue(getTemp());
52 double getTemp()
override {
53 double t = TEMPERATURE_NOT_AVAILABLE;
54 if (mutex_) mutex_->lock();
56 t = tmp102_.readTempC();
58 if (mutex_) mutex_->unlock();
59 if (t == TEMPERATURE_NOT_AVAILABLE) {
62 if (t < -40.0 || t > 125.0) {
63 SUPLA_LOG_WARNING(
"[TMP102] invalid reading: %.2f", t);
64 return TEMPERATURE_NOT_AVAILABLE;
66 if (lastValidTemperature_ != TEMPERATURE_NOT_AVAILABLE) {
67 double diff = percentageDifference(t, lastValidTemperature_);
68 if (diff > cfg_.thresholdPercentage) {
69 SUPLA_LOG_DEBUG(
"[TMP102] rejected value: %.2f (diff: %d%%)", t,
70 static_cast<int>(diff));
71 return lastValidTemperature_;
74 lastValidTemperature_ = std::round(t * 100) / 100.0;
75 return lastValidTemperature_;
78 bool getAlertState() {
79 if (mutex_) mutex_->lock();
80 bool raw = tmp102_.alert();
81 if (mutex_) mutex_->unlock();
82 return cfg_.alertMode ? raw : !raw;
88 TwoWire *wire_ =
nullptr;
90 uint8_t address_ = 0x48;
91 bool isConnected_ =
false;
92 double lastValidTemperature_ = TEMPERATURE_NOT_AVAILABLE;
94 void initSensor(uint8_t address, TwoWire *wire) {
95 if (mutex_) mutex_->lock();
96 if (tmp102_.begin(address, *wire)) {
97 SUPLA_LOG_DEBUG(
"TMP102 connected at 0x%x", address);
99 tmp102_.setHighTempC(cfg_.hTemp);
100 tmp102_.setLowTempC(cfg_.lTemp);
101 tmp102_.setExtendedMode(cfg_.extMode);
102 tmp102_.setAlertPolarity(cfg_.alertPolarity);
103 tmp102_.setFault(cfg_.fault);
104 tmp102_.setAlertMode(cfg_.alertMode);
107 SUPLA_LOG_ERROR(
"Unable to find TMP102 at 0x%x", address);
109 if (mutex_) mutex_->unlock();
112 static double percentageDifference(
double a,
double b) {
113 return std::abs(a - b) / b * 100.0;