add valve and pump
This commit is contained in:
parent
5305fd80f9
commit
2554c35090
3 changed files with 138 additions and 5 deletions
|
@ -7,6 +7,9 @@
|
||||||
|
|
||||||
#include "wifi_settings.h"
|
#include "wifi_settings.h"
|
||||||
#include "helpfunctions.h"
|
#include "helpfunctions.h"
|
||||||
|
#if defined(RELAISCOUNT) || defined(VALVECOUNT)
|
||||||
|
#include "pump.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
bool force_ec_measurement=false;
|
bool force_ec_measurement=false;
|
||||||
|
|
||||||
|
@ -32,6 +35,7 @@ bool enableTiming=true;
|
||||||
bool publishValueTimed(String topic,float value,uint8_t decimals,mqttValueTiming &mqttvt,unsigned long loopmillis);
|
bool publishValueTimed(String topic,float value,uint8_t decimals,mqttValueTiming &mqttvt,unsigned long loopmillis);
|
||||||
void publishValue(String topic,float value,uint8_t decimals);
|
void publishValue(String topic,float value,uint8_t decimals);
|
||||||
void publishInfo(String topic,String text);
|
void publishInfo(String topic,String text);
|
||||||
|
bool isInt(String str);
|
||||||
|
|
||||||
void connect() {
|
void connect() {
|
||||||
Serial.print("checking wifi...");
|
Serial.print("checking wifi...");
|
||||||
|
@ -57,9 +61,25 @@ void connect() {
|
||||||
}else{
|
}else{
|
||||||
Serial.println("\nconnected!");
|
Serial.println("\nconnected!");
|
||||||
client.subscribe((String)client_id+"/sendall");
|
client.subscribe((String)client_id+"/sendall");
|
||||||
|
#ifdef EC_CALIBRATION_POLYNOM
|
||||||
client.subscribe((String)client_id+"/ec/trigger");
|
client.subscribe((String)client_id+"/ec/trigger");
|
||||||
|
#endif
|
||||||
client.subscribe((String)client_id+"/errorack");
|
client.subscribe((String)client_id+"/errorack");
|
||||||
client.subscribe((String)client_id+"/reboot");
|
client.subscribe((String)client_id+"/reboot");
|
||||||
|
|
||||||
|
#ifdef RELAISCOUNT
|
||||||
|
client.subscribe((String)client_id+"/pump");
|
||||||
|
#endif
|
||||||
|
#ifdef VALVECOUNT
|
||||||
|
client.subscribe((String)client_id+"/pump1");
|
||||||
|
client.subscribe((String)client_id+"/pump2");
|
||||||
|
client.subscribe((String)client_id+"/pump3");
|
||||||
|
client.subscribe((String)client_id+"/pump4");
|
||||||
|
client.subscribe((String)client_id+"/pump5");
|
||||||
|
client.subscribe((String)client_id+"/pump6");
|
||||||
|
client.subscribe((String)client_id+"/pump7");
|
||||||
|
client.subscribe((String)client_id+"/pump8");
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -73,10 +93,12 @@ void messageReceived(String &topic, String &payload) {
|
||||||
sendallnext_flag=true;
|
sendallnext_flag=true;
|
||||||
Serial.println("Send all values next time");
|
Serial.println("Send all values next time");
|
||||||
}
|
}
|
||||||
|
#ifdef EC_CALIBRATION_POLYNOM
|
||||||
if (topic==((String)client_id+"/ec/trigger") && payload=="true") {
|
if (topic==((String)client_id+"/ec/trigger") && payload=="true") {
|
||||||
force_ec_measurement=true;
|
force_ec_measurement=true;
|
||||||
Serial.println("Forced EC Measurement");
|
Serial.println("Forced EC Measurement");
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
if (topic==((String)client_id+"/errorack") && payload=="true") { //error acknowledge
|
if (topic==((String)client_id+"/errorack") && payload=="true") { //error acknowledge
|
||||||
valueError=false;
|
valueError=false;
|
||||||
Serial.println("Reset value error flag");
|
Serial.println("Reset value error flag");
|
||||||
|
@ -86,6 +108,44 @@ void messageReceived(String &topic, String &payload) {
|
||||||
delay(100);
|
delay(100);
|
||||||
ESP.restart();
|
ESP.restart();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(VALVECOUNT) || defined(RELAISCOUNT)
|
||||||
|
if (topic.startsWith((String)client_id+"/pump")) {
|
||||||
|
|
||||||
|
if (isInt(payload)){
|
||||||
|
int duration=payload.toInt(); //duration in seconds
|
||||||
|
|
||||||
|
if (duration>0 && duration<=10*60) {
|
||||||
|
String valveidStr=topic.substring( ((String)client_id+"/pump").length() );
|
||||||
|
|
||||||
|
if (valveidStr.length()==0) { //topic is "/pump" without number
|
||||||
|
//Turn on just the pump for duration time
|
||||||
|
Serial.print("Turning on just the pump for "); Serial.println(duration);
|
||||||
|
enableRelais(1,duration*1000); //Only relais 1 for pump used at the moment
|
||||||
|
}else{
|
||||||
|
if (isInt(valveidStr)){
|
||||||
|
int valveid=valveidStr.toInt();
|
||||||
|
//Turn on pump and valve for duration time
|
||||||
|
Serial.print("Turning on pump and valve "); Serial.print(valveid); Serial.print("for "); Serial.println(duration);
|
||||||
|
bool res=enableValve(valveid,duration*1000+500);
|
||||||
|
enableRelais(1,duration*1000); //Only relais 1 for pump used at the moment
|
||||||
|
if (!res) {
|
||||||
|
publishInfo("error/pump","failed to set valve");
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
publishInfo("error/pump","topic id not int");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
publishInfo("error/pump","duration too high or low");
|
||||||
|
}
|
||||||
|
|
||||||
|
}else{
|
||||||
|
publishInfo("error/pump","payload not int");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
bool mqtt_loop(unsigned long loopmillis) {
|
bool mqtt_loop(unsigned long loopmillis) {
|
||||||
|
@ -145,4 +205,12 @@ void publishInfo(String topic,String text) {
|
||||||
Serial.print("Publish Topic="); Serial.print((String)client_id+"/"+topic); Serial.print(" Message="); Serial.println(text);
|
Serial.print("Publish Topic="); Serial.print((String)client_id+"/"+topic); Serial.print(" Message="); Serial.println(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool isInt(String str){
|
||||||
|
bool isnumeric=true;
|
||||||
|
for (uint8_t i=0;i<str.length();i++) {
|
||||||
|
isnumeric&=isDigit(str.charAt(0));
|
||||||
|
}
|
||||||
|
return isnumeric;
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -18,7 +18,6 @@ monitor_port = /dev/ttyUSB0
|
||||||
|
|
||||||
build_flags=
|
build_flags=
|
||||||
'-D CLIENT_ID="waterreservoir"'
|
'-D CLIENT_ID="waterreservoir"'
|
||||||
-D PIN_BUTTON=12
|
|
||||||
-D PIN_LED=13
|
-D PIN_LED=13
|
||||||
|
|
||||||
-D ONE_WIRE_BUS_PIN=18
|
-D ONE_WIRE_BUS_PIN=18
|
||||||
|
@ -27,9 +26,18 @@ build_flags=
|
||||||
-D PIN_SCL=22
|
-D PIN_SCL=22
|
||||||
-D WATERLEVEL_OFFSET=-490.0
|
-D WATERLEVEL_OFFSET=-490.0
|
||||||
-D WATERLEVEL_FACTOR=-1.0
|
-D WATERLEVEL_FACTOR=-1.0
|
||||||
-D RES_AREA=20*20*3.1416
|
-D RES_AREA=77*56.8*3.1416
|
||||||
-D WATERLEVELSENSOR_VL53L1X
|
-D WATERLEVELSENSOR_VL53L1X
|
||||||
|
|
||||||
|
-D VALVECOUNT=8
|
||||||
|
-D PIN_SERIAL_DATA=25
|
||||||
|
-D PIN_SERIAL_LATCH=33
|
||||||
|
-D PIN_SERIAL_CLOCK=32
|
||||||
|
-D RELAISCOUNT=4
|
||||||
|
-D PIN_RELAIS={12,14,27,26}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
lib_deps =
|
lib_deps =
|
||||||
https://github.com/milesburton/Arduino-Temperature-Control-Library/
|
https://github.com/milesburton/Arduino-Temperature-Control-Library/
|
||||||
https://github.com/emilv/ArduinoSort/
|
https://github.com/emilv/ArduinoSort/
|
||||||
|
|
63
src/main.cpp
63
src/main.cpp
|
@ -26,7 +26,9 @@ bool valuesStabilized=false; //gets set true when values are stable (avaeraging
|
||||||
|
|
||||||
|
|
||||||
#include "helpfunctions.h"
|
#include "helpfunctions.h"
|
||||||
|
#ifdef EC_CALIBRATION_POLYNOM
|
||||||
#include "ADS1X15.h"
|
#include "ADS1X15.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -48,7 +50,10 @@ bool adsenabled=true;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
// PUMP
|
||||||
|
#if defined(VALVECOUNT) || defined(RELAISCOUNT)
|
||||||
|
#include "pump.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
// ######## Flow Rate
|
// ######## Flow Rate
|
||||||
|
@ -68,11 +73,18 @@ Adafruit_NeoPixel pixels(1, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
|
||||||
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
#ifdef PIN_BUTTON
|
||||||
pinMode(PIN_BUTTON,INPUT_PULLUP);
|
pinMode(PIN_BUTTON,INPUT_PULLUP);
|
||||||
#ifdef PIN_LED
|
#endif
|
||||||
|
#ifdef PIN_LED
|
||||||
pinMode(PIN_LED,OUTPUT);
|
pinMode(PIN_LED,OUTPUT);
|
||||||
digitalWrite(PIN_LED,LOW);
|
digitalWrite(PIN_LED,LOW);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#if defined(VALVECOUNT) || defined(RELAISCOUNT)
|
||||||
|
pump_setup();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -112,6 +124,11 @@ void setup() {
|
||||||
connect();
|
connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(VALVECOUNT) || defined(RELAISCOUNT)
|
||||||
|
pump_setup();
|
||||||
|
srOutputOff();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#ifdef PIN_SDA
|
#ifdef PIN_SDA
|
||||||
Wire.begin(PIN_SDA,PIN_SCL);
|
Wire.begin(PIN_SDA,PIN_SCL);
|
||||||
|
@ -154,6 +171,7 @@ void setup() {
|
||||||
flow_setup();
|
flow_setup();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Serial.println("Setup Soilmoisture");
|
Serial.println("Setup Soilmoisture");
|
||||||
sm_setup();
|
sm_setup();
|
||||||
|
@ -210,12 +228,18 @@ void loop() {
|
||||||
flow_loop(loopmillis);
|
flow_loop(loopmillis);
|
||||||
#endif
|
#endif
|
||||||
//sm_loop(loopmillis);
|
//sm_loop(loopmillis);
|
||||||
|
|
||||||
|
|
||||||
|
#if defined(VALVECOUNT) || defined(RELAISCOUNT)
|
||||||
|
pump_loop(loopmillis);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
static bool getReading=false;
|
static bool getReading=false;
|
||||||
|
|
||||||
|
|
||||||
if (!eccalibrationoutput) { //Is in normal operation mode
|
if (!eccalibrationoutput) { //Is in normal operation mode
|
||||||
|
#ifdef PIN_BUTTON
|
||||||
if (!digitalRead(PIN_BUTTON)) { //button pressed
|
if (!digitalRead(PIN_BUTTON)) { //button pressed
|
||||||
valueError=false;
|
valueError=false;
|
||||||
Serial.println("Reset ValueError flag by user");
|
Serial.println("Reset ValueError flag by user");
|
||||||
|
@ -228,6 +252,7 @@ void loop() {
|
||||||
#endif
|
#endif
|
||||||
delay(100);
|
delay(100);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
static bool last_valueError=true;
|
static bool last_valueError=true;
|
||||||
if (!valuesStabilized) { //if values are not okay since boot
|
if (!valuesStabilized) { //if values are not okay since boot
|
||||||
|
@ -546,6 +571,38 @@ void loop() {
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef VALVECOUNT
|
||||||
|
String valvestatestring="";
|
||||||
|
static String last_valvestatestring="";
|
||||||
|
for (uint8_t i=0;i<VALVECOUNT;i++){
|
||||||
|
if (valveOffTime[i]!=0) { //valve on
|
||||||
|
valvestatestring+="1";
|
||||||
|
}else{
|
||||||
|
valvestatestring+="0";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!last_valvestatestring.equals(valvestatestring)) { //has changed
|
||||||
|
publishInfo("valve/state",valvestatestring); //Example: valve 2 active: 010000000
|
||||||
|
}
|
||||||
|
last_valvestatestring=valvestatestring;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef RELAISCOUNT
|
||||||
|
String relaisstatestring="";
|
||||||
|
static String last_relaisstatestring="";
|
||||||
|
for (uint8_t i=0;i<RELAISCOUNT;i++){
|
||||||
|
if (relaisOffTime[i]!=0) { //valve on
|
||||||
|
relaisstatestring+="1";
|
||||||
|
}else{
|
||||||
|
relaisstatestring+="0";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!last_relaisstatestring.equals(relaisstatestring)) { //has changed
|
||||||
|
publishInfo("relais/state",relaisstatestring); //Example: valve 2 active: 010000000
|
||||||
|
}
|
||||||
|
last_relaisstatestring=relaisstatestring;
|
||||||
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
if (ec_adc!=0) {
|
if (ec_adc!=0) {
|
||||||
publishValueTimed("ec/adc",ec_adc,0,timing_ec_adc,loopmillis);
|
publishValueTimed("ec/adc",ec_adc,0,timing_ec_adc,loopmillis);
|
||||||
|
|
Loading…
Add table
Reference in a new issue