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();
}`