Created
March 7, 2025 07:11
-
-
Save fxprime/72cdbad16338b47c251d9a6e5a030f1e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <SoftwareSerial.h> | |
#include <TinyGPSPlus.h> | |
EspSoftwareSerial::UART gpsSerial; | |
TinyGPSPlus gps; | |
static void smartDelay(unsigned long ms); | |
static void printFloat(float val, bool valid, int len, int prec); | |
static void printInt(unsigned long val, bool valid, int len); | |
static void printDateTime(TinyGPSDate &d, TinyGPSTime &t); | |
static void printStr(const char *str, int len); | |
void setup() { | |
Serial.begin(115200); | |
gpsSerial.begin(9600, EspSoftwareSerial::SWSERIAL_8N1, 5, 18, false, 95, 11); | |
Serial.println(); | |
Serial.println(F("Sats HDOP Latitude Longitude Fix Date Time Date Alt Course Speed Card Distance Course Card Chars Sentences Checksum")); | |
Serial.println(F(" (deg) (deg) Age Age (m) --- from GPS ---- ---- to BKK ---- RX RX Fail")); | |
Serial.println(F("----------------------------------------------------------------------------------------------------------------------------------------")); | |
} | |
void loop() { | |
static const double BANGKOK_LAT = 13.7563, BANGKOK_LON = 100.5018; | |
printInt(gps.satellites.value(), gps.satellites.isValid(), 5); | |
printFloat(gps.hdop.hdop(), gps.hdop.isValid(), 6, 1); | |
printFloat(gps.location.lat(), gps.location.isValid(), 11, 6); | |
printFloat(gps.location.lng(), gps.location.isValid(), 12, 6); | |
printInt(gps.location.age(), gps.location.isValid(), 5); | |
printDateTime(gps.date, gps.time); | |
printFloat(gps.altitude.meters(), gps.altitude.isValid(), 7, 2); | |
printFloat(gps.course.deg(), gps.course.isValid(), 7, 2); | |
printFloat(gps.speed.kmph(), gps.speed.isValid(), 6, 2); | |
printStr(gps.course.isValid() ? TinyGPSPlus::cardinal(gps.course.deg()) : "*** ", 6); | |
unsigned long distanceKmToLondon = | |
(unsigned long)TinyGPSPlus::distanceBetween( | |
gps.location.lat(), | |
gps.location.lng(), | |
BANGKOK_LAT, | |
BANGKOK_LON) / 1000; | |
printInt(distanceKmToLondon, gps.location.isValid(), 9); | |
double courseToLondon = | |
TinyGPSPlus::courseTo( | |
gps.location.lat(), | |
gps.location.lng(), | |
BANGKOK_LAT, | |
BANGKOK_LON); | |
printFloat(courseToLondon, gps.location.isValid(), 7, 2); | |
const char *cardinalToLondon = TinyGPSPlus::cardinal(courseToLondon); | |
printStr(gps.location.isValid() ? cardinalToLondon : "*** ", 6); | |
printInt(gps.charsProcessed(), true, 6); | |
printInt(gps.sentencesWithFix(), true, 10); | |
printInt(gps.failedChecksum(), true, 9); | |
Serial.println(); | |
smartDelay(1000); | |
if (millis() > 5000 && gps.charsProcessed() < 10) | |
Serial.println(F("No GPS data received: check wiring")); | |
} | |
// This custom version of delay() ensures that the gps object | |
// is being "fed". | |
static void smartDelay(unsigned long ms) | |
{ | |
unsigned long start = millis(); | |
do | |
{ | |
while (gpsSerial.available()) | |
gps.encode(gpsSerial.read()); | |
} while (millis() - start < ms); | |
} | |
static void printFloat(float val, bool valid, int len, int prec) | |
{ | |
if (!valid) | |
{ | |
while (len-- > 1) | |
Serial.print('*'); | |
Serial.print(' '); | |
} | |
else | |
{ | |
Serial.print(val, prec); | |
int vi = abs((int)val); | |
int flen = prec + (val < 0.0 ? 2 : 1); // . and - | |
flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1; | |
for (int i=flen; i<len; ++i) | |
Serial.print(' '); | |
} | |
smartDelay(0); | |
} | |
static void printInt(unsigned long val, bool valid, int len) | |
{ | |
char sz[32] = "*****************"; | |
if (valid) | |
sprintf(sz, "%ld", val); | |
sz[len] = 0; | |
for (int i=strlen(sz); i<len; ++i) | |
sz[i] = ' '; | |
if (len > 0) | |
sz[len-1] = ' '; | |
Serial.print(sz); | |
smartDelay(0); | |
} | |
static void printDateTime(TinyGPSDate &d, TinyGPSTime &t) | |
{ | |
if (!d.isValid()) | |
{ | |
Serial.print(F("********** ")); | |
} | |
else | |
{ | |
char sz[32]; | |
sprintf(sz, "%02d/%02d/%02d ", d.month(), d.day(), d.year()); | |
Serial.print(sz); | |
} | |
if (!t.isValid()) | |
{ | |
Serial.print(F("******** ")); | |
} | |
else | |
{ | |
char sz[32]; | |
sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second()); | |
Serial.print(sz); | |
} | |
printInt(d.age(), d.isValid(), 5); | |
smartDelay(0); | |
} | |
static void printStr(const char *str, int len) | |
{ | |
int slen = strlen(str); | |
for (int i=0; i<len; ++i) | |
Serial.print(i<slen ? str[i] : ' '); | |
smartDelay(0); | |
} | |
// ค่าที่ออกมา โดยประมาณ หลังมีไฟกระพริบตรงโมดูล GPS โดยปกติ เปิดใช้งานกับบ้านชั้นเดียวรอไม่เกิน10นาทีในครั้งแรก หลังจากเปิดมาแล้วหลายครั้งจะเร็วขึ้น และมีข้อมูลที่ออกมาดังนี้ | |
/* | |
Sats HDOP Latitude Longitude Fix Date Time Date Alt Course Speed Card Distance Course Card Chars Sentences Checksum | |
(deg) (deg) Age Age (m) --- from GPS ---- ---- to London ---- RX RX Fail | |
---------------------------------------------------------------------------------------------------------------------------------------- | |
**** ***** ********** *********** **** ********** ******** **** ****** ****** ***** *** ******** ****** *** 4 0 0 | |
11 0.8 7.855806 98.358467 138 03/06/2025 12:42:37 257 34.00 0.00 0.30 N 696 19.45 NNE 563 2 0 | |
10 1.1 7.855805 98.358467 142 03/06/2025 12:42:38 260 34.20 0.00 0.26 N 696 19.45 NNE 1116 4 0 | |
11 0.8 7.855806 98.358467 139 03/06/2025 12:42:39 258 35.10 0.00 0.37 N 696 19.45 NNE 1662 6 0 | |
11 0.8 7.855804 98.358467 142 03/06/2025 12:42:40 260 34.70 0.00 0.35 N 696 19.45 NNE 2214 8 0 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment