|
| 1 | +/** |
| 2 | + * @file getSensorData.ino |
| 3 | + * @author SeanKwok (shaoxiang@m5stack.com) |
| 4 | + * @brief M5Module GNSS Get Possition Demo. |
| 5 | + * @version 0.1 |
| 6 | + * @date 2023-08-31 |
| 7 | + * |
| 8 | + * |
| 9 | + * @Hardwares:M5Module GNSS |
| 10 | + * @Platform Version: Arduino M5Stack Board Manager v2.0.7 |
| 11 | + * @Dependent Library: |
| 12 | + * TinyGPSPlus: https://github.com/mikalhart/TinyGPSPlus |
| 13 | + */ |
| 14 | + |
| 15 | +#include <TinyGPSPlus.h> |
| 16 | + |
| 17 | +TinyGPSPlus gps; |
| 18 | + |
| 19 | +static void smartDelay(unsigned long ms); |
| 20 | +static void printFloat(float val, bool valid, int len, int prec); |
| 21 | +static void printInt(unsigned long val, bool valid, int len); |
| 22 | +static void printDateTime(TinyGPSDate &d, TinyGPSTime &t); |
| 23 | +static void printStr(const char *str, int len); |
| 24 | + |
| 25 | +void setup() { |
| 26 | + Serial.begin(115200); |
| 27 | + Serial2.begin(115200, SERIAL_8N1, 16, 17); |
| 28 | + |
| 29 | + Serial.println(); |
| 30 | + Serial.println(F( |
| 31 | + "Sats HDOP Latitude Longitude Fix Date Time Date Alt " |
| 32 | + " Course Speed Card Distance Course Card Chars Sentences Checksum")); |
| 33 | + Serial.println( |
| 34 | + F(" (deg) (deg) Age Age (m) " |
| 35 | + " --- from GPS ---- ---- to London ---- RX RX Fail")); |
| 36 | + Serial.println(F( |
| 37 | + "----------------------------------------------------------------------" |
| 38 | + "------------------------------------------------------------------")); |
| 39 | +} |
| 40 | + |
| 41 | +void loop() { |
| 42 | + static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002; |
| 43 | + |
| 44 | + printInt(gps.satellites.value(), gps.satellites.isValid(), 5); |
| 45 | + printFloat(gps.hdop.hdop(), gps.hdop.isValid(), 6, 1); |
| 46 | + printFloat(gps.location.lat(), gps.location.isValid(), 11, 6); |
| 47 | + printFloat(gps.location.lng(), gps.location.isValid(), 12, 6); |
| 48 | + printInt(gps.location.age(), gps.location.isValid(), 5); |
| 49 | + printDateTime(gps.date, gps.time); |
| 50 | + printFloat(gps.altitude.meters(), gps.altitude.isValid(), 7, 2); |
| 51 | + printFloat(gps.course.deg(), gps.course.isValid(), 7, 2); |
| 52 | + printFloat(gps.speed.kmph(), gps.speed.isValid(), 6, 2); |
| 53 | + printStr( |
| 54 | + gps.course.isValid() ? TinyGPSPlus::cardinal(gps.course.deg()) : "*** ", |
| 55 | + 6); |
| 56 | + |
| 57 | + unsigned long distanceKmToLondon = |
| 58 | + (unsigned long)TinyGPSPlus::distanceBetween( |
| 59 | + gps.location.lat(), gps.location.lng(), LONDON_LAT, LONDON_LON) / |
| 60 | + 1000; |
| 61 | + printInt(distanceKmToLondon, gps.location.isValid(), 9); |
| 62 | + |
| 63 | + double courseToLondon = TinyGPSPlus::courseTo( |
| 64 | + gps.location.lat(), gps.location.lng(), LONDON_LAT, LONDON_LON); |
| 65 | + |
| 66 | + printFloat(courseToLondon, gps.location.isValid(), 7, 2); |
| 67 | + |
| 68 | + const char *cardinalToLondon = TinyGPSPlus::cardinal(courseToLondon); |
| 69 | + |
| 70 | + printStr(gps.location.isValid() ? cardinalToLondon : "*** ", 6); |
| 71 | + |
| 72 | + printInt(gps.charsProcessed(), true, 6); |
| 73 | + printInt(gps.sentencesWithFix(), true, 10); |
| 74 | + printInt(gps.failedChecksum(), true, 9); |
| 75 | + Serial.println(); |
| 76 | + |
| 77 | + smartDelay(1000); |
| 78 | + |
| 79 | + if (millis() > 5000 && gps.charsProcessed() < 10) |
| 80 | + Serial.println(F("No GPS data received: check wiring")); |
| 81 | +} |
| 82 | + |
| 83 | +// This custom version of delay() ensures that the gps object |
| 84 | +// is being "fed". |
| 85 | +static void smartDelay(unsigned long ms) { |
| 86 | + unsigned long start = millis(); |
| 87 | + do { |
| 88 | + while (Serial2.available()) gps.encode(Serial2.read()); |
| 89 | + } while (millis() - start < ms); |
| 90 | +} |
| 91 | + |
| 92 | +static void printFloat(float val, bool valid, int len, int prec) { |
| 93 | + if (!valid) { |
| 94 | + while (len-- > 1) Serial.print('*'); |
| 95 | + Serial.print(' '); |
| 96 | + } else { |
| 97 | + Serial.print(val, prec); |
| 98 | + int vi = abs((int)val); |
| 99 | + int flen = prec + (val < 0.0 ? 2 : 1); // . and - |
| 100 | + flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1; |
| 101 | + for (int i = flen; i < len; ++i) Serial.print(' '); |
| 102 | + } |
| 103 | + smartDelay(0); |
| 104 | +} |
| 105 | + |
| 106 | +static void printInt(unsigned long val, bool valid, int len) { |
| 107 | + char sz[32] = "*****************"; |
| 108 | + if (valid) sprintf(sz, "%ld", val); |
| 109 | + sz[len] = 0; |
| 110 | + for (int i = strlen(sz); i < len; ++i) sz[i] = ' '; |
| 111 | + if (len > 0) sz[len - 1] = ' '; |
| 112 | + Serial.print(sz); |
| 113 | + smartDelay(0); |
| 114 | +} |
| 115 | + |
| 116 | +static void printDateTime(TinyGPSDate &d, TinyGPSTime &t) { |
| 117 | + if (!d.isValid()) { |
| 118 | + Serial.print(F("********** ")); |
| 119 | + } else { |
| 120 | + char sz[32]; |
| 121 | + sprintf(sz, "%02d/%02d/%02d ", d.month(), d.day(), d.year()); |
| 122 | + Serial.print(sz); |
| 123 | + } |
| 124 | + |
| 125 | + if (!t.isValid()) { |
| 126 | + Serial.print(F("******** ")); |
| 127 | + } else { |
| 128 | + char sz[32]; |
| 129 | + sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second()); |
| 130 | + Serial.print(sz); |
| 131 | + } |
| 132 | + |
| 133 | + printInt(d.age(), d.isValid(), 5); |
| 134 | + smartDelay(0); |
| 135 | +} |
| 136 | + |
| 137 | +static void printStr(const char *str, int len) { |
| 138 | + int slen = strlen(str); |
| 139 | + for (int i = 0; i < len; ++i) Serial.print(i < slen ? str[i] : ' '); |
| 140 | + smartDelay(0); |
| 141 | +} |
0 commit comments