fix code for new pcb

This commit is contained in:
interfisch 2024-07-28 21:32:22 +02:00
parent 92118cfb72
commit 513111b656
4 changed files with 107 additions and 24 deletions

View File

@ -131,11 +131,20 @@ void ec_setup() {
ledcWrite(EC_PWM_CH, 127); //50% duty cycle ledcWrite(EC_PWM_CH, 127); //50% duty cycle
pinMode(EC_PIN_RELAY_PROBE,OUTPUT); //LOW=Calibration/idle, HIGH=Probe connected 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(); ec_releaseRelay();
} }
void ec_loop(unsigned long loopmillis) { void ec_loop(unsigned long loopmillis) {
if (!adsenabled) {
return;
}
static unsigned long last_read_ec=0; static unsigned long last_read_ec=0;

View File

@ -44,6 +44,9 @@ float waterlevelMean_array[WATERLEVELMEAN_SIZE];
uint16_t waterlevelMean_array_pos=0; uint16_t waterlevelMean_array_pos=0;
float waterlevel=WATERLEVEL_UNAVAILABLE; float waterlevel=WATERLEVEL_UNAVAILABLE;
float watervolume=WATERLEVEL_UNAVAILABLE; float watervolume=WATERLEVEL_UNAVAILABLE;
#define DISTANCE_UNAVAILABLE 65535
uint16_t distance=DISTANCE_UNAVAILABLE;
//Calibration //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; uint16_t distance_unsuccessful_count=0;
bool tofenabled=true;
float waterlevel_heightToVolume(float distance); float waterlevel_heightToVolume(float distance);
@ -113,14 +116,13 @@ void waterlevel_setup() {
timing_waterlevel.mintime=30*000; timing_waterlevel.mintime=30*000;
timing_waterlevel.maxtime=60*60*1000; timing_waterlevel.maxtime=60*60*1000;
for (uint16_t i=0;i<WATERLEVELMEAN_SIZE;i++) {
waterlevelMean_array[i]=WATERLEVEL_UNAVAILABLE; //-1 is also timeout value
}
delay(50); delay(50);
Wire.begin(PIN_SDA,PIN_SCL);
Serial.print("I2C Clock Speed=");
Serial.println(Wire.getClock());
tofsensor.setTimeout(2000); tofsensor.setTimeout(2000);
if (!tofsensor.init()) if (!tofsensor.init())
@ -128,6 +130,8 @@ void waterlevel_setup() {
Serial.println("Failed to detect and initialize tofsensor!"); Serial.println("Failed to detect and initialize tofsensor!");
publishInfo("error/waterlevel","Failed to detect and initialize tofsensor"); publishInfo("error/waterlevel","Failed to detect and initialize tofsensor");
delay(1000); delay(1000);
tofenabled=false;
return;
} }
@ -149,25 +153,24 @@ void waterlevel_setup() {
#endif #endif
for (uint16_t i=0;i<WATERLEVELMEAN_SIZE;i++) {
waterlevelMean_array[i]=WATERLEVEL_UNAVAILABLE; //-1 is also timeout value
}
} }
void waterlevel_loop(unsigned long loopmillis) { void waterlevel_loop(unsigned long loopmillis) {
if (!tofenabled) {
return;
}
static unsigned long last_read_waterlevelB; static unsigned long last_read_waterlevelB;
if (loopmillis>=last_read_waterlevelB+READINTERVAL_WATERLEVEL) { if (loopmillis>=last_read_waterlevelB+READINTERVAL_WATERLEVEL) {
last_read_waterlevelB=loopmillis; 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(); //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[waterlevelMean_array_pos]=distance;
waterlevelMean_array_pos++; waterlevelMean_array_pos++;
waterlevelMean_array_pos%=WATERLEVELMEAN_SIZE; waterlevelMean_array_pos%=WATERLEVELMEAN_SIZE;

View File

@ -56,18 +56,18 @@ build_flags=
'-D CLIENT_ID="hydroponic-Test"' '-D CLIENT_ID="hydroponic-Test"'
-D PIN_BUTTON=12 -D PIN_BUTTON=12
-D EC_PIN_RELAY_PROBE=6 -D EC_PIN_RELAY_PROBE=35
-D EC_PIN_FREQ=5 -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_POLYNOM={8.718380956513695,-0.026463423062356713,3.425216464107108e-05,-4.069826379094172e-09,2.478900495960682e-13}
-D EC_CALIBRATION_LINEARIZE_BELOW_ADC=2000 -D EC_CALIBRATION_LINEARIZE_BELOW_ADC=2000
-D EC_CALIBRATION_LINEAR_LOWADC=728 -D EC_CALIBRATION_LINEAR_LOWADC=728
-D EC_CALIBRATION_LINEAR_LOWEC=0 -D EC_CALIBRATION_LINEAR_LOWEC=0
-D FLOW_PIN=16 -D FLOW_PIN=17
-D ONE_WIRE_BUS_PIN=18 -D ONE_WIRE_BUS_PIN=18
-D THERMOMETER_ADDR_RESERVOIR={0x28,0xFF,0x30,0xBA,0x85,0x16,0x03,0xB5} -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_SDA=1
-D PIN_SCL=2 -D PIN_SCL=2

View File

@ -1,5 +1,5 @@
#include <Arduino.h> #include <Arduino.h>
#include <Wire.h>
bool valueError=false; bool valueError=false;
@ -8,8 +8,8 @@ unsigned long last_check=0;
#include "wifi_functions.h" #include "wifi_functions.h"
bool debug=false; //print Serial information bool debug=true; //print Serial information
bool mqtt=true; bool mqtt=false;
bool eccalibrationoutput=false; //serial output for ec calibration bool eccalibrationoutput=false; //serial output for ec calibration
/* Write to file with: /* Write to file with:
sudo stty -F /dev/ttyUSB0 115200 sudo stty -F /dev/ttyUSB0 115200
@ -21,6 +21,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 "helpfunctions.h"
#include "ADS1X15.h" #include "ADS1X15.h"
@ -36,11 +75,13 @@ bool valuesStabilized=false; //gets set true when values are stable (avaeraging
// ######## EC // ######## EC
ADS1115 ADS(0x48); ADS1115 ADS(0x48);
bool adsenabled=true;
#include "ec.h" #include "ec.h"
// ######## Flow Rate // ######## Flow Rate
#include "flow.h" #include "flow.h"
@ -75,6 +116,19 @@ void setup() {
pixels.begin(); pixels.begin();
pixels.setBrightness(100); // not so bright pixels.setBrightness(100); // not so bright
pixels.clear(); 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 #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"); Serial.println("Setup Waterlevel");
waterlevel_setup(); waterlevel_setup();
@ -95,8 +156,11 @@ void setup() {
if (!ADS.begin()) { if (!ADS.begin()) {
Serial.println("Error:"); delay(2000); Serial.println("ADS1115 Init Error!"); Serial.println("Error:"); delay(2000); Serial.println("ADS1115 Init Error!");
publishInfo("error/general","ADS1115 Init Error"); publishInfo("error/general","ADS1115 Init Error");
adsenabled=false;
}
if (adsenabled){
ADS.setGain(0);
} }
ADS.setGain(0);
Serial.println("Setup EC"); Serial.println("Setup EC");
@ -292,6 +356,7 @@ void loop() {
if (distance_unsuccessful_count>20) { if (distance_unsuccessful_count>20) {
if (!valueError && valuesStabilized) { //error just appeared if (!valueError && valuesStabilized) { //error just appeared
Serial.println("valueError distance"); Serial.println("valueError distance");
@ -303,6 +368,7 @@ void loop() {
if (_noErrorsDuringLoop && !valuesStabilized) { if (_noErrorsDuringLoop && !valuesStabilized) {
valuesStabilized=true; //gets only set to true once valuesStabilized=true; //gets only set to true once
valueError=false; //clear error flag once after boot valueError=false; //clear error flag once after boot
@ -316,6 +382,9 @@ void loop() {
Serial.print("temperature reservoir = "); Serial.print("temperature reservoir = ");
Serial.print(tempCmean_reservoir); Serial.print(tempCmean_reservoir);
Serial.println(); 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(waterlevel); Serial.print(",");
Serial.print(watervolume); Serial.println(); Serial.print(watervolume); Serial.println();
@ -418,6 +488,7 @@ void loop() {
} }
if (ec_flag_measurement_available){ if (ec_flag_measurement_available){
ec_flag_measurement_available=false; ec_flag_measurement_available=false;