Hello
I am making a project in which i will be watering (via module relay 5v) a flower on esp32s via wifi on blynk 2.0 in three modes. the first mode is the presentation mode
to check if everything is working properly. I wonder if it would be better to give a push button instead switch but I’m don’t know how do it properly
in mode 1. In the second mode after switching on the switch I would like to use the slider to determine how many days to water the flower and I want to see this value on gauge.
In the third mode after pressing the switch I would like the soil moisture to decide to water the flower
I have measured that from 640 it is dry and from 270 it is wet. I am using a capacitive soil moisture sensor and they are on the analog pins on the esp32s board
I specifically checked which GPIO is intended for this and chose pin 25.
for each mode i want to add a switch to make it work when switched on. at first the project worked but after
adding this condition, complications started to appear.
Can someone please take a look why this code is not working properly and tell me what I write wrong and what to do to make it correct.
// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""
#define BLYNK_AUTH_TOKEN ""
// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = BLYNK_AUTH_TOKEN;
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";
BlynkTimer timer;
#define relay1_pin 13 // water pump // on board 5 pin left
#define sensor 25 // soil moisture // 11 pin left
const int dry = 640;
const int wet = 270;
int relay1_state = 0; // state mode 1
int relay2_state = 0; // state mode 2 auto
int pinValue = 0; // state v4 - gauge
int sensorVal = 0; // state for soil moisture
//Change the virtual pins according the rooms
#define button1_vpin V1 // 1st mode on/off
#define button2_vpin V2 // auto
#define button3_vpin V3 // slider
#define button4_vpin V4 // gauge slider
#define button5_vpin V5 // soil moisture
//------------------------------------------------------------------------------
// This function is called every time the device is connected to the Blynk.Cloud
// Request the latest state from the server
BLYNK_CONNECTED() {
Blynk.syncVirtual(button1_vpin);
Blynk.syncVirtual(button2_vpin);
Blynk.syncVirtual(button3_vpin);
Blynk.syncVirtual(button4_vpin);
Blynk.syncVirtual(button5_vpin);
}
//--------------------------------------------------------------------------
// This function is called every time the Virtual Pin state change
//i.e when web push switch from Blynk App or Web Dashboard
BLYNK_CONNECTED() {
Blynk.syncAll();
}
BLYNK_WRITE(button1_vpin)
{
if(button1_vpin == HIGH)
{
Blynk.virtualWrite(button2_vpin, LOW);
button2_vpin = LOW;
Serial.println("11111 "); // test this function in console
relay1_state = param.asInt();
// Blynk.notify("You just watered your plant."); // for info ??
if(relay1_state == HIGH) {// if mode 1 is ON
digitalWrite(relay1_pin, relay1_state); // instead state1 write LOW??
Serial.println("water flow 2 sek:");
delay(2000);
}
else {
relay1_state = LOW;
digitalWrite(relay1_pin, HIGH);
Serial.print("no water");
}
}
//--------------------------------------------------------------------------
BLYNK_WRITE(button2_vpin) {
Serial.print("2222: "); // check if function works
if(button2_vpin == HIGH)
{
button1_vpin = relay1_state; // or low??
relay2_state = param.asInt();
digitalWrite(relay1_pin, relay2_state);
Serial.print("mode 2 auto is on");
}
//--------------------------------------------------------------------------
BLYNK_WRITE(button3_vpin)
{
Serial.print(" 3333i : "); // check if function work
if(relay2_state == HIGH) {
pinValue = param.asInt(); // assigning incoming value from pin V3 to a variable
Serial.print("auto switch is ON - confirmation");
// analogWrite(miernik, pinValue); // not analogwrite on esp32 is something different
Blynk.virtualWrite(button4_vpin, pinValue); // v4 is gauge slidera
Serial.print("V3 Slider value is: ");
Serial.println(pinValue);
}
}
// ---- // moisture sensor v5
BLYNK_WRITE(button5_vpin) {
Serial.print(" moisture lvl : ");
sensorVal = analogRead(sensor); // can i use analogRead here on virtual pins
int proWilg = map(sensorVal, wet, dry, 100, 0);
Serial.print(sensorVal);
if(sensorVal > 640 ){ // moisture sensor on this lvl is dry
digitalWrite(relay1_pin, LOW); // turn on water pump
Serial.print("watering ");
}
else {
digitalWrite(relay1_pin, HIGH);
Serial.println("end watering");
Serial.print(proWilg);
Serial.println("%");
delay(100);
}
}
//--------------------------------------------------------------------------
void setup()
{
// Debug console
Serial.begin(115200);
//--------------------------------------------------------------------
pinMode(relay1_pin, OUTPUT);
pinMode(sensor, OUTPUT);
//--------------------------------------------------------------------
//During Starting all Relays should TURN OFF
digitalWrite(relay1_pin, HIGH);
digitalWrite(sensor, HIGH);
//--------------------------------------------------------------------
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
//--------------------------------------------------------------------
//update button states
//Blynk.virtualWrite(button1_vpin, relay1_state);
// Blynk.virtualWrite(button2_vpin, relay2_state);
// Blynk.virtualWrite(button3_vpin, relay2_state);
// Blynk.virtualWrite(button4_vpin, pinValue);
// Blynk.virtualWrite(button5_vpin, relay1_state);
//--------------------------------------------------------------------
}
void loop()
{
Blynk.run();
timer.run();
// You can inject your own code or combine it with other sketches.
// Check other examples on how to communicate with Blynk. Remember
// to avoid delay() function!
Blynk.virtualWrite(button1_vpin, relay1_state);
Blynk.virtualWrite(button2_vpin, relay2_state);
Blynk.virtualWrite(button3_vpin, relay2_state);
Blynk.virtualWrite(button4_vpin, pinValue);
Blynk.virtualWrite(button5_vpin, relay1_state);
delay(1000);
}