Need help with my project code to automate hydroponics

Hi, I am automating my hydroponics system using arduino uno, esp8266 and Blynk2.0 app. i am able to get all the values like ph (using ph sensor), temperature (dht22), humidity (dht22), real time clock on the Blynk2.0 app but as soon as i introduced code for water sensor (DS18B20), TDS value (TDS meter sensor), communication is stopped working with arduino uno and bynk2.0 app, serial monitor stuck on “connected to wifi, ready (ping: 24ms)”.

note: as soon as i removed Blynk code, all the values started appearing on the serial monitor in Arduino IDE.

below is the code:

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <SoftwareSerial.h>
#include <DHT.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <EEPROM.h>
#include "GravityTDS.h"



#define BLYNK_TEMPLATE_ID "xxxxxxxx"
#define BLYNK_DEVICE_NAME "Mavi Hydroponic"
#define BLYNK_AUTH_TOKEN "xxxxxxxxxx"

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "xxxx";
char pass[] = "xxxxxxxxxx";

SoftwareSerial EspSerial(2, 3); // RX, TX
// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);
#define DHTPIN 4          // Mention the digital pin where you connected 
#define DHTTYPE DHT22     // DHT 22 
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
WidgetRTC rtc;
#define SensorPin A0          // the pH meter Analog output is connected with the Arduino’s Analog

#define ONE_WIRE_BUS 7
#define TdsSensorPin A1
 
OneWire oneWire(ONE_WIRE_BUS); 
GravityTDS gravityTds;
 
DallasTemperature sensors(&oneWire);
 
float tdsValue = 0;
float ecValue=0;

void sendTempTdsEC()
{

    //Get Water Temperature, TDS and EC values
    sensors.requestTemperatures();
 
    gravityTds.setTemperature(sensors.getTempCByIndex(0));  // set the temperature and execute temperature compensation
    gravityTds.update();  //sample and calculate
    tdsValue = gravityTds.getTdsValue();  // then get the value
    ecValue = (tdsValue*2)/1000;
    Serial.print("TDS is: "); 
    Serial.print(tdsValue,0);
    Serial.println("ppm");
    Serial.print("EC is: "); 
    Serial.println(ecValue);
    Serial.print("Water Temperature is: "); 
    Serial.println(sensors.getTempCByIndex(0));

 Blynk.virtualWrite(V7, sensors.getTempCByIndex(0));
  Blynk.virtualWrite(V8, tdsValue);
  Blynk.virtualWrite(V9, ecValue);
}

//Light
BLYNK_WRITE(V5)
{
  int light = param.asInt(); 
  if (light==1)
  {
    digitalWrite(5,HIGH);
  }
  else
  {
    digitalWrite(5, LOW);
  }
  
}
//Water Pump
BLYNK_WRITE(V6)
{
  int waterPump = param.asInt(); 
  if (waterPump==1)
  {
    digitalWrite(6,HIGH);
  }
  else
  {
    digitalWrite(6, LOW);
  }
  
}

// 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(V3, currentTime);
  // Send date to the App
  Blynk.virtualWrite(V4, currentDate);
}

BLYNK_CONNECTED() {
  // Synchronize time on connection
  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;
  }
    Serial.println(t);
  Blynk.virtualWrite(V0, t);
  Blynk.virtualWrite(V1, h);
    Serial.print("Temperature : ");
    Serial.print(t);
    Serial.print("    Humidity : ");
    Serial.println(h);


}

void sendPhvalue()
{
  unsigned long int avgValue;  //Store the average value of the sensor feedback
float b;
int buf[10],temp;
float calibration_value = 21.34-0.18;
   for(int i=0;i<10;i++)       //Get 10 sample value from the sensor for smooth the value
  { 
    buf[i]=analogRead(SensorPin);
    delay(10);
  }
  for(int i=0;i<9;i++)        //sort the analog from small to large
  {
    for(int j=i+1;j<10;j++)
    {
      if(buf[i]>buf[j])
      {
        temp=buf[i];
        buf[i]=buf[j];
        buf[j]=temp;
      }
    }
  }
  avgValue=0;
  for(int i=2;i<8;i++)                      //take the average value of 6 center sample
    avgValue+=buf[i];
  float phValue=(float)avgValue*5.0/1024/6; //convert the analog into millivolt
  phValue = -5.70 * phValue + calibration_value;                     //convert the millivolt into pH value
  Blynk.virtualWrite(V2, phValue);
  Serial.print("    pH:");  
  Serial.print(phValue,2);
  Serial.println(" ");
}

void setup()
{
 
  // Set ESP8266 baud rate
 Serial.begin(ESP8266_BAUD);
 delay(10);
// Debug console
 EspSerial.begin(9600);
 delay(10);
Blynk.begin(auth, wifi, ssid, pass, "blynk.cloud", 80);
 sensors.begin();
  gravityTds.setPin(TdsSensorPin);
   gravityTds.setAref(5.0);  //reference voltage on ADC, default 5.0V on Arduino UNO
   gravityTds.setAdcRange(1024);  //1024 for 10bit ADC;4096 for 12bit ADC
   gravityTds.begin();  //initialization
 dht.begin();
timer.setInterval(2500L, sendSensor);
 setSyncInterval(10 * 60); // Sync interval in seconds (10 minutes)

  // Display digital clock every 10 seconds
 timer.setInterval(10000L, clockDisplay);
 timer.setInterval(5000L, sendPhvalue);
 timer.setInterval(500L, sendTempTdsEC);
  pinMode(5, OUTPUT); // for light
  pinMode (6, OUTPUT); //for water pump

}
void loop()
{

Blynk.run();
 timer.run();
}
'''

@smavi please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

done

Your code is a messy Legacy sketch that hasn’t been converted to Blynk IoT very well.

I’d suggest that you have a single timed function which takes readings from each sensor in turn, then pushed the results to Blynk and your srial monitor. That way you don’t have multiple overlapping timers which are always competing with each other, and your sensor code scattered overt three different functions.

It would be much better to copy/paste the serial monitor output (use triple backticks again when posting it here).

Pete.

tried below but no luck.

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <SoftwareSerial.h>
#include <DHT.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <EEPROM.h>
#include "GravityTDS.h"



#define BLYNK_TEMPLATE_ID "xxxxxxxx"
#define BLYNK_DEVICE_NAME "Mavi Hydroponic"
#define BLYNK_AUTH_TOKEN "xxxxxxxxxx"

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "xxxx";
char pass[] = "xxxxxxxxxx";

SoftwareSerial EspSerial(2, 3); // RX, TX
// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);
#define DHTPIN 4          // Mention the digital pin where you connected 
#define DHTTYPE DHT22     // DHT 22 
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
WidgetRTC rtc;
#define SensorPin A0          // the pH meter Analog output is connected with the Arduino’s Analog

#define ONE_WIRE_BUS 7
#define TdsSensorPin A1
 
OneWire oneWire(ONE_WIRE_BUS); 
GravityTDS gravityTds;
 
DallasTemperature sensors(&oneWire);
 
float tdsValue = 0;
float ecValue=0;

void sendSensor()
{

    //Get Water Temperature, TDS and EC values
    sensors.requestTemperatures();
 
    gravityTds.setTemperature(sensors.getTempCByIndex(0));  // set the temperature and execute temperature compensation
    gravityTds.update();  //sample and calculate
    tdsValue = gravityTds.getTdsValue();  // then get the value
    ecValue = (tdsValue*2)/1000;
    Serial.print("TDS is: "); 
    Serial.print(tdsValue,0);
    Serial.println("ppm");
    Serial.print("EC is: "); 
    Serial.println(ecValue);
    Serial.print("Water Temperature is: "); 
    Serial.println(sensors.getTempCByIndex(0));

 Blynk.virtualWrite(V7, sensors.getTempCByIndex(0));
  Blynk.virtualWrite(V8, tdsValue);
  Blynk.virtualWrite(V9, ecValue);

  //Get Temperature and Humidity values

   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;
  }
    Serial.println(t);
  Blynk.virtualWrite(V0, t);
  Blynk.virtualWrite(V1, h);
    Serial.print("Temperature : ");
    Serial.print(t);
    Serial.print("    Humidity : ");
    Serial.println(h);

    //Get Ph values
     unsigned long int avgValue;  //Store the average value of the sensor feedback
float b;
int buf[10],temp;
float calibration_value = 21.34-0.18;
   for(int i=0;i<10;i++)       //Get 10 sample value from the sensor for smooth the value
  { 
    buf[i]=analogRead(SensorPin);
    delay(10);
  }
  for(int i=0;i<9;i++)        //sort the analog from small to large
  {
    for(int j=i+1;j<10;j++)
    {
      if(buf[i]>buf[j])
      {
        temp=buf[i];
        buf[i]=buf[j];
        buf[j]=temp;
      }
    }
  }
  avgValue=0;
  for(int i=2;i<8;i++)                      //take the average value of 6 center sample
    avgValue+=buf[i];
  float phValue=(float)avgValue*5.0/1024/6; //convert the analog into millivolt
  phValue = -5.70 * phValue + calibration_value;                     //convert the millivolt into pH value
  Blynk.virtualWrite(V2, phValue);
  Serial.print("    pH:");  
  Serial.print(phValue,2);
  Serial.println(" ");

  //Get real time clock

  // 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(V3, currentTime);
  // Send date to the App
  Blynk.virtualWrite(V4, currentDate);
}

//Light
BLYNK_WRITE(V5)
{
  int light = param.asInt(); 
  if (light==1)
  {
    digitalWrite(5,HIGH);
  }
  else
  {
    digitalWrite(5, LOW);
  }
  
}
//Water Pump
BLYNK_WRITE(V6)
{
  int waterPump = param.asInt(); 
  if (waterPump==1)
  {
    digitalWrite(6,HIGH);
  }
  else
  {
    digitalWrite(6, LOW);
  }
  
}

BLYNK_CONNECTED() {
  // Synchronize time on connection
  rtc.begin();
}


void setup()
{
 
  // Set ESP8266 baud rate
 Serial.begin(ESP8266_BAUD);
 delay(10);
// Debug console
 EspSerial.begin(9600);
 delay(10);
Blynk.begin(auth, wifi, ssid, pass, "blynk.cloud", 80);
 sensors.begin();
  gravityTds.setPin(TdsSensorPin);
   gravityTds.setAref(5.0);  //reference voltage on ADC, default 5.0V on Arduino UNO
   gravityTds.setAdcRange(1024);  //1024 for 10bit ADC;4096 for 12bit ADC
   gravityTds.begin();  //initialization
 dht.begin();
timer.setInterval(2500L, sendSensor);
setSyncInterval(10 * 60); // Sync interval in seconds (10 minutes)
  pinMode(5, OUTPUT); // for light
  pinMode (6, OUTPUT); //for water pump

}
void loop()
{

Blynk.run();
 timer.run();
}

here is the output

20:46:31.159 ->     ___  __          __
20:46:31.159 ->    / _ )/ /_ _____  / /__
20:46:31.159 ->   / _  / / // / _ \/  '_/
20:46:31.159 ->  /____/_/\_, /_//_/_/\_\
20:46:31.159 ->         /___/ v1.0.1 on Arduino Uno
20:46:31.159 -> 
20:46:31.660 -> [527] Connecting to abc
20:46:34.850 -> [3735] AT version:1.2.0.0(Jul  1 2016 20:04:45)
20:46:34.850 -> SDK version:1.5.4.1(39cb9a32)
20:46:34.897 -> Ai-Thinker Technology Co. Ltd.
20:46:34.897 -> Dec  2 2016 14:21:16
20:46:34.897 -> OK
20:46:42.108 -> [10969] 
20:46:42.108 -> [10969] Connected to WiFi
20:46:52.468 -> [21329] Ready (ping: 24ms).
20:47:28.036 -> [56896] Ready (ping: 25ms).
20:48:03.629 -> [92480] Ready (ping: 24ms).
20:48:39.183 -> [128046] Ready (ping: 23ms).
20:49:14.777 -> [163634] Ready (ping: 23ms).
20:49:50.325 -> [199195] Ready (ping: 24ms).
20:50:25.945 -> [234788] Ready (ping: 24ms).
20:51:01.605 -> [270447] Ready (ping: 24ms).

Disconnect each sensor in turn, and reboot your board each time.
What effect does this have?

Pete.

tried disconnecting each sensor and reboot, issue persists. Then i removed all the sensor still the same issue.
As soon as i commented out the code for TDS sensor and Water temperature sensor. it started working.

below is the working code having TDS and Water sensor code commented out:

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <SoftwareSerial.h>
#include <DHT.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <EEPROM.h>
#include "GravityTDS.h"



#define BLYNK_TEMPLATE_ID "xxxxxxxx"
#define BLYNK_DEVICE_NAME "Mavi Hydroponic"
#define BLYNK_AUTH_TOKEN "xxxxxxxxxx"

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "xxxx";
char pass[] = "xxxxxxxxxx";

SoftwareSerial EspSerial(2, 3); // RX, TX
// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);
#define DHTPIN 4          // Mention the digital pin where you connected 
#define DHTTYPE DHT22     // DHT 22 
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
WidgetRTC rtc;
#define SensorPin A0          // the pH meter Analog output is connected with the Arduino’s Analog

#define ONE_WIRE_BUS 7
#define TdsSensorPin A1
 
OneWire oneWire(ONE_WIRE_BUS); 
//GravityTDS gravityTds;
 
DallasTemperature sensors(&oneWire);
 
float tdsValue = 0;
float ecValue=0;

void sendSensor()
{

    //Get Water Temperature, TDS and EC values
     /* sensors.requestTemperatures();
 
    gravityTds.setTemperature(sensors.getTempCByIndex(0));  // set the temperature and execute temperature compensation
    gravityTds.update();  //sample and calculate
    tdsValue = gravityTds.getTdsValue();  // then get the value
    ecValue = (tdsValue*2)/1000;
    Serial.print("TDS is: "); 
    Serial.print(tdsValue,0);
    Serial.println("ppm");
    Serial.print("EC is: "); 
    Serial.println(ecValue);
    Serial.print("Water Temperature is: "); 
    Serial.println(sensors.getTempCByIndex(0));

 Blynk.virtualWrite(V7, sensors.getTempCByIndex(0));
  Blynk.virtualWrite(V8, tdsValue);
  Blynk.virtualWrite(V9, ecValue);*/

  //Get Temperature and Humidity values

   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;
  }
    Serial.println(t);
  Blynk.virtualWrite(V0, t);
  Blynk.virtualWrite(V1, h);
    Serial.print("Temperature : ");
    Serial.print(t);
    Serial.print("    Humidity : ");
    Serial.println(h);

    //Get Ph values
     unsigned long int avgValue;  //Store the average value of the sensor feedback
float b;
int buf[10],temp;
float calibration_value = 21.34-0.18;
   for(int i=0;i<10;i++)       //Get 10 sample value from the sensor for smooth the value
  { 
    buf[i]=analogRead(SensorPin);
    delay(10);
  }
  for(int i=0;i<9;i++)        //sort the analog from small to large
  {
    for(int j=i+1;j<10;j++)
    {
      if(buf[i]>buf[j])
      {
        temp=buf[i];
        buf[i]=buf[j];
        buf[j]=temp;
      }
    }
  }
  avgValue=0;
  for(int i=2;i<8;i++)                      //take the average value of 6 center sample
    avgValue+=buf[i];
  float phValue=(float)avgValue*5.0/1024/6; //convert the analog into millivolt
  phValue = -5.70 * phValue + calibration_value;                     //convert the millivolt into pH value
  Blynk.virtualWrite(V2, phValue);
  Serial.print("    pH:");  
  Serial.print(phValue,2);
  Serial.println(" ");

  //Get real time clock

  // 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(V3, currentTime);
  // Send date to the App
  Blynk.virtualWrite(V4, currentDate);
}

//Light
BLYNK_WRITE(V5)
{
  int light = param.asInt(); 
  if (light==1)
  {
    digitalWrite(5,HIGH);
  }
  else
  {
    digitalWrite(5, LOW);
  }
  
}
//Water Pump
BLYNK_WRITE(V6)
{
  int waterPump = param.asInt(); 
  if (waterPump==1)
  {
    digitalWrite(6,HIGH);
  }
  else
  {
    digitalWrite(6, LOW);
  }
  
}

BLYNK_CONNECTED() {
  // Synchronize time on connection
  rtc.begin();
}


void setup()
{
 
  // Set ESP8266 baud rate
 Serial.begin(ESP8266_BAUD);
 delay(10);
// Debug console
 EspSerial.begin(9600);
 delay(10);
Blynk.begin(auth, wifi, ssid, pass, "blynk.cloud", 80);
sensors.begin();
 /* gravityTds.setPin(TdsSensorPin);
   gravityTds.setAref(5.0);  //reference voltage on ADC, default 5.0V on Arduino UNO
   gravityTds.setAdcRange(1024);  //1024 for 10bit ADC;4096 for 12bit ADC
   gravityTds.begin();  //initialization*/
 dht.begin();
timer.setInterval(2500L, sendSensor);
setSyncInterval(10 * 60); // Sync interval in seconds (10 minutes)
  pinMode(5, OUTPUT); // for light
  pinMode (6, OUTPUT); //for water pump

}
void loop()
{

Blynk.run();
 timer.run();
} 

I guess it could be a driver conflict, but it’s difficult to say.

Have you tried commenting-out the other sensor code, and just leaving the TDS and water sensor?

Have you tried just the TDS or just the water sensors?

You are the person with access to the hardware, so it’s you that needs to take a fully structured and systematic approach to this, or to split the sensors across multiple devices.

Pete.

Yes I tried water sensor, tds separately and they are working fine with blynk2.0 but not with when other sensors are in picture.

and…

Pete.

This is the issue when I am adding tds and water sensor code that’s when communication with blynk app stop working. If I am using all the sensors without blynk app code then all of them are working

So you aren’t interested in taking a systematic and structured approach to testing with combinations of sensors, so that you can identify where the potential conflicts?

Pete.

1 Like

I am interested and have no issue following instruction. What I meant to say was that I already tried tds, and water temperature sensors alone and they are working fine.

Isn’t the reference voltage on the esp8266 3.3V?

That’s correct.

Okay, then your reading will be wrong if you get the TDS to work. :face_with_raised_eyebrow:

But I am reading the value of all my sensors through arduino uno.

Ah… Sorry, my bad!

Are you sure you don’t get any errors when compiling? And are your values correct in the serial mon?

To me, this looks like your sketch is just a copy/paste from another forum with a bare minimum of changes. Sadly, with the same mistakes!

Start from the beginning, one sensor at the time, write your own code, get it to work!

Yes I am not getting any errors and reading are fairly accurate.

You are right I copy paste code and made minor modifications as I am new to arduino programming as this is my first project.