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();
}
'''