Try to solved it the problem esp8266 always connect and disconnect

halo now iam trying to solve the problem the connection of shiled-esp-wifi rev 2.0 with esp8266, it always connect and disconnect the reading of sensor also with apps blynk

10

i connect the esp8266 with arduino mega and already change the rx & tx for mega but when i upload the programming it always connect and disconnect pls help me

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <DHT.h>

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; //Auth Token in the Blynk App.
char ssid[] = "xxxxxxxx";// Your WiFi credentials
char pass[] = "xxxxxxxxxx";// Set password to "" for open networks.

#include <SoftwareSerial.h>//Software Serial on Uno
SoftwareSerial EspSerial(10, 11); // RX, TX 2,3

#define ESP8266_BAUD 9600 // Your ESP8266 baud rate:

ESP8266 wifi(&EspSerial);
//--------------------------   DHT11  -----------------------------------//

#define DHTPIN A2// The DHT 11 Analog A2 pin we're connected 
#define DHTTYPE DHT11     // DHT 11
DHT dht(DHTPIN, DHTTYPE);

//--------------------------   pH sENSOR  ------------------------------//

#define SensorPin A0// The pH Sensor Analog A2 pin we're connected
unsigned long int avgValue;  //Store the average value of the sensor feedback
float b;
int buf[10],temp;

int dosingPumpAc = 8;       // select the pin for the dosing pump high
int dosingPumpAl = 9;       // select the pin for the dosing pump low


//--------------------------   TDS sensor  ------------------------------//

#define TdsSensorPin A1// The TDS Meter Analog A2 pin we're connected
#define VREF 5.0 // analog reference voltage(Volt) of the ADC
#define SCOUNT 1 // sum of sample point
int analogBuffer[SCOUNT]; // store the analog value in the array, read from ADC
int analogBufferTemp[SCOUNT];
int analogBufferIndex = 0,copyIndex = 0;
float averageVoltage = 0,tdsValue = 0,temperature = 25;

int dosingPumpA = 6;// Tank A connected Digital pin 6
int dosingPumpB = 7;// Tank B connected Digital pin 6

//--------------------------   Water Level  ------------------------------//

int sensorPin = A3;      // input pin A1 for the water level sensor module
int sensorValue = 0;
int solenoidinPin = 4;   // select the pin for the solenoid valve in
int solenoidoutPin = 5;  // select the pin for the solenoid valve out
int motorOut = 10;       // select the pin for the Motor valve out
int speakerPin = 11;      // select the pin for the buzzer


BlynkTimer timer;

void sendSensor()
{

//--------------------------   DHT11  -----------------------------------//

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

  Serial.print("Humidity (%): ");
  Serial.println((float)h, 0);

  Serial.print("Temperature (C): ");
  Serial.println((float)t, 0);

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

  dht.begin();

//--------------------------   pH sENSOR  ------------------------------//

  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=3.5*phValue;  //convert the millivolt into pH value
  
  Serial.print("pH : ");
  Serial.println(phValue,2);
  
  Blynk.virtualWrite(V7, phValue);

  if(phValue<7)                             //Ph value below 5                       
  {
     digitalWrite(dosingPumpAc,LOW);        // turn on dosing pump for Alcali
     digitalWrite(dosingPumpAl,HIGH);       // turn on dosing pump for Acid
  }
  else if(phValue>7 && phValue <8)          //Ph value between 6 and 7
  {
     digitalWrite(dosingPumpAc,LOW);        // turn off dosing pump for Alcali
     digitalWrite(dosingPumpAl,LOW);        // turn on dosing pump for Acid
  }
  else if(phValue>8)                        //Ph value above 7  
  {
     digitalWrite(dosingPumpAc,HIGH);       // turn on dosing pump for Alcali
     digitalWrite(dosingPumpAl,LOW);        // turn off dosing pump for Acid
  }
  
//--------------------------   TDS sensor  ------------------------------//

  static unsigned long analogSampleTimepoint = millis();
 if(millis()-analogSampleTimepoint > 40U) //every 40 milliseconds,read the analog value from the ADC
 {
 analogSampleTimepoint = millis();
 analogBuffer[analogBufferIndex] = analogRead(TdsSensorPin); //read the analog value and store into the buffer
 analogBufferIndex++;
 if(analogBufferIndex == SCOUNT)
 analogBufferIndex = 0;
 }
 static unsigned long printTimepoint = millis();
 if(millis()-printTimepoint > 800U)
 {
 printTimepoint = millis();
 for(copyIndex=0;copyIndex<SCOUNT;copyIndex++)
 analogBufferTemp[copyIndex]= analogBuffer[copyIndex];
 averageVoltage = getMedianNum(analogBufferTemp,SCOUNT) * (float)VREF/ 1024.0; // read the analog value more stable by the median filtering algorithm, and convert to voltage value
 float compensationCoefficient=1.0+0.02*(temperature-25.0); //temperature compensation formula: fFinalResult(25^C) = fFinalResult(current )/(1.0+0.02*(fTP-25.0));
 float compensationVolatge=averageVoltage/compensationCoefficient; 
 tdsValue=(133.42*compensationVolatge*compensationVolatge*compensationVolatge - 255.86*compensationVolatge*compensationVolatge + 857.39*compensationVolatge)*0.5; //convert voltage value to tds value

 
  Serial.print("TDS :  ");
  Serial.print(tdsValue,2);
  Serial.print("ppm ");

  Serial.print("EC : ");
  Serial.print(averageVoltage,2);
  Serial.println("mS/cm");
  
  Blynk.virtualWrite(V8, tdsValue);
  Blynk.virtualWrite(V9, averageVoltage);

  if  (tdsValue <1960 && averageVoltage< 2.8) { // Range of ppm
    
  digitalWrite(dosingPumpA, HIGH); // turn the solenoid valve outlet off
  digitalWrite(dosingPumpB, HIGH); // turn the solenoid valve inlet on
  
 }

 else if  (tdsValue >1960 && tdsValue <2450 && averageVoltage >2.8 && averageVoltage <3.5) {

  digitalWrite(dosingPumpA, HIGH); // turn the solenoid valve outlet off
  digitalWrite(dosingPumpB, HIGH); // turn the solenoid valve inlet on
  
 }

 else if  (tdsValue >2450 && averageVoltage >3.5) {

  digitalWrite(dosingPumpA, LOW); // turn the solenoid valve outlet off
  digitalWrite(dosingPumpB, LOW); // turn the solenoid valve inlet on
 }
 
   } 
  }
  int getMedianNum(int bArray[], int iFilterLen)
  {
   int bTab[iFilterLen];
   for (byte i = 0; i<iFilterLen; i++)
   bTab[i] = bArray[i];
   int i, j, bTemp;
   for (j = 0; j < iFilterLen - 1; j++)
   {
   for (i = 0; i < iFilterLen - j - 1; i++)
   {
   if (bTab[i] > bTab[i + 1])
   {
   bTemp = bTab[i];
   bTab[i] = bTab[i + 1];
   bTab[i + 1] = bTemp;
   }
   }
   }
   if ((iFilterLen & 1) > 0)
   bTemp = bTab[(iFilterLen - 1) / 2];
   else
   bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
   
   return bTemp;


//--------------------------   Water Level  ------------------------------//

  sensorValue = analogRead(sensorPin); // read the value from the sensor
  // send the message about water level to serial monitor
  
    Blynk.virtualWrite(V10, sensorValue);

  if (sensorValue <= 0) {
    
     //Serial.println("Water level: 50mm - Low water level");
     digitalWrite(speakerPin, LOW);     // turn the speaker off
     digitalWrite(solenoidoutPin, LOW); // turn the solenoid valve outlet off
     digitalWrite(solenoidinPin, HIGH); // turn the solenoid valve inlet on
     digitalWrite(motorOut,LOW);
 
  }

  else if (sensorValue > 0 && sensorValue <= 223) { 
    
     digitalWrite(speakerPin, LOW);     // turn the speaker off
     digitalWrite(solenoidoutPin, LOW); // turn the solenoid valve outlet off
     digitalWrite(solenoidinPin, LOW); // turn the solenoid valve inlet on
     digitalWrite(motorOut,LOW);
  }
  else if (sensorValue > 323) {
    
    //Serial.println("Water lvl: 220mm - Warning ! Water high level");
    digitalWrite(speakerPin, HIGH);     // turn the speaker on
    digitalWrite(solenoidoutPin,HIGH);  // turn the solenoid valve outlet on
    digitalWrite(solenoidinPin, LOW);   // turn the solenoid valve inlet off
    digitalWrite(motorOut,HIGH);
  }
 
} 

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

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

  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8080);
  pinMode(DHTPIN, INPUT);
  
  pinMode(sensorPin, INPUT);       // declare the sensorPin as an INPUT (WATER LEVEL )
  pinMode(solenoidinPin,OUTPUT);   // declare the solenoiindPin as an OUTPUT (WATER LEVEL )
  pinMode(solenoidoutPin,OUTPUT);  // declare the solenoidoutPin as an OUTPUT (WATER LEVEL )
  pinMode(speakerPin, OUTPUT);     // declare the speakerPin as an OUTPUT (WATER LEVEL )
  pinMode(motorOut,OUTPUT);        // declare the motor pump as an OUTPUT (WATER LEVEL )

  pinMode(SensorPin, INPUT);
  pinMode(dosingPumpAc, OUTPUT);   // declare the dosing pump high as an OUTPUT (Ph)
  pinMode(dosingPumpAl, OUTPUT);   // declare the dosing pump low as an OUTPUT (Ph)

  pinMode(dosingPumpA, OUTPUT);   // declare the dosing pump high as an OUTPUT (TDS)
  pinMode(dosingPumpB, OUTPUT);   // declare the dosing pump low as an OUTPUT (TDS)

  

  timer.setInterval(1000L, sendSensor);
}

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

this is my coding and i already try many times but always happen connect and disconnect. its my coding wrong or my esp8266 module. i want all the sensor sent the value to apps blynk.

You’re doing a lot in your timed function, and calling it quite often (the DHT sensors are quite slow).

Try calling it less frequently, and if you still have problems then add some Blynk.run() statements into your function, so that the Blynk library isn’t starved of attention while the sensors are being read.

Pete.

2 Likes

thank you sir for reply my problem… its correct ? that i just put the Blynk.run() just like this

52
i just put Blynk.run() at the bottom its correct ?
or can u help me with example ?

Adding it at the end of the function isn’t going to help.

Pete.

so where did i need to put it ? i try to solved this over 2 weeks still did not get solution

1st, Don’t use Software Serial with a Mega… it has multiple Hardware Serial ports that are more reliable.

2nd, As already mentioned, Don’t run your timer function so fast… DHT sensors should have about 2-5 seconds to do their thing… Outside of testing, in which 15-30 seconds is better, one only needs to read temperature every 5-10 minutes

As I explained before, the Blynk library needs regular processor time to allow it to communicate with the server in the background. While your sendSensor code is running, it’s starving the Blynk library of processor time, so you need to ‘feed’ Blynk during the execution of the code. This is done with Blynk.run commands.

Putting a single Blynk.run command immediately before the sendSensor code is about to finish doesn’t help.
I would add Blynk.run commands after you’ve taken the DHT11 readings, after you’ve taken the pH sensor readings and after you’ve taken the TDS sensor readings.

However, there are some things about the code used to take some of these readings that seems odd to me when I take a quick look at it.

For example, i don’t understand why you’re calling dht.begin(); after you’ve taken your DHT readings.
Also, you appear to have a function called getMedianNum within your TDS code, which doesn’t appear to be structured correctly.

Pete.

2 Likes