Full code
//Pool monitoring & automation
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire from DS18B20 sensors "OneWire" on pin 4 on the ESP32
#define ONE_WIRE_BUS 4
//Timing
unsigned long previousMillis = 0;
const long interval = 1000; //Interval to read
//Pump variables
int pumpRunLED=12; //LED Indicator for pump running (works on pump pressure)
int pumpStart=27; //Output pin for pump start relay
float startDT; //PARAM1 Differential between pool temp and solarpanel out temp managed from Blynk App
BLYNK_WRITE(V15){
startDT = param.asFloat();
}
float stopDT; //PARAM2 Differential to stop pump when the diff temp is below thi set value from Blynk App
BLYNK_WRITE(V16){
stopDT = param.asFloat();
}
//Variables for ESP32 supply voltage measurement from DC-DC Converter (24VDC-5.5VDC)
int VHealthy=25; //LED output pin 5 Volt healthy
int vPin=33; // 5V pin
int voltValue=0.0; //Variable to store value in
float VDC=0.0; //Float voltage value
//Variables for Pump Pressure Monitoring
int highDPin=13; //Warning light for high discharge pressure across sand filter
int pressPin=34; //4-20mA pin via voltage drop resistor
int voltIn = 0.0; //Analog Input
float OutputValue = 0.0; //Voltage In after voltage divider
float Press = 0.0; //Actual Pressure after calculation
float pumpPressHigh=0.7; //Parameter for pump pressure too high
float pumpPressLow=0.6; // Parameter for pump pressure OK
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
uint8_t sensor1[8] = { 0x28, 0xC6, 0xB5, 0x56, 0xB5, 0x01, 0x3C, 0x2F };
uint8_t sensor2[8] = { 0x28, 0xC9, 0xD0, 0x56, 0xB5, 0x01, 0x3C, 0xC8 };
uint8_t sensor3[8] = { 0x28, 0xD0, 0x9F, 0x56, 0xB5, 0x01, 0x3C, 0x83 };
uint8_t sensor4[8] = { 0x28, 0x61, 0x0D, 0x56, 0xB5, 0x01, 0x3C, 0x87 };
uint8_t sensor5[8] = { 0x28, 0x35, 0xD0, 0x56, 0xB5, 0x01, 0x3C, 0xC7 };
float tempSensor1, tempSensor2, tempSensor3, tempSensor4, tempSensor5;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "XXXXXXXXXXXXXXXXXXX";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxx";
BlynkTimer timer;
WidgetLED led1(V11); //pumprunLED
WidgetLED led2(V12); //pump filter high DP LED
WidgetLED led3(V13); //Vhealthy LED
BLYNK_WRITE(V14) {
switch (param.asInt()) {
case 1: //AUTO Selected
//Start pump when the solar temp. sensor is above PARAM1
pumpautoStart();
break;
case 2: // ON Selected
led1.on();
digitalWrite(pumpRunLED,HIGH);
digitalWrite(pumpStart,HIGH);
break;
case 3: // OFF Selected
led1.off();
digitalWrite(pumpRunLED,LOW);
digitalWrite(pumpStart,LOW);
break;
}
}
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V5, millis() / 1000);
}
BLYNK_CONNECTED()
{
Blynk.syncVirtual(V14);
Blynk.syncVirtual(V15);
Blynk.syncVirtual(V16);
}
void setup()
{
// Debug console
Serial.begin(115200);
sensors.begin();
pinMode(pressPin,INPUT); // Input from 4-20mA Pressure Transmitter via a 165ohm voltage drop resistor (3.3/0.020=165ohm)
pinMode(vPin,INPUT); // Input via voltage devider 5V=2.73VDC
pinMode(pumpStart, OUTPUT); // Output to relay for starting pump when the conditions are met
pinMode(VHealthy,OUTPUT); //LED "green" for ESP32 Voltage supply is healthy
pinMode(highDPin,OUTPUT); //LED "yellow" for high DP across sand filter
pinMode(pumpRunLED,OUTPUT); //LED Inditation for pump running
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
// Setup a function to be called every second
timer.setInterval(1000L, myTimerEvent);
Serial.println("Connecting to ");
Serial.println(ssid);
//connect to your local wi-fi network
WiFi.begin(ssid, pass);
//check wi-fi is connected to wi-fi network
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected..!");
Serial.print("Got IP: "); Serial.println(WiFi.localIP());
}
void Temp(void){
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
//Update sensors in loop for the if statements to work
sensors.requestTemperatures(); // Send the command to get temperatures
tempSensor1 = sensors.getTempC(sensor1); // Reads the values of Solar panel inlet temperature
tempSensor2 = sensors.getTempC(sensor2); // Reads the values of Solar panel outlet temperature
tempSensor3 = sensors.getTempC(sensor3); // Reads the values of air temperature
tempSensor4 = sensors.getTempC(sensor4); // Reads the values of pool temperature
tempSensor5 = sensors.getTempC(sensor5); // Reads the PCB temperature
}
}
void pumpautoStart(void){
if (tempSensor2>=(tempSensor4+startDT)){
led1.on();
digitalWrite(pumpRunLED,HIGH);
digitalWrite(pumpStart,HIGH);
}
//Stop pump when the differential temperature is below PARAM2 & pumpOff timer has expired
if ((tempSensor2-tempSensor4)<stopDT){
led1.off();
digitalWrite(pumpRunLED,LOW);
digitalWrite(pumpStart,LOW);
}
}
void Pressure(void){
//Pressure Transmitter
voltIn=analogRead(pressPin); //Read analog values
OutputValue = map(voltIn,650,4095,0,4000); //Convert input to an actual pressure reading using a 4-20ma pressure transmitter remember the digits on the converted value to have decimals
Press=OutputValue/1000;
//Press = OutputValue; // Using a voltage drop resistor and the formula to work out the resistor: VoltIn(max 3.3V)/max current "20ma" (0.020A)= 165ohm
if (Press>pumpPressHigh){
led2.on();
digitalWrite(highDPin,HIGH); //Sets Yellow warning LED on
}
if (Press<pumpPressLow){
led2.off();
digitalWrite(highDPin,LOW); //Sets Yellow warning LED off
}
}
void Voltage(void){
//5VDC Supply from DC-DC Converter via a voltage devider (max 3.3V = 820ohm
voltValue=analogRead(vPin);
VDC = map(voltValue,0,4095,0,6000);
VDC=VDC/1000;
if (VDC>5.0 && VDC<6.2){
led3.on();
digitalWrite(VHealthy,HIGH); //Sets green voltage healthy LED on)
}
if (VDC<5.0 || VDC>6.2){
led3.off();
digitalWrite(VHealthy,LOW); //Sets green voltage NOT healthy LED off)
}
}
void loop(){
Blynk.run();
timer.run(); // Initiates BlynkTimer
//Functions
Temp();
Pressure();
Voltage();
//Blynk variables
Blynk.virtualWrite(V1, tempSensor1); //Solar panel inlet temperature
Blynk.virtualWrite(V2, tempSensor2); //Solar panel outlet temperature
Blynk.virtualWrite(V3, tempSensor3); //Ambient temperature
Blynk.virtualWrite(V4, tempSensor4); //Pool temperature
Blynk.virtualWrite(V6, Press); //Pump Discharge pressure
Blynk.virtualWrite(V7, VDC); //5VDC supply voltage variable
Blynk.virtualWrite(V8, tempSensor5); //PCB Temp
Serial.print("startDT: "); Serial.print(startDT); Serial.print(", stopDT: ");Serial.println(stopDT);
}