-
Notifications
You must be signed in to change notification settings - Fork 19
/
DallasTemperature.ino
195 lines (162 loc) · 5.03 KB
/
DallasTemperature.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Copyright (c) 2019-2022,2024 Piotr Stolarz
* OneWireNg: Arduino 1-wire service library
*
* Distributed under the 2-clause BSD License (the License)
* see accompanying file LICENSE for details.
*
* This software is distributed WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the License for more information.
*/
/**
* Dallas family thermometers access example (Arduino).
*
* Required configuration:
* - @c CONFIG_SEARCH_ENABLED for non single sensor setup,
* - @c CONFIG_PWR_CTRL_ENABLED if @c PWR_CTRL_PIN is configured.
*/
#include "OneWireNg_CurrentPlatform.h"
#include "drivers/DSTherm.h"
#include "utils/Placeholder.h"
/*
* 1-wire bus pin number.
*/
#ifndef OW_PIN
# define OW_PIN 13
#endif
/*
* If defined: sensors powered parasitically.
*/
//#define PARASITE_POWER
/*
* If defined: only one sensor device is allowed to be connected to the bus.
* The library may be configured with 1-wire search activity disabled to
* reduce its footprint.
*/
//#define SINGLE_SENSOR
/*
* For parasitically powered sensors the parameter specifies type of power
* provisioning:
* - Not defined: power provided by bus pin.
* - Defined: power provided by a switching transistor and controlled by the
* pin number specified by the parameter.
*/
//#define PWR_CTRL_PIN 9
/*
* If defined: set permanent, common resolution for all sensors on the bus.
* Resolution may vary from 9 to 12 bits.
*/
//#define COMMON_RES (DSTherm::RES_12_BIT)
#if !defined(SINGLE_SENSOR) && !CONFIG_SEARCH_ENABLED
# error "CONFIG_SEARCH_ENABLED is required for non single sensor setup"
#endif
#if defined(PWR_CTRL_PIN) && !CONFIG_PWR_CTRL_ENABLED
# error "CONFIG_PWR_CTRL_ENABLED is required if PWR_CTRL_PIN is configured"
#endif
#if (CONFIG_MAX_SEARCH_FILTERS > 0)
static_assert(CONFIG_MAX_SEARCH_FILTERS >= DSTherm::SUPPORTED_SLAVES_NUM,
"CONFIG_MAX_SEARCH_FILTERS too small");
#endif
#ifdef PARASITE_POWER
# define PARASITE_POWER_ARG true
#else
# define PARASITE_POWER_ARG false
#endif
static Placeholder<OneWireNg_CurrentPlatform> ow;
/* returns false if not supported */
static bool printId(const OneWireNg::Id& id)
{
const char *name = DSTherm::getFamilyName(id);
Serial.print(id[0], HEX);
for (size_t i = 1; i < sizeof(OneWireNg::Id); i++) {
Serial.print(':');
Serial.print(id[i], HEX);
}
if (name) {
Serial.print(" -> ");
Serial.print(name);
}
Serial.println();
return (name != NULL);
}
static void printScratchpad(const DSTherm::Scratchpad& scrpd)
{
const uint8_t *scrpd_raw = scrpd.getRaw();
Serial.print(" Scratchpad:");
for (size_t i = 0; i < DSTherm::Scratchpad::LENGTH; i++) {
Serial.print(!i ? ' ' : ':');
Serial.print(scrpd_raw[i], HEX);
}
Serial.print("; Th:");
Serial.print(scrpd.getTh());
Serial.print("; Tl:");
Serial.print(scrpd.getTl());
Serial.print("; Resolution:");
Serial.print(9 + (int)(scrpd.getResolution() - DSTherm::RES_9_BIT));
long temp = scrpd.getTemp2();
Serial.print("; Temp:");
Serial.print((float)temp / 16);
Serial.print(" C");
Serial.println();
}
void setup()
{
#ifdef PWR_CTRL_PIN
new (&ow) OneWireNg_CurrentPlatform(OW_PIN, PWR_CTRL_PIN, false);
#else
new (&ow) OneWireNg_CurrentPlatform(OW_PIN, false);
#endif
DSTherm drv(ow);
Serial.begin(115200);
#if (CONFIG_MAX_SEARCH_FILTERS > 0)
drv.filterSupportedSlaves();
#endif
#ifdef COMMON_RES
/*
* Set common resolution for all sensors.
* Th, Tl (high/low alarm triggers) are set to 0.
*/
drv.writeScratchpadAll(0, 0, COMMON_RES);
/*
* The configuration above is stored in volatile sensors scratchpad
* memory and will be lost after power unplug. Therefore store the
* configuration permanently in sensors EEPROM.
*/
drv.copyScratchpadAll(PARASITE_POWER_ARG);
#endif
}
void loop()
{
DSTherm drv(ow);
/* convert temperature on all sensors connected... */
drv.convertTempAll(DSTherm::MAX_CONV_TIME, PARASITE_POWER_ARG);
#ifdef SINGLE_SENSOR
/* single sensor environment */
/*
* Scratchpad placeholder is static to allow reuse of the associated
* sensor id while reissuing readScratchpadSingle() calls.
* Note, due to its storage class the placeholder is zero initialized.
*/
static Placeholder<DSTherm::Scratchpad> scrpd;
OneWireNg::ErrorCode ec = drv.readScratchpadSingle(scrpd);
if (ec == OneWireNg::EC_SUCCESS) {
printId(scrpd->getId());
printScratchpad(scrpd);
} else if (ec == OneWireNg::EC_CRC_ERROR)
Serial.println(" CRC error.");
#else
/* read sensors one-by-one */
Placeholder<DSTherm::Scratchpad> scrpd;
for (const auto& id: *ow) {
if (printId(id)) {
if (drv.readScratchpad(id, scrpd) == OneWireNg::EC_SUCCESS)
printScratchpad(scrpd);
else
Serial.println(" Read scratchpad error.");
}
}
#endif
Serial.println("----------");
delay(1000);
}