-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDS18B20Codes.ino
40 lines (35 loc) · 941 Bytes
/
DS18B20Codes.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
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into pin D1 on the ESP8266 12-E - GPIO 5
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
bool DS18B20_Init(){
Serial.println("Start inicialize DS18B20");
DS18B20.begin();
delay(1000);
Serial.println("DS18B20 sensor was initialized.");
return true;
}
String DS18B20_ReadStrTemperature(){
double data = DS18B20_ReadTemperature();
if(data == 8888)
return "NaN";
char charData[7];
dtostrf(data, 2, 2, charData);
return String(charData);
}
double DS18B20_ReadTemperature(){
for(int i=0;i<22;i++) {
DS18B20.requestTemperatures();
double data = DS18B20.getTempCByIndex(0);
if (data != 85.0 && data != (-127.0))
return data;
delay(100);
if(i%10==0){
Serial.println("DS18B20: 10 errors in line. Reinit!");
DS18B20_Init();
}
}
return 8888;
}