BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Need help on GPS STREAM

Hello,
I need help with my project using GPS stream.
My project is to set a relay contact during a second when i arrive near my home location to automaticaly open my gate .
i use ESP8266 with wifi connection my phone is a oneplus 6 .

It doesn’t work , the location send by my phone GPS seem no to be update .
this is my sketch code .


/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************

  App project setup:
    GPS Stream widget on V1.
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "****************";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "************";
char pass[] = "*************";

// definition variables
float homeLat = xxxxxxxxxxx;
float homeLon = xxxxxxxxxxxxxxxx;
float thresholdDistance =100; // meter



BLYNK_WRITE(V1) 
{
  
  GpsParam gps(param);
  float distance=0;
  float distance2=0;
  long x=0;
  long y=0;
  digitalWrite(0, HIGH);  
  
  
surveille:  


   x=0; //init x
do
    {  
     
      distance = distanceCalc(homeLat, homeLon, gps.getLat(), gps.getLon()); 
      Blynk.virtualWrite(V6,x);
      Blynk.virtualWrite(V5,distance);
      x=x+1;
      delay(1500);
    } while(distance>thresholdDistance);
    
   digitalWrite(0, LOW);   // mets à 1a sortie  
   delay(1000);               // Attendre 1 seconde - Wait for a second
   digitalWrite(0,HIGH);    // Mets la sortie à 0
   y=0; //init y
  do
    {     
         
      distance2 = distanceCalc(homeLat, homeLon, gps.getLat(), gps.getLon()); 
      Blynk.virtualWrite(V8,distance2);
      Blynk.virtualWrite(V7,y );
      y=y+1;
      delay(1500);
      
    } while(distance2<thresholdDistance);
    
    goto surveille;
 
}


float distanceCalc(float lat1, float long1, float lat2, float long2)
{
  float distLat, distLong, distance;
  
  distLat = (lat1 - lat2) * 111194.9;
  distLong = 111194.9 * (long1 - long2) * cos(radians((lat1 + lat2) / 2));
  distance = sqrt(pow(distLat, 2) + pow(distLong, 2));
 
  return distance; // in meter
}

void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(0, OUTPUT);     // Initialise la broche 0 comme une sortie
}

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

I’ve taken a quick look at your code and the do…while…goto loop seems to be the problem. Your code is going around this loop and not getting any new values from the GPS stream widget.

Also, the delays in those loops, and the fact that your code isn’t being allowed to return to the void loop and process the Blynk.run command, mean that your device will most likely disconnect from the Blynk server after around 10 seconds.

Pete.

Thanks @PeteKnight,
I try this code , what do you think about it ?

BlynkTimer timer;

BLYNK_WRITE(V1) 
{
      GpsParam gps(param);
      distance = distanceCalc(homeLat, homeLon, gps.getLat(), gps.getLon()); 
    
    
 
}

void Affiche_Distance()

{
if(distance > thresholdDistance)

    {
      digitalWrite(0,HIGH);    // Mets la sortie à 0
      Flag=0;    
    }
    else
    {
        if( Flag==0)
        {    
           digitalWrite(0, LOW);   // mets à 1a sortie  
           delay(1000);               // Attendre 1 seconde - Wait for a second
           digitalWrite(0,HIGH);    // Mets la sortie à 0
          Flag=1; 
        }
    }
  Blynk.virtualWrite(V5,distance);
  Blynk.virtualWrite(V6,Flag);

}



float distanceCalc(float lat1, float long1, float lat2, float long2)
{
  float distLat, distLong, distance;
  
  distLat = (lat1 - lat2) * 111194.9;
  distLong = 111194.9 * (long1 - long2) * cos(radians((lat1 + lat2) / 2));
  distance = sqrt(pow(distLat, 2) + pow(distLong, 2));
 
  return distance; // in meter
}

void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(0, OUTPUT);     // Initialise la broche 0 comme une sortie
  timer.setInterval(1000L,Affiche_Distance);
}

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

It looks better, but does it work?

Pete.

Thank you @PeteKnight for your advise.
It’s works now !!!

Benj

1 Like