197 lines
6.1 KiB
C
197 lines
6.1 KiB
C
#ifndef _TEMPERATURE_H_
|
|
#define _TEMPERATURE_H_
|
|
|
|
|
|
mqttValueTiming timing_temperature_reservoir_a;
|
|
mqttValueTiming timing_temperature_reservoir_b;
|
|
mqttValueTiming timing_temperature_case;
|
|
|
|
#include <OneWire.h>
|
|
#include <DallasTemperature.h>
|
|
|
|
void printAddress(DeviceAddress deviceAddress);
|
|
|
|
//first address: 28FF6C1C7216058B
|
|
//second address:
|
|
|
|
#define ONE_WIRE_BUS 18 //GPIO pin
|
|
#define TEMPERATURE_PRECISION 12 //max is 12
|
|
#define READINTERVAL_DS18B20 1000 //ms
|
|
|
|
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
|
|
OneWire oneWire(ONE_WIRE_BUS);
|
|
|
|
// Pass our oneWire reference to Dallas Temperature.
|
|
DallasTemperature sensors(&oneWire);
|
|
|
|
|
|
#define TEMPMEAN_SIZE 16
|
|
uint16_t tempCmean_pos=0;
|
|
// arrays to hold device addresses
|
|
DeviceAddress thermometerReservoirA={0x28,0xFF,0x6F,0x19,0x72,0x16,0x05,0x44};
|
|
float tempC_reservoir_a; //last reading
|
|
float tempCmean_reservoir_a_array[TEMPMEAN_SIZE];
|
|
float tempCmean_reservoir_a=DEVICE_DISCONNECTED_C;
|
|
|
|
|
|
|
|
//DeviceAddress thermometerAir={0x28,0xFF,0x6C,0x1C,0x72,0x16,0x05,0x8B};
|
|
|
|
DeviceAddress thermometerReservoirB={0x28,0xFF,0x30,0xBA,0x85,0x16,0x03,0xB5};
|
|
float tempC_reservoir_b; //last reading
|
|
float tempCmean_reservoir_b_array[TEMPMEAN_SIZE];
|
|
float tempCmean_reservoir_b=DEVICE_DISCONNECTED_C;
|
|
|
|
DeviceAddress thermometerCase={0x10,0x7E,0x22,0x99,0x01,0x08,0x00,0xA4};
|
|
float tempC_case; //last reading
|
|
float tempCmean_case_array[TEMPMEAN_SIZE];
|
|
float tempCmean_case=DEVICE_DISCONNECTED_C;
|
|
|
|
|
|
|
|
|
|
void temperature_setup() {
|
|
timing_temperature_reservoir_a.minchange=0.0;
|
|
timing_temperature_reservoir_a.maxchange=0.5;
|
|
timing_temperature_reservoir_a.mintime=2*1000;
|
|
timing_temperature_reservoir_a.maxtime=60*60*1000;
|
|
|
|
timing_temperature_reservoir_b.minchange=0.0;
|
|
timing_temperature_reservoir_b.maxchange=0.5;
|
|
timing_temperature_reservoir_b.mintime=2*1000;
|
|
timing_temperature_reservoir_b.maxtime=60*60*1000;
|
|
|
|
|
|
timing_temperature_case.minchange=0.0;
|
|
timing_temperature_case.maxchange=0.5;
|
|
timing_temperature_case.mintime=2*1000;
|
|
timing_temperature_case.maxtime=60*60*1000;
|
|
|
|
//initialize mean array
|
|
for (uint16_t i=0;i<TEMPMEAN_SIZE;i++) {
|
|
tempCmean_reservoir_a_array[i]=-127;
|
|
tempCmean_reservoir_b_array[i]=-127;
|
|
tempCmean_case_array[i]=-127;
|
|
}
|
|
|
|
sensors.begin();
|
|
delay(1000);
|
|
|
|
|
|
Serial.print("Locating devices...");
|
|
Serial.print("Found ");
|
|
Serial.print(sensors.getDeviceCount(), DEC);
|
|
Serial.println(" devices.");
|
|
|
|
delay(1000);
|
|
|
|
//Serial.print("Parasite power is: ");
|
|
//if (sensors.isParasitePowerMode()) Serial.println("ON");
|
|
//else Serial.println("OFF");
|
|
|
|
delay(1000);
|
|
|
|
|
|
//Just search for devices. Only needed when connecting a new sensor to find the address
|
|
oneWire.reset_search();
|
|
|
|
for (uint8_t i=0;i<sensors.getDeviceCount();i++){
|
|
DeviceAddress _addr;
|
|
if (!oneWire.search(_addr)) {
|
|
Serial.print("Error: Device not found");
|
|
String _text="Error: Device not found. id=";
|
|
_text.concat(i);
|
|
publishInfo("error/temperature",_text);
|
|
}else{
|
|
Serial.print("Found device. Address:");
|
|
printAddress(_addr);
|
|
}
|
|
Serial.println();
|
|
|
|
}
|
|
|
|
sensors.setResolution(thermometerReservoirA, TEMPERATURE_PRECISION);
|
|
sensors.setResolution(thermometerReservoirB, TEMPERATURE_PRECISION);
|
|
sensors.setResolution(thermometerCase, TEMPERATURE_PRECISION);
|
|
}
|
|
|
|
|
|
void temperature_loop(unsigned long loopmillis) {
|
|
|
|
static unsigned long last_read_ds18b20;
|
|
static bool flag_requestTemperatures=false;
|
|
if (loopmillis>last_read_ds18b20+READINTERVAL_DS18B20) {
|
|
if (loopmillis>last_read_ds18b20+READINTERVAL_DS18B20*10) { //timeout
|
|
Serial.println("Warn: Request Temperatures Timeout!");
|
|
publishInfo("error/temperature","Warn: Request Temperatures Timeout!");
|
|
flag_requestTemperatures=false;
|
|
}
|
|
if (!flag_requestTemperatures) {
|
|
sensors.requestTemperatures(); //this takes ~600ms
|
|
flag_requestTemperatures=true;
|
|
}
|
|
if (sensors.isConversionComplete()) {
|
|
flag_requestTemperatures=false;
|
|
last_read_ds18b20=loopmillis;
|
|
|
|
tempC_reservoir_a = sensors.getTempC(thermometerReservoirA);
|
|
if (tempC_reservoir_a == DEVICE_DISCONNECTED_C)
|
|
{
|
|
Serial.print(" Error reading: "); printAddress(thermometerReservoirA);
|
|
publishInfo("error/temperature","Error reading thermometerReservoirA");
|
|
}else{
|
|
tempCmean_reservoir_a_array[tempCmean_pos]=tempC_reservoir_a;
|
|
if (isValueArrayOKf(tempCmean_reservoir_a_array,TEMPMEAN_SIZE,DEVICE_DISCONNECTED_C)) {
|
|
tempCmean_reservoir_a=getMeanf(tempCmean_reservoir_a_array,TEMPMEAN_SIZE);
|
|
}else{
|
|
tempCmean_reservoir_a=DEVICE_DISCONNECTED_C;
|
|
}
|
|
}
|
|
|
|
|
|
tempC_reservoir_b = sensors.getTempC(thermometerReservoirB);
|
|
if (tempC_reservoir_b == DEVICE_DISCONNECTED_C)
|
|
{
|
|
Serial.print(" Error reading: "); printAddress(thermometerReservoirB);
|
|
publishInfo("error/temperature","Error reading thermometerReservoirB");
|
|
}else{
|
|
tempCmean_reservoir_b_array[tempCmean_pos]=tempC_reservoir_b;
|
|
if (isValueArrayOKf(tempCmean_reservoir_b_array,TEMPMEAN_SIZE,DEVICE_DISCONNECTED_C)) {
|
|
tempCmean_reservoir_b=getMeanf(tempCmean_reservoir_b_array,TEMPMEAN_SIZE);
|
|
}else{
|
|
tempCmean_reservoir_b=DEVICE_DISCONNECTED_C;
|
|
}
|
|
}
|
|
|
|
tempC_case = sensors.getTempC(thermometerCase);
|
|
if (tempC_case == DEVICE_DISCONNECTED_C)
|
|
{
|
|
Serial.print(" Error reading: "); printAddress(thermometerCase);
|
|
publishInfo("error/temperature","Error reading thermometerCase");
|
|
}else{
|
|
tempCmean_case_array[tempCmean_pos]=tempC_case;
|
|
if (isValueArrayOKf(tempCmean_case_array,TEMPMEAN_SIZE,DEVICE_DISCONNECTED_C)) {
|
|
tempCmean_case=getMeanf(tempCmean_case_array,TEMPMEAN_SIZE);
|
|
}else{
|
|
tempCmean_case=DEVICE_DISCONNECTED_C;
|
|
}
|
|
}
|
|
|
|
tempCmean_pos++;
|
|
tempCmean_pos%=TEMPMEAN_SIZE;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
void printAddress(DeviceAddress deviceAddress)
|
|
{
|
|
for (uint8_t i = 0; i < 8; i++)
|
|
{
|
|
// zero pad the address if necessary
|
|
if (deviceAddress[i] < 16) Serial.print("0");
|
|
Serial.print(deviceAddress[i], HEX);
|
|
}
|
|
}
|
|
#endif |