I have a stripped down version of the TinyGPS example sketch.
The GPS module is switched with a P-Channel MOSFET and this works great.
This is my full code:
#include <TinyGPSPlus.h>
/*
This sample sketch demonstrates the normal use of a TinyGPSPlus (TinyGPSPlus) object.
It requires the use of SoftwareSerial, and assumes that you have a
4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
The GPS module is switched using a P-Channel MOSFET
The GPS Tx/Rx are conneted to the Arduino Hardware UART
*/
// The TinyGPSPlus object
TinyGPSPlus gps;
void setup()
{
Serial.begin(9600);
pinMode(5, OUTPUT);// GPS Pin
digitalWrite(5, LOW);// GPS OFF
getGPS();
}
void loop()
{
// while (Serial.available() > 0)
// if (gps.encode(Serial.read()))
// displayInfo();
//
// if (millis() > 5000 && gps.charsProcessed() < 10)
// {
// Serial.println(F("No GPS detected: check wiring."));
// while (true);
// }
}
void getGPS(){
digitalWrite(5, LOW);// GPS ON
while (Serial.available() > 0)
if (gps.encode(Serial.read()))
displayInfo();
}
void displayInfo()
{
Serial.print(F("Location: "));
if (gps.location.isValid())
{
Serial.print(gps.location.lat(), 6);
Serial.print(F(","));
Serial.print(gps.location.lng(), 6);
Serial.print(F(","));
int Speed = (gps.speed.kmph() * 100);
Serial.print(Speed);
int Heading = (gps.course.deg() * 100);
Serial.print(F(","));
Serial.print(Heading);
}
else
{
Serial.print(F("INVALID"));
}
Serial.println();
}
If I run the code in void loop() all is OK.
// while (Serial.available() > 0)
// if (gps.encode(Serial.read()))
// displayInfo();
//
// if (millis() > 5000 && gps.charsProcessed() < 10)
// {
// Serial.println(F("No GPS detected: check wiring."));
// while (true);
// }
However, if I comment the code in void loop() out and try to call void getGPS() from the void setup(), it does not run.
What am I doing that is preventing me from call the void getGPS() function?
If you turn the GPS on by supplying power to it then immediately try to read data from it then the chances of it having powered up and locked on to the required number of satellites is vanishingly small
void getGPS() {
Serial.println("Goto get GPS");
digitalWrite(5, LOW);// GPS ON
delay(5000);
while (Serial.available() > 0)
if (gps.encode(Serial.read()))
displayInfo();
}
My Serial output is:
Goto get GPS
Location: INVALID
Location: INVALID
Location: INVALID
It stops after I have 3 "INVALID" from displayInfo()
Is that 3 - 4 seconds from a cold start or 3 - 4 seconds from resuming from sleep or some similar mode ?
Can you provide a link to a reference to the 3 - 4 second acquisition of a fix from a cold start ?
What indication does the GPS give that it has acquired a fix ?
This is not from a Cold Start - It is from a Hot Start as the GPS has battery backup.
The TinyGPS uses the NMEA RMC message which gives an indication of Fix:
"A" = Valid Fix
"V" = No Valid Fix
Are there any more important details that you have not shared ?
How is the GPS connected to the Arduino ?
Which Arduino are you using ?
How is the GPS powered ?
Why does your sketch call displayInfo() when as little as a single character has been received ?
If you call getGPS() from setup(), you get one, and only one, chance for it to work. If, 5 seconds after you call getGPS(), there is no character available from Serial, or if there is even a single garbage character, you will not send anything useful in the gps.encode() call, so it will fail. And, you will never call getGPS() again, because setup() is called once, and only once. This logic will never work as you want, unless you wrap the call to getGPS() in a loop, which is precisely why calling it from loop() does work.