Skip to main content
Arduino

Return to Answer

Commonmark migration
Source Link

###Solutions:

Solutions:

###Solutions:

Solutions:

replaced http://arduino.stackexchange.com/ with https://arduino.stackexchange.com/
Source Link
  • The flush method waits for everything you have written to go out. You haven't written anything to the GPS or Serial, so there's no reason to flush the output. And you don't need to call it at the beginning of loop.

  • Calling delay is the surest way to lose GPS characters. Never use delay in a GPS sketch.

  • You are calling read without calling available first. Because you called delay, there are probably chars available, but not necessarily, and they will not be a complete sentence. This explains the partial NMEA data in your question.

  • You are printing too much in the plot routine. After 64 characters are printed (actually queued for printing), the Serial.print call must wait for some characters to be sent (making room in the output buffer). At 9600, each character takes 1ms to be sent. During that time, the GPS device is still sending characters. Eventually, the input buffer overflows and GPS characters are lost. When properly structured (see below), there is barely enough time to print all those messages.

  • You have connected the GPS to pins 1 & 2, but pins 0 & 1 are the HardwareSerial pins (Rx & Tx), dedicated to Serial . They are conflicting with each other, and may interfere with uploading new sketches via USB.

  • You do not have good satellite reception, according to the GPS data fragments you show. Check the antenna, get near a window, or go outside. It may take 10 minutes before a "fix" is acquired.

  • You are using SoftwareSerial for the GPS port. SoftwareSerial is very inefficient, because it disables interrupts for long periods of time. At 4800, the Arduino is dedicated to receiving each char for 2ms, an eternity for a 16MHz processor. This will interfere with other parts of the sketch or other libraries. This answer This answer gives several alternatives, like AltSoftSerial, on different pins.

  • The flush method waits for everything you have written to go out. You haven't written anything to the GPS or Serial, so there's no reason to flush the output. And you don't need to call it at the beginning of loop.

  • Calling delay is the surest way to lose GPS characters. Never use delay in a GPS sketch.

  • You are calling read without calling available first. Because you called delay, there are probably chars available, but not necessarily, and they will not be a complete sentence. This explains the partial NMEA data in your question.

  • You are printing too much in the plot routine. After 64 characters are printed (actually queued for printing), the Serial.print call must wait for some characters to be sent (making room in the output buffer). At 9600, each character takes 1ms to be sent. During that time, the GPS device is still sending characters. Eventually, the input buffer overflows and GPS characters are lost. When properly structured (see below), there is barely enough time to print all those messages.

  • You have connected the GPS to pins 1 & 2, but pins 0 & 1 are the HardwareSerial pins (Rx & Tx), dedicated to Serial . They are conflicting with each other, and may interfere with uploading new sketches via USB.

  • You do not have good satellite reception, according to the GPS data fragments you show. Check the antenna, get near a window, or go outside. It may take 10 minutes before a "fix" is acquired.

  • You are using SoftwareSerial for the GPS port. SoftwareSerial is very inefficient, because it disables interrupts for long periods of time. At 4800, the Arduino is dedicated to receiving each char for 2ms, an eternity for a 16MHz processor. This will interfere with other parts of the sketch or other libraries. This answer gives several alternatives, like AltSoftSerial, on different pins.

  • The flush method waits for everything you have written to go out. You haven't written anything to the GPS or Serial, so there's no reason to flush the output. And you don't need to call it at the beginning of loop.

  • Calling delay is the surest way to lose GPS characters. Never use delay in a GPS sketch.

  • You are calling read without calling available first. Because you called delay, there are probably chars available, but not necessarily, and they will not be a complete sentence. This explains the partial NMEA data in your question.

  • You are printing too much in the plot routine. After 64 characters are printed (actually queued for printing), the Serial.print call must wait for some characters to be sent (making room in the output buffer). At 9600, each character takes 1ms to be sent. During that time, the GPS device is still sending characters. Eventually, the input buffer overflows and GPS characters are lost. When properly structured (see below), there is barely enough time to print all those messages.

  • You have connected the GPS to pins 1 & 2, but pins 0 & 1 are the HardwareSerial pins (Rx & Tx), dedicated to Serial . They are conflicting with each other, and may interfere with uploading new sketches via USB.

  • You do not have good satellite reception, according to the GPS data fragments you show. Check the antenna, get near a window, or go outside. It may take 10 minutes before a "fix" is acquired.

  • You are using SoftwareSerial for the GPS port. SoftwareSerial is very inefficient, because it disables interrupts for long periods of time. At 4800, the Arduino is dedicated to receiving each char for 2ms, an eternity for a 16MHz processor. This will interfere with other parts of the sketch or other libraries. This answer gives several alternatives, like AltSoftSerial, on different pins.

added 1 character in body
Source Link
slash-dev
  • 2k
  • 13
  • 12
// Set up the GPS serial port (pick *one* of these choices)
//#include <AltSoftSerial.h>
//AltSoftSerial gps_port; // always 8 & 9 on an UNO
//#include <NeoSWSerial.h>
//#define rxPin 2
//#define txPin 3
//NeoSWSerial gps_port( rxPin, txPin );
#define gps_port Serial1 // for a Mega2560, Leo or Due
// Variables
#include <NMEAGPS.h>
NMEAGPS gps;
gps_fix fix;
#ifndef NMEAGPS_PARSE_GGA
 #error You must uncomment NMEAGPS_PARSE_GGA in NMEAGPS_cfg.h!
#endif
void setup() {
 Serial.begin(9600);
 gps_port.begin(4800); // 9600 is the default for NEO-6M
 //setup satellites signal
 pinMode(13, OUTPUT);
 // Turn off the led until a satellite signal
 digitalWrite(13, LOW);
 Serial.println( F("Waiting GPS data...") );
}
void loop() {
 while (gps.available( gps_port )) {
 fix = gps.read(); // a new fix structure has been parsed
 plot();
 }
}
/*
 This function organize the gps data received for printing in the serial monitor.
*/
void plot() {
 if (fix.valid.location) {
 // or if (fix.valid.status && (fix.status >= NeoGPSgps_fix::STATUS_STD)) {
 digitalWrite(13, HIGH); // Then there are satellites, the LED switch ON
 Serial.println();
 Serial.println( F("----------------------------------------------") );
 Serial.print ( F("UTC Hour -> ") );
 if (fix.valid.time)
 Serial.println( fix.dateTime.hours );
 Serial.print ( F("Latitude -> ") );
 Serial.println( fix.latitude(), 6 );
 Serial.print ( F("Longitude -> ") );
 Serial.println( fix.longitude(), 6 );
 Serial.print ( F("GPS status: 0=NONE; 1=EST, 2=TIME, 3=STD, 4=DGPS -> ") );
 if (fix.valid.status)
 Serial.print( fix.status );
 Serial.println();
 Serial.print ( F("Number of satellites -> ") );
 if (fix.valid.satellites)
 Serial.print( fix.satellites );
 Serial.println();
 #ifdef GPS_FIX_HDOP
 Serial.print ( F("Horizontal Dilution of Precision (x1000) -> ") );
 if (fix.valid.hdop)
 Serial.print( fix.hdop );
 Serial.println();
 #else
 #error You must uncomment GPS_FIX_HDOP in GPSfix_cfg.h!
 #endif
 Serial.print ( F("Antenna altitude (m) -> ") );
 if (fix.valid.altitude)
 Serial.print ( fix.altitude(), 2 );
 Serial.println();
 #ifdef GPS_FIX_GEOID_HEIGHT
 Serial.print ( F("Geoid Separation (m) -> ") );
 if (fix.valid.geoidHeight)
 Serial.print ( fix.geoidHeight(), 2 );
 Serial.println();
 #else
 #error You must uncomment GPS_FIX_GEOID_HEIGHT in GPSfix_cfg.h!
 #endif
 Serial.println( F("----------------------------------------------") );
 Serial.println();
 } else {
 // If no satellite connection
 digitalWrite(13, LOW); // Turn off the LED
 Serial.println();
 Serial.println( F("-----------------------------") );
 Serial.print ( F("|--- Status -->") );
 if (fix.valid.status)
 Serial.print ( fix.status );
 Serial.println();
 Serial.println( F(" |") );
 Serial.println( F("|----Waiting location----|") );
 Serial.println( F("-----------------------------") );
 Serial.println();
 }
}
// Set up the GPS serial port (pick *one* of these choices)
//#include <AltSoftSerial.h>
//AltSoftSerial gps_port; // always 8 & 9 on an UNO
//#include <NeoSWSerial.h>
//#define rxPin 2
//#define txPin 3
//NeoSWSerial gps_port( rxPin, txPin );
#define gps_port Serial1 // for a Mega2560, Leo or Due
// Variables
#include <NMEAGPS.h>
NMEAGPS gps;
gps_fix fix;
#ifndef NMEAGPS_PARSE_GGA
 #error You must uncomment NMEAGPS_PARSE_GGA in NMEAGPS_cfg.h!
#endif
void setup() {
 Serial.begin(9600);
 gps_port.begin(4800); // 9600 is the default for NEO-6M
 //setup satellites signal
 pinMode(13, OUTPUT);
 // Turn off the led until a satellite signal
 digitalWrite(13, LOW);
 Serial.println( F("Waiting GPS data...") );
}
void loop() {
 while (gps.available( gps_port )) {
 fix = gps.read(); // a new fix structure has been parsed
 plot();
 }
}
/*
 This function organize the gps data received for printing in the serial monitor.
*/
void plot() {
 if (fix.valid.location) {
 // or if (fix.valid.status && (fix.status >= NeoGPS::STATUS_STD)) {
 digitalWrite(13, HIGH); // Then there are satellites, the LED switch ON
 Serial.println();
 Serial.println( F("----------------------------------------------") );
 Serial.print ( F("UTC Hour -> ") );
 if (fix.valid.time)
 Serial.println( fix.dateTime.hours );
 Serial.print ( F("Latitude -> ") );
 Serial.println( fix.latitude(), 6 );
 Serial.print ( F("Longitude -> ") );
 Serial.println( fix.longitude(), 6 );
 Serial.print ( F("GPS status: 0=NONE; 1=EST, 2=TIME, 3=STD, 4=DGPS -> ") );
 if (fix.valid.status)
 Serial.print( fix.status );
 Serial.println();
 Serial.print ( F("Number of satellites -> ") );
 if (fix.valid.satellites)
 Serial.print( fix.satellites );
 Serial.println();
 #ifdef GPS_FIX_HDOP
 Serial.print ( F("Horizontal Dilution of Precision (x1000) -> ") );
 if (fix.valid.hdop)
 Serial.print( fix.hdop );
 Serial.println();
 #else
 #error You must uncomment GPS_FIX_HDOP in GPSfix_cfg.h!
 #endif
 Serial.print ( F("Antenna altitude (m) -> ") );
 if (fix.valid.altitude)
 Serial.print ( fix.altitude(), 2 );
 Serial.println();
 #ifdef GPS_FIX_GEOID_HEIGHT
 Serial.print ( F("Geoid Separation (m) -> ") );
 if (fix.valid.geoidHeight)
 Serial.print ( fix.geoidHeight(), 2 );
 Serial.println();
 #else
 #error You must uncomment GPS_FIX_GEOID_HEIGHT in GPSfix_cfg.h!
 #endif
 Serial.println( F("----------------------------------------------") );
 Serial.println();
 } else {
 // If no satellite connection
 digitalWrite(13, LOW); // Turn off the LED
 Serial.println();
 Serial.println( F("-----------------------------") );
 Serial.print ( F("|--- Status -->") );
 if (fix.valid.status)
 Serial.print ( fix.status );
 Serial.println();
 Serial.println( F(" |") );
 Serial.println( F("|----Waiting location----|") );
 Serial.println( F("-----------------------------") );
 Serial.println();
 }
}
// Set up the GPS serial port (pick *one* of these choices)
//#include <AltSoftSerial.h>
//AltSoftSerial gps_port; // always 8 & 9 on an UNO
//#include <NeoSWSerial.h>
//#define rxPin 2
//#define txPin 3
//NeoSWSerial gps_port( rxPin, txPin );
#define gps_port Serial1 // for a Mega2560, Leo or Due
// Variables
#include <NMEAGPS.h>
NMEAGPS gps;
gps_fix fix;
#ifndef NMEAGPS_PARSE_GGA
 #error You must uncomment NMEAGPS_PARSE_GGA in NMEAGPS_cfg.h!
#endif
void setup() {
 Serial.begin(9600);
 gps_port.begin(4800); // 9600 is the default for NEO-6M
 //setup satellites signal
 pinMode(13, OUTPUT);
 // Turn off the led until a satellite signal
 digitalWrite(13, LOW);
 Serial.println( F("Waiting GPS data...") );
}
void loop() {
 while (gps.available( gps_port )) {
 fix = gps.read(); // a new fix structure has been parsed
 plot();
 }
}
/*
 This function organize the gps data received for printing in the serial monitor.
*/
void plot() {
 if (fix.valid.location) {
 // or if (fix.valid.status && (fix.status >= gps_fix::STATUS_STD)) {
 digitalWrite(13, HIGH); // Then there are satellites, the LED switch ON
 Serial.println();
 Serial.println( F("----------------------------------------------") );
 Serial.print ( F("UTC Hour -> ") );
 if (fix.valid.time)
 Serial.println( fix.dateTime.hours );
 Serial.print ( F("Latitude -> ") );
 Serial.println( fix.latitude(), 6 );
 Serial.print ( F("Longitude -> ") );
 Serial.println( fix.longitude(), 6 );
 Serial.print ( F("GPS status: 0=NONE; 1=EST, 2=TIME, 3=STD, 4=DGPS -> ") );
 if (fix.valid.status)
 Serial.print( fix.status );
 Serial.println();
 Serial.print ( F("Number of satellites -> ") );
 if (fix.valid.satellites)
 Serial.print( fix.satellites );
 Serial.println();
 #ifdef GPS_FIX_HDOP
 Serial.print ( F("Horizontal Dilution of Precision (x1000) -> ") );
 if (fix.valid.hdop)
 Serial.print( fix.hdop );
 Serial.println();
 #else
 #error You must uncomment GPS_FIX_HDOP in GPSfix_cfg.h!
 #endif
 Serial.print ( F("Antenna altitude (m) -> ") );
 if (fix.valid.altitude)
 Serial.print ( fix.altitude(), 2 );
 Serial.println();
 #ifdef GPS_FIX_GEOID_HEIGHT
 Serial.print ( F("Geoid Separation (m) -> ") );
 if (fix.valid.geoidHeight)
 Serial.print ( fix.geoidHeight(), 2 );
 Serial.println();
 #else
 #error You must uncomment GPS_FIX_GEOID_HEIGHT in GPSfix_cfg.h!
 #endif
 Serial.println( F("----------------------------------------------") );
 Serial.println();
 } else {
 // If no satellite connection
 digitalWrite(13, LOW); // Turn off the LED
 Serial.println();
 Serial.println( F("-----------------------------") );
 Serial.print ( F("|--- Status -->") );
 if (fix.valid.status)
 Serial.print ( fix.status );
 Serial.println();
 Serial.println( F(" |") );
 Serial.println( F("|----Waiting location----|") );
 Serial.println( F("-----------------------------") );
 Serial.println();
 }
}
alternative test
Source Link
slash-dev
  • 2k
  • 13
  • 12
Loading
add compile-time check for HDOP configuration item
Source Link
slash-dev
  • 2k
  • 13
  • 12
Loading
added 1 character in body
Source Link
slash-dev
  • 2k
  • 13
  • 12
Loading
Source Link
slash-dev
  • 2k
  • 13
  • 12
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /