add soil moisture sensor and temporarily change print settings for csv output

This commit is contained in:
interfisch 2023-04-22 03:14:51 +02:00
parent 8949c62742
commit d911efcd25
5 changed files with 116 additions and 55 deletions

View File

@ -3,28 +3,32 @@
#include <Arduino.h> #include <Arduino.h>
float ec_mean=0; float ecEC_ADS_CHANNEL_mean=0;
bool ec_flag_measurement_available=false;
#define EC_PIN_RELAY_PROBE 27 #define EC_PIN_RELAY_PROBE 27
#define EC_PIN_RELAY_RANGEMSB 25 #define EC_PIN_RELAY_RANGEMSB 25
#define EC_PIN_RELAY_RANGELSB 26 #define EC_PIN_RELAY_RANGELSB 26
#define EC_PIN_ADC 4 //#define EC_PIN_ADC 4
#define EC_ADS_CHANNEL 0
#define EC_PIN_FREQ 5 #define EC_PIN_FREQ 5
#define EC_PWM_CH 0 #define EC_PWM_CH 0
#define EC_RESOLUTION 8 #define EC_RESOLUTION 8
#define EC_FREQUENCY 5000 #define EC_FREQUENCY 5000
#define EC_ARRAY_SIZE 128 #define EC_ARRAY_SIZE 64
uint16_t ec_array_range00[EC_ARRAY_SIZE]; //00=NC,NC = highest value uint16_t ec_array_range00[EC_ARRAY_SIZE]; //00=NC,NC = highest value
uint16_t ec_array_range01[EC_ARRAY_SIZE]; uint16_t ec_array_range01[EC_ARRAY_SIZE];
uint16_t ec_array_range10[EC_ARRAY_SIZE]; uint16_t ec_array_range10[EC_ARRAY_SIZE];
uint16_t ec_array_range11[EC_ARRAY_SIZE]; //11= NO,NO = lowest value uint16_t ec_array_range11[EC_ARRAY_SIZE]; //11= NO,NO = lowest value
uint16_t ec_array_pos=EC_ARRAY_SIZE*4; uint16_t ec_array_pos=EC_ARRAY_SIZE*4;
unsigned long last_measurement_ec=0;
#define EC_MEASUREMENT_INTERVAL 10000 //complete filtered measurement every x ms #define EC_MEASUREMENT_INTERVAL 10000 //complete filtered measurement every x ms
//One filtered measurement takes EC_READ_INTERVAL*EC_ARRAY_SIZE*4 //One filtered measurement takes EC_READ_INTERVAL*EC_ARRAY_SIZE*4
#define EC_READ_INTERVAL 5 //interval of reading adc value inside a measurement #define EC_READ_INTERVAL 2 //interval of reading adc value inside a measurement
#define EC_RELAY_SWITCH_SETTLETIME 500 //time until voltage of ec circuit has settled #define EC_RELAY_SWITCH_SETTLETIME 500 //time until voltage of ec circuit has settled
@ -36,6 +40,11 @@ enum ECState{IDLE,MEASURE};
ECState ecstate=IDLE; ECState ecstate=IDLE;
float adc_range00;
float adc_range01;
float adc_range10;
float adc_range11;
bool ec_measurementReady(); bool ec_measurementReady();
void ec_startMeasurement(); void ec_startMeasurement();
@ -44,7 +53,7 @@ void ec_connectProbe(bool);
void ec_releaseRelay(); void ec_releaseRelay();
void ec_setup() { void ec_setup() {
pinMode(EC_PIN_ADC,INPUT); //pinMode(EC_PIN_ADC,INPUT);
ledcSetup(EC_PWM_CH, EC_FREQUENCY, EC_RESOLUTION); ledcSetup(EC_PWM_CH, EC_FREQUENCY, EC_RESOLUTION);
ledcAttachPin(EC_PIN_FREQ, EC_PWM_CH); ledcAttachPin(EC_PIN_FREQ, EC_PWM_CH);
@ -58,7 +67,7 @@ void ec_setup() {
} }
void ec_loop(unsigned long loopmillis) { void ec_loop(unsigned long loopmillis) {
static unsigned long last_measurement_ec=0;
static unsigned long last_read_ec=0; static unsigned long last_read_ec=0;
switch (ecstate) { switch (ecstate) {
@ -75,20 +84,16 @@ void ec_loop(unsigned long loopmillis) {
case MEASURE: case MEASURE:
if (ec_measurementReady()) { if (ec_measurementReady()) {
ec_releaseRelay(); ec_releaseRelay();
float adc_range00=getMean(ec_array_range00,EC_ARRAY_SIZE); //good for low conductivity/high resistance adc_range00=getMean(ec_array_range00,EC_ARRAY_SIZE); //good for low conductivity/high resistance
float adc_range01=getMean(ec_array_range01,EC_ARRAY_SIZE); adc_range01=getMean(ec_array_range01,EC_ARRAY_SIZE);
float adc_range10=getMean(ec_array_range10,EC_ARRAY_SIZE); adc_range10=getMean(ec_array_range10,EC_ARRAY_SIZE);
float adc_range11=getMean(ec_array_range11,EC_ARRAY_SIZE); //good for high conductivity/low resistance adc_range11=getMean(ec_array_range11,EC_ARRAY_SIZE); //good for high conductivity/low resistance
ec_mean=0; //TODO select right range of all readings //ec_mean=0; //TODO select right range of all readings
ec_flag_measurement_available=true;
Serial.println("EC ADC");
Serial.println(adc_range00);
Serial.println(adc_range01);
Serial.println(adc_range10);
Serial.println(adc_range11);
@ -111,7 +116,9 @@ void ec_loop(unsigned long loopmillis) {
if (loopmillis>ec_last_change_relay+EC_RELAY_SWITCH_SETTLETIME) { //values have settled if (loopmillis>ec_last_change_relay+EC_RELAY_SWITCH_SETTLETIME) { //values have settled
uint16_t value=analogRead(EC_PIN_ADC); //uint16_t value=analogRead(EC_PIN_ADC);
uint16_t value = ADS.readADC(EC_ADS_CHANNEL);
switch (ec_array_pos/EC_ARRAY_SIZE){ //low range switch (ec_array_pos/EC_ARRAY_SIZE){ //low range
case 0: case 0:
ec_array_range00[ec_array_pos%EC_ARRAY_SIZE]=value; ec_array_range00[ec_array_pos%EC_ARRAY_SIZE]=value;
@ -129,23 +136,8 @@ void ec_loop(unsigned long loopmillis) {
ec_array_range11[ec_array_pos%EC_ARRAY_SIZE]=value; ec_array_range11[ec_array_pos%EC_ARRAY_SIZE]=value;
break; break;
} }
/*
if (ec_array_pos==0) {
Serial.println(""); Serial.print("Lowrange:");
}
if (ec_array_pos==EC_ARRAY_SIZE) {
Serial.println(""); Serial.print("Highrange:");
}
Serial.print(value); Serial.print(" ");
if (ec_array_pos==EC_ARRAY_SIZE*2-1) {
Serial.println("");
}
*/
ec_array_pos++; ec_array_pos++;
} }
} }
@ -172,10 +164,8 @@ void ec_setRange(uint8_t range) {
uint8_t crange=digitalRead(EC_PIN_RELAY_RANGELSB)+2*digitalRead(EC_PIN_RELAY_RANGEMSB); uint8_t crange=digitalRead(EC_PIN_RELAY_RANGELSB)+2*digitalRead(EC_PIN_RELAY_RANGEMSB);
if (crange!=range) { //write only if different if (crange!=range) { //write only if different
Serial.print("setRange("); Serial.print(range); Serial.print("), was "); Serial.print(crange);
digitalWrite(EC_PIN_RELAY_RANGELSB,range%2); digitalWrite(EC_PIN_RELAY_RANGELSB,range%2);
digitalWrite(EC_PIN_RELAY_RANGEMSB,range/2); digitalWrite(EC_PIN_RELAY_RANGEMSB,range/2);
Serial.print(". Relay set to "); Serial.print(digitalRead(EC_PIN_RELAY_RANGEMSB)); Serial.print(""); Serial.print(digitalRead(EC_PIN_RELAY_RANGELSB)); Serial.println();
ec_last_change_relay=millis(); ec_last_change_relay=millis();
} }
} }

30
include/soilmoisture.h Normal file
View File

@ -0,0 +1,30 @@
#ifndef _SOILMOISTURE_H_
#define _SOILMOISTURE_H_
#define SM1_ADS_CHANNEL 1
#define READINTERVAL_SM 100
unsigned long last_read_sm=0;
#define SM_SIZE 16
uint8_t sm_mean_pos=0;
uint16_t sm_mean[SM_SIZE];
void sm_loop(unsigned long loopmillis) {
if (loopmillis>=last_read_sm+READINTERVAL_SM) {
last_read_sm=loopmillis;
uint16_t value = ADS.readADC(SM1_ADS_CHANNEL);
sm_mean[sm_mean_pos]=value;
sm_mean_pos++;
sm_mean_pos%=SM_SIZE;
//Serial.print(getMean(sm_mean,SM_SIZE)); Serial.print("\t "); Serial.println(value);
}
}
#endif

View File

@ -44,16 +44,17 @@ void temperature_setup() {
sensors.begin(); sensors.begin();
delay(1000); delay(1000);
Serial.print("Locating devices...");
Serial.print("Found "); //Serial.print("Locating devices...");
Serial.print(sensors.getDeviceCount(), DEC); //Serial.print("Found ");
Serial.println(" devices."); //Serial.print(sensors.getDeviceCount(), DEC);
//Serial.println(" devices.");
delay(1000); delay(1000);
Serial.print("Parasite power is: "); //Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON"); //if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF"); //else Serial.println("OFF");
delay(1000); delay(1000);

View File

@ -19,3 +19,4 @@ lib_deps =
https://github.com/milesburton/Arduino-Temperature-Control-Library/ https://github.com/milesburton/Arduino-Temperature-Control-Library/
d03n3rfr1tz3/HC-SR04@^1.1.2 d03n3rfr1tz3/HC-SR04@^1.1.2
https://github.com/emilv/ArduinoSort/ https://github.com/emilv/ArduinoSort/
robtillaart/ADS1X15@^0.3.9

View File

@ -3,6 +3,9 @@
bool flag_print=false; bool flag_print=false;
#include "helpfunctions.h" #include "helpfunctions.h"
#include "ADS1X15.h"
ADS1115 ADS(0x48);
// ######## EC // ######## EC
#include "ec.h" #include "ec.h"
@ -18,6 +21,9 @@ bool flag_print=false;
// ######## Flow Rate // ######## Flow Rate
#include "flow.h" #include "flow.h"
// ######## Soilmoisture
#include "soilmoisture.h"
unsigned long last_print=0; unsigned long last_print=0;
@ -27,12 +33,23 @@ unsigned long last_print=0;
#define PIN_BUTTON 12
#define PIN_LED 2
void setup() { void setup() {
pinMode(PIN_BUTTON,INPUT_PULLUP);
pinMode(PIN_LED,OUTPUT);
digitalWrite(PIN_LED,LOW);
Serial.begin(115200); Serial.begin(115200);
//init ADS1115
if (!ADS.begin()) {
Serial.println("Error:"); delay(2000); Serial.println("ADS1115 Init Error!");
}
ADS.setGain(0);
ec_setup(); ec_setup();
waterlevel_setup(); waterlevel_setup();
@ -41,8 +58,11 @@ void setup() {
flow_setup(); flow_setup();
Serial.println("Setup finished"); //Serial.println("Setup finished");
delay(500); delay(200);
Serial.println("time,tempReservoir,EC00,EC01,EC10,EC11");
} }
void loop() { void loop() {
@ -62,21 +82,39 @@ void loop() {
flow_loop(loopmillis); flow_loop(loopmillis);
sm_loop(loopmillis);
if (loopmillis>last_print+500) { 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);
}
if (loopmillis>last_print+10000 && loopmillis>60000) {
//if (ec_flag_measurement_available && getReading) {
last_print=loopmillis; last_print=loopmillis;
getReading=false;
ec_flag_measurement_available=false;
digitalWrite(PIN_LED,LOW);
Serial.print(millis()/1000.0,2); Serial.print(",");
Serial.print(getMeanf(tempCmean_reservoir,TEMPMEAN_SIZE)); Serial.print(",");
//Serial.print(getMean(sm_mean,SM_SIZE)); Serial.print(",");
Serial.print(adc_range00); Serial.print(",");
Serial.print(adc_range01); Serial.print(",");
Serial.print(adc_range10); Serial.print(",");
Serial.print(adc_range11);
Serial.println();
/* /*
if (isValueArrayOK(ec_array,EC_ARRAY_SIZE,0))
{
Serial.print("EC=");
Serial.print(getMean(ec_array,EC_ARRAY_SIZE),3);
Serial.print(" count (+- "); Serial.print( (getMax(ec_array,EC_ARRAY_SIZE) - getMin(ec_array,EC_ARRAY_SIZE))/2.0); Serial.print(")");
}else{
Serial.print("Waiting for EC");
}
if (isValueArrayOKf(tempCmean_reservoir,TEMPMEAN_SIZE,DEVICE_DISCONNECTED_C)){ if (isValueArrayOKf(tempCmean_reservoir,TEMPMEAN_SIZE,DEVICE_DISCONNECTED_C)){
@ -108,6 +146,7 @@ void loop() {
Serial.println(); Serial.println();
*/ */
//}
} }
} }