|
| 1 | +#include <GPS.h> |
1 | 2 | #include <GSM.h>
|
2 | 3 | #include <MicroNMEA.h>
|
3 | 4 |
|
4 | 5 | #include "arduino_secrets.h"
|
5 |
| -char pin[] = SECRET_PIN; |
6 |
| -char apn[] = SECRET_APN; |
7 |
| -char username[] = SECRET_USERNAME; |
8 |
| -char pass[] = SECRET_PASSWORD; |
9 |
| - |
10 |
| -void mycallback(char * output); |
11 |
| - |
12 |
| -void setup() { |
13 |
| - Serial.begin(115200); |
14 |
| - while (!Serial) {} |
15 |
| - //GSM.debug(Serial); |
16 |
| - Serial.println("\nStarting connection to server..."); |
17 |
| - GSM.begin(pin, apn, username, pass, CATNB); |
18 |
| - Serial.println("\nStarting connection ..."); |
19 |
| - |
20 |
| - Serial.println("\nEnable GNSS Engine..."); |
21 |
| - GSM.beginGNSS(mycallback); |
22 |
| - GSM.startGNSS(); |
23 |
| -} |
| 6 | +constexpr auto pin { SECRET_PIN }; |
| 7 | +constexpr auto apn { SECRET_APN }; |
| 8 | +constexpr auto username { SECRET_USERNAME }; |
| 9 | +constexpr auto pass { SECRET_PASSWORD }; |
24 | 10 |
|
25 | 11 | char nmeaBuffer[100];
|
26 | 12 | MicroNMEA nmea(nmeaBuffer, sizeof(nmeaBuffer));
|
27 | 13 |
|
28 |
| -void loop() { |
29 |
| - |
30 |
| - delay(1000); |
31 |
| - |
32 |
| - // Output GPS information from previous second |
33 |
| - Serial.print("Valid fix: "); |
34 |
| - Serial.println(nmea.isValid() ? "yes" : "no"); |
35 |
| - |
36 |
| - if (nmea.isValid()) { |
37 |
| - |
38 |
| - Serial.print("Nav. system: "); |
39 |
| - if (nmea.getNavSystem()) |
40 |
| - Serial.println(nmea.getNavSystem()); |
41 |
| - else |
42 |
| - Serial.println("none"); |
43 |
| - |
44 |
| - Serial.print("Num. satellites: "); |
45 |
| - Serial.println(nmea.getNumSatellites()); |
46 |
| - |
47 |
| - Serial.print("HDOP: "); |
48 |
| - Serial.println(nmea.getHDOP() / 10., 1); |
49 |
| - |
50 |
| - Serial.print("Date/time: "); |
51 |
| - Serial.print(nmea.getYear()); |
52 |
| - Serial.print('-'); |
53 |
| - Serial.print(int(nmea.getMonth())); |
54 |
| - Serial.print('-'); |
55 |
| - Serial.print(int(nmea.getDay())); |
56 |
| - Serial.print('T'); |
57 |
| - Serial.print(int(nmea.getHour())); |
58 |
| - Serial.print(':'); |
59 |
| - Serial.print(int(nmea.getMinute())); |
60 |
| - Serial.print(':'); |
61 |
| - Serial.println(int(nmea.getSecond())); |
62 |
| - |
63 |
| - long latitude_mdeg = nmea.getLatitude(); |
64 |
| - long longitude_mdeg = nmea.getLongitude(); |
65 |
| - Serial.print("Latitude (deg): "); |
66 |
| - Serial.println(latitude_mdeg / 1000000., 6); |
67 |
| - |
68 |
| - Serial.print("Longitude (deg): "); |
69 |
| - Serial.println(longitude_mdeg / 1000000., 6); |
70 |
| - |
71 |
| - long alt; |
72 |
| - Serial.print("Altitude (m): "); |
73 |
| - if (nmea.getAltitude(alt)) |
74 |
| - Serial.println(alt / 1000., 3); |
75 |
| - else |
76 |
| - Serial.println("not available"); |
77 |
| - |
78 |
| - Serial.print("Speed: "); |
79 |
| - Serial.println(nmea.getSpeed() / 1000., 3); |
80 |
| - Serial.print("Course: "); |
81 |
| - Serial.println(nmea.getCourse() / 1000., 3); |
82 |
| - } |
| 14 | +// Keep track of NMEA string processing |
| 15 | +auto nmeaProcessStatus { false }; |
| 16 | + |
| 17 | +// Check for valid fix every checkValidInterval ms |
| 18 | +constexpr unsigned long checkValidInterval { 5000 }; |
| 19 | +unsigned long checkValidNow {}; |
| 20 | + |
| 21 | +void setup() |
| 22 | +{ |
| 23 | + Serial.begin(115200); |
| 24 | + for (const auto timeout = millis() + 2500; !Serial && millis() < timeout; delay(250)) |
| 25 | + ; |
| 26 | + |
| 27 | + // GSM.debug(Serial); |
| 28 | + delay(1000); |
| 29 | + |
| 30 | + Serial.println("Starting Carrier Network registration"); |
| 31 | + if (!GSM.begin(pin, apn, username, pass, CATNB)) { |
| 32 | + Serial.println("The board was not able to register to the network..."); |
| 33 | + // do nothing forevermore: |
| 34 | + while (1) |
| 35 | + ; |
| 36 | + } |
| 37 | + Serial.println("Enable GNSS Engine..."); |
| 38 | + // GPS.begin() start and eanble the GNSS engine |
| 39 | + GPS.begin(); |
| 40 | + Serial.println("GNSS Engine enabled..."); |
| 41 | + Serial.println("Waiting for a valid fix."); |
| 42 | + |
| 43 | + checkValidNow = millis(); |
83 | 44 | }
|
84 | 45 |
|
85 |
| -void mycallback(char * output) |
| 46 | +void loop() |
86 | 47 | {
|
87 |
| - for (size_t i = 0; i < strlen(output); i++) { |
88 |
| - nmea.process(output[i]); |
89 |
| - } |
90 |
| - nmea.process('\0'); |
91 |
| - //Serial.println(output); |
| 48 | + while (GPS.available()) { |
| 49 | + char c = GPS.read(); |
| 50 | + // process is true when a valid NMEA string has been processed |
| 51 | + nmeaProcessStatus = nmea.process(c); |
| 52 | + } |
| 53 | + |
| 54 | + if (nmeaProcessStatus && millis() > checkValidNow) { |
| 55 | + checkValidNow = millis() + checkValidInterval; |
| 56 | + |
| 57 | + // Output GPS information from previous second |
| 58 | + Serial.print("Valid fix: "); |
| 59 | + Serial.println(nmea.isValid() ? "yes" : "no"); |
| 60 | + |
| 61 | + if (!nmea.isValid()) |
| 62 | + return; |
| 63 | + |
| 64 | + String navSystem; |
| 65 | + switch (nmea.getNavSystem()) { |
| 66 | + case 'N': |
| 67 | + navSystem = "GNSS"; |
| 68 | + break; |
| 69 | + case 'P': |
| 70 | + navSystem = "GPS"; |
| 71 | + break; |
| 72 | + case 'L': |
| 73 | + navSystem = "GLONASS"; |
| 74 | + break; |
| 75 | + case 'A': |
| 76 | + navSystem = "Galileo"; |
| 77 | + break; |
| 78 | + default: |
| 79 | + break; |
| 80 | + } |
| 81 | + Serial.print("Nav. system: "); |
| 82 | + Serial.println(navSystem); |
| 83 | + |
| 84 | + Serial.print("Num. satellites: "); |
| 85 | + Serial.println(nmea.getNumSatellites()); |
| 86 | + |
| 87 | + Serial.print("HDOP: "); |
| 88 | + Serial.println(nmea.getHDOP() / 10., 1); |
| 89 | + |
| 90 | + Serial.print("Date/time: "); |
| 91 | + Serial.print(nmea.getYear()); |
| 92 | + Serial.print('-'); |
| 93 | + Serial.print(int(nmea.getMonth())); |
| 94 | + Serial.print('-'); |
| 95 | + Serial.print(int(nmea.getDay())); |
| 96 | + Serial.print('T'); |
| 97 | + Serial.print(int(nmea.getHour())); |
| 98 | + Serial.print(':'); |
| 99 | + Serial.print(int(nmea.getMinute())); |
| 100 | + Serial.print(':'); |
| 101 | + Serial.println(int(nmea.getSecond())); |
| 102 | + |
| 103 | + long latitude_mdeg = nmea.getLatitude(); |
| 104 | + long longitude_mdeg = nmea.getLongitude(); |
| 105 | + Serial.print("Latitude (deg): "); |
| 106 | + Serial.println(latitude_mdeg / 1000000., 6); |
| 107 | + |
| 108 | + Serial.print("Longitude (deg): "); |
| 109 | + Serial.println(longitude_mdeg / 1000000., 6); |
| 110 | + |
| 111 | + long alt; |
| 112 | + Serial.print("Altitude (m): "); |
| 113 | + if (nmea.getAltitude(alt)) |
| 114 | + Serial.println(alt / 1000., 3); |
| 115 | + else |
| 116 | + Serial.println("not available"); |
| 117 | + |
| 118 | + Serial.print("Speed: "); |
| 119 | + Serial.println(nmea.getSpeed() / 1000., 3); |
| 120 | + Serial.print("Course: "); |
| 121 | + Serial.println(nmea.getCourse() / 1000., 3); |
| 122 | + nmea.clear(); |
| 123 | + } |
92 | 124 | }
|
0 commit comments