forked from pstolarz/OneWireNg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDallasTemperature.ino
150 lines (124 loc) · 3.6 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
/*
* Copyright (c) 2019-2021 Piotr Stolarz
* OneWireNg: Ardiono 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.
*/
#include "OneWireNg_CurrentPlatform.h"
#include "drivers/DSTherm.h"
#define OW_PIN 10
/*
* Set to true for parasitically powered sensors.
*/
#define PARASITE_POWER false
/*
* Uncomment for power provided by a switching
* transistor and controlled by this pin.
*/
//#define PWR_CTRL_PIN 9
static OneWireNg *ow = NULL;
static DSTherm *dsth = NULL;
/* 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(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->getTemp();
Serial.print("; Temp:");
if (temp < 0) {
temp = -temp;
Serial.print('-');
}
Serial.print(temp / 1000);
Serial.print('.');
Serial.print(temp % 1000);
Serial.print(" C");
Serial.println();
}
void setup()
{
#ifdef PWR_CTRL_PIN
ow = new OneWireNg_CurrentPlatform(OW_PIN, PWR_CTRL_PIN, false);
#else
ow = new OneWireNg_CurrentPlatform(OW_PIN, false);
#endif
dsth = new DSTherm(*ow);
delay(500);
Serial.begin(115200);
#if (CONFIG_MAX_SRCH_FILTERS > 0)
dsth->filterSupportedSlaves();
#endif
/*
* Uncomment to set common configuration for all sensors connected to
* the bus:
* - Th = 0, Tl = 0 (high/low alarm triggers),
* - Resolution: 12-bits.
*/
//dsth->writeScratchpadAll(0, 0, DSTherm::RES_12_BIT);
/*
* The configuration above is stored in volatile sensors scratchpad
* memory and will be lost after power unplug. Uncomment this line to
* store the configuration permanently in sensors EEPROM.
*
* NOTE: It will affect all sensors connected to the bus!
*/
//dsth->copyScratchpadAll(PARASITE_POWER);
}
void loop()
{
OneWireNg::Id id;
OneWireNg::ErrorCode ec;
MAKE_SCRATCHPAD(scrpd);
/* convert temperature on all sensors connected... */
dsth->convertTempAll(DSTherm::SCAN_BUS, PARASITE_POWER);
/* ...and read them one-by-one */
ow->searchReset();
do
{
ec = ow->search(id);
if (!(ec == OneWireNg::EC_MORE || ec == OneWireNg::EC_DONE))
break;
if (printId(id)) {
if (dsth->readScratchpad(id, scrpd) == OneWireNg::EC_SUCCESS) {
printScratchpad(scrpd);
} else {
Serial.println(" Invalid CRC!");
}
}
} while (ec == OneWireNg::EC_MORE);
Serial.println("----------");
delay(1000);
}