From 513111b656f3a52ee9baca9c01aa4ed95a98aa6e Mon Sep 17 00:00:00 2001 From: Fisch Date: Sun, 28 Jul 2024 21:32:22 +0200 Subject: [PATCH] fix code for new pcb --- include/ec.h | 9 +++++ include/waterlevel.h | 29 ++++++++------- platformio.ini | 8 ++--- src/main.cpp | 85 ++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 107 insertions(+), 24 deletions(-) diff --git a/include/ec.h b/include/ec.h index c384841..3e826fc 100644 --- a/include/ec.h +++ b/include/ec.h @@ -131,11 +131,20 @@ void ec_setup() { ledcWrite(EC_PWM_CH, 127); //50% duty cycle pinMode(EC_PIN_RELAY_PROBE,OUTPUT); //LOW=Calibration/idle, HIGH=Probe connected + + //Test Relay + digitalWrite(EC_PIN_RELAY_PROBE,HIGH); + delay(500); + digitalWrite(EC_PIN_RELAY_PROBE,LOW); + ec_releaseRelay(); } void ec_loop(unsigned long loopmillis) { + if (!adsenabled) { + return; + } static unsigned long last_read_ec=0; diff --git a/include/waterlevel.h b/include/waterlevel.h index 928be37..8760cb9 100644 --- a/include/waterlevel.h +++ b/include/waterlevel.h @@ -44,6 +44,9 @@ float waterlevelMean_array[WATERLEVELMEAN_SIZE]; uint16_t waterlevelMean_array_pos=0; float waterlevel=WATERLEVEL_UNAVAILABLE; float watervolume=WATERLEVEL_UNAVAILABLE; +#define DISTANCE_UNAVAILABLE 65535 +uint16_t distance=DISTANCE_UNAVAILABLE; + //Calibration @@ -55,7 +58,7 @@ float waterlevel_calib_reservoirArea=20*20*3.1416; //area in cm^2. barrel diamet uint16_t distance_unsuccessful_count=0; - +bool tofenabled=true; float waterlevel_heightToVolume(float distance); @@ -113,14 +116,13 @@ void waterlevel_setup() { timing_waterlevel.mintime=30*000; timing_waterlevel.maxtime=60*60*1000; + + for (uint16_t i=0;i=last_read_waterlevelB+READINTERVAL_WATERLEVEL) { last_read_waterlevelB=loopmillis; - uint16_t distance=tofsensor.readRangeSingleMillimeters(); //out of range =255 + distance=tofsensor.readRangeSingleMillimeters(); //out of range =255 //Serial.print("Distance reading B="); Serial.print(distance);Serial.println(); - if (distance!=WATERLEVEL_UNAVAILABLE && distance!=65535) { //successful + if (distance!=DISTANCE_UNAVAILABLE) { //successful waterlevelMean_array[waterlevelMean_array_pos]=distance; waterlevelMean_array_pos++; waterlevelMean_array_pos%=WATERLEVELMEAN_SIZE; diff --git a/platformio.ini b/platformio.ini index b165b16..84743ed 100644 --- a/platformio.ini +++ b/platformio.ini @@ -56,18 +56,18 @@ build_flags= '-D CLIENT_ID="hydroponic-Test"' -D PIN_BUTTON=12 - -D EC_PIN_RELAY_PROBE=6 - -D EC_PIN_FREQ=5 + -D EC_PIN_RELAY_PROBE=35 + -D EC_PIN_FREQ=38 -D EC_CALIBRATION_POLYNOM={8.718380956513695,-0.026463423062356713,3.425216464107108e-05,-4.069826379094172e-09,2.478900495960682e-13} -D EC_CALIBRATION_LINEARIZE_BELOW_ADC=2000 -D EC_CALIBRATION_LINEAR_LOWADC=728 -D EC_CALIBRATION_LINEAR_LOWEC=0 - -D FLOW_PIN=16 + -D FLOW_PIN=17 -D ONE_WIRE_BUS_PIN=18 -D THERMOMETER_ADDR_RESERVOIR={0x28,0xFF,0x30,0xBA,0x85,0x16,0x03,0xB5} - -D THERMOMETER_ADDR_CASE={0x28,0xFF,0x30,0xBA,0x85,0x16,0x03,0xB6} + -D THERMOMETER_ADDR_CASE={0x28,0xFF,0x64,0x0E,0x75,0x83,0x09,0x6F} -D PIN_SDA=1 -D PIN_SCL=2 diff --git a/src/main.cpp b/src/main.cpp index 4620c3f..491136a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,5 @@ #include - +#include bool valueError=false; @@ -8,8 +8,8 @@ unsigned long last_check=0; #include "wifi_functions.h" -bool debug=false; //print Serial information -bool mqtt=true; +bool debug=true; //print Serial information +bool mqtt=false; bool eccalibrationoutput=false; //serial output for ec calibration /* Write to file with: sudo stty -F /dev/ttyUSB0 115200 @@ -20,6 +20,45 @@ bool valuesStabilized=false; //gets set true when values are stable (avaeraging + +void i2cscan() +{ + byte error, address; + int nDevices; + + delay(500); + Serial.println("Scanning..."); + + nDevices = 0; + for(address = 1; address < 127; address++ ) + { + // The i2c_scanner uses the return value of + // the Write.endTransmisstion to see if + // a device did acknowledge to the address. + Wire.beginTransmission(address); + error = Wire.endTransmission(); + + if (error == 0) + { + Serial.print("I2C device found at address 0x"); + if (address<16) + Serial.print("0"); + Serial.print(address,HEX); + Serial.println(" !"); + + nDevices++; + } + else if (error==4) + { + Serial.print("Unknown error at address 0x"); + if (address<16) + Serial.print("0"); + Serial.println(address,HEX); + } + } +} + + #include "helpfunctions.h" #include "ADS1X15.h" @@ -31,16 +70,18 @@ bool valuesStabilized=false; //gets set true when values are stable (avaeraging // ######## Water Level -#include "waterlevel.h" +#include "waterlevel.h" // ######## EC ADS1115 ADS(0x48); +bool adsenabled=true; #include "ec.h" + // ######## Flow Rate #include "flow.h" @@ -75,6 +116,19 @@ void setup() { pixels.begin(); pixels.setBrightness(100); // not so bright pixels.clear(); + + //Flash colors for debug + pixels.setPixelColor(0, pixels.Color(255, 0,0)); + pixels.show(); + delay(250); + pixels.setPixelColor(0, pixels.Color(0, 255,0)); + pixels.show(); + delay(250); + pixels.setPixelColor(0, pixels.Color(0, 0,255)); + pixels.show(); + delay(250); + pixels.clear(); + #endif @@ -87,6 +141,13 @@ void setup() { } + + Wire.begin(PIN_SDA,PIN_SCL); + Serial.print("I2C Clock Speed="); + Serial.println(Wire.getClock()); + + i2cscan(); + Serial.println("Setup Waterlevel"); waterlevel_setup(); @@ -95,8 +156,11 @@ void setup() { if (!ADS.begin()) { Serial.println("Error:"); delay(2000); Serial.println("ADS1115 Init Error!"); publishInfo("error/general","ADS1115 Init Error"); + adsenabled=false; + } + if (adsenabled){ + ADS.setGain(0); } - ADS.setGain(0); Serial.println("Setup EC"); @@ -292,6 +356,7 @@ void loop() { + if (distance_unsuccessful_count>20) { if (!valueError && valuesStabilized) { //error just appeared Serial.println("valueError distance"); @@ -300,6 +365,7 @@ void loop() { valueError=true; _noErrorsDuringLoop=false; } + @@ -316,6 +382,9 @@ void loop() { Serial.print("temperature reservoir = "); Serial.print(tempCmean_reservoir); Serial.println(); + Serial.print("temperature case = "); + Serial.print(tempCmean_case); + Serial.println(); @@ -361,7 +430,8 @@ void loop() { - Serial.print("Waterlevel,Volume = "); + Serial.print("distance,Waterlevel,Volume = "); + Serial.print(distance); Serial.print(","); Serial.print(waterlevel); Serial.print(","); Serial.print(watervolume); Serial.println(); @@ -409,13 +479,14 @@ void loop() { } - + if (waterlevel!=WATERLEVEL_UNAVAILABLE) { bool _published=publishValueTimed("waterlevel/height",waterlevel,2,timing_waterlevel,loopmillis); if (_published) { //use height for timing. send calculated volume with it publishValue("waterlevel/volume",watervolume,2); } } + if (ec_flag_measurement_available){