I have an Adafruit Ultimate GPS wired up to an arduino mega. When I run this code, the GPS generally gives accurate data, but there can be a 10-15 second delay on speed updates. I am using this setup to measure speed, so it's essential that it can detect changes and display them with low latency. Any ideas? (p.s. commented out some code that is not essential for speed measurement, also I'm transmitting this via an Adafruit LoRa 433MHz radio, but it's not the radio causing latency, I've tested without)
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
// you can change the pin numbers to match your wiring:
SoftwareSerial mySerial(10, 9);
Adafruit_GPS GPS(&mySerial);
#define GPSECHO true
#include <SPI.h>
#include <RH_RF95.h>
#define RFM95_CS 4
#define RFM95_RST 2
#define RFM95_INT 3
// Change to 434.0 or other frequency, must match RX's freq!
#define RF95_FREQ 434.0
// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS, RFM95_INT);
void setup()
{
// connect at 115200 so we can read the GPS fast enough and echo without dropping chars
// also spit it out
Serial.begin(115200);
delay(1000);
Serial.println("Adafruit GPS library basic parsing test!");
// 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
GPS.begin(9600);
// uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
// Set the update rate
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_5HZ);
// For the parsing code to work nicely and have time to sort thru the data, and
// print it out we don't suggest using anything higher than 1 Hz
// Request updates on antenna status, comment out to keep quiet
GPS.sendCommand(PGCMD_ANTENNA);
delay(1000);
// Ask for firmware version
mySerial.println(PMTK_Q_RELEASE);
pinMode(RFM95_RST, OUTPUT);
digitalWrite(RFM95_RST, HIGH);
while (!Serial);
delay(100);
Serial.println("Arduino LoRa TX Test!");
// manual reset
digitalWrite(RFM95_RST, LOW);
delay(10);
digitalWrite(RFM95_RST, HIGH);
delay(10);
while (!rf95.init()) {
Serial.println("LoRa radio init failed");
delay(500);
}
Serial.println("LoRa radio init OK!");
// Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM
if (!rf95.setFrequency(RF95_FREQ)) {
Serial.println("setFrequency failed");
while (1);
}
Serial.print("Set Freq to: "); Serial.println(RF95_FREQ);
// Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on
// The default transmitter power is 13dBm, using PA_BOOST.
// If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then
// you can set transmitter powers from 5 to 23 dBm:
rf95.setTxPower(23, false);
}
String fix = "--";
String qual = "--";
String lat = "--";
String lon = "--";
String speed = "--";
String angle = "--";
String altitude = "--";
String satellites = "--";
String antenna = "--";
String b1 = "--";
String b2 = "--";
String b3 = "--";
String b4 = "--";
uint32_t timer = millis();
void loop() // run over and over again
{
char c = GPS.read();
if (GPS.newNMEAreceived()) {
char *rawNMEA = GPS.lastNMEA();
Serial.println(rawNMEA); // Print the raw NMEA sentence for debugging
if (!GPS.parse(rawNMEA))
{
return;
}
}
if (millis() - timer > 200) {
timer = millis(); // reset the timer
//Serial.print("Fix: "); Serial.print((int)GPS.fix);
//Serial.print(" quality: "); Serial.println((int)GPS.fixquality);
fix = String(GPS.fix);
qual = String(GPS.fixquality);
if (GPS.fix) {
//Serial.print("Location: ");
//Serial.print(GPS.latitude, 4); Serial.print(GPS.lat);
//Serial.print(", ");
//Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);
//Serial.print("Speed (knots): "); Serial.println(GPS.speed);
//Serial.print("Angle: "); Serial.println(GPS.angle);
//Serial.print("Altitude: "); Serial.println(GPS.altitude);
//Serial.print("Satellites: "); Serial.println((int)GPS.satellites);
//Serial.print("Antenna status: "); Serial.println((int)GPS.antenna);
//lat = String(GPS.latitude) + String(GPS.lat);
//lon = String(GPS.longitude) + String(GPS.lon);
speed = String(GPS.speed);
//angle = String(GPS.angle);
//altitude = String(GPS.altitude);
satellites = String(GPS.satellites);
//antenna = String(GPS.antenna);
}
float then = millis();
//String radiopacket = lat + " , " + lon + " , " + speed + " , " + angle + " , " + altitude + " , " + fix + " , " + qual + " , " + satellites + " , " + b1 + " , " + b2 + " , " + b3 + " , " + b4 + " , ";
String radiopacket = speed + " , " + fix + " , " + qual + " , " + satellites + " , " + b1 + " , " + b2 + " , " + b3 + " , " + b4 + " , ";
Serial.println(radiopacket);
Serial.println(speed);
const char *radiopacketCharArray = radiopacket.c_str();
rf95.send((uint8_t *)radiopacketCharArray, strlen(radiopacketCharArray));
rf95.waitPacketSent();
Serial.println((millis() - then));
}
}