Okay, I’ve made some changes to the structure of this, to make it more likely to work with Blynk.
The code isn’t tested, as I don’t have your hardware or the libraries installed that you are using, so you may get some silly compilation errors to sort-out.
I’ve added some comments to help you understand and learn. Please read them. If you don’t understand any of them then please ask.
This may not work with Blynk yet, it depends on how you’ve configured your datastreams. I’d suggest you try it and provide some feedback and details of your V1 datastream configuration and map widget setup if it doesn’t work as expected…
#define BLYNK_TEMPLATE_ID "*****"
#define BLYNK_DEVICE_NAME "*****"
#define BLYNK_FIRMWARE_VERSION "0.1.0"
#define BLYNK_PRINT Serial
#define APP_DEBUG
#include "BlynkEdgent.h"
#include <TinyGPS++.h>
#include <TinyGPSPlus.h>
#include <SoftwareSerial.h>
#include <NMEAGPS.h>
#include <Adafruit_SSD1306.h>
#include <splash.h>
#include <Wire.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define RXD2 16
#define TXD2 17
HardwareSerial neogps(1);
TinyGPSPlus gps;
SoftwareSerial ss(RXD2, TXD2);
//WidgetMap myMap(V1);
BlynkTimer timer; // Declare a BlynkTimer object called 'timer'
// These variables move out of void setup, so that they become global variables which are visible everywhere in the sketch
int index = 0;
float lat = 51.5074;
float lon = 0.1278;
// and some new global variables created to store some other values...
float speed;
float satellites;
float altitude;
void setup()
{
Serial.begin(9600);
delay(100);
BlynkEdgent.begin();
neogps.begin(9600, SERIAL_8N1, RXD2, TXD2);
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.clearDisplay();
display.display();
delay(2000);
// You shouldn't decalre variables in void setup, it makes them local variables, so they cant bve seen in the rest of the sketch
// These have been moved up to the top of the sketch to make them global
// int index = 0;
// float lat = 51.5074;
// float lon = 0.1278;
//Blynk.virtualWrite(V1, lon, lat); // we don't want this here, as it would only run once and Blynk may not be listening yet anyway
timer.setInterval(1000L, readGPS; // initialise a timer that runs every second and reads the GPS data
}
void loop()
{
BlynkEdgent.run();
timer run(); // feed the BlynkTimer object
}
void readGPS()
{
// This code was in your void loop, it's now been moved out and placed into a new function which we've called 'readGPS'
// the function will be triggered once every second by the timer
boolean newData = false;
for (unsigned long start = millis(); millis() - start < 1000;)
{
while (neogps.available())
{
if (gps.encode(neogps.read()))
{
newData = true;
}
}
}
if(newData == true)
{
newData = false;
Serial.println(gps.satellites.value());
print_speed();
}
else
{
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.setTextSize(3);
display.print("No Data");
display.display();
}
}
void print_speed() // Not a very accurate name for this function!
{
// Code changed to capture the lat, lon, speed etc to the global variables we created, so that
// they can be re-used multiple times...
lat = gps.location.lat(); // capture latitude to the `lat` global variable
lon = gps.location.lng(); // capture longitude to the `lon` global variable
speed = gps.speed.kmph(); // you get the general idea of what's happening here...
satellites = gps.satellites.value();
altitude = gps.altitude.meters();
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
if (gps.location.isValid() == 1)
{
//String gps_speed = String(gps.speed.kmph());
display.setTextSize(1);
display.setCursor(25, 5);
display.print("Lat: ");
display.setCursor(50, 5);
display.print(lat,6); // uses the 'lat` global variable value
display.setCursor(25, 20);
display.print("Lng: ");
display.setCursor(50, 20);
display.print(lon,6); // uses the 'lon` global variable value
display.setCursor(25, 35);
display.print("Speed: ");
display.setCursor(65, 35);
display.print(speed); // you get the general idea...
display.setTextSize(1);
display.setCursor(0, 50);
display.print("SAT:");
display.setCursor(25, 50);
display.print(satellites);
display.setTextSize(1);
display.setCursor(70, 50);
display.print("ALT:");
display.setCursor(95, 50);
display.print(altitude, 0);
display.display();
// new code added to the existing function to write the data to Blynk...
Blynk.virtualWrite(V1, lon, lat); // re-uses the values we stored in the global variables earlier
// you couldf all extra lines in here to send the spped, satellites & altitude to display widgets in
// Blynk that are connected to their own vitrual pins by ysing code like this:
// Blynk.virtualWrite(V99, speed);
// We can also send the info we've collected to the serial monitor...
Serial.print("Lat: ");
Serial.println(lat,6);
Serial.print("Lng: ");
Serial.println(lon,6);
Serial.print("Speed: ");
Serial.println(speed);
Serial.print("SAT:");
Serial.println(satellites);
Serial.print("ALT:");
Serial.println(altitude, 0);
Serial.println();
}
else
{
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.setTextSize(3);
display.print("No Data");
display.display();
// You could send invalid or default coordinates to Blynk here if you wanted to
// but at this stage that may give some confusing results, so we'll leave that out
//Blynk.virtualWrite(V1, 0, 0);
// Blynk.virtualWrite(V99, 0);
Serial.println("No Data");
Serial.println();
}
}
Note that I am NOT going to keep tweaking this code for you to add-in additional features etc. once it’s working. I’ve given you some tools and knowledge for you to do that yourself.
Pete.