Skip to content

Commit 2ed61aa

Browse files
committed
Support for configurable threshold
1 parent 7d07b04 commit 2ed61aa

10 files changed

Lines changed: 73 additions & 22 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ void setup() {
232232
continue;
233233
234234
temp.begin(18);
235-
235+
236236
temp.listen([](float temperature, bool changed) {
237237
JsonDocument doc;
238238
temp.toJson(doc.to<JsonObject>());
@@ -260,21 +260,21 @@ void setup() {
260260
continue;
261261

262262
temp.begin(18);
263-
263+
264264
// Temperature readings expire after 30 seconds
265265
temp.setExpirationDelay(30);
266266
}
267267

268268
void loop() {
269269
temp.read();
270-
270+
271271
if (temp.isValid()) {
272272
float t = temp.getTemperature().value_or(0);
273273
Serial.printf("Valid temperature: %.2f°C\n", t);
274274
} else if (temp.isExpired()) {
275275
Serial.println("Temperature reading expired!");
276276
}
277-
277+
278278
delay(2000);
279279
}
280280
```

docs/index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ void setup() {
232232
continue;
233233
234234
temp.begin(18);
235-
235+
236236
temp.listen([](float temperature, bool changed) {
237237
JsonDocument doc;
238238
temp.toJson(doc.to<JsonObject>());
@@ -260,21 +260,21 @@ void setup() {
260260
continue;
261261

262262
temp.begin(18);
263-
263+
264264
// Temperature readings expire after 30 seconds
265265
temp.setExpirationDelay(30);
266266
}
267267

268268
void loop() {
269269
temp.read();
270-
270+
271271
if (temp.isValid()) {
272272
float t = temp.getTemperature().value_or(0);
273273
Serial.printf("Valid temperature: %.2f°C\n", t);
274274
} else if (temp.isExpired()) {
275275
Serial.println("Temperature reading expired!");
276276
}
277-
277+
278278
delay(2000);
279279
}
280280
```

examples/Threshold/Threshold.ino

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <Arduino.h>
2+
#include <MycilaDS18.h>
3+
4+
Mycila::DS18 temp;
5+
6+
void setup() {
7+
Serial.begin(115200);
8+
while (!Serial)
9+
continue;
10+
11+
temp.setThreshold(0.0f); // any temperature change will be reported in callback
12+
13+
temp.listen([](float temperature, bool changed) {
14+
if (changed) {
15+
Serial.printf("Temperature changed: %f°C\n", temperature);
16+
}
17+
});
18+
19+
temp.begin(18);
20+
}
21+
22+
void loop() {
23+
temp.read();
24+
delay(1000);
25+
}

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "MycilaDS18",
3-
"version": "5.1.1",
3+
"version": "5.2.0",
44
"description": "ESP32 / Arduino Library for Dallas / Maxim DS18 sensor using RMT peripheral",
55
"homepage": "https://github.com/mathieucarbou/MycilaDS18",
66
"repository": {

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=MycilaDS18
2-
version=5.1.1
2+
version=5.2.0
33
author=Mathieu Carbou <mathieu.carbou@gmail.com>
44
maintainer=Mathieu Carbou <mathieu.carbou@gmail.com>
55
sentence=ESP32 / Arduino Library for Dallas / Maxim DS18 sensor using RMT peripheral

platformio.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ lib_dir = .
2626
; src_dir = examples/Search
2727
; src_dir = examples/Json
2828
; src_dir = examples/SetAddress
29-
src_dir = examples/MultipleDS18
29+
; src_dir = examples/MultipleDS18
30+
src_dir = examples/Threshold
3031

3132
[env:arduino-3]
3233
platform = https://github.com/pioarduino/platform-espressif32/releases/download/55.03.34/platform-espressif32.zip

src/MycilaDS18.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,11 @@ bool Mycila::DS18::read() {
148148
// read is valid, record the time
149149
_lastTime = millis();
150150

151-
// make it on 2 decimals
152-
read = round(read * 100.0f) / 100.0f;
153-
154-
const bool changed = std::abs(read - _temperature) >= MYCILA_DS18_RELEVANT_TEMPERATURE_CHANGE || !isValid();
151+
const bool changed = std::abs(read - _temperature) > _threshold || !isValid();
155152

156153
if (changed) {
157154
_temperature = read;
158-
ESP_LOGD(TAG, "%s 0x%llx @ pin %d: %.2f °C", _name, _deviceAddress, _pin, read);
155+
ESP_LOGD(TAG, "%s 0x%llx @ pin %d: %f °C", _name, _deviceAddress, _pin, read);
159156
}
160157

161158
if (_callback)

src/MycilaDS18.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#pragma once
66

77
#include <esp_idf_version.h>
8+
89
#include "./esp32-ds18b20/OneWireESP32.h"
910

1011
#ifdef MYCILA_JSON_SUPPORT
@@ -15,12 +16,11 @@
1516

1617
#include <mutex>
1718
#include <optional>
18-
#include <utility>
1919

20-
#define MYCILA_DS18_VERSION "5.1.1"
20+
#define MYCILA_DS18_VERSION "5.2.0"
2121
#define MYCILA_DS18_VERSION_MAJOR 5
22-
#define MYCILA_DS18_VERSION_MINOR 1
23-
#define MYCILA_DS18_VERSION_REVISION 1
22+
#define MYCILA_DS18_VERSION_MINOR 2
23+
#define MYCILA_DS18_VERSION_REVISION 0
2424

2525
// If the temperature is changing from less than 0.3 degrees, we consider it has not changed, to avoid too many updates
2626
// Example:
@@ -107,6 +107,14 @@ namespace Mycila {
107107

108108
OneWire32* getOneWire() const { return _oneWire; }
109109

110+
float getThreshold() const { return _threshold; }
111+
112+
/**
113+
* @brief Set the temperature change threshold to consider a change relevant enough to trigger the callback
114+
* @param threshold The temperature change threshold in degrees Celsius, default is MYCILA_DS18_RELEVANT_TEMPERATURE_CHANGE
115+
*/
116+
void setThreshold(float threshold) { _threshold = threshold; }
117+
110118
#ifdef MYCILA_JSON_SUPPORT
111119
void toJson(const JsonObject& root) const;
112120
#endif
@@ -119,6 +127,7 @@ namespace Mycila {
119127
bool _enabled = false;
120128
const char* _name = "Unknown";
121129
float _temperature = 0;
130+
float _threshold = MYCILA_DS18_RELEVANT_TEMPERATURE_CHANGE;
122131
uint32_t _lastTime = 0;
123132
uint32_t _expirationDelay = 0;
124133
DS18ChangeCallback _callback = nullptr;

src/esp32-ds18b20/OneWireESP32.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,26 @@ void OneWire32::request(uint64_t& addr) {
263263
}
264264
}
265265

266-
const uint8_t crc_table[] = {0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65, 157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220, 35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98, 190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255, 70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7, 219, 133, 103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154, 101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36, 248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185, 140, 210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205, 17, 79, 173, 243, 112, 46, 204, 146, 211, 141, 111, 49, 178, 236, 14, 80, 175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82, 176, 238, 50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115, 202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139, 87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22, 233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168, 116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53};
266+
// clang-format off
267+
const uint8_t crc_table[] = {
268+
0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65,
269+
157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220,
270+
35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98,
271+
190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255,
272+
70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7,
273+
219, 133, 103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154,
274+
101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36,
275+
248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185,
276+
140, 210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205,
277+
17, 79, 173, 243, 112, 46, 204, 146, 211, 141, 111, 49, 178, 236, 14, 80,
278+
175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82, 176, 238,
279+
50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115,
280+
202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139,
281+
87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22,
282+
233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168,
283+
116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53
284+
};
285+
// clang-format on
267286

268287
OneWire32::Result OneWire32::getTemp(uint64_t& addr, float& temp) {
269288
if (!drv) {

src/esp32-ds18b20/OneWireESP32.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class OneWire32 {
3636
gpio_num_t pin() const { return owpin; }
3737
bool reset();
3838
void request();
39-
void request(uint64_t &addr);
39+
void request(uint64_t& addr);
4040
Result getTemp(uint64_t& addr, float& temp);
4141
uint8_t search(uint64_t* addresses, uint8_t total);
4242
bool read(uint8_t& data, uint8_t len = 8);

0 commit comments

Comments
 (0)