From 576860ae3191903032bb31c34c10e23c051b209a Mon Sep 17 00:00:00 2001 From: Fisch Date: Mon, 17 Apr 2023 21:32:34 +0200 Subject: [PATCH] move sensors to separate files --- include/ec.h | 41 +++++ include/flow.h | 38 +++++ include/helpfunctions.h | 117 ++++++++++++++ include/temperature.h | 131 ++++++++++++++++ include/waterlevel.h | 44 ++++++ src/main.cpp | 335 +++------------------------------------- 6 files changed, 392 insertions(+), 314 deletions(-) create mode 100644 include/ec.h create mode 100644 include/flow.h create mode 100644 include/helpfunctions.h create mode 100644 include/temperature.h create mode 100644 include/waterlevel.h diff --git a/include/ec.h b/include/ec.h new file mode 100644 index 0000000..e2da759 --- /dev/null +++ b/include/ec.h @@ -0,0 +1,41 @@ +#ifndef _EC_H_ +#define _EC_H_ + +#include + + + +#define EC_PIN_ADC 4 +#define EC_PIN_FREQ 5 +#define EC_PWM_CH 0 +#define EC_RESOLUTION 8 +#define EC_FREQUENCY 5000 + +#define EC_ARRAY_SIZE 1024 +uint16_t ec_array[EC_ARRAY_SIZE]; +uint16_t ec_array_pos=0; +unsigned long last_read_ec=0; +#define EC_READ_INTERVAL 1 + +void ec_setup() { + pinMode(EC_PIN_ADC,INPUT); + + ledcSetup(EC_PWM_CH, EC_FREQUENCY, EC_RESOLUTION); + ledcAttachPin(EC_PIN_FREQ, EC_PWM_CH); + ledcWrite(EC_PWM_CH, 127); + +} + +void ec_loop(unsigned long loopmillis, unsigned long pInterval) { + if (loopmillis>last_read_ec+pInterval) { + last_read_ec=loopmillis; + ec_array_pos++; + flag_print= ec_array_pos==EC_ARRAY_SIZE; + ec_array_pos%=EC_ARRAY_SIZE; + ec_array[ec_array_pos]=analogRead(EC_PIN_ADC); + + //Serial.print(ec_array[ec_array_pos]); Serial.print(" "); + } +} + +#endif \ No newline at end of file diff --git a/include/flow.h b/include/flow.h new file mode 100644 index 0000000..53f2e44 --- /dev/null +++ b/include/flow.h @@ -0,0 +1,38 @@ +#ifndef _FLOW_H_ +#define _FLOW_H_ + +#define FLOW_PIN 19 +uint16_t flow_counter=0; //maximum counts/s measured with Eden 128 Pump was 171 +void IRAM_ATTR isr_flow(); +unsigned long last_read_flow=0; +#define READINTERVAL_FLOW 1000 +float flow_factor=7.5; //F=7.5*flowrate[L/min] +float flow; + +uint32_t flow_counter_sum=0; + + + +void flow_setup() { + + pinMode(FLOW_PIN, INPUT_PULLUP); + attachInterrupt(FLOW_PIN, isr_flow, CHANGE); +} + +void flow_loop(unsigned long loopmillis, unsigned long pInterval) { + if (loopmillis>=last_read_flow+pInterval) { + flow=flow_counter*1000.0/(loopmillis-last_read_flow)/2.0; //Frequency [Hz] + flow/=flow_factor; //[L/min] + + flow_counter=0; + last_read_flow=loopmillis; + } +} + +void IRAM_ATTR isr_flow() { + flow_counter++; + flow_counter_sum++; +} + + +#endif \ No newline at end of file diff --git a/include/helpfunctions.h b/include/helpfunctions.h new file mode 100644 index 0000000..b074498 --- /dev/null +++ b/include/helpfunctions.h @@ -0,0 +1,117 @@ +#ifndef _HELPFUNCTIONS_H_ +#define _HELPFUNCTIONS_H_ + +#include +#include + +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); +float getMaxf(float *parray,uint16_t psize); +float getMinf(float *parray, uint16_t psize); +bool isValueArrayOK(uint16_t *parray,uint16_t psize, uint16_t pcheck); +bool isValueArrayOKf(float *parray,uint16_t psize, float pcheck); +float getFilteredf(float *parray,uint16_t psize, uint16_t pcutOff); + + +float getMean(uint16_t *parray,uint16_t psize) { + double mean=0; + for (uint16_t i=0;imax) { + max=parray[i]; + } + } + + return max; +} + +float getMinf(float *parray, uint16_t psize) { + float min=65535; + for (uint16_t i=0;imax) { + max=parray[i]; + } + } + + return max; +} + + +float getFilteredf(float *parray,uint16_t psize, uint16_t pcutOff) { + //cuts off lowest and highest pcutOff values from array, then returns the mean of the psize-2*pcutOff center values. + //pcutOff < psize/2 + + float _copy[psize]; + std::copy(parray,parray + psize, _copy); + sortArray(_copy,psize); + + double mean=0; + for (uint16_t i=pcutOff;i +#include + +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 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]; + + +void temperature_setup() { + + //initialize mean array + for (uint16_t i=0;ilast_read_ds18b20+pInterval) { + if (loopmillis>last_read_ds18b20+pInterval*10) { //timeout + Serial.println("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 = 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; + } + + } +} + + +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 \ No newline at end of file diff --git a/include/waterlevel.h b/include/waterlevel.h new file mode 100644 index 0000000..c34d792 --- /dev/null +++ b/include/waterlevel.h @@ -0,0 +1,44 @@ +#ifndef _WATERLEVEL_H_ +#define _WATERLEVEL_H_ + + +#include +#define HCSR04_PIN_ECHO 17 +#define HCSR04_PIN_TRIGGER 16 +#define HCSR04_TIMEOUT 5000 //default is 100000 (uS) +#define READINTERVAL_HCSR04 100 + +#define WATERLEVELMEAN_SIZE 32 +float waterlevelMean[WATERLEVELMEAN_SIZE]; +uint16_t waterlevelMean_pos=0; + + + +void waterlevel_setup() { + + //HCSR04.begin(HCSR04_PIN_TRIGGER, HCSR04_PIN_ECHO); + HCSR04.begin(HCSR04_PIN_TRIGGER, HCSR04_PIN_ECHO,HCSR04_TIMEOUT, HCSR04.eUltraSonicUnlock_t::unlockSkip); + for (uint16_t i=0;i=last_read_hcsr04+pInterval) { + last_read_hcsr04=loopmillis; + float temperature=20.0; + if (tempC_air!=DEVICE_DISCONNECTED_C && isValueArrayOKf(tempCmean_air,TEMPMEAN_SIZE,DEVICE_DISCONNECTED_C)) { //sensor ok + temperature=getMeanf(tempCmean_air,TEMPMEAN_SIZE); + } + + double* distances = HCSR04.measureDistanceMm(temperature); + + waterlevelMean[waterlevelMean_pos]=distances[0]; + waterlevelMean_pos++; + waterlevelMean_pos%=WATERLEVELMEAN_SIZE; + } +} +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 4b6900b..9d85f6b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,71 +1,22 @@ #include -#include + +bool flag_print=false; + +#include "helpfunctions.h" // ######## EC -#define EC_PIN_ADC 4 -#define EC_PIN_FREQ 5 -#define EC_PWM_CH 0 -#define EC_RESOLUTION 8 -#define EC_FREQUENCY 5000 - -#define EC_ARRAY_SIZE 1024 -uint16_t ec_array[EC_ARRAY_SIZE]; -uint16_t ec_array_pos=0; -unsigned long last_read_ec=0; -#define EC_READ_INTERVAL 1 +#include "ec.h" // ######## 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]; +#include "temperature.h" // ######## Water Level -#include -#define HCSR04_PIN_ECHO 17 -#define HCSR04_PIN_TRIGGER 16 -#define HCSR04_TIMEOUT 5000 //default is 100000 (uS) -#define READINTERVAL_HCSR04 100 - -#define WATERLEVELMEAN_SIZE 32 -float waterlevelMean[WATERLEVELMEAN_SIZE]; -uint16_t waterlevelMean_pos=0; +#include "waterlevel.h" // ######## Flow Rate -#define FLOW_PIN 19 -uint16_t flow_counter=0; //maximum counts/s measured with Eden 128 Pump was 171 -void IRAM_ATTR isr_flow(); -unsigned long last_read_flow=0; -#define READINTERVAL_FLOW 1000 -float flow_factor=7.5; //F=7.5*flowrate[L/min] -float flow; - -uint32_t flow_counter_sum=0; +#include "flow.h" unsigned long last_print=0; @@ -73,84 +24,22 @@ unsigned long last_print=0; -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); -float getMaxf(float *parray,uint16_t psize); -float getMinf(float *parray, uint16_t psize); -bool isValueArrayOK(uint16_t *parray,uint16_t psize, uint16_t pcheck); -bool isValueArrayOKf(float *parray,uint16_t psize, float pcheck); -float getFilteredf(float *parray,uint16_t psize, uint16_t pcutOff); -void printAddress(DeviceAddress deviceAddress); -void printTemperature(DeviceAddress deviceAddress); -void printResolution(DeviceAddress deviceAddress); -void printData(DeviceAddress deviceAddress); void setup() { Serial.begin(115200); - pinMode(EC_PIN_ADC,INPUT); - ledcSetup(EC_PWM_CH, EC_FREQUENCY, EC_RESOLUTION); - ledcAttachPin(EC_PIN_FREQ, EC_PWM_CH); - ledcWrite(EC_PWM_CH, 127); + ec_setup(); - //HCSR04.begin(HCSR04_PIN_TRIGGER, HCSR04_PIN_ECHO); - HCSR04.begin(HCSR04_PIN_TRIGGER, HCSR04_PIN_ECHO,HCSR04_TIMEOUT, HCSR04.eUltraSonicUnlock_t::unlockSkip); - for (uint16_t i=0;ilast_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; - ec_array[ec_array_pos]=analogRead(EC_PIN_ADC); - - //Serial.print(ec_array[ec_array_pos]); Serial.print(" "); - } - - - 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(); //this takes ~600ms - 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; - } - - } - + ec_loop(loopmillis, EC_READ_INTERVAL); + temperature_loop(loopmillis, READINTERVAL_DS18B20); - static unsigned long last_read_hcsr04; - if (loopmillis>=last_read_hcsr04+READINTERVAL_HCSR04) { - last_read_hcsr04=loopmillis; - float temperature=20.0; - if (tempC_air!=DEVICE_DISCONNECTED_C && isValueArrayOKf(tempCmean_air,TEMPMEAN_SIZE,DEVICE_DISCONNECTED_C)) { //sensor ok - temperature=getMeanf(tempCmean_air,TEMPMEAN_SIZE); - } - - double* distances = HCSR04.measureDistanceMm(temperature); - - waterlevelMean[waterlevelMean_pos]=distances[0]; - waterlevelMean_pos++; - waterlevelMean_pos%=WATERLEVELMEAN_SIZE; - } - - - static uint16_t _last_flowconter; //for debugging - if (loopmillis>=last_read_flow+READINTERVAL_FLOW) { - flow=flow_counter*1000.0/(loopmillis-last_read_flow)/2.0; //Frequency [Hz] - flow/=flow_factor; //[L/min] - _last_flowconter=flow_counter; //for debugging - flow_counter=0; - last_read_flow=loopmillis; - } + waterlevel_loop(loopmillis, READINTERVAL_HCSR04); + + + flow_loop(loopmillis, READINTERVAL_FLOW); + if (loopmillis>last_print+500) { @@ -274,7 +97,7 @@ void loop() { Serial.print("\t waiting for distance"); } - Serial.print("\t Flow="); Serial.print(flow,2); Serial.print(" ("); Serial.print(_last_flowconter); Serial.print(")"); + Serial.print("\t Flow="); Serial.print(flow,2); Serial.print("\t Flowsum="); Serial.print(flow_counter_sum); @@ -284,119 +107,3 @@ void loop() { } } - -float getMean(uint16_t *parray,uint16_t psize) { - double mean=0; - for (uint16_t i=0;imax) { - max=parray[i]; - } - } - - return max; -} - -float getMinf(float *parray, uint16_t psize) { - float min=65535; - for (uint16_t i=0;imax) { - max=parray[i]; - } - } - - return max; -} - - - - -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); - } -} - - -void IRAM_ATTR isr_flow() { - flow_counter++; - flow_counter_sum++; -} - -float getFilteredf(float *parray,uint16_t psize, uint16_t pcutOff) { - //cuts off lowest and highest pcutOff values from array, then returns the mean of the psize-2*pcutOff center values. - //pcutOff < psize/2 - - float _copy[psize]; - std::copy(parray,parray + psize, _copy); - sortArray(_copy,psize); - - double mean=0; - for (uint16_t i=pcutOff;i