forked from arendst/Tasmota
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxsns_ds18b20.ino
214 lines (188 loc) · 5.4 KB
/
xsns_ds18b20.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
xsns_ds18b20.ino - DS18B20 temperature sensor support for Sonoff-Tasmota
Copyright (C) 2017 Theo Arends
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef USE_DS18B20
/*********************************************************************************************\
* DS18B20 - Temperature
*
* Source: Marinus vd Broek https://github.com/ESP8266nu/ESPEasy and AlexTransit (CRC)
\*********************************************************************************************/
float dsb_mt = 0;
uint8_t dsb_reset()
{
uint8_t r;
uint8_t retries = 125;
pinMode(pin[GPIO_DSB], INPUT);
do { // wait until the wire is high... just in case
if (--retries == 0) {
return 0;
}
delayMicroseconds(2);
} while (!digitalRead(pin[GPIO_DSB]));
pinMode(pin[GPIO_DSB], OUTPUT);
digitalWrite(pin[GPIO_DSB], LOW);
delayMicroseconds(492); // Dallas spec. = Min. 480uSec. Arduino 500uSec.
pinMode(pin[GPIO_DSB], INPUT); // Float
delayMicroseconds(40);
r = !digitalRead(pin[GPIO_DSB]);
delayMicroseconds(420);
return r;
}
uint8_t dsb_read_bit(void)
{
uint8_t r;
pinMode(pin[GPIO_DSB], OUTPUT);
digitalWrite(pin[GPIO_DSB], LOW);
delayMicroseconds(3);
pinMode(pin[GPIO_DSB], INPUT); // let pin float, pull up will raise
delayMicroseconds(10);
r = digitalRead(pin[GPIO_DSB]);
delayMicroseconds(53);
return r;
}
uint8_t dsb_read(void)
{
uint8_t bitMask;
uint8_t r = 0;
for (bitMask = 0x01; bitMask; bitMask <<= 1) {
if (dsb_read_bit()) {
r |= bitMask;
}
}
return r;
}
void dsb_write_bit(uint8_t v)
{
if (v & 1) {
digitalWrite(pin[GPIO_DSB], LOW);
pinMode(pin[GPIO_DSB], OUTPUT);
delayMicroseconds(10);
digitalWrite(pin[GPIO_DSB], HIGH);
delayMicroseconds(55);
} else {
digitalWrite(pin[GPIO_DSB], LOW);
pinMode(pin[GPIO_DSB], OUTPUT);
delayMicroseconds(65);
digitalWrite(pin[GPIO_DSB], HIGH);
delayMicroseconds(5);
}
}
void dsb_write(uint8_t ByteToWrite)
{
uint8_t bitMask;
for (bitMask = 0x01; bitMask; bitMask <<= 1) {
dsb_write_bit((bitMask & ByteToWrite) ? 1 : 0);
}
}
uint8 dsb_crc(uint8 inp, uint8 crc)
{
inp ^= crc;
crc = 0;
if (inp & 0x1) crc ^= 0x5e;
if (inp & 0x2) crc ^= 0xbc;
if (inp & 0x4) crc ^= 0x61;
if (inp & 0x8) crc ^= 0xc2;
if (inp & 0x10) crc ^= 0x9d;
if (inp & 0x20) crc ^= 0x23;
if (inp & 0x40) crc ^= 0x46;
if (inp & 0x80) crc ^= 0x8c;
return crc;
}
void dsb_readTempPrep()
{
dsb_reset();
dsb_write(0xCC); // Skip ROM
dsb_write(0x44); // Start conversion
}
boolean dsb_readTemp(float &t)
{
int16_t DSTemp;
byte msb, lsb, crc, sign = 1;
if (!dsb_mt) {
t = NAN;
} else {
t = dsb_mt;
}
if (!dsb_read_bit()) { //check measurement end
addLog_P(LOG_LEVEL_DEBUG, PSTR("DSB: Sensor busy"));
return !isnan(t);
}
/*
dsb_reset();
dsb_write(0xCC); // Skip ROM
dsb_write(0x44); // Start conversion
delay(800);
*/
dsb_reset();
dsb_write(0xCC); // Skip ROM
dsb_write(0xBE); // Read scratchpad
lsb = dsb_read();
msb = dsb_read();
crc = dsb_crc(lsb, crc);
crc = dsb_crc(msb, crc);
crc = dsb_crc(dsb_read(), crc);
crc = dsb_crc(dsb_read(), crc);
crc = dsb_crc(dsb_read(), crc);
crc = dsb_crc(dsb_read(), crc);
crc = dsb_crc(dsb_read(), crc);
crc = dsb_crc(dsb_read(), crc);
crc = dsb_crc(dsb_read(), crc);
dsb_reset();
if (crc) { //check crc
addLog_P(LOG_LEVEL_DEBUG, PSTR("DSB: Sensor CRC error"));
} else {
DSTemp = (msb << 8) + lsb;
if (DSTemp > 2047) {
DSTemp = (~DSTemp) +1;
sign = -1;
}
t = convertTemp((float)sign * DSTemp * 0.0625);
}
if (!isnan(t)) dsb_mt = t;
return !isnan(t);
}
/*********************************************************************************************\
* Presentation
\*********************************************************************************************/
void dsb_mqttPresent(char* svalue, uint16_t ssvalue, uint8_t* djson)
{
char stemp1[10];
float t;
if (dsb_readTemp(t)) { // Check if read failed
dtostrf(t, 1, sysCfg.flag.temperature_resolution, stemp1);
snprintf_P(svalue, ssvalue, PSTR("%s, \"DS18B20\":{\"Temperature\":%s}"), svalue, stemp1);
*djson = 1;
#ifdef USE_DOMOTICZ
domoticz_sensor1(stemp1);
#endif // USE_DOMOTICZ
}
}
#ifdef USE_WEBSERVER
String dsb_webPresent()
{
// Needs TelePeriod to refresh data (Do not do it here as it takes too much time)
String page = "";
float st;
if (dsb_readTemp(st)) { // Check if read failed
char stemp[10];
char sensor[80];
dtostrf(st, 1, sysCfg.flag.temperature_resolution, stemp);
snprintf_P(sensor, sizeof(sensor), HTTP_SNS_TEMP, "DS18B20", stemp, tempUnit());
page += sensor;
}
dsb_readTempPrep();
return page;
}
#endif // USE_WEBSERVER
#endif // USE_DS18B20