supla-device
Loading...
Searching...
No Matches
TMP102.h
1/*
2 Copyright (C) AC SOFTWARE SP. Z O.O.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17*/
18
19#pragma once
20
21/*
22Dependency: https://github.com/sparkfun/SparkFun_TMP102_Arduino_Library
23Use library manager to install it
24*/
25
26#include <SparkFunTMP102.h>
27
28#include <supla/log_wrapper.h>
29#include "thermometer.h"
30
31namespace Supla {
32namespace Sensor {
33
34class TMP102 : public Thermometer {
35 public:
36 struct Config {
37 float hTemp = 70.0;
38 float lTemp = 65.0;
39 bool extMode = false;
40 bool alertPolarity = false;
41 uint8_t fault = 0;
42 bool alertMode = false;
43 double thresholdPercentage = 30.0;
44 };
45
46 explicit TMP102(uint8_t address,
47 Supla::Mutex *mutex,
48 TwoWire *wire,
49 const Config &cfg)
50 : address_(address),
51 mutex_(mutex),
52 wire_(wire),
53 cfg_(cfg) {
54 }
55
56 explicit TMP102(uint8_t address = 0x48,
57 Supla::Mutex *mutex = nullptr,
58 TwoWire *wire = &Wire)
59 : TMP102(address, mutex, wire, Config{}) {
60 }
61
62 void onInit() override {
63 initSensor(address_, wire_);
64 channel.setNewValue(getTemp());
65 }
66
67 double getTemp() override {
68 double t = TEMPERATURE_NOT_AVAILABLE;
69 if (mutex_) mutex_->lock();
70 if (isConnected_) {
71 t = tmp102_.readTempC();
72 }
73 if (mutex_) mutex_->unlock();
74 if (t == TEMPERATURE_NOT_AVAILABLE) {
75 return t;
76 }
77 if (t < -40.0 || t > 125.0) {
78 SUPLA_LOG_WARNING("[TMP102] invalid reading: %.2f", t);
79 return TEMPERATURE_NOT_AVAILABLE;
80 }
81 if (lastValidTemperature_ != TEMPERATURE_NOT_AVAILABLE) {
82 double diff = percentageDifference(t, lastValidTemperature_);
83 if (diff > cfg_.thresholdPercentage) {
84 SUPLA_LOG_DEBUG("[TMP102] rejected value: %.2f (diff: %d%%)", t,
85 static_cast<int>(diff));
86 return lastValidTemperature_;
87 }
88 }
89 lastValidTemperature_ = std::round(t * 100) / 100.0;
90 return lastValidTemperature_;
91 }
92
93 bool getAlertState() {
94 if (mutex_) mutex_->lock();
95 bool raw = tmp102_.alert();
96 if (mutex_) mutex_->unlock();
97 return cfg_.alertMode ? raw : !raw;
98 }
99
100 protected:
101 ::TMP102 tmp102_;
102 Config cfg_;
103 TwoWire *wire_ = nullptr;
104 Supla::Mutex *mutex_ = nullptr;
105 uint8_t address_ = 0x48;
106 bool isConnected_ = false;
107 double lastValidTemperature_ = TEMPERATURE_NOT_AVAILABLE;
108
109 void initSensor(uint8_t address, TwoWire *wire) {
110 if (mutex_) mutex_->lock();
111 if (tmp102_.begin(address, *wire)) {
112 SUPLA_LOG_DEBUG("TMP102 connected at 0x%x", address);
113 tmp102_.wakeup();
114 tmp102_.setHighTempC(cfg_.hTemp);
115 tmp102_.setLowTempC(cfg_.lTemp);
116 tmp102_.setExtendedMode(cfg_.extMode);
117 tmp102_.setAlertPolarity(cfg_.alertPolarity);
118 tmp102_.setFault(cfg_.fault);
119 tmp102_.setAlertMode(cfg_.alertMode);
120 isConnected_ = true;
121 } else {
122 SUPLA_LOG_ERROR("Unable to find TMP102 at 0x%x", address);
123 }
124 if (mutex_) mutex_->unlock();
125 }
126
127 static double percentageDifference(double a, double b) {
128 return std::abs(a - b) / b * 100.0;
129 }
130};
131
132}; // namespace Sensor
133}; // namespace Supla
Definition config.h:56
Definition mutex.h:22
Definition TMP102.h:34
void onInit() override
Third method called on element in SuplaDevice.begin()
Definition TMP102.h:62
Definition TMP102.h:36