hydroponic-controller/src/main.cpp

209 lines
4.5 KiB
C++
Raw Normal View History

2023-03-30 15:55:45 +00:00
#include <Arduino.h>
2023-04-04 05:53:20 +00:00
2023-04-17 19:32:34 +00:00
bool flag_print=false;
2023-04-04 05:53:20 +00:00
2023-04-17 19:32:34 +00:00
#include "helpfunctions.h"
#include "ADS1X15.h"
ADS1115 ADS(0x48);
2023-04-04 05:53:20 +00:00
2023-04-17 19:32:34 +00:00
// ######## EC
#include "ec.h"
2023-04-04 05:53:20 +00:00
2023-04-17 19:32:34 +00:00
// ######## Temperature
#include "temperature.h"
2023-04-04 05:53:20 +00:00
2023-04-05 07:22:57 +00:00
// ######## Water Level
2023-04-17 19:32:34 +00:00
#include "waterlevel.h"
2023-04-04 05:53:20 +00:00
2023-04-07 16:03:41 +00:00
// ######## Flow Rate
2023-04-17 19:32:34 +00:00
#include "flow.h"
2023-04-07 16:03:41 +00:00
// ######## Soilmoisture
#include "soilmoisture.h"
2023-04-07 16:03:41 +00:00
2023-03-30 15:55:45 +00:00
unsigned long last_print=0;
2023-04-04 05:53:20 +00:00
2023-04-07 16:03:41 +00:00
2023-04-04 05:53:20 +00:00
#define PIN_BUTTON 12
#define PIN_LED 2
2023-04-04 05:53:20 +00:00
2023-03-30 15:55:45 +00:00
void setup() {
pinMode(PIN_BUTTON,INPUT_PULLUP);
pinMode(PIN_LED,OUTPUT);
digitalWrite(PIN_LED,LOW);
2023-03-30 15:55:45 +00:00
Serial.begin(115200);
//init ADS1115
if (!ADS.begin()) {
Serial.println("Error:"); delay(2000); Serial.println("ADS1115 Init Error!");
}
ADS.setGain(0);
2023-04-17 19:32:34 +00:00
ec_setup();
2023-04-04 05:53:20 +00:00
2023-04-17 19:32:34 +00:00
waterlevel_setup();
2023-04-05 07:22:57 +00:00
2023-04-17 19:32:34 +00:00
temperature_setup();
2023-04-04 05:53:20 +00:00
2023-04-17 19:32:34 +00:00
flow_setup();
2023-04-07 16:03:41 +00:00
//Serial.println("Setup finished");
delay(200);
2023-04-30 18:20:09 +00:00
//Test adc to ec function output
2023-05-02 23:47:42 +00:00
/*
2023-04-30 18:20:09 +00:00
Serial.println();
for (int i=830;i<13300;i+=100) {
float _ec=ec_getECfromADC(i);
Serial.print(i); Serial.print(","); Serial.print(_ec); Serial.println();
}
delay(100000);
2023-05-02 23:47:42 +00:00
*/
2023-04-30 18:20:09 +00:00
2023-05-02 23:47:42 +00:00
//Serial.println("time,tempReservoir,ECadcCalib,ECadc,ECadcAdjusted,EC,EC25");
Serial.println("time,tempReservoir,ECadcCalib,ECadc,ECadcAdjusted");
2023-03-30 15:55:45 +00:00
}
void loop() {
unsigned long loopmillis=millis();
2023-04-17 19:32:34 +00:00
flag_print=false;
2023-04-05 07:22:57 +00:00
2023-03-30 15:55:45 +00:00
2023-05-03 00:55:12 +00:00
//ec_loop(loopmillis);
2023-04-05 07:22:57 +00:00
2023-05-03 00:55:12 +00:00
//temperature_loop(loopmillis);
2023-04-05 07:22:57 +00:00
2023-04-17 19:32:34 +00:00
2023-05-03 00:55:12 +00:00
//waterlevel_loop(loopmillis);
2023-04-04 05:53:20 +00:00
2023-04-07 16:03:41 +00:00
flow_loop(loopmillis);
2023-05-03 00:55:12 +00:00
//sm_loop(loopmillis);
2023-04-17 19:32:34 +00:00
2023-04-07 16:03:41 +00:00
static bool getReading=false;
if (!getReading && !digitalRead(PIN_BUTTON)) {
getReading=true;
last_measurement_ec=0; //force ec reading now
ec_flag_measurement_available=false;
digitalWrite(PIN_LED,HIGH);
}
2023-05-03 00:55:12 +00:00
static bool started=false;
static bool ended=false;
static unsigned long started_time=0;
static unsigned long ended_time=0;
if (flow_counter_sum>10 && !started && !ended) { //start
started=true;
started_time=millis();
Serial.println("Started!");
}
static uint32_t last_flow_counter_sum=0;
if (loopmillis>last_print+1000) { //test check
last_print=loopmillis;
if (started && !ended) {
Serial.println(flow_counter_sum-last_flow_counter_sum);
if (flow_counter_sum-last_flow_counter_sum<=2){ //stopped
ended=true;
ended_time=millis();
Serial.println("Ended!");
}
last_flow_counter_sum=flow_counter_sum;
}
if (ended) {
Serial.println("counts,time");
Serial.print(flow_counter_sum);
Serial.print(",");
Serial.print((ended_time-started_time)/1000.0);
Serial.println();
delay(10000);
}
}
2023-04-05 07:22:57 +00:00
2023-05-03 00:55:12 +00:00
if (loopmillis>last_print+2000 && 1==2) {
//if (ec_flag_measurement_available && getReading) {
2023-04-28 13:07:47 +00:00
if (ec_flag_measurement_available) {
last_print=loopmillis;
getReading=false;
ec_flag_measurement_available=false;
digitalWrite(PIN_LED,LOW);
2023-04-04 05:53:20 +00:00
2023-04-28 13:07:47 +00:00
Serial.print(millis()/1000.0,2); Serial.print(",");
2023-05-03 00:55:12 +00:00
/*
2023-04-28 13:07:47 +00:00
Serial.print(getMeanf(tempCmean_reservoir,TEMPMEAN_SIZE)); Serial.print(",");
//Serial.print(getMean(sm_mean,SM_SIZE)); Serial.print(",");
2023-04-30 18:20:09 +00:00
Serial.print(ec_calib_adc); Serial.print(",");
2023-04-30 17:00:12 +00:00
Serial.print(ec_adc); Serial.print(",");
2023-05-02 23:47:42 +00:00
Serial.print(ec_adc_adjusted); //Serial.print(",");
//Serial.print(ec); Serial.print(",");
//Serial.print(ec25);
2023-05-03 00:55:12 +00:00
*/
2023-04-30 18:20:09 +00:00
2023-04-30 17:00:12 +00:00
2023-04-28 13:07:47 +00:00
Serial.println();
2023-04-04 05:53:20 +00:00
2023-04-28 13:07:47 +00:00
/*
2023-04-05 07:22:57 +00:00
2023-04-28 13:07:47 +00:00
if (isValueArrayOKf(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");
}
2023-04-09 18:33:45 +00:00
2023-04-28 13:07:47 +00:00
if (isValueArrayOKf(waterlevelMean,WATERLEVELMEAN_SIZE,-1.0)){
float _max=getMaxf(waterlevelMean,WATERLEVELMEAN_SIZE);
float _min=getMinf(waterlevelMean,WATERLEVELMEAN_SIZE);
float _filteredWaterlevel=getFilteredf(waterlevelMean,WATERLEVELMEAN_SIZE,8);
float _meanWaterlevel=getMeanf(waterlevelMean,WATERLEVELMEAN_SIZE);
Serial.print("\t Dist="); Serial.print(_filteredWaterlevel); Serial.print("mm"); Serial.print("(+- "); Serial.print((_max-_min)/2.0); Serial.print(")"); Serial.print(" [mean="); Serial.print(_meanWaterlevel); Serial.print("]");
}else{
Serial.print("\t waiting for distance");
}
Serial.print("\t Flow="); Serial.print(flow,2);
Serial.print("\t Flowsum="); Serial.print(flow_counter_sum);
2023-04-09 18:33:45 +00:00
2023-04-28 13:07:47 +00:00
Serial.println();
*/
2023-04-09 18:33:45 +00:00
2023-04-05 07:22:57 +00:00
}
2023-03-30 15:55:45 +00:00
}
}