Hi, Im trying to send GPS coordinates to the blynk app using a map widget but it doesn’t seem to work. It connects to the network fine and displays the coordinates in the serial mon. Can someone help pls?
(im a bit of a beginner so explain like im 5)
Code:
#define SerialMon Serial
// Set serial for AT commands (to the module)
// Use Hardware Serial on Mega, Leonardo, Micro
#define SerialAT Serial1
#define TINY_GSM_MODEM_SIM7000
#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb
#define SerialAT Serial1
#define BLYNK_PRINT Serial
#define BLYNK_HEARTBEAT 30
// See all AT commands, if wanted
// #define DUMP_AT_COMMANDS
// set GSM PIN, if any
#define GSM_PIN ""
// Your GPRS credentials, if any
#include <SimpleTimer.h>
#include <TinyGPS++.h>
#include <TinyGsmClient.h>
#include <SPI.h>
#include <SD.h>
#include <Ticker.h>
#include <BlynkSimpleTinyGSM.h>
#include <Wire.h>
#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif
#define UART_BAUD 9600
#define PIN_DTR 25
#define PIN_TX 27
#define PIN_RX 26
#define PWR_PIN 4
#define SD_MISO 2
#define SD_MOSI 15
#define SD_SCLK 14
#define SD_CS 13
#define LED_PIN 12
#define I2C_SDA 21
#define I2C_SCL 22
#define SMS_TARGET ""
bool smsSent = false;
float movingspeed = 10;
float latitude;
float longitude;
float speed;
float satellites;
String direction;
const char apn[] = "";
const char user[] = "";
const char pass[] = "";
const char auth[] = "";
static const uint32_t GPSBaud = 9600;
TinyGPSPlus gps;
WidgetMap myMap(V0);
BlynkTimer timer;
unsigned int move_index = 1;
void enableGPS(void)
{
// Set SIM7000G GPIO4 LOW ,turn on GPS power
// CMD:AT+SGPIO=0,4,1,1
// Only in version 20200415 is there a function to control GPS power
modem.sendAT("+SGPIO=0,4,1,1");
if (modem.waitResponse(10000L) != 1) {
DBG(" SGPIO=0,4,1,1 false ");
}
modem.enableGPS();
}
void disableGPS(void)
{
// Set SIM7000G GPIO4 LOW ,turn off GPS power
// CMD:AT+SGPIO=0,4,1,0
// Only in version 20200415 is there a function to control GPS power
modem.sendAT("+SGPIO=0,4,1,0");
if (modem.waitResponse(10000L) != 1) {
DBG(" SGPIO=0,4,1,0 false ");
}
modem.disableGPS();
}
void modemPowerOn()
{
pinMode(PWR_PIN, OUTPUT);
digitalWrite(PWR_PIN, LOW);
delay(1000); //Datasheet Ton mintues = 1S
digitalWrite(PWR_PIN, HIGH);
}
void modemPowerOff()
{
pinMode(PWR_PIN, OUTPUT);
digitalWrite(PWR_PIN, LOW);
delay(1500); //Datasheet Ton mintues = 1.2S
digitalWrite(PWR_PIN, HIGH);
}
void modemRestart()
{
modemPowerOff();
delay(1000);
modemPowerOn();
}
void setup()
{
// Set console baud rate
SerialMon.begin(115200);
delay(10);
// Set LED OFF
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
modemPowerOn();
SerialAT.begin(UART_BAUD, SERIAL_8N1, PIN_RX, PIN_TX);
Serial.println("/**********************************************************/");
Serial.println("To initialize the network test, please make sure your GPS");
Serial.println("antenna has been connected to the GPS port on the board.");
Serial.println("/**********************************************************/\n\n");
delay(10000);
String modemInfo = modem.getModemInfo();
SerialMon.print("Modem: ");
SerialMon.println(modemInfo);
// Unlock your SIM card with a PIN
//modem.simUnlock("1234");
SerialMon.print("Waiting for network...");
if (!modem.waitForNetwork(240000L)) {
SerialMon.println(" fail");
delay(10000);
return;
}
SerialMon.println(" OK");
if (modem.isNetworkConnected()) {
SerialMon.println("Network connected");
}
SerialMon.print(F("Connecting to APN: "));
SerialMon.print(apn);
if (!modem.gprsConnect(apn, user, pass)) {
SerialMon.println(" fail");
delay(10000);
return;
}
SerialMon.println(" OK");
// ss.begin(GPSBaud);
Blynk.begin(auth, modem, apn, user, pass);
timer.setInterval(5000L, checkGPS);
}
void checkGPS()
{
if (gps.charsProcessed() < 10)
{
//Serial.println(F("No GPS detected: check wiring."));
Blynk.virtualWrite(V4, "GPS ERROR");
}
}
void loop()
{
if (!modem.testAT()) {
Serial.println("Failed to restart modem, attempting to continue without restarting");
modemRestart();
return;
}
Serial.println("Start positioning . Make sure to locate outdoors.");
Serial.println("The blue indicator light flashes to indicate positioning.");
enableGPS();
float lat, lng;
while (1) {
if (modem.getGPS(&lat, &lng)) {
Serial.println("The location has been locked, the latitude and longitude are:");
Serial.print("latitude:"); Serial.println(lat);
Serial.print("longitude:"); Serial.println(lng);
break;
}
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
delay(2000);
}
while (1) {
while (SerialAT.available()) {
SerialMon.write(SerialAT.read());
}
while (SerialMon.available()) {
SerialAT.write(SerialMon.read());
}
}
Blynk.run();
timer.run();
}
void displayInfo()
{
if (gps.location.isValid() )
{
latitude = (gps.location.lat()); //Storing the Lat. and Lon.
longitude = (gps.location.lng());
//Serial.print("LAT: ");
//Serial.println(latitude, 6); // float to x decimal places
//Serial.print("LONG: ");
//Serial.println(longitude, 6);
Blynk.virtualWrite(V1, String(latitude, 6));
Blynk.virtualWrite(V2, String(longitude, 6));
myMap.location(move_index, latitude, longitude, "GPS_Location");
speed = gps.speed.kmph(); //get speed
Blynk.virtualWrite(V3, speed);
direction = TinyGPSPlus::cardinal(gps.course.value()); // get the direction
Blynk.virtualWrite(V4, direction);
if((speed > movingspeed) && !smsSent){
String smsMessage = String("Speed over 10km/h, please check the blynk app");
if(modem.sendSMS(SMS_TARGET, smsMessage)){
SerialMon.println(smsMessage);
smsSent = true;
}
else{
SerialMon.println("SMS failed to send");
}
}
}
}