LoRaWAN-Signal-Mapping/iot/main/gps.cpp

85 lines
2.1 KiB
C++
Raw Normal View History

#include "gps.h"
#include "settings.h"
SoftwareSerial gps_begin() {
SoftwareSerial serial_gps(PIN_GPS_RX, PIN_GPS_TX);
// Initialise the software-based serial connection to the GPS device
serial_gps.begin(BAUD_GPS);
return serial_gps;
}
GPSLocation gps_fetch() {
// The serial connection to the GPS module
SoftwareSerial serial_gps = gps_begin();
// The GPS message decoder
TinyGPSPlus gps;
// The struct that will hold the result
GPSLocation result;
Serial.print(F("[gps] Working: "));
uint32_t ms_last_update = millis();
2019-06-27 15:27:33 +00:00
uint8_t ticks = 0;
// We WILL discover our location - if it's the last thing we do!
while(true) {
if(millis() > 5000 && gps.charsProcessed() < 10) {
Serial.println(F("\n[error] GPS device init failed"));
while(true) { delay(1); }
}
// Make sure there's something to read
if(serial_gps.available() <= 0)
continue;
// If it failed, go around again
if(!gps.encode(serial_gps.read()))
continue;
// If we haven't acquired a lock yet, go around again
if(!gps.location.isValid()
#ifdef SD_DEBUG_ENABLED
|| !gps.time.isValid()
#endif
) {
// NOTE: It can take a rather long time to get a lock. Just wait patiently :-)
2019-06-27 15:27:33 +00:00
if(millis() - ms_last_update > 500) {
ticks++;
#ifdef SD_DEBUG_ENABLED
2019-06-27 15:27:33 +00:00
Serial.print(gps.time.isValid() ? '-' : '.');
#else
Serial.print('.');
#endif
2019-06-27 15:27:33 +00:00
if(ticks > 80) {
Serial.println();
ticks = 0;
}
// Serial.println(gps.location.lat());
// Serial.println(gps.location.lng());
// Serial.println(gps.date.year());
// Serial.println(gps.time.hour());
// Serial.println(gps.time.minute());
}
ms_last_update = millis();
continue;
}
// Hooray!
2019-06-24 14:47:00 +00:00
Serial.println(F("ok"));
result.lat = gps.location.lat();
result.lng = gps.location.lng();
#ifdef SD_DEBUG_ENABLED
snprintf(result.time, 19, "%04d-%02d-%02d %02d:%02d:%02d",
gps.date.year(), gps.date.month(), gps.date.day(),
gps.time.hour(), gps.time.minute(), gps.time.second()
);
#endif
return result;
}
gps_end(serial_gps);
}
void gps_end(SoftwareSerial serial_gps) {
serial_gps.end();
}