Hi guys,
I have a very strange problem.
I made a class to encapsulate the Adafruit_GPS (https://github.com/adafruit/Adafruit-GPS-Library/blob/master/Adafruit_GPS.cpp) library to abstract from other parts of the code to a possible change of GPS.
So I make the call to class, which creates the object using the class of Adafruit_GPS Serial1.
Until then OK, it works. The problem happens when I turn off the board completely and turn on again.
The Code is considering the Serial1 is a SW, not HW. (Line 252-269 of their library.)
teste.ino
#include <GPSClass.h>
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
GPSClass *gps;
void setup()
{
Serial.begin(9600);
delay(200);
Serial.println("Comecando!!!!");
gps = new GPSClass(5,20);
gps->on();
gps->begin(&Serial1);
gps->start();
}
void loop() {
gps->getData();
gps->show();
}
GPSClass.h
#ifndef _GPSCLASS_H
#define _GPSCLASS_H
#include <Adafruit_GPS.h>
#define MAXWAITSENTENCE 5
#include "Arduino.h"
class GPSClass {
public:
GPSClass(int , int);
~GPSClass();
void start();
void on();
void off();
void getData();
void show();
void begin(HardwareSerial *serIn);
private:
void readData();
void parseData();
int _gpsEnablePin;
int _gpsFixPin;
uint32_t timer;
Adafruit_GPS *_gpsModule;
HardwareSerial *_Serial;
};
#endif
GPSClass.cpp
#include "GPSClass.h"
#include <Adafruit_GPS.h>
#define GPSECHO true
GPSClass::GPSClass(int gpsEnablePin, int gpsFixPin) {
_Serial = NULL;
timer = millis();
_gpsEnablePin = gpsEnablePin;
_gpsFixPin = gpsFixPin;
pinMode(_gpsEnablePin, OUTPUT);
pinMode(_gpsFixPin, INPUT);
};
GPSClass::~GPSClass() {
delete _gpsModule;
};
void GPSClass::begin(HardwareSerial *serIn)
{
_Serial = serIn;
_Serial->begin(9600);
_Serial->println("Ready to Rip!");
}
void GPSClass::start() {
_gpsModule = new Adafruit_GPS(_Serial);
_gpsModule->begin(9600);
_gpsModule->sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
_gpsModule->sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
_gpsModule->sendCommand(PGCMD_ANTENNA);
delay(1000);
}
void GPSClass::on() {
digitalWrite(_gpsEnablePin, HIGH);
}
void GPSClass::off() {
digitalWrite(_gpsEnablePin, LOW);
}
void GPSClass::getData() {
this->readData();
this->parseData();
}
void GPSClass::show() {
// if millis() or timer wraps around, we'll just reset it
if (timer > millis()) timer = millis();
// approximately every 2 seconds or so, print out the current stats
if (millis() - timer > 2000) {
timer = millis(); // reset the timer
//this->getLatitude();
//this->getLongitude();
Serial.print("\nTime: ");
Serial.print(_gpsModule->hour, DEC); Serial.print(':');
Serial.print(_gpsModule->minute, DEC); Serial.print(':');
Serial.print(_gpsModule->seconds, DEC); Serial.print('.');
Serial.println(_gpsModule->milliseconds);
Serial.print("Date: ");
Serial.print(_gpsModule->day, DEC); Serial.print('/');
Serial.print(_gpsModule->month, DEC); Serial.print("/20");
Serial.println(_gpsModule->year, DEC);
Serial.print("Fix: "); Serial.print((int)_gpsModule->fix);
Serial.print(" quality: "); Serial.println((int)_gpsModule->fixquality);
if (_gpsModule->fix) {
Serial.print("Location: ");
Serial.print(_gpsModule->latitude, 4); Serial.print(_gpsModule->lat);
Serial.print(", ");
Serial.print(_gpsModule->longitude, 4); Serial.println(_gpsModule->lon);
Serial.print("Speed (knots): "); Serial.println(_gpsModule->speed);
Serial.print("Angle: "); Serial.println(_gpsModule->angle);
Serial.print("Altitude: "); Serial.println(_gpsModule->altitude);
Serial.print("Satellites: "); Serial.println((int)_gpsModule->satellites);
}
}
}
void GPSClass::readData() {
//char c = _gpsModule->read();
// if you want to debug, this is a good time to do it!
// if ((c) && (GPSECHO))
// Serial.write(c);
_gpsModule->read();
}
void GPSClass::parseData() {
// if a sentence is received, we can check the checksum, parse it...
if (_gpsModule->newNMEAreceived()) {
if (!_gpsModule->parse(_gpsModule->lastNMEA())) // this also sets the newNMEAreceived() flag to false
return; // we can fail to parse a sentence in which case we should just wait for another
}
}
I removed some parts of the code. Maybe I have deleted a funcção or other unintentionally. If you feel a lack of something, let me know.
But as I said the problem is happening when I unplug the board and again I reconnect. I never saw it and I have not found much help on the internet …
I thank anyone who can help.
Thank you
Sorry for my poor english …