SHOW:
|
|
- or go back to the newest paste.
1 | - | [code] |
1 | + | |
2 | ||
3 | // *************************************** | |
4 | // Notes & Comments: | |
5 | // Code for GPS tracking with sleep functionality and logging. | |
6 | // Design Notes: | |
7 | // you could use the locus logger instead of the shield... and then send from the locus to the gprs | |
8 | // *************************************** | |
9 | ||
10 | // *************************************** | |
11 | // Library Inclusions | |
12 | // *************************************** | |
13 | ||
14 | //#include <Sleep_n0m1.h> //Sleep library | |
15 | #include <Narcoleptic.h> //New sleep library | |
16 | #include <math.h> //Math function library | |
17 | #include <Adafruit_GPS.h> //Ultimate GPS breakout library | |
18 | #include <SoftwareSerial.h> //Software serial library | |
19 | ||
20 | // *************************************** | |
21 | // Initiate variables and vlaues | |
22 | // *************************************** | |
23 | ||
24 | SoftwareSerial mySerial(2, 3); //Software serial setup on pin 2 and 3 | |
25 | Adafruit_GPS GPS(&mySerial); //Setup connection as gps | |
26 | //Sleep sleep; //Define sleep | |
27 | unsigned long sleepTime; //Define sleepTime to hold millisecond of sleep required | |
28 | #define GPSECHO true //Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console | |
29 | int GPSEnablePin = 7; //Set digital pin number to be linked to gps enable pin. This allows GPS to be powered on and off by setting HIGH/LOW | |
30 | boolean usingInterrupt = false; //Initiate true/false value against interrupt. Set to false initially so no floating | |
31 | void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy | |
32 | int fix_try_counter; //Initiate counter to hold value for number of fix retries | |
33 | uint32_t timer = millis(); //Set our timer to be linked to millis() | |
34 | ||
35 | // *************************************** | |
36 | // Setup Interrupt | |
37 | // Interrupt is called once a millisecond, looks for any new GPS data, and stores it | |
38 | // *************************************** | |
39 | ||
40 | SIGNAL(TIMER0_COMPA_vect) { | |
41 | char c = GPS.read(); | |
42 | } | |
43 | ||
44 | void useInterrupt(boolean v) { | |
45 | if (v) { | |
46 | // Timer0 is already used for millis() - we'll just interrupt somewhere | |
47 | // in the middle and call the "Compare A" function above | |
48 | OCR0A = 0xAF; //Timer 0 OCR0A Register for low threshold - 175 | |
49 | TIMSK0 |= _BV(OCIE0A); //TiIMSK0 Timer/Counter Interrupt Mask Register Counter 0 Compare OCROA(TIMER0_COMPA_vect) | |
50 | usingInterrupt = true; | |
51 | } | |
52 | else { | |
53 | // do not call the interrupt function COMPA anymore | |
54 | TIMSK0 &= ~_BV(OCIE0A); | |
55 | usingInterrupt = false; | |
56 | } | |
57 | } | |
58 | ||
59 | // *************************************** | |
60 | // Setup script | |
61 | // *************************************** | |
62 | ||
63 | void setup() | |
64 | { | |
65 | ||
66 | //Some power saving attempts | |
67 | ||
68 | ||
69 | //End of saving attempts | |
70 | ||
71 | pinMode(GPSEnablePin, OUTPUT); //Sets the enable pin as output to allow voltage. | |
72 | digitalWrite(GPSEnablePin,HIGH); //Ensure GPS enabled. | |
73 | fix_try_counter=0; //start the counter from 0. | |
74 | useInterrupt(true); //We want to use interrupt for best capture of strings from GPS. | |
75 | [b] //Serial.begin(115200); //Connect at 115200 so we can read the GPS fast enough and echo without dropping chars. | |
76 | //Serial.println("Car Tracker Alpha");[/b] | |
77 | GPS.begin(9600); //9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800. | |
78 | GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); //Define output data required. | |
79 | GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); //Define update rate of GPS which is currently 1HZ. | |
80 | //GPS.sendCommand(PGCMD_ANTENNA); | |
81 | sleepTime = 1800000; //Set sleep time in ms, max sleep time is 49.7 days. Set to 30 minutes | |
82 | delay(1000); | |
83 | mySerial.println(PMTK_Q_RELEASE); // Ask for firmware version | |
84 | } | |
85 | ||
86 | ||
87 | // *************************************** | |
88 | // Main Loop | |
89 | // *************************************** | |
90 | ||
91 | void loop() | |
92 | { | |
93 | digitalWrite(GPSEnablePin,HIGH); //Ensure gps is enabled at startup for first fix. | |
94 | if (GPS.newNMEAreceived()) { //If a sentence is received, we can check the checksum, parse it. | |
95 | if (!GPS.parse(GPS.lastNMEA())) //This also sets the newNMEAreceived() flag to false | |
96 | return; //We can fail to parse a sentence in which case we should just wait for another. | |
97 | } | |
98 | if (timer > millis()) timer = millis(); //If millis() or timer wraps around, we'll just reset it. | |
99 | if (millis() - timer > 2000) { //Approximately every 2 seconds or so, print out the current stats. | |
100 | timer = millis(); //Reset the timer. | |
101 | // Serial.print("\nTime: "); | |
102 | // Serial.print(GPS.hour, DEC); | |
103 | // Serial.print(':'); | |
104 | // Serial.print(GPS.minute, DEC); | |
105 | // Serial.print(':'); | |
106 | // Serial.print(GPS.seconds, DEC); | |
107 | // Serial.print('.'); | |
108 | // Serial.println(GPS.milliseconds); | |
109 | // Serial.print("Date: "); | |
110 | // Serial.print(GPS.day, DEC); | |
111 | // Serial.print('/'); | |
112 | // Serial.print(GPS.month, DEC); | |
113 | // Serial.print("/20"); | |
114 | // Serial.println(GPS.year, DEC); | |
115 | // Serial.print("Fix: "); | |
116 | // Serial.print((int)GPS.fix); | |
117 | // Serial.print(" quality: "); | |
118 | // Serial.println((int)GPS.fixquality); | |
119 | if (GPS.fix) { | |
120 | // Serial.print("Location: "); | |
121 | // Serial.print(GPS.latitude, 8); | |
122 | // Serial.print(GPS.lat); | |
123 | // Serial.print(", "); | |
124 | // Serial.print(GPS.longitude, 8); | |
125 | // Serial.println(GPS.lon); | |
126 | // Serial.print("Speed (knots): "); | |
127 | // Serial.println(GPS.speed); | |
128 | // Serial.print("Angle: "); | |
129 | // Serial.println(GPS.angle); | |
130 | // Serial.print("Altitude: "); | |
131 | // Serial.println(GPS.altitude); | |
132 | // Serial.print("Satellites: "); | |
133 | // Serial.println((int)GPS.satellites); | |
134 | // Serial.print("Google Maps Link: https://maps.google.com/maps?q="); | |
135 | // Serial.println(gps2string((String) GPS.lat,GPS.latitude,(String) GPS.lon,GPS.longitude)); | |
136 | } | |
137 | ||
138 | delay(100); //Delay to allow serial to fully print. | |
139 | ||
140 | if (GPS.fix && GPS.fixquality) { //If we have a good fix and good fix quality then we record and shut down. | |
141 | //Serial.println("WE GOT A QUALITY FIX!"); | |
142 | //Serial.println("Recording data..."); | |
143 | //Serial.print("sleeping for "); | |
144 | //Serial.println(sleepTime); | |
145 | //Serial.print("Counter: "); | |
146 | //Serial.println(fix_try_counter); | |
147 | fix_try_counter=0; //Reset our fix counter ready for next run. | |
148 | delay(100); //Delay to allow serial to fully print before sleep. | |
149 | digitalWrite(GPSEnablePin,LOW); //Ensure gps is disabled. | |
150 | Narcoleptic.delay(sleepTime); | |
151 | } | |
152 | else | |
153 | { | |
154 | if (fix_try_counter > 60) //We will only allow the code to execute for 2 minutes to get a fix, if not then we will sleep and try again next time. | |
155 | { | |
156 | //Serial.print("Couldnt get a fix so sleeping for "); | |
157 | //Serial.println(sleepTime); | |
158 | digitalWrite(GPSEnablePin,LOW); //Ensure gps is disabled. | |
159 | //Serial.print("Counter: "); | |
160 | //Serial.println(fix_try_counter); | |
161 | fix_try_counter=0; //Reset our fix try counter. | |
162 | delay(100); //Delay to allow serial to fully print before sleep. | |
163 | Narcoleptic.delay(sleepTime); | |
164 | } | |
165 | //Serial.print("Counter: "); | |
166 | //Serial.println(fix_try_counter); | |
167 | fix_try_counter = fix_try_counter +1; //Fallen through all checks so run loop again and increment our counter. | |
168 | } | |
169 | } | |
170 | } | |
171 | ||
172 | ||
173 | // *************************************** | |
174 | // Custom Functions | |
175 | // *************************************** | |
176 | ||
177 | // returns a string of length n (fixed-width) | |
178 | String int2fw (int x, int n) { | |
179 | String s = (String) x; | |
180 | while (s.length() < n) { | |
181 | s = "0" + s; | |
182 | } | |
183 | return s; | |
184 | } | |
185 | ||
186 | // returns "Ndd mm.mmm, Wddd mm.mmm"; | |
187 | String gps2string (String lat, float latitude, String lon, float longitude) { | |
188 | ||
189 | int dd = (int) latitude/100; | |
190 | int mm = (int) latitude % 100; | |
191 | int mmm = (int) round(1000 * (latitude - floor(latitude))); | |
192 | String gps2lat = lat + int2fw(dd, 2) + " " + int2fw(mm, 2) + "." + int2fw(mmm, 3); | |
193 | dd = (int) longitude/100; | |
194 | mm = (int) longitude % 100; | |
195 | mmm = (int) round(1000 * (longitude - floor(longitude))); | |
196 | String gps2lon = lon + int2fw(dd, 3) + " " + int2fw(mm, 2) + "." + int2fw(mmm, 3); | |
197 | String myString = gps2lat + ", " + gps2lon; | |
198 | return myString; | |
199 | - | }; |
199 | + | }; |