hydroponic-controller/include/temperature.h

174 lines
5.2 KiB
C
Raw Normal View History

2023-04-17 19:32:34 +00:00
#ifndef _TEMPERATURE_H_
#define _TEMPERATURE_H_
2024-06-25 19:10:12 +00:00
mqttValueTiming timing_temperature_reservoir;
2023-05-15 16:00:51 +00:00
mqttValueTiming timing_temperature_case;
2023-04-17 19:32:34 +00:00
#include <OneWire.h>
#include <DallasTemperature.h>
void printAddress(DeviceAddress deviceAddress);
//first address: 28FF6C1C7216058B
//second address:
2024-06-25 19:10:12 +00:00
//#define ONE_WIRE_BUS_PIN 18 //GPIO pin moved to platformio.ini
2023-04-17 19:32:34 +00:00
#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)
2024-06-25 19:10:12 +00:00
OneWire oneWire(ONE_WIRE_BUS_PIN);
2023-04-17 19:32:34 +00:00
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
#define TEMPMEAN_SIZE 16
uint16_t tempCmean_pos=0;
// arrays to hold device addresses
2024-05-04 11:04:05 +00:00
//DeviceAddress thermometerAir={0x28,0xFF,0x6C,0x1C,0x72,0x16,0x05,0x8B};
2024-06-25 19:10:12 +00:00
//DeviceAddress thermometerReservoirA={0x28,0xFF,0x6F,0x19,0x72,0x16,0x05,0x44}; //NFT
//DeviceAddress thermometerReservoirB={0x28,0xFF,0x30,0xBA,0x85,0x16,0x03,0xB5}; //DB
DeviceAddress thermometerReservoir=THERMOMETER_ADDR_RESERVOIR;
float tempC_reservoir; //last reading
float tempCmean_reservoir_array[TEMPMEAN_SIZE];
float tempCmean_reservoir=DEVICE_DISCONNECTED_C;
2024-05-04 11:04:05 +00:00
2023-04-17 19:32:34 +00:00
2024-06-25 19:10:12 +00:00
DeviceAddress thermometerCase=THERMOMETER_ADDR_CASE;
2023-05-15 16:00:51 +00:00
float tempC_case; //last reading
float tempCmean_case_array[TEMPMEAN_SIZE];
float tempCmean_case=DEVICE_DISCONNECTED_C;
2023-04-17 19:32:34 +00:00
void temperature_setup() {
2024-06-25 19:10:12 +00:00
timing_temperature_reservoir.minchange=0.0;
timing_temperature_reservoir.maxchange=0.5;
timing_temperature_reservoir.mintime=2*1000;
timing_temperature_reservoir.maxtime=60*60*1000;
2023-04-17 19:32:34 +00:00
2023-05-15 16:00:51 +00:00
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;
2023-04-17 19:32:34 +00:00
//initialize mean array
for (uint16_t i=0;i<TEMPMEAN_SIZE;i++) {
2024-06-25 19:10:12 +00:00
tempCmean_reservoir_array[i]=-127;
2023-05-15 16:00:51 +00:00
tempCmean_case_array[i]=-127;
2023-04-17 19:32:34 +00:00
}
sensors.begin();
delay(1000);
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" devices.");
2023-04-17 19:32:34 +00:00
delay(1000);
//Serial.print("Parasite power is: ");
//if (sensors.isParasitePowerMode()) Serial.println("ON");
//else Serial.println("OFF");
2023-04-17 19:32:34 +00:00
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");
2024-05-11 07:20:37 +00:00
String _text="Error: Device not found. id=";
_text.concat(i);
publishInfo("error/temperature",_text);
2023-04-17 19:32:34 +00:00
}else{
Serial.print("Found device. Address:");
printAddress(_addr);
}
Serial.println();
}
2024-06-25 19:10:12 +00:00
sensors.setResolution(thermometerReservoir, TEMPERATURE_PRECISION);
2023-05-15 16:00:51 +00:00
sensors.setResolution(thermometerCase, TEMPERATURE_PRECISION);
2023-04-17 19:32:34 +00:00
}
void temperature_loop(unsigned long loopmillis) {
2023-04-17 19:32:34 +00:00
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
2023-04-17 19:32:34 +00:00
Serial.println("Warn: Request Temperatures Timeout!");
2024-05-11 07:20:37 +00:00
publishInfo("error/temperature","Warn: Request Temperatures Timeout!");
2023-04-17 19:32:34 +00:00
flag_requestTemperatures=false;
2024-06-27 18:39:12 +00:00
last_read_ds18b20=loopmillis;
2023-04-17 19:32:34 +00:00
}
if (!flag_requestTemperatures) {
2024-06-27 18:39:12 +00:00
sensors.setWaitForConversion(false); //make async
sensors.requestTemperatures(); //this takes ~600ms in non async mode. about 2ms in async mode
sensors.setWaitForConversion(true);
2023-04-17 19:32:34 +00:00
flag_requestTemperatures=true;
}
if (sensors.isConversionComplete()) {
flag_requestTemperatures=false;
last_read_ds18b20=loopmillis;
2024-06-25 19:10:12 +00:00
tempC_reservoir = sensors.getTempC(thermometerReservoir);
if (tempC_reservoir == DEVICE_DISCONNECTED_C)
2023-04-17 19:32:34 +00:00
{
2024-06-25 19:10:12 +00:00
Serial.print(" Error reading: "); printAddress(thermometerReservoir); Serial.println();
publishInfo("error/temperature","Error reading thermometerReservoir");
2023-04-17 19:32:34 +00:00
}else{
2024-06-25 19:10:12 +00:00
tempCmean_reservoir_array[tempCmean_pos]=tempC_reservoir;
if (isValueArrayOKf(tempCmean_reservoir_array,TEMPMEAN_SIZE,DEVICE_DISCONNECTED_C)) {
tempCmean_reservoir=getMeanf(tempCmean_reservoir_array,TEMPMEAN_SIZE);
}else{
2024-06-25 19:10:12 +00:00
tempCmean_reservoir=DEVICE_DISCONNECTED_C;
}
2023-04-17 19:32:34 +00:00
}
2024-05-04 11:04:05 +00:00
2023-05-15 16:00:51 +00:00
tempC_case = sensors.getTempC(thermometerCase);
if (tempC_case == DEVICE_DISCONNECTED_C)
{
Serial.print(" Error reading: "); printAddress(thermometerCase);
2024-05-11 07:20:37 +00:00
publishInfo("error/temperature","Error reading thermometerCase");
2023-05-15 16:00:51 +00:00
}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;
}
}
2023-04-17 19:32:34 +00:00
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