219 lines
4.8 KiB
C++
219 lines
4.8 KiB
C++
#include <Arduino.h>
|
|
|
|
|
|
#include "wifi_functions.h"
|
|
|
|
|
|
bool debug=true; //print Serial information
|
|
|
|
#include "helpfunctions.h"
|
|
#include "ADS1X15.h"
|
|
|
|
ADS1115 ADS(0x48);
|
|
|
|
|
|
// ######## Temperature
|
|
#include "temperature.h"
|
|
|
|
// ######## EC
|
|
#include "ec.h"
|
|
|
|
|
|
|
|
// ######## Water Level
|
|
//#include "waterlevel.h"
|
|
|
|
|
|
// ######## Flow Rate
|
|
#include "flow.h"
|
|
|
|
// ######## Soilmoisture
|
|
#include "soilmoisture.h"
|
|
|
|
|
|
unsigned long last_check=0;
|
|
|
|
|
|
|
|
|
|
|
|
bool valueError=false;
|
|
|
|
#define PIN_BUTTON 12
|
|
#define PIN_LED 13
|
|
|
|
|
|
void setup() {
|
|
pinMode(PIN_BUTTON,INPUT_PULLUP);
|
|
pinMode(PIN_LED,OUTPUT);
|
|
digitalWrite(PIN_LED,LOW);
|
|
Serial.begin(115200);
|
|
WiFi.begin(ssid, pass);
|
|
client.begin(mqtt_host, net);
|
|
client.onMessage(messageReceived);
|
|
connect();
|
|
|
|
//init ADS1115
|
|
if (!ADS.begin()) {
|
|
Serial.println("Error:"); delay(2000); Serial.println("ADS1115 Init Error!");
|
|
}
|
|
ADS.setGain(0);
|
|
|
|
|
|
Serial.println("Setup EC");
|
|
ec_setup();
|
|
|
|
//Serial.println("Setup Waterlevel");
|
|
//waterlevel_setup();
|
|
|
|
Serial.println("Setup Temperature");
|
|
temperature_setup();
|
|
|
|
Serial.println("Setup Flow");
|
|
flow_setup();
|
|
|
|
Serial.println("Finished Setup");
|
|
delay(200);
|
|
|
|
//Test adc to ec function output
|
|
/*
|
|
Serial.println();
|
|
for (int i=750;i<14183;i+=100) {
|
|
float _ec=ec_getECfromADC(i);
|
|
Serial.print(i); Serial.print(","); Serial.print(_ec); Serial.println();
|
|
}
|
|
|
|
delay(100000);
|
|
*/
|
|
|
|
|
|
|
|
|
|
//Serial.println("time,tempReservoir,ECadcCalib,ECadc,ECadcAdjusted,EC,EC25");
|
|
//Serial.println("time,tempReservoir,ECadcCalib,ECadc,ECadcAdjusted");
|
|
|
|
}
|
|
|
|
void loop() {
|
|
unsigned long loopmillis=millis();
|
|
|
|
ec_loop(loopmillis);
|
|
|
|
temperature_loop(loopmillis);
|
|
|
|
//waterlevel_loop(loopmillis);
|
|
|
|
flow_loop(loopmillis);
|
|
|
|
sm_loop(loopmillis);
|
|
|
|
|
|
static bool getReading=false;
|
|
|
|
if (!digitalRead(PIN_BUTTON)) {
|
|
valueError=false;
|
|
Serial.println("Reset ValueError flag by user");
|
|
digitalWrite(PIN_LED,valueError);
|
|
delay(100);
|
|
}
|
|
|
|
/*
|
|
if (ec_flag_measurement_available && getReading) {
|
|
ec_flag_measurement_available=false;
|
|
getReading=false;
|
|
digitalWrite(PIN_LED,LOW);
|
|
Serial.print(loopmillis); Serial.print(",");
|
|
Serial.print(tempCmean_reservoir); Serial.print(",");
|
|
Serial.print(ec_calib_adc); Serial.print(",");
|
|
Serial.print(ec_adc); Serial.print(",");
|
|
Serial.print(ec_adc_adjusted);
|
|
Serial.println();
|
|
}*/
|
|
|
|
if (loopmillis>last_check+2000) { //check values
|
|
|
|
last_check=loopmillis;
|
|
|
|
|
|
|
|
|
|
if (tempCmean_air==DEVICE_DISCONNECTED_C || tempCmean_reservoir==DEVICE_DISCONNECTED_C) {
|
|
valueError=true;
|
|
}
|
|
//if (waterlevel==WATERLEVEL_UNAVAILABLE) {
|
|
// valueError=true;
|
|
//}
|
|
if (sm_mean1==SM_DISCONNECTED || sm_mean2==SM_DISCONNECTED) {
|
|
valueError=true;
|
|
}
|
|
if (ec==EC_UNAVAILABLE){
|
|
valueError=true;
|
|
}
|
|
|
|
|
|
digitalWrite(PIN_LED,valueError);
|
|
|
|
|
|
|
|
|
|
if (debug) {
|
|
Serial.println("_______________________");
|
|
Serial.print(millis()/1000.0,2); Serial.println(":");
|
|
|
|
Serial.print("temperature reservoir,air = ");
|
|
Serial.print(tempCmean_reservoir); Serial.print(","); Serial.print(tempCmean_air);
|
|
Serial.println();
|
|
|
|
|
|
|
|
Serial.print("sm_mean 1,2,3 = ");
|
|
Serial.print(sm_mean1); Serial.print(",");
|
|
Serial.print(sm_mean2); Serial.print(",");
|
|
Serial.print(sm_mean3);
|
|
Serial.println();
|
|
|
|
/*
|
|
Serial.print("sm_mean 1,2,3 = ");
|
|
Serial.print(getMean(sm_mean1array,SM_SIZE)); Serial.print(",");
|
|
Serial.print(getMean(sm_mean2array,SM_SIZE)); Serial.print(",");
|
|
Serial.print(getMean(sm_mean3array,SM_SIZE));
|
|
Serial.println();
|
|
Serial.print("sm_max 1,2,3 = ");
|
|
Serial.print(getMax(sm_mean1array,SM_SIZE)); Serial.print(",");
|
|
Serial.print(getMax(sm_mean2array,SM_SIZE)); Serial.print(",");
|
|
Serial.print(getMax(sm_mean3array,SM_SIZE));
|
|
Serial.println();
|
|
Serial.print("sm_min 1,2,3 = ");
|
|
Serial.print(getMin(sm_mean1array,SM_SIZE)); Serial.print(",");
|
|
Serial.print(getMin(sm_mean2array,SM_SIZE)); Serial.print(",");
|
|
Serial.print(getMin(sm_mean3array,SM_SIZE));
|
|
Serial.println();
|
|
//Serial.print(getMax(sm_mean3array,SM_SIZE)); Serial.println();
|
|
*/
|
|
|
|
Serial.print("Flow = "); Serial.print(flow);
|
|
Serial.println();
|
|
|
|
Serial.print("EC ec_calib_adc,ec_adc,ec_adc_adjusted = ");
|
|
Serial.print(ec_calib_adc); Serial.print(",");
|
|
Serial.print(ec_adc); Serial.print(",");
|
|
Serial.print(ec_adc_adjusted);
|
|
Serial.println();
|
|
Serial.print("EC ec,ec25 = ");
|
|
Serial.print(ec); Serial.print(",");
|
|
Serial.print(ec25);
|
|
Serial.println();
|
|
|
|
/*Serial.print("Waterlevel distance,volume = "); Serial.print(waterlevel); Serial.print(","); Serial.print(watervolume);
|
|
if (waterlevel_failcounter>0) {
|
|
Serial.print(" fails="); Serial.print(waterlevel_failcounter);
|
|
}*/
|
|
Serial.println();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
} |