supla-device
Loading...
Searching...
No Matches
TMP102.h
1// SPDX-FileCopyrightText: AC SOFTWARE SP. Z O.O.
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#pragma once
5
6/*
7Dependency: https://github.com/sparkfun/SparkFun_TMP102_Arduino_Library
8Use library manager to install it
9*/
10
11#include <SparkFunTMP102.h>
12
13#include <supla/log_wrapper.h>
14#include "thermometer.h"
15
16namespace Supla {
17namespace Sensor {
18
19class TMP102 : public Thermometer {
20 public:
21 struct Config {
22 float hTemp = 70.0;
23 float lTemp = 65.0;
24 bool extMode = false;
25 bool alertPolarity = false;
26 uint8_t fault = 0;
27 bool alertMode = false;
28 double thresholdPercentage = 30.0;
29 };
30
31 explicit TMP102(uint8_t address,
32 Supla::Mutex *mutex,
33 TwoWire *wire,
34 const Config &cfg)
35 : address_(address),
36 mutex_(mutex),
37 wire_(wire),
38 cfg_(cfg) {
39 }
40
41 explicit TMP102(uint8_t address = 0x48,
42 Supla::Mutex *mutex = nullptr,
43 TwoWire *wire = &Wire)
44 : TMP102(address, mutex, wire, Config{}) {
45 }
46
47 void onInit() override {
48 initSensor(address_, wire_);
49 channel.setNewValue(getTemp());
50 }
51
52 double getTemp() override {
53 double t = TEMPERATURE_NOT_AVAILABLE;
54 if (mutex_) mutex_->lock();
55 if (isConnected_) {
56 t = tmp102_.readTempC();
57 }
58 if (mutex_) mutex_->unlock();
59 if (t == TEMPERATURE_NOT_AVAILABLE) {
60 return t;
61 }
62 if (t < -40.0 || t > 125.0) {
63 SUPLA_LOG_WARNING("[TMP102] invalid reading: %.2f", t);
64 return TEMPERATURE_NOT_AVAILABLE;
65 }
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_;
72 }
73 }
74 lastValidTemperature_ = std::round(t * 100) / 100.0;
75 return lastValidTemperature_;
76 }
77
78 bool getAlertState() {
79 if (mutex_) mutex_->lock();
80 bool raw = tmp102_.alert();
81 if (mutex_) mutex_->unlock();
82 return cfg_.alertMode ? raw : !raw;
83 }
84
85 protected:
86 ::TMP102 tmp102_;
87 Config cfg_;
88 TwoWire *wire_ = nullptr;
89 Supla::Mutex *mutex_ = nullptr;
90 uint8_t address_ = 0x48;
91 bool isConnected_ = false;
92 double lastValidTemperature_ = TEMPERATURE_NOT_AVAILABLE;
93
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);
98 tmp102_.wakeup();
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);
105 isConnected_ = true;
106 } else {
107 SUPLA_LOG_ERROR("Unable to find TMP102 at 0x%x", address);
108 }
109 if (mutex_) mutex_->unlock();
110 }
111
112 static double percentageDifference(double a, double b) {
113 return std::abs(a - b) / b * 100.0;
114 }
115};
116
117}; // namespace Sensor
118}; // namespace Supla
Definition config.h:44
Definition mutex.h:9
Definition TMP102.h:19
void onInit() override
Third method called on element in SuplaDevice.begin()
Definition TMP102.h:47
Definition thermometer.h:13
Definition TMP102.h:21