-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTemperatureService.cpp
93 lines (69 loc) · 3.15 KB
/
TemperatureService.cpp
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
#include "TemperatureService.h"
//const char * TemperatureService::ADDRESS_OUT = "28afa4459207029e";
//const char * TemperatureService::ADDRESS_IN = "282c4445920e0245";
const char * TemperatureService::ADDRESS_OUT = "28afa4459207029e"; // Output air t sernsor
const char * TemperatureService::ADDRESS_BOARD = "2838c145920c024a"; // on board t sensor
const char * TemperatureService::ADDRESS_IN = "282522459205027d"; // Input air t sensor
TemperatureService* TemperatureService::instance = NULL;
TemperatureService::TemperatureService()
{
instance = this;
}
void TemperatureService::init(int _pin)
{
pin = _pin;
oneWire = new OneWire(pin);
DS18B20 = new DallasTemperature(oneWire);
DS18B20->begin();
// Loop through each device, save its address
Serial.print("DS18B20 device count: ");
Serial.println(DS18B20->getDeviceCount());
for (int i = 0; i < DS18B20->getDeviceCount(); i++) {
// save device address
DS18B20->getAddress(addresses[i], i);
Serial.println(String("device: ") + getAddressToString(addresses[i]));
}
this->DS18B20->setWaitForConversion(true); // Wait for measurement
this->DS18B20->requestTemperatures(); // Initiate the temperature measurement
this->ready = true;
}
float TemperatureService::getTemperature(int deviceIndex)
{
if(deviceIndex >= this->DS18B20->getDeviceCount() || deviceIndex < 0)
{
return 0.0;
}
//return this->temperatures[deviceIndex];
return this->queues[deviceIndex].average();
}
float TemperatureService::getTemperatureByAddress(const char * sensorAddress)
{
int index = this->getDeviceIndex(sensorAddress);
return this->getTemperature(index);
}
int TemperatureService::getDeviceIndex(const char * sensorAddress)
{
for(int i = 0; i < DS18B20->getDeviceCount(); i++){
if (strcmp(getAddressToString(addresses[i]).c_str(), sensorAddress) == 0){
return i;
}
}
return -1;
}
void TemperatureService::loop()
{
long now = millis();
if ( now - lastUpdateTime > interval ) { // Take a measurement at a fixed time (tempMeasInterval = 1000ms, 1s)
for (int i = 0; i < DS18B20->getDeviceCount(); i++) {
float temp = this->DS18B20->getTempC( addresses[i] ); // Measuring temperature in Celsius
this->temperatures[i] = round(temp * 10) / 10; // save the measured value to the array
this->queues[i].add(temp); // Add to queue
Serial.print(String() + i + ") " + getAddressToString(addresses[i]) + " = " + this->temperatures[i] + " ºC \t");
}
Serial.println(String(" dev count: ") + DS18B20->getDeviceCount()) ;
// request next measurement
this->DS18B20->setWaitForConversion(false); //No waiting for measurement
this->DS18B20->requestTemperatures(); //Initiate the temperature measurement
lastUpdateTime = millis(); //Remember the last time measurement
}
}