{GPS MODULE + ARDUINO + Esp8266} Push sensor code running only for 10 seconds. Need help with Blynk timer

I am using Esp8266 wifi module and Arduino. The setup is giving out never ending NMEA output without connecting to Blynk.
But while working with Blynk app i am getting only a single output and its not working after that . The location is accurate as well as there is no problem with the GPS code .

OUTPUT-------------------------------------------------------------------------------------------->

image

CODE---------------------------------------------------------------------------------------------->

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

static const int RXPin = 8, TXPin = 9;  
static const uint32_t GPSBaud = 9600; 

TinyGPSPlus gps;
WidgetMap myMap(V0);  

SoftwareSerial ss(RXPin, TXPin); 


float spd;      
float sats;     
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";             
char ssid[] = "abhi";                                     
char pass[] = "12345678abhinav";                                    


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


#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

        
unsigned int move_index = 1;       
  

void setup()
{
  Serial.begin(9600);
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  Blynk.begin(auth, wifi, ssid, pass);
  ss.begin(GPSBaud);
  
}

void displayInfo()
{ 

  if (gps.location.isValid() ) 
  {
    
    float latitude = (gps.location.lat());     
    float longitude = (gps.location.lng()); 
    
    Serial.print("LAT:  ");
    Serial.println(latitude, 6);  
    Serial.print("LONG: ");
    Serial.println(longitude, 6);
    Blynk.virtualWrite(V0, String(latitude, 6));   
    Blynk.virtualWrite(V1, String(longitude, 6));  
    myMap.location(move_index, latitude, longitude, "GPS_Location");
    spd = gps.speed.kmph();         
       Blynk.virtualWrite(V3, spd);
       
       sats = gps.satellites.value();    
       Blynk.virtualWrite(V4, sats);

       bearing = TinyGPSPlus::cardinal(gps.course.value()); 
       Blynk.virtualWrite(V5, bearing);               
      
    
  }
  

  Serial.println();
}


void loop()
{
   Blynk.run();
    while (ss.available() > 0) 
    {
      if (gps.encode(ss.read()))
        displayInfo();
  }

}

Hello and welcome. Please read the Welcome Topic, and properly format your code for viewing. Thank you.

Done the necessary changes. Can i please get the help now?

I guess there is some problem in ‘void setup()’ as it is unable to go hand in hand with the Blynk app.

I would suspect that since you are running your GPS reading function directly from the main loop (which runs hundreds of times a second) then you are simply getting flooding errors and disconnections… Blynk needs to be able to constantly function in the background… maintaining a link to the Server and reading/writing data to the Server as required.

Look into running this from a timer instead

while (ss.available() > 0) 
    {
      if (gps.encode(ss.read()))
        displayInfo();
  }

Actually, try to avoid the while() function all together, as it will lock you into that ‘loop’ while the condition is met… and thus… yep… disconnections :wink:

http://docs.blynk.cc/#blynk-firmware-blynktimer

Ok so as to test, i modified it to:

void loop()
{
  Blynk.run();
  while (ss.available() > 0)
    if (gps.encode(ss.read()))
    {
      Serial.print(F("Latitude: "));
    Serial.print(gps.location.lat(), 6);
    Serial.print(F("  Longitude: "));
    Serial.println(gps.location.lng(), 6);
    }
}

As a result, i am getting output only for 10 seconds every time. Can you please suggest any alternative other than while which i can use as its getting disconnected exactly after 10 seconds. Btw thank you for ur help .

That is because there is a 10 second heartbeat check with Blynk… and since you are probably stuck in your while() loop, Blynk is not able to run in the background.

Check out the BlynkTimer link and read up on the reasons it is needed as already posted above.

Thank you for the link you provided but still i am having the same problem. I have reduced the earlier code. Can you please please add blynk timer to it. Btw this is giving out the exact location i need but only for 10 seconds on serial monitor.

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


static const int RXPin = 8, TXPin = 9; 
static const uint32_t GPSBaud = 9600;

TinyGPSPlus gps; 
SoftwareSerial ss(RXPin, TXPin);  
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";             
char ssid[] = "abhi";                                     
char pass[] = "12345678abhinav";     
                                

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

#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);



unsigned int move_index = 1;       


void setup()
{
  Serial.begin(9600); 
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  Blynk.begin(auth, wifi, ssid, pass);
  ss.begin(GPSBaud);
  
}


void loop()
{
  Blynk.run();
  float latitude = (gps.location.lat());     
  while (ss.available() > 0)
    if (gps.encode(ss.read()))
    {

    
    Serial.print(F("Latitude: "));
    Serial.print(latitude, 6);
    Serial.print(F("  Longitude: "));
    Serial.println(longitude, 6);
    delay(1000);
    
  
    

    
    }


}

The link you have already been given leads to 2 further links. A Blynk example and the Arduino docs. You need to spend your time working through them.

If after a few hours effort on your side you are still struggling then post precise details of what you have tried.

I did try. I will try it again and send you the code. I know its hard for u guys to help everyone with code.

@Abhinav_Thakur If we give you the code you will learn nothing.

I tried a lot with Blynk timer but it is unable to read from the module. But, still this code is able to run for 10 seconds without Blynk timer. Can someone plz help.

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


static const int RXPin = 8, TXPin = 9;   
static const uint32_t GPSBaud = 9600; 

TinyGPSPlus gps; 

SoftwareSerial ss(RXPin, TXPin);  

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";             
char ssid[] = "abhi";                                      
char pass[] = "12345678abhinav";                                      


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


#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

BlynkTimer timer; 

  
unsigned int move_index = 1;         





void setup()
{
  Serial.begin(9600); 
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  Blynk.begin(auth, wifi, ssid, pass);
  ss.begin(GPSBaud);
  timer.setInterval(1000L, myTimerEvent);
}


void displaypd()
{
    if (gps.location.isValid())
  {
    Serial.print(F("Latitude: "));
    Serial.print(gps.location.lat(), 6);
    Serial.print(F("  Longitude: "));
    Serial.println(gps.location.lng(), 6);
  }  
    
}


void myTimerEvent()
{
   while(ss.available() > 0)
    {
      if (gps.encode(ss.read()))
        displaypd();
      
       }
}




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

As I have already stated… there is a Blynk heartbeat of about 10 seconds… in which your code can sit there and work, not work, twiddle it’s itty bity bytes or flat out do notheng… but if if after those 10 seconds, Blynk can not get on with it’s required housekeeping, then your wholes sketch stops.

So please stop using the blocking while() loop and focus on learning how to use timers instead of while() to get the job done :wink:

Dude as you told me to change the while loop i tried to modify by using if loop. On using it without Blynk it gives the right response but when i use it with Blynk, it gives nothing.
I am not even getting output for 10 seconds as i was getting using while statement using Blynk. Can you please tell where i am wrong.

void loop()
{
  Blynk.run();
 if (ss.available() )
    {
      if (gps.encode(ss.read()))
      {  if (gps.location.isValid())
  {
    Serial.print(F("Latitude: "));
    Serial.print(gps.location.lat(), 6);
    Serial.print(F("  Longitude: "));
    Serial.println(gps.location.lng(), 6);
  }  
    } }
  
}

I also suggested using timers… so that you are not flooding or overloading your void loop() with scanning every few hundredths of a second. :wink:

Put everything you currently have, but the Blynk.run(); in a timed function and start testing how best to make it work with Blynk that way.

Aside from these suggestions… no, not really… you just need to slowly learn how Blynk works, that is all.

No magic bullet answers are otherwise available.

I studied about interrupts and timers and about blynk timer. I never used Simple timer before so i am new to it. Can you please tell if my timer code is technically correct. Secondly, can you please tell me where i should use which time interval. I am not clear about that .

And what does it really mean in GPS problem. Does this has anything to do with my problem:

Plz dude help. I am a noob :yum:

I don’t have any of those GPS devices to test, but probably something like this in a timed loop…

In your case, set for every 1000ms (1 second) or whatever works best for you.

timer.setInterval(1000L, myTimerEvent);  // This goes in the void setup() and runs the function every 1 second
void myTimerEvent()
{
  if (gps.encode(ss.read()))  // I don't know why you need an if condition here?
  {
    if (gps.location.isValid())
    {
      Serial.print(F("Latitude: "));
      Serial.print(gps.location.lat(), 6);
      Serial.print(F("  Longitude: "));
      Serial.println(gps.location.lng(), 6);
    }
  }
}

It will not work without

As it is used to repeatedly feed characters from your GPS device.

http://arduiniana.org/libraries/tinygpsplus/

I am unable to find any gps codes linked to Blynk. Bro, i have very less time left to finish my project so plz i need to solve it quick. Btw, thanks for all ur help.

Blynk is a IoT GUI interface… so it has uses in multiple fields, not just GPS, of which there are many, many differing ways to process that data… but the particular method you seem to need might be a little too demanding in it’s own timing needs.

I suppose you could try to reintroduce the while() but add in another Blynk.run(); command after every data dump (e.g. coordinate printout and Blynk.virtualWrite() - if used) as that will allow Blynk to keep up with it’s own timing needs for housekeeping and communication.

Then tweak your timer until you get a smooth data flow without causing flooding from too much data flow to the App or disconnections from not enough Blynk housekeeping.

As I said, I have nothing to test or compare with (and my own projects to work on), so while I can throw out a few ideas, you need to keep reading and trying things on your own as well… a lot of the flooding and disconnection reasons and solutions have already been documented and discussed in this forum.

@Costas can u plz help me out here. I read an answer written by u on gps.