-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathds18b20_sensor_tester.ino
115 lines (107 loc) · 3.34 KB
/
ds18b20_sensor_tester.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
#include <OneWire.h>
#include <DallasTemperature.h>
//Pin 2
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
struct error{
int number;
unsigned char* address;
float temperature;
unsigned long timeFromStart;
};
int currentError = 0;
float realTemp;
int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
void setup() {
// Start serial port
Serial.begin(9600);
// Start up the library
sensors.begin();
numberOfDevices = sensors.getDeviceCount();
Serial.print("Locating devices...");
Serial.print(" Found ");
Serial.print(numberOfDevices);
Serial.println(" devices.");
Serial.print("Enter the ambient temperature: ");
while (Serial.available() == 0)
realTemp = Serial.parseFloat();
if (Serial.available() > 0)
Serial.println(realTemp);
}
void loop () {
numberOfDevices = sensors.getDeviceCount();
while (Serial.available() > 0){
for(int i = 0; i < numberOfDevices; i++) {
// Search the wire for address
if(!sensors.getAddress(tempDeviceAddress, i)) {
Serial.print("Found ghost device at ");
Serial.print(i+1);
Serial.print(" but could not detect address. Check power and cabling.");
}
}
if (numberOfDevices > 0){
Serial.println("Readings:");
Serial.println("Sensor | Address | Temperature");
}
else
Serial.print("Sensor cannot be checked.");
sensors.requestTemperatures(); // Send the command to get temperatures
float tempC;
float tempe[numberOfDevices];
unsigned char* addr[numberOfDevices];
unsigned long time = millis();
struct error errors[100];
// Loop through each device
for ( int i = 0; i < numberOfDevices; i++ ) {
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i)){
tempC = sensors.getTempC(tempDeviceAddress);
tempe[i] = tempC;
addr[i] = tempDeviceAddress;
Serial.print(" ");
Serial.print(i+1);
Serial.print(" ");
printAddress(addr[i]);
Serial.print(" ");
Serial.println(tempe[i]);
}
}
Serial.println("______________________________________");
Serial.println("Errors:");
Serial.print("Sensor | Address | Temperature | Time[min]");
Serial.println();
for (int j = 0; j < numberOfDevices; j++){
if(sensors.getAddress(tempDeviceAddress, j) && abs(realTemp - tempe[j]) > 2){
currentError++;
errors[currentError].number = j;
errors[currentError].address = addr[j];
errors[currentError].temperature= tempe[j];
errors[currentError].timeFromStart = (time/60000);
}
for (int l = 0; l < currentError; l++){
if(j == errors[l+1].number){
Serial.print(" ");
Serial.print(j+1);
Serial.print(" ");
printAddress(errors[l+1].address);
Serial.print(" ");
Serial.print(errors[l+1].temperature);
Serial.print(" ");
Serial.println(errors[l+1].timeFromStart);
}
}
}
Serial.println();
delay(5000);
}
}
// function to print a device address
void printAddress(DeviceAddress deviceAddress) {
for (uint8_t i = 0; i < 8; i++) {
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}