From ca7ccc023d2b607d092849e444b6ef40ac5b4d71 Mon Sep 17 00:00:00 2001 From: Fisch Date: Tue, 4 Apr 2023 07:53:20 +0200 Subject: [PATCH] add ds18b20 temperature sensors --- platformio.ini | 5 +- src/main.cpp | 219 ++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 204 insertions(+), 20 deletions(-) diff --git a/platformio.ini b/platformio.ini index 7997102..0c55a85 100644 --- a/platformio.ini +++ b/platformio.ini @@ -13,4 +13,7 @@ platform = espressif32 board = esp32doit-devkit-v1 framework = arduino -monitor_speed = 115200 \ No newline at end of file +monitor_speed = 115200 + +lib_deps = + https://github.com/milesburton/Arduino-Temperature-Control-Library/ \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index e2a26ae..485b09a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,7 @@ #include + +// ######## EC #define EC_PIN_ADC 4 #define EC_PIN_FREQ 5 #define EC_PWM_CH 0 @@ -9,14 +11,61 @@ #define EC_ARRAY_SIZE 1024 uint16_t ec_array[EC_ARRAY_SIZE]; uint16_t ec_array_pos=0; -unsigned long ec_last_read=0; +unsigned long last_read_ec=0; #define EC_READ_INTERVAL 1 +// ######## Temperature +#include +#include + +//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 thermometerReservoir={0x28,0xFF,0x30,0xBA,0x85,0x16,0x03,0xB5}; +float tempC_reservoir; +float tempCmean_reservoir[TEMPMEAN_SIZE]; + +DeviceAddress thermometerAir={0x28,0xFF,0x6C,0x1C,0x72,0x16,0x05,0x8B}; +float tempC_air; +float tempCmean_air[TEMPMEAN_SIZE]; + + + + + unsigned long last_print=0; -float getMean(uint16_t* parray); -uint16_t getMin(uint16_t *parray); -uint16_t getMax(uint16_t *parray); + + +float getMean(uint16_t *parray,uint16_t psize); +float getMeanf(float *parray,uint16_t psize); +uint16_t getMin(uint16_t *parray, uint16_t psize); +uint16_t getMax(uint16_t *parray, uint16_t psize); +bool isTempArrayOK(uint16_t *parray,uint16_t psize, uint16_t pcheck); +bool isTempArrayOKf(float *parray,uint16_t psize, float pcheck); + + +void printAddress(DeviceAddress deviceAddress); +void printTemperature(DeviceAddress deviceAddress); +void printResolution(DeviceAddress deviceAddress); +void printData(DeviceAddress deviceAddress); + + + void setup() { Serial.begin(115200); @@ -25,6 +74,51 @@ void setup() { ledcSetup(EC_PWM_CH, EC_FREQUENCY, EC_RESOLUTION); ledcAttachPin(EC_PIN_FREQ, EC_PWM_CH); ledcWrite(EC_PWM_CH, 127); + + //initialize mean array + for (uint16_t i=0;iec_last_read+EC_READ_INTERVAL) { - ec_last_read=loopmillis; + if (loopmillis>last_read_ec+EC_READ_INTERVAL) { + last_read_ec=loopmillis; ec_array_pos++; flag_print= ec_array_pos==EC_ARRAY_SIZE; ec_array_pos%=EC_ARRAY_SIZE; @@ -42,30 +136,104 @@ void loop() { //Serial.print(ec_array[ec_array_pos]); Serial.print(" "); } - - //if (flag_print) { - if (loopmillis>last_print+250) { + 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!"); + flag_requestTemperatures=false; + } + if (!flag_requestTemperatures) { + sensors.requestTemperatures(); + flag_requestTemperatures=true; + } + if (sensors.isConversionComplete()) { + flag_requestTemperatures=false; + last_read_ds18b20=loopmillis; + + tempC_reservoir = sensors.getTempC(thermometerReservoir); + if (tempC_reservoir == DEVICE_DISCONNECTED_C) + { + Serial.print(" Error reading: "); printAddress(thermometerReservoir); + }else{ + tempCmean_reservoir[tempCmean_pos]=tempC_reservoir; + } + + tempC_air = sensors.getTempC(thermometerAir); + if (tempC_air == DEVICE_DISCONNECTED_C) + { + Serial.print(" Error reading: "); printAddress(thermometerReservoir); + }else{ + tempCmean_air[tempCmean_pos]=tempC_air; + } + + tempCmean_pos++; + tempCmean_pos%=TEMPMEAN_SIZE; + } + + + } + + if (loopmillis>last_print+500) { last_print=loopmillis; - Serial.print(getMean(ec_array),3); - Serial.print("\t"); Serial.print(getMax(ec_array) - getMin(ec_array)); + + Serial.print("EC="); + Serial.print(getMean(ec_array,EC_ARRAY_SIZE),3); + Serial.print("\t spread="); Serial.print(getMax(ec_array,EC_ARRAY_SIZE) - getMin(ec_array,EC_ARRAY_SIZE)); + + if (isTempArrayOKf(tempCmean_reservoir,TEMPMEAN_SIZE,DEVICE_DISCONNECTED_C)){ + Serial.print("\t Treservoir="); Serial.print(getMeanf(tempCmean_reservoir,TEMPMEAN_SIZE)); Serial.print("\t Tair="); Serial.print(getMeanf(tempCmean_air,TEMPMEAN_SIZE)); + }else{ + Serial.print("\t waiting for temperature array"); + } + + Serial.println(); + } } -float getMean(uint16_t *parray) { +float getMean(uint16_t *parray,uint16_t psize) { double mean=0; - for (uint16_t i=0;imax) { max=parray[i]; } } return max; -} \ No newline at end of file +} + + + + +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); + } +}