Real Time GPS with Neo 6M and Sim800L and Arduino mega2560

Hello
I searched the Internet and community blynk and did not find a comprehensive solution to how to keep the void loop clean and how to send gps data to the server. I am sending my code sample that does not work. Dear dear admins, I would like to declare a map widget a comprehensive solution. Thankful.

//#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
#define TINY_GSM_MODEM_SIM800
#define BLYNK_HEARTBEAT 5
#include <TinyGsmClient.h>
#include <BlynkSimpleTinyGSM.h>
#include <TinyGPS++.h>
#include <SimpleTimer.h>
#define GSMSerial Serial2
#define GPSSerial Serial3

char auth[] = "******************"; // Your Project authentication key
char apn[] = "*************";                       // Your Sim Card Network APN
char user[] = "";
char pass[] = "";
const int SerialBaud = 9600;    // Serial Baud Rate
const int GSMBaud = 9600;       // GSM Buad Rate
const int GPSBaud = 9600;       // GPS Buad Rate
const int ArduinoResetPin = 54; // Send Reset to Arduino Mega
const int SimResetPin = 28;     // Send Resert To Sim800L
const int Lock = 8;             // Car remote lock pin
const int Unlock = 9;           // Car remote unlock pin
const int Trunk = 10;           // Car remote trunk pin
unsigned int move_index = 1;    // fixed location for now
unsigned int last_index = 0;
unsigned int spd;                      // Variable  to store the speed
unsigned int sats;                     // Variable to store no. of satellites response
unsigned long myServerTimeout = 60000; // One min server connection timeout (SCT)
float GPSChars = 0;
String bearing; //Variable to store orientation or direction of GPS

TinyGPSPlus gps; // The TinyGPS++ object
TinyGsm modem(GSMSerial);
WidgetMap myMap(V0); // V0 for virtual pin of Map Widget

SimpleTimer timer;

void setup() {

  pinMode(ArduinoResetPin, OUTPUT);
  pinMode(SimResetPin, OUTPUT);
  pinMode(Lock, OUTPUT);
  pinMode(Unlock, OUTPUT);
  pinMode(Trunk, OUTPUT);
  ResetVoid();
  Serial.begin(SerialBaud);
  delay(100);
  Serial.println("‌Real Time GPS Tracker");
  delay(100);
  Serial.println("‌By Amir Khazaee");
  delay(100);
  Serial.println("‌t.me/AmirKhazaee");
  delay(100);
  GPSSerial.begin(GPSBaud);
  delay(100);
  GSMSerial.begin(GSMBaud);
  delay(100);
  Serial.println("Initializing modem...");
  modem.restart();

  Blynk.begin(auth, modem, apn, user, pass, "********************************", 8080);
  Blynk.virtualWrite(V10, 1);

  timer.setInterval(10000L,  checkGPS);   // every 5s check if GPS is connected, only really needs to be done once
  timer.setInterval(5000L,   GPSData);    // check GPS data
  timer.setInterval(30000L,  checkBlynk); // check connection to blynk server
  
  }

BLYNK_WRITE(7) {
  int i = param.asInt();
  if (i == 1) {
    digitalWrite(Lock, HIGH);
    Blynk.virtualWrite(V6, "Locked");
    delay(1000);
  } else {
    digitalWrite(Lock, LOW);
  }
}

BLYNK_WRITE(8) {
  int i = param.asInt();
  if (i == 1) {
    digitalWrite(Unlock, HIGH);
    Blynk.virtualWrite(V6, "Unlocked");
    delay(1000);
  } else {
    digitalWrite(Unlock, LOW);
  }
}

BLYNK_WRITE(9) {
  int i = param.asInt();
  if (i == 1) {
    digitalWrite(Trunk, HIGH);
    Blynk.virtualWrite(V6, "Trunk is open");
    delay(3000);
  } else {
    digitalWrite(Trunk, LOW);
  }
}

BLYNK_WRITE(10) {
  move_index = param.asInt();
  if (move_index < 1) {
    move_index = 1;
  }
}

void ResetVoid() {
  
  delay(500);
  digitalWrite(SimResetPin, HIGH);
  delay(500);
  digitalWrite(SimResetPin, LOW);
  delay(500);

}

void checkGPS() {

  if (gps.charsProcessed() < 10) {
    Serial.println(F("No GPS detected: check wiring."));
    Blynk.virtualWrite(V4, "No GPS detected"); // Value Display widget on V4 if GPS not detected

  }
}

void checkBlynk() {

  Blynk.virtualWrite(V11, millis() / 1000);
  
  unsigned long startConnecting = millis();
  while (!Blynk.connected()) {
    Blynk.connect();
    if (millis() > startConnecting + myServerTimeout) {
      digitalWrite(ArduinoResetPin, HIGH);
      delay(100);
      break;
    }
  }
}

void GPSData() {

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

void displayInfo() {

  if (gps.location.isValid()) {

    float latitude = (gps.location.lat()); //Storing the Lat. and Lon. 
    float longitude = (gps.location.lng());

    Blynk.virtualWrite(V1, String(latitude, 6));
    Blynk.virtualWrite(V2, String(longitude, 6));
    myMap.location(move_index, latitude, longitude, "Peugeot");
    spd = gps.speed.kmph(); //get speed
    Blynk.virtualWrite(V3, spd);

    sats = gps.satellites.value(); //get number of satellites
    Blynk.virtualWrite(V4, sats);

    bearing = TinyGPSPlus::cardinal(gps.course.value()); // get the direction
    Blynk.virtualWrite(V5, bearing);

  } else {
    Blynk.virtualWrite(V4, "No Signal");
  }
}

void loop() {

  Blynk.run();
  timer.run();

}`

Could you provide the text displayed in the Serial Monitor when this code is run, and if possible describe what part (unless nothing works) isn’t functioning as you would expect :slight_smile:

void gpsdata is called with the timer, but in any case void displayinfo is not executed

This would help :slight_smile:

Nothing appears inside the serial monitor after it is connected to the blynk server.

So you get a successful connection to the Blynk server?

yes. but gps data not coming

I’ve never used any GPS functionality, but I presume that you’ve tested your hardware - validating that it works before you try and combine it with Blynk. Other than that, I don’t think I will be much help in resolving your issue.

I wish you the best of luck, and hope that someone more qualified will be able to assist you :wink:

thank you martin. If I enter the void GPSData code in the void loop, everything works fine, but everywhere I read the void loop to keep it clean.

Yeah, with IoT it’s often really important as a cluttered void loop() can suffocate the WiFi tasks that need to be performed in order to maintain a reliable connection to the Blynk servers.

Wait… I’m not sure… if this is the case, but it’s a practice I use - try it and see if it makes a difference.
Replace your SimpleTimer timer; with the Blynk equivalent:

BlynkTimer timer1;
BlynkTimer timer2;
BlynkTimer timer3;

Then replace:

timer.setInterval(10000L,  checkGPS);   // every 5s check if GPS is connected
timer.setInterval(5000L,   GPSData);    // check GPS data
timer.setInterval(30000L,  checkBlynk); // check connection to blynk server

so that each one uses it’s own timer. I’m pretty sure each timer can only have one function attached?

timer1.setInterval(10000L,  checkGPS);   // every 5s check if GPS is connected
timer2.setInterval(5000L,   GPSData);    // check GPS data
timer3.setInterval(30000L,  checkBlynk); // check connection to blynk server

Again, this is just a hunch, but it doesn’t hurt to try right? @khazaee

#define BLYNK_HEARTBEAT 5
do u using a pulse sensor in ur project?

This has nothing to do with a human pulse, as you may initially expect. It’s to do with how frequently the hardware interfaces with the server. There’s another post that has more information about this <3

In this; setting it to 5 means that it will refresh every 5 seconds, instead of 10 seconds (the defualt).

Hope this was helpful and cleared some things up :slight_smile:

1 Like

In your GPSdata function. Try changing the while statement to if. Because if there is no data yet it will hang there until there is which will kill the connection to the server.

I also tried this method but still blynk connects but no GPS information is sent. I need to say when I use GPSData in the void loop, everything works fine

I do blynk any way you think I do not send gps information I do not think the problem is of time

hi @Gunner You’ve announced everywhere that the void loop is clean, but you did not give any guidance in the gps project with the neo6m module. Please give a little more guidance

Gunner is taking a break from the forum for a while for personal reasons, so you probably won’t be getting an answer from him.
He also probably doesn’t have this hardware anyways so wouldn’t be angle to give you a specific answer.

If you want help from the forum then id’d start giving detailed feedback to the suggestions that are made, otherwise people tend to stop making them.

Pete.

how about you ? Can you answer questions?

I can answer questions.
I have no experience with the hardware that you’re using, but I am an experienced C++ programmer and understand Blynk.

However, I won’t attempt to give advice on issues where you won’t give extensive feedback on suggestions that have already been made and you won’t copy/paste the output from your serial monitor when you are asked to.

Pete.

3 Likes

It’s hard to believe that nobody can be found by using blynk GPS. There are, of course, examples inside the YouTube that insert the code into the void loop. However, if you want to use your c ++ knowledge, then the problem is that when you clean the void loop and transfer code to another void and use the timer, blynk will be properly connected to it, but it is not news of the position of the gps.

Code in loop :

void loop() {

  while (GPSSerial.available() > 0){
    if (gps.encode(GPSSerial.read())){
      displayInfo();
    }
  }
       
  Blynk.run();
  timer.run();

}

Code out of loop but not working :

timer.setInterval(5000L, GPSData); // check GPS data

void GPSData() {
   
  while (GPSSerial.available() > 0) {
    if (gps.encode(GPSSerial.read())) {
      displayInfo();
    }
  }
}