Hydroponic Greenhouse Management System Virtual PINs not responding

Hi
I am using Arduino UNO+ESP8266-01+sensors
Using Blynk app on Android MotoG5

Code compiled, however

  1. arduino not reading Virtual input from app for slider(V28) and output slider reading on V29.
  2. Also reading value from Analog A1-A3 but not from attached virtual pin.
  3. Tank level responded once since started device after code upload
  4. Wifi still disconnecting, using Blynk connect function also
    Need help, i have spent 2months with different codes. this was final field working code.
//Irrigation Controller documentation

//#define BLYNK_PRINT Serial

#include <SPI.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <DHT.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <WidgetLED.h>

int isFirstConnect = true;
int humLowTrigger;


//Defining Ultrasonic Sensor PINs and variables
const int ROTRIGPIN = 5;
const int ROECHOPIN = 6;
const int NUTRIGPIN = 8;
const int NUECHOPIN = 7;
long duration, distance;
long duration1, distance1;
long volume, volume1;
float percent, percent1;

//*Defining Relay PINs
const int IRRIGATIONPIN = 9;
const int FOGGERPIN = 10;
const int ROPUMPPIN = 11; //RO, Nutrient A & Nutrient B submersible pumps are connected to PIN 12
const int RELAY4 = 12;

//Defining LED PIN outs
WidgetLED ROLED(20);
WidgetLED IRRIGATIONLED(21);
WidgetLED FOGGERLED(22);
WidgetLED RAINLED(24);
WidgetLED RELAY4LED(23);

//defining RTC
WidgetRTC rtc;

void updateHum(int param);
BLYNK_WRITE(V28) {
  updateHum(param.asInt()); //Reading slider value
}


char auth[] = "de60d990f6184f8191e4a56d4250d02a";

char ssid[] = "D-Link";
char pass[] = "hathway123";

#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX


#define ESP8266_BAUD 115200

ESP8266 wifi(&EspSerial);

#define DHTPIN 4

#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 15 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
  Blynk.virtualWrite(V29, humLowTrigger);
  if(h < humLowTrigger) {
      FOGGERLED.off();
      digitalWrite(FOGGERPIN, LOW); 
      Blynk.virtualWrite(V22, 0);
  } else {
      FOGGERLED.on();
      digitalWrite(FOGGERPIN, HIGH);
      Blynk.virtualWrite(V22, 255);
  }
//Serial.println(h);
//Serial.println(t);
}

void updateHum(int param) {
  humLowTrigger = param;
}


void setup()
{
  // Debug console
  Serial.begin(9600);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

 // Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8442);



  // Setup a function to be called every second
  pinMode(ROTRIGPIN, OUTPUT);
  pinMode(ROECHOPIN, INPUT);
  pinMode(NUTRIGPIN, OUTPUT);
  pinMode(NUECHOPIN, INPUT);

  pinMode(ROPUMPPIN, OUTPUT);
  pinMode(FOGGERPIN, OUTPUT);
  pinMode(IRRIGATIONPIN, OUTPUT);
  pinMode(RELAY4, OUTPUT);

  digitalWrite(FOGGERPIN, LOW);
  int humLowTrigger = 40;

// Display digital clock every 10 seconds
  timer.setInterval(1000L, clockDisplay);

  timer.setInterval(1000L, rolevelSENSOR);
  timer.setInterval(1030L, moistureSENSOR);
  timer.setInterval(1070L, sendSensor);
  
  rtc.begin();   
  dht.begin();
  }

BLYNK_CONNECTED() {
if (isFirstConnect) {
Blynk.syncAll();
isFirstConnect = false;
}
}

void rolevelSENSOR()
{
digitalWrite(ROTRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(ROTRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(ROTRIGPIN, LOW);
duration = pulseIn(ROECHOPIN, HIGH);
distance = (duration/58.2);
volume = (212823.300 - (distance*(3.14*(28.75*28.75))))/1000;
percent = (volume/200)*100;

digitalWrite(NUTRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(NUTRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(NUTRIGPIN, LOW);
duration1 = pulseIn(NUECHOPIN, HIGH);
distance1 = (duration1/58.2);
volume1 = (425646.600 - (2*(distance1*(3.14*(28.75*28.75)))))/1000;
percent1 = (volume1/400)*100;

if(distance1 <15 || distance >65)
{
ROLED.off();
digitalWrite(ROPUMPPIN, LOW);
}
else
{
ROLED.on();
digitalWrite(ROPUMPPIN, HIGH);
}
}

int rainSENSOR = A0;
int bedmoisturePIN = A1;
int growbagmoisture1PIN = A2;
int growbagmoisture2PIN = A3;
int waterSENSOR = A4;

void moistureSENSOR()
{
if(bedmoisturePIN < 400 || bedmoisturePIN > 1023)
{
IRRIGATIONLED.off();
digitalWrite(IRRIGATIONPIN, LOW);
}
else 
{
IRRIGATIONLED.on();
digitalWrite(IRRIGATIONPIN, HIGH);
}
}

// Digital clock display of the time
void clockDisplay()
{
  // You can call hour(), minute(), ... at any time
  // Please see Time library examples for details

  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + " " + month() + " " + year();
  Serial.print("Current time: ");
  Serial.print(currentTime);
  Serial.print(" ");
  Serial.print(currentDate);
  Serial.println();

  // Send time to the App
  Blynk.virtualWrite(V1, currentTime);
  // Send date to the App
  Blynk.virtualWrite(V2, currentDate);
}
BLYNK_READ(V7)
{
Blynk.virtualWrite(7,distance);// virtualpin 7 distance
}

BLYNK_READ(V8)
{
  Blynk.virtualWrite(8, percent); //virtualpin 8 percent
}

BLYNK_READ(V9)
{
Blynk.virtualWrite(9, distance1);
}

BLYNK_READ(V10)
{
Blynk.virtualWrite(10, percent1);
}
BLYNK_READ(V26)
{
Blynk.virtualWrite(26, volume);
}
BLYNK_READ(V27)
{
Blynk.virtualWrite(27, volume1);
}

//BLYNK_READ(V11)
//{
//Blynk.virtualWrite(11, ROPUMPPIN);
//}
//BLYNK_READ(V12)
//{
//Blynk.virtualWrite(12, IRRIGATIONPIN);
//}
//BLYNK_READ(V13)
//{
//Blynk.virtualWrite(13, FOGGERPIN);
//}
//BLYNK_READ(V14)
//{
//Blynk.virtualWrite(14, RELAY4);
//}
BLYNK_READ(V15)
{
Blynk.virtualWrite(15, rainSENSOR);
}
BLYNK_READ(V16)
{
Blynk.virtualWrite(16, bedmoisturePIN);
}
BLYNK_READ(V17)
{
Blynk.virtualWrite(17, growbagmoisture1PIN);
}
BLYNK_READ(V18)
{
Blynk.virtualWrite(18, growbagmoisture2PIN);
}
BLYNK_READ(V19)
{
Blynk.virtualWrite(19, waterSENSOR);
}
//BLYNK_READ(V22)
//{
//Blynk.virtualWrite(22, FOGGERPIN);
//}
//BLYNK_READ(V23)
//{
//Blynk.virtualWrite(23, RELAY4PIN);
//}
//BLYNK_READ(V24)
//{
//Blynk.virtualWrite(24, rainSENSOR);
//}

void loop()
{  
  Blynk.run();
  timer.run();
}

Mapping table:
Pin Mapping Table
Analog/Digital Pin Virtual Pin Device Name
0
1 Water Flow sensor
2 ESP RX
3 ESP TX
4 DHT DHT11
5 1 HR-S04 Trig ROTRIGPIN
6 1 HR-S04 Echo ROECHOPIN
7 2 HR-S04 Trig NUTRIGPIN
8 2 HR-S04 Echo NUECHOPIN
9 V11 Relay1 ROPUMPPIN
10 V12 Relay2 IRRIGATIONPIN
11 V13 Relay3 FOGGERPIN
12 V14 Relay4 RELAY4
13
A0 V15 Rain Sensor rainSENSOR
A1 V16 Soil Moisture Sensor bedmoisturePIN
A2 V17 Soil Moisture Sensor growbagmoisture1PIN
A3 V18 Soil Moisture Sensor growbagmoisture2PIN
A4 V19 Water level sensor waterSENSOR
A5
Humidity V5 DHT H h
Temperature V6 DHT T t
Ultrasonic 1 Distance V7 1 Distance distance
Ultrasonic 1 Percentage V8 1 Percentage perent
Ultrasonic 2 Distance V9 2 Distance distance1
Ultrasonic 2 Percentage V10 2 Percentage percent1
RO LED V20 ROLED
Irrigation LED V21 IRRIGATIONLED
Fogger LED V22 FOGGERLED
Relay4 LED V23 RELAY4LED
Rain LED V24 RAINLED
RTC V25 rtc
Ultrasonic 1 Volume V26 RO Tank volume volume
Ultrasonic 2 Volume V27 Nutrition Tank Volume volume1
Slider Value to control Humidity V28 Humidy control variable Param
Slider value display V29 Humidy control value humLOWTrigger

Please help .

Well, your code is a mess!
I started top try picking some of it apart and gave up. Here’s a bit of feedback on what I found, which I’d change before going any further if I were you…

This bit of code is stranded up at the top of your sketch, in amongst the declarations. I think it needs to be moved.
I’m not clear if the void updateHum(int param); is meant to be defining a function or calling it (it’s defined later in your code) but the syntax, is wrong and it will never run anyway where it is within your sketch.
I’m not sure that the updateHum function will be called when the V28 slider value changes. I’d go for something like this instead:

BLYNK_WRITE(V28) {
  int x = param.asInt(); //Reading slider value
  updateHum(x);
}

Having said that, the updateHum function doesn’t need to be called anyway, as all that does is to pass the value from the slider to the variable humLowTrigger…

so the whole thing could be achieved with:

BLYNK_WRITE(V28) {
  humLowTrigger = param.asInt(); //Reading slider value

}

Other things I spotted are:

You appear to be re-defining the humLowTrigger variable. Not sire why the compiler is letting you get away with that.

also, shouldn’t these be defined as V pins (WidgetLED ROLED(V20);)?

EDITED TO ADD:
The other thing I meant to say was that the best way to figure out what’s happening in your program flow is to throw in lots of Serial.Print statemens so that you can see when a particular function is called, and what the values of key variables are.

Pete.

BTW, it is good practice to include the V, but it is not necessary, as the library knows what to expect.

1 Like

One day I might actually get around to using Blynk in the way that it was designed to be used, then I might start to learn this stuff :grinning:

Pete.

Conform to nothing (well breathing and eating is not optional)… stay flexible and creative :smiley:

1 Like

Hi Pete,

Appreciate your response. I tried each module seperately earlier. So now thought of compiling complete code. I agree there are gaps in coding. I’ll follow changes suggested by you and update with status. I tried to interpret each line too but coz I’m not from coding background I couldn’t identify gaps.

Thank you

Its not easy to start thinking like a ‘coder’, but basic stuff that you can do which make one hell of a difference: being tidy!

  • make sure ALL your indentations are in order (they’re not)
  • use spacings at LEAST on between all the functions, so its clear where one ends and the other starts (together with the indentation)
  • make sure you group you functions as logically as you can (they’re all over the place)
  • make sure ALL global declerations are FIRST. ACtually the correct order is:
  1. inclusion of #header files
  2. #definitions
  3. global constants
  4. global variables
  5. constructs (e.g. WidgetRTC rtc;)
    (3, 4 and 5 may also be logically mixed, just make sure it makes sense)
  6. function declerations
  7. functions/routines
  • use CAPITAL for constants and titleCase for variables (from what I see you got that right!!)

Can you check my revised code:

//Irrigation Controller documentation

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <DHT.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <SoftwareSerial.h>

#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

int isFirstConnect = true;

#define waterflowSENSOR 1
#define ROTRIGPIN 5
#define ROECHOPIN 6
#define NUECHOPIN 7
#define NUTRIGPIN 8

long roduration, rodistance, rovolume;
long nuduration, nudistance, nuvolume;
float ropercent, nupercent;

#define IRRIGATIONPIN 9
#define FOGGERPIN 10
#define ROPUMPPIN 11 
#define RELAY4 12

#define rainSENSOR A0
#define bedmoisturePIN A1
#define growbagmoisture1PIN A2
#define growbagmoisture2PIN A3
#define waterSENSOR A4

#define ROLED V18;
#define IRRIGATIONLED V19;
#define FOGGERLED V20;
#define RELAY4LED V21;
#define RAINLED V22;

WidgetRTC rtc;

BLYNK_WRITE(V23) 
{
int humLowTrigger = param.asInt(); //Reading slider value
}


char auth[] = "de60d990f6184f8191e4a56d4250d02a";

char ssid[] = "D-Link";
char pass[] = "hathway123";

SoftwareSerial EspSerial(2, 3); // RX, TX
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);

BlynkTimer timer;

BLYNK_CONNECTED() 
{
if (isFirstConnect) 
{
Blynk.syncAll();
isFirstConnect = false;
}
}

void setup()
{
  Serial.begin(9600);

  EspSerial.begin(ESP8266_BAUD);
  delay(10);

 Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 8442);
 
 pinMode(ROTRIGPIN, OUTPUT);
 pinMode(ROECHOPIN, INPUT);
 pinMode(NUTRIGPIN, OUTPUT);
 pinMode(NUECHOPIN, INPUT);

 pinMode(ROPUMPPIN, OUTPUT);
 pinMode(FOGGERPIN, OUTPUT);
 pinMode(IRRIGATIONPIN, OUTPUT);
 pinMode(RELAY4, OUTPUT);

digitalWrite(ROPUMPPIN, LOW);
digitalWrite(FOGGERPIN, LOW);
digitalWrite(IRRIGATIONPIN, LOW);
digitalWrite(RELAY4, LOW);
  

  timer.setInterval(1000L, clockDisplay);
  timer.setInterval(1000L, rolevelSENSOR);
  timer.setInterval(1000L, moistureSENSOR);
  timer.setInterval(1000L, sendSensor);
  timer.setInterval(1000L, rainSensor);

  dht.begin();
  rtc.begin();     
  }




void sendSensor()
{
float h = dht.readHumidity();
 	float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  	if (isnan(h) || isnan(t)) 
{
    		Serial.println("Failed to read from DHT sensor!");
    		return;
  	}

  	Blynk.virtualWrite(V5, h);
  	Blynk.virtualWrite(V6, t);
  	
if(h >= humLowTrigger) 
{
      		digitalWrite(FOGGERPIN, LOW);
		Blynk.virtualWrite(FOGGERLED, LOW);
		
  	} 
else {

      		digitalWrite(FOGGERPIN, HIGH);
 		Blynk.virtualWrite(FOGGERLED, HIGH);
}

Serial.println(h);
Serial.println(t);
}

void rolevelSENSOR()
{
digitalWrite(ROTRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(ROTRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(ROTRIGPIN, LOW);
roduration = pulseIn(ROECHOPIN, HIGH);
rodistance = (roduration/58.2);
rovolume = (212823.300 - (rodistance*(3.14*(28.75*28.75))))/1000;
ropercent = (rovolume/200)*100;

digitalWrite(NUTRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(NUTRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(NUTRIGPIN, LOW);
nuduration = pulseIn(NUECHOPIN, HIGH);
nudistance = (nuDuration/58.2);
nuvolume = (425646.600 - (2*(nudistance*(3.14*(28.75*28.75)))))/1000;
nupercent = (nuvolume/400)*100;

if(nudistance <10 || rodistance >=62)
{
digitalWrite(ROPUMPPIN, LOW);
	Blynk.virtualWrite(ROLED, LOW);

}
else {
	digitalWrite(ROPUMPPIN, HIGH);
Blynk.virtualWrite(ROLED, HIGH);
}

Blynk.virtualWrite(V7, rodistance);
Blynk.virtualWrite(V8, rovolume);
Blynk.virtualWrite(V9, ropercent);
Blynk.virtualWrite(V10, nudistance);
Blynk.virtualWrite(V11, nuvolume);
Blynk.virtualWrite(V12, nupercent);	
}


void moistureSENSOR()
{
if(bedmoisturePIN < 350)
{
digitalWrite(IRRIGATIONPIN, LOW);
Blynk.virtualWrite(IRRIGATIONLED, LOW);

}
else {
digitalWrite(IRRIGATIONPIN, HIGH);
Blynk.virtualWrite(IRRIGATIONLED, HIGH);
}

	Blynk.virtualWrite(V14, bedmoisturePIN);
	Blynk.virtualWrite(V15, growbagmoisture1PIN);
	Blynk.virtualWrite(V16, growbagmoisture2PIN);

}

// Digital clock display of the time
void clockDisplay()
{
 	 // You can call hour(), minute(), ... at any time
  	// Please see Time library examples for details

  	String currentTime = String(hour()) + ":" + minute() + ":" + second();
  	String currentDate = String(day()) + " " + month() + " " + year();
  	Serial.print("Current time: ");
  	Serial.print(currentTime);
  	Serial.print(" ");
  	Serial.print(currentDate);
  	Serial.println();

  	// Send time to the App
  	Blynk.virtualWrite(V1, currentTime);
  	// Send date to the App
  	Blynk.virtualWrite(V2, currentDate);
}

void rainSensor()
{
	if(rainSENSOR<700)
	{
		Blynk.virtualWrite(RAINLED, HIGH);

	} else {

		Blynk.virtualWrite(RAINLED, LOW);
	}
)

BLYNK_READ(V13)
{
Blynk.virtualWrite(13, rainSENSOR);
}

BLYNK_READ(V17)
{
Blynk.virtualWrite(17, waterSENSOR);
}

BLYNK_READ(V24)
{
Blynk.virtualWrite(24, humLowTrigger);
}


BLYNK_READ(V26)
{
Blynk.virtualWrite(26, waterflowSENSOR);
}

void loop()
{  
  Blynk.run();
  timer.run();
}

Guys please help with revised code review.

  1. I can’t find ANY reference to V28 nor V29 in your code, which is probably the cause!
  2. have absolutely no idead what you’re saying
  3. great, so what’s the issue?
  4. yes… thats a familiar issue with esp’s and blynk. Most of the time they don’t actually disconnect, but they loose heartbeat which cause a ‘disconnect’ (it takes too long for the code to send a ‘I’m still alive’ to blynk. This is usually the result of either VERY long routines, stacking timers or delay()'s.

This is a nice example of stacking timers, check this out.

  timer.setInterval(1000L, clockDisplay);
  timer.setInterval(1000L, rolevelSENSOR);
  timer.setInterval(1000L, moistureSENSOR);
  timer.setInterval(1000L, sendSensor);
  timer.setInterval(1000L, rainSensor);

furthermore…here’s a nice example of indentations going all over the place making code unreadable and thus errorprone:

void sendSensor()
{
float h = dht.readHumidity();
 	float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  	if (isnan(h) || isnan(t)) 
{
    		Serial.println("Failed to read from DHT sensor!");
    		return;
  	}

  	Blynk.virtualWrite(V5, h);
  	Blynk.virtualWrite(V6, t);
  	
if(h >= humLowTrigger) 
{
      		digitalWrite(FOGGERPIN, LOW);
		Blynk.virtualWrite(FOGGERLED, LOW);
		
  	} 
else {

      		digitalWrite(FOGGERPIN, HIGH);
 		Blynk.virtualWrite(FOGGERLED, HIGH);
}

Serial.println(h);
Serial.println(t);
}

FIX THIS!! You’ll thank me later (and not only the example, your entire code!!
In case you have no idea what I’m talking about, here a correct example:

void reconnectBlynk() {
  if (!Blynk.connected()) {
    if(Blynk.connect()) {
      BLYNK_LOG("Reconnected");
	  isFirstConnect = true;
	 } else {
      BLYNK_LOG("Not reconnected");
    }
  }
}

or (matter of taste)

void reconnectBlynk() 
{
  if (!Blynk.connected()) 
  {
    if(Blynk.connect()) 
	{
      BLYNK_LOG("Reconnected");
	  isFirstConnect = true;
	 } 
	 else 
	 {
      BLYNK_LOG("Not reconnected");
    }
  }
}

and as it happens, that example is also very useful to get blynk reconnected!! Put the function on a e.g. 30s timer (so it runs every 30s) which helps with the reconnect.

Yeh, so you are right.
I copied code from google drive hence indentation is coming haywire. However in IDE is indented it properly. While after fixing earlier issues i was able to compile and upload code, however I am still facing following problems:

  1. ESP is still frequently disconnecting
  2. I tried serial print for temp & humidity, which is working fine on serial monitor, but no value is displayed on APP
  3. None of the values are refreshing on APP. i.e. Temp & Humidity not reflecting any value and so other like ultrasonic sensor values (distance, volume, percentage)
  4. RTC showing 1 1 1970 date and some non relevant time. for once it reflected correct time for 30 seconds, but again back to square one.
  5. Serial monitor msg: Buffer Overflow and timeout, I am not using any write command in Void loop.

please help, i have been trying this for quite sometime no success yet.

did you read and implement the suggestions I mentioned?

search this forum for 1970 its a commonly made mistake.

you’re probably asking too much too quick, double or quadruple your timer period.

did you check the vpins and device? Are they correct? (check again!)

  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);

don’t do this. Define ALL your pins at the top and use the definitions. If you have a short script its ok, but with longer scripts ALWAYS use definitions and group your pins so you have an overview of which are used for what.

Blynk.virtualWrite(FOGGERLED, HIGH);
assuming you’re using led, the app leds have an intensity of 0 to 200. So setting it from 0 (LOW) to 1 (HIGH) will have a very limited effect on the app.

Also in your case place your virtualwrites next to you serial.print so that you are SURE that when the serial is updated the app should be updated. If the latter is not the case AND you are connected!! THEN you have set it up wrong in the app, not in your code!

Hi Wolph

I made suggested changes and here is response I am getting:


[675] Connecting to D-Link
[1685] ESP is not responding
[27141] Ready (ping: 12ms).
[71707] Ready (ping: 829ms).
[71727] Packet too big: 25088
[101638] Buffer overflow
[108389] Ready (ping: 13ms).
[114646] Buffer overflow
[115645] Buffer overflow
[116647] Buffer overflow
[117645] Buffer overflow
[133661] Buffer overflow
[134661] Buffer overflow

here is the code I am using:

//Irrigation Controller documentation

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <DHT.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <SoftwareSerial.h>

#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);


#define waterflowSENSOR 1
#define ROTRIGPIN 5
#define ROECHOPIN 6
#define NUECHOPIN 7
#define NUTRIGPIN 8
#define IRRIGATIONPIN 9
#define FOGGERPIN 10
#define ROPUMPPIN 11 
#define RELAY4 12
#define rainSENSOR A0
#define bedmoisturePIN A1
#define growbagmoisture1PIN A2
#define growbagmoisture2PIN A3
#define waterSENSOR A4
#define ROLED V18
#define IRRIGATIONLED V19
#define FOGGERLED V20
#define RELAY4LED V21
#define RAINLED V22
#define t V5
#define h V6

long roduration, nuduration;
long rodistance = V7;
long rovolume = V8;
float ropercent = V9;
long nudistance = V10;
long nuvolume = V11;
float nupercent = V12;
int humLowTrigger;
int isFirstConnect = true;



WidgetRTC rtc;

BLYNK_WRITE(V23) 
{
  humLowTrigger = param.asInt(); //Reading slider value
  Blynk.virtualWrite(V24, humLowTrigger);
}


char auth[] = "************************";

char ssid[] = "*********";
char pass[] = "***********";

SoftwareSerial EspSerial(2, 3); // RX, TX
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);

BlynkTimer timer;

void setup()
{
  Serial.begin(9600);

  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 8442);
 
  pinMode(ROTRIGPIN, OUTPUT);
  pinMode(ROECHOPIN, INPUT);
  pinMode(NUTRIGPIN, OUTPUT);
  pinMode(NUECHOPIN, INPUT);

  pinMode(ROPUMPPIN, OUTPUT);
  pinMode(FOGGERPIN, OUTPUT);
  pinMode(IRRIGATIONPIN, OUTPUT);
  pinMode(RELAY4, OUTPUT);

  digitalWrite(ROPUMPPIN, LOW);
  digitalWrite(FOGGERPIN, LOW);
  digitalWrite(IRRIGATIONPIN, LOW);
  digitalWrite(RELAY4, LOW);
  
  timer.setInterval(1000L, clockDisplay);
  timer.setInterval(5000L, rolevelSENSOR);
  timer.setInterval(10000L, moistureSENSOR);
  timer.setInterval(15000L, sendSensor);
  timer.setInterval(20000L, rainSensor);

  dht.begin();
  }

  void reconnectBlynk() {
        if (!Blynk.connected()) {
        if(Blynk.connect()) {
        BLYNK_LOG("Reconnected");
        isFirstConnect = true;
        rtc.begin();
        } else {
        BLYNK_LOG("Not reconnected");
      }
    }
  }

void sendSensor()
{
    dht.readHumidity(h);
    dht.readTemperature(t); // or dht.readTemperature(true) for Fahrenheit

    if (isnan(h) || isnan(t)) 
    {
        Serial.println("Failed to read from DHT sensor!");
        return;
    }

    if(h >= humLowTrigger) 
    {
          digitalWrite(FOGGERPIN, LOW);
          Blynk.virtualWrite(FOGGERLED, 0);
    
    } 
    else {

          digitalWrite(FOGGERPIN, HIGH);
          Blynk.virtualWrite(FOGGERLED, 255);
    }

      //Serial.println(h);
      //Serial.println(t);
 }

void rolevelSENSOR()
  {
    digitalWrite(ROTRIGPIN, LOW);
    delayMicroseconds(2);
    digitalWrite(ROTRIGPIN, HIGH);
    delayMicroseconds(10);
    digitalWrite(ROTRIGPIN, LOW);
    roduration = pulseIn(ROECHOPIN, HIGH);
    rodistance = (roduration/58.2);
    rovolume = (212823.300 - (rodistance*(3.14*(28.75*28.75))))/1000;
    ropercent = (rovolume/200)*100;

    digitalWrite(NUTRIGPIN, LOW);
    delayMicroseconds(2);
    digitalWrite(NUTRIGPIN, HIGH);
    delayMicroseconds(10);
    digitalWrite(NUTRIGPIN, LOW);
    nuduration = pulseIn(NUECHOPIN, HIGH);
    nudistance = (nuduration/58.2);
    nuvolume = (425646.600 - (2*(nudistance*(3.14*(28.75*28.75)))))/1000;
    nupercent = (nuvolume/400)*100;

    if(nudistance <10 || rodistance >=62)
    {
        digitalWrite(ROPUMPPIN, LOW);
        Blynk.virtualWrite(ROLED, 0);

    }
    else {
        
        digitalWrite(ROPUMPPIN, HIGH);
        Blynk.virtualWrite(ROLED, 255);
    }
  }


void moistureSENSOR()
  {
    if(bedmoisturePIN < 350)
      {
        digitalWrite(IRRIGATIONPIN, LOW);
        Blynk.virtualWrite(IRRIGATIONLED, 0);
      }
      else {
        
        digitalWrite(IRRIGATIONPIN, HIGH);
        Blynk.virtualWrite(IRRIGATIONLED, 255);
      }
  }

// Digital clock display of the time
void clockDisplay()
  {
   // You can call hour(), minute(), ... at any time
    // Please see Time library examples for details

    String currentTime = String(hour()) + ":" + minute() + ":" + second();
    String currentDate = String(day()) + " " + month() + " " + year();
    Serial.print("Current time: ");
    Serial.print(currentTime);
    Serial.print(" ");
    Serial.print(currentDate);
    Serial.println();

    // Send time to the App
    Blynk.virtualWrite(V1, currentTime);
    // Send date to the App
    Blynk.virtualWrite(V2, currentDate);
}

void rainSensor()
  {
    if(rainSENSOR<700)
    {
      Blynk.virtualWrite(RAINLED, 255);

    } else {

      Blynk.virtualWrite(RAINLED, 0);
    }
  }

void loop()
  {
    Blynk.run();
    timer.run();
  }

This is app screenshot:

All values on App are old values, aren’t getting refreshed.

I didn’t go through your code with a dustcomb but what I saw looked good!

this appears something between the arduino-esp communication which is beyond my expertise, I’ve never worked with that setup and cannot help you with that. Somebody else has to step in on that.
My suggestion would be: google something like ‘arduino esp8266 packet too big’ and see what shows up.

You were right. Seems my Uno isn’t working properly. Using Mega2560 now. Stable connection. Reading all sensors.

I am revising program to work properly. Relay pins are working with reverse logic. Fixed if loop and relay pin logic for Tank level. Working on next.

Thank you everyone for kind support.I have completed compilation and everything is fine so far. Here are the snapshots:

image

How can I move topic to [Solved] category?

edit first topic, change the label.

I’ve done it for you.

Pete.