Problem in reading/displaying sensor data

Hi,
i m working on project which aims to develop a soil sensor which will display soil moisture and ph value on android mobile app.
i am using Arduino UNO, ESP8266, and Blynk app to display data. This is my first project using blynk.
the problem is when i just read data from sensor and display its value on blynk app it works well. But when i apply if conditions on sensor readings, to make it more easier for a common person to understand what is meant by this particular reading of sensor, it does not work. It gets connect to wifi but does not read any sensor value and displays nothing on app.


#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
float moisture[10],Ph[10],light[10],sum_Moisture=0, sum_Ph=0, sum_light=0,moistureAvg=0,Avg_Ph=0,Avg_light=0 ;
   //Your Project authentication key
char auth[] = "*********";  
//char ssid[] = "*********;
//char pass[] = "*********";  // Corresponding Password
#include <SoftwareSerial.h>

SoftwareSerial EspSerial(2, 3); //RXPin, TXPin 
//SoftwareSerial ss(RXPin, TXPin);
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);

void setup()
{
  pinMode(A0,INPUT);
  pinMode(A2,INPUT);
  Serial.begin(9600);
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  Blynk.begin(auth, wifi, ssid, pass); 

}

void loop()
{
sensor();
Blynk.run();   
}
 
void sensor()
{
   sum_Moisture=0, sum_Ph=0, sum_light=0,moistureAvg=0,Avg_Ph=0,Avg_light=0 ;
    for (int i=0; i<10; i++){
    moisture[i] = analogRead(A0);
    moisture[i]=moisture[i]*5/1023; 
    Ph[i]=analogRead(A2);
    Ph[i]=Ph[i]*5/1023;
  sum_Moisture=sum_Moisture+moisture[i];
  sum_Ph=sum_Ph+Ph[i];
  }
   moistureAvg=sum_Moisture/10;
  Avg_Ph=sum_Ph/10; 
//  Blynk.virtualWrite(V5,moistureAvg);
  //Blynk.virtualWrite(V6,Avg_Ph);

// if conditions
    if(Avg_Ph>=0.38){
     Serial.println("Ph < 3, Not Suitable");
    Blynk.virtualWrite(V5,"Ph < 3, Not Suitable");
    }
    else if(Avg_Ph<=0.37&Avg_Ph>=0.36){
     Serial.println("Ph:3-4, Not Suitable");
     Blynk.virtualWrite(V5,"Ph:3-4, Not Suitable");
    }
     else if(Avg_Ph<=0.35&Avg_Ph>=0.34){
     Serial.println("Ph:4-5, Not Suitable");
     Blynk.virtualWrite(V5,"Ph:4-5, Not Suitable");
    }
    else if(Avg_Ph<=0.33&Avg_Ph>=0.32){
     Serial.println("Ph:5-6, Not Suitable");
     Blynk.virtualWrite(V5,"Ph:5-6, Not Suitable");
    }
    else if(Avg_Ph<=0.31&Avg_Ph>=0.20){
     Serial.println("Ph:6-7, Suitable");
     Blynk.virtualWrite(V5,"Ph:6-7, Suitable");
    }
     else{
     Serial.println("Ph:7-8, Suitable");
     Blynk.virtualWrite(V5,"Ph:7-8, Suitable");
     }

     if(moistureAvg<=0.65){
     Serial.println("soil is too much DRY");
         Blynk.virtualWrite(V6,"soil is too much DRY");
      }
    else if(moistureAvg>=0.66 & moistureAvg<=0.68){
     Serial.println("soil is much DRY");
         Blynk.virtualWrite(V6, "soil is much DRY");
    }

    else if(moistureAvg>=0.69 & moistureAvg<=0.73){
     Serial.println("soil is DRY");
         Blynk.virtualWrite(V6,"soil is DRY");
    }
    else if(moistureAvg>=0.74 & moistureAvg<=0.76){
     Serial.println("soil is least moist");
         Blynk.virtualWrite(V6,"soil is least moist");
    }
  else if(moistureAvg>=0.77 & moistureAvg<=0.79){
     Serial.println("soil is Partially moist");
        Blynk.virtualWrite(V6,"soil is Partially moist");
      }
     else if(moistureAvg>=0.80 & moistureAvg<=0.82){
     Serial.println("soil is moist");
     Blynk.virtualWrite(V6,"soil is moist");
    }
     else if(moistureAvg>=0.83 & moistureAvg<=0.85){
     Serial.println("soil is least wet");
     Blynk.virtualWrite(V6,"soil is least wet");
    }
     else if(moistureAvg>=0.86 & moistureAvg<=0.88){
     Serial.println("soil is partially wet");
     Blynk.virtualWrite(V6,"soil is partially wet");
    }
    else {
     Serial.println("soil is fully wet");
     Blynk.virtualWrite(V6,"soil is fully wet");
    }
    }




   

You need to start by moving the call to the ```sensor`` function out of your void loop and call it with a timer instead.
Read this:

Pete.

i have also tried it, calling sensor function using timer, but problem did not resolve.

Post your code with the timer.

Pete.

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
float moisture[10],Ph[10],light[10],sum_Moisture=0, sum_Ph=0, sum_light=0,moistureAvg=0,Avg_Ph=0,Avg_light=0 ;
   //Your Project authentication key
char auth[] = "*********";  
//char ssid[] = "*********;
//char pass[] = "*********";  // Corresponding Password
#include <SoftwareSerial.h>

SoftwareSerial EspSerial(2, 3); //RXPin, TXPin 
//SoftwareSerial ss(RXPin, TXPin);
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);



BlynkTimer sensorTimer;

void setup()
{
  pinMode(A0,INPUT);
  pinMode(A2,INPUT);
  Serial.begin(9600);
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  Blynk.begin(auth, wifi, ssid, pass); 
 sensorTimer.setInterval(100L, sensor); 
}

 void sensor()
{
   sum_Moisture=0, sum_Ph=0, sum_light=0,moistureAvg=0,Avg_Ph=0,Avg_light=0 ;
    for (int i=0; i<10; i++){
    moisture[i] = analogRead(A0);
    moisture[i]=moisture[i]*5/1023; 
    Ph[i]=analogRead(A2);
    Ph[i]=Ph[i]*5/1023;
  sum_Moisture=sum_Moisture+moisture[i];
  sum_Ph=sum_Ph+Ph[i];
  }
   moistureAvg=sum_Moisture/10;
  Avg_Ph=sum_Ph/10; 
//  Blynk.virtualWrite(V5,moistureAvg);
  //Blynk.virtualWrite(V6,Avg_Ph);

// if conditions
    if(Avg_Ph>=0.38){
     Serial.println("Ph < 3, Not Suitable");
    Blynk.virtualWrite(V5,"Ph < 3, Not Suitable");
    }
    else if(Avg_Ph<=0.37&Avg_Ph>=0.36){
     Serial.println("Ph:3-4, Not Suitable");
     Blynk.virtualWrite(V5,"Ph:3-4, Not Suitable");
    }
     else if(Avg_Ph<=0.35&Avg_Ph>=0.34){
     Serial.println("Ph:4-5, Not Suitable");
     Blynk.virtualWrite(V5,"Ph:4-5, Not Suitable");
    }
    else if(Avg_Ph<=0.33&Avg_Ph>=0.32){
     Serial.println("Ph:5-6, Not Suitable");
     Blynk.virtualWrite(V5,"Ph:5-6, Not Suitable");
    }
    else if(Avg_Ph<=0.31&Avg_Ph>=0.20){
     Serial.println("Ph:6-7, Suitable");
     Blynk.virtualWrite(V5,"Ph:6-7, Suitable");
    }
     else{
     Serial.println("Ph:7-8, Suitable");
     Blynk.virtualWrite(V5,"Ph:7-8, Suitable");
     }

     if(moistureAvg<=0.65){
     Serial.println("soil is too much DRY");
         Blynk.virtualWrite(V6,"soil is too much DRY");
      }
    else if(moistureAvg>=0.66 & moistureAvg<=0.68){
     Serial.println("soil is much DRY");
         Blynk.virtualWrite(V6, "soil is much DRY");
    }

    else if(moistureAvg>=0.69 & moistureAvg<=0.73){
     Serial.println("soil is DRY");
         Blynk.virtualWrite(V6,"soil is DRY");
    }
    else if(moistureAvg>=0.74 & moistureAvg<=0.76){
     Serial.println("soil is least moist");
         Blynk.virtualWrite(V6,"soil is least moist");
    }
  else if(moistureAvg>=0.77 & moistureAvg<=0.79){
     Serial.println("soil is Partially moist");
        Blynk.virtualWrite(V6,"soil is Partially moist");
      }
     else if(moistureAvg>=0.80 & moistureAvg<=0.82){
     Serial.println("soil is moist");
     Blynk.virtualWrite(V6,"soil is moist");
    }
     else if(moistureAvg>=0.83 & moistureAvg<=0.85){
     Serial.println("soil is least wet");
     Blynk.virtualWrite(V6,"soil is least wet");
    }
     else if(moistureAvg>=0.86 & moistureAvg<=0.88){
     Serial.println("soil is partially wet");
     Blynk.virtualWrite(V6,"soil is partially wet");
    }
    else {
     Serial.println("soil is fully wet");
     Blynk.virtualWrite(V6,"soil is fully wet");
    }
    }



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

Try this, you can’t just call a function in void loop and expect it to run. This needs to be done with Blynk Timer. I have set the interval to 100 ms, which is the maximum data rate that Blynk cloud will accept ( 10 data set per second )

i tried your code but the problem is still there means if i comment all of the if conditions it displays value but when i include if conditions to display pH and moisture state it does not display anything on mobile.

i applied timer the same way as Mr Suntop applied in his code.

Can you share the QR code of your Blnyk app so I can check all the settings for the widgets ?
There is an option “Clone Project” in Blynk app which will generate the QR code. Don’t worry, I won’t get access to your hardware, it will only share the App Layout !

where do i share it.

Here of course ! Where else ? It’s just an image, take a screenshot and post it here.
I think your project’s problem is with datatype selection of virtual pins (String Integer)

1 Like

Also change timer interval to 1000 or 2000 ms. 100 ms will flood your terminal with lots of text.

do i need to make some changing in code to add Terminal Widget?

Yes ! Replace all
Blynk.virtualWrite(V5,“Ph < 3, Not Suitable”) ;
lines with
terminal.println(“Ph < 3, Not Suitable”);
Here is an example sketch : https://github.com/blynkkk/blynk-library/blob/master/examples/Widgets/Terminal/Terminal.ino

Well, actually you have to make BLYNK_WRITE(V5) {} function inside your sensor function and add WidgetTerminal terminal(V5); before void setup. Try yourself now, I am going to drink a tea now. If you still can’t figure it out, I will help you then !

1 Like

thank you Mr Suntop for helping.I will let you know whether it worked or not.

That statement is incorrect.
Value widgets and labelled value widgets can display text as well as integer/float values.

This is not the issue with this code.

Pete.

1 Like

Then what can be the possible issue? plz help me!

Ok, but I pushed text only to terminal widget and it worked, so …
I am removing that comment !

@Mustafeez change the timer value to 10000 in @suntop’s code then compile and upload it, then copy and paste your serial output.

Pete.

@Mustafeez, did PeteKnight’s suggestion work ?