Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ***************************************
- // Notes & Comments:
- // Code for GPS tracking with sleep functionality and logging.
- // Design Notes:
- // you could use the locus logger instead of the shield... and then send from the locus to the gprs
- // ***************************************
- // ***************************************
- // Library Inclusions
- // ***************************************
- //#include <Sleep_n0m1.h> //Sleep library
- #include <Narcoleptic.h> //New sleep library
- #include <math.h> //Math function library
- #include <Adafruit_GPS.h> //Ultimate GPS breakout library
- #include <SoftwareSerial.h> //Software serial library
- // ***************************************
- // Initiate variables and vlaues
- // ***************************************
- SoftwareSerial mySerial(2, 3); //Software serial setup on pin 2 and 3
- Adafruit_GPS GPS(&mySerial); //Setup connection as gps
- //Sleep sleep; //Define sleep
- unsigned long sleepTime; //Define sleepTime to hold millisecond of sleep required
- #define GPSECHO true //Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
- 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
- boolean usingInterrupt = false; //Initiate true/false value against interrupt. Set to false initially so no floating
- void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy
- int fix_try_counter; //Initiate counter to hold value for number of fix retries
- uint32_t timer = millis(); //Set our timer to be linked to millis()
- // ***************************************
- // Setup Interrupt
- // Interrupt is called once a millisecond, looks for any new GPS data, and stores it
- // ***************************************
- SIGNAL(TIMER0_COMPA_vect) {
- char c = GPS.read();
- }
- void useInterrupt(boolean v) {
- if (v) {
- // Timer0 is already used for millis() - we'll just interrupt somewhere
- // in the middle and call the "Compare A" function above
- OCR0A = 0xAF; //Timer 0 OCR0A Register for low threshold - 175
- TIMSK0 |= _BV(OCIE0A); //TiIMSK0 Timer/Counter Interrupt Mask Register Counter 0 Compare OCROA(TIMER0_COMPA_vect)
- usingInterrupt = true;
- }
- else {
- // do not call the interrupt function COMPA anymore
- TIMSK0 &= ~_BV(OCIE0A);
- usingInterrupt = false;
- }
- }
- // ***************************************
- // Setup script
- // ***************************************
- void setup()
- {
- //Some power saving attempts
- //End of saving attempts
- pinMode(GPSEnablePin, OUTPUT); //Sets the enable pin as output to allow voltage.
- digitalWrite(GPSEnablePin,HIGH); //Ensure GPS enabled.
- fix_try_counter=0; //start the counter from 0.
- useInterrupt(true); //We want to use interrupt for best capture of strings from GPS.
- [b] //Serial.begin(115200); //Connect at 115200 so we can read the GPS fast enough and echo without dropping chars.
- //Serial.println("Car Tracker Alpha");[/b]
- GPS.begin(9600); //9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800.
- GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); //Define output data required.
- GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); //Define update rate of GPS which is currently 1HZ.
- //GPS.sendCommand(PGCMD_ANTENNA);
- sleepTime = 1800000; //Set sleep time in ms, max sleep time is 49.7 days. Set to 30 minutes
- delay(1000);
- mySerial.println(PMTK_Q_RELEASE); // Ask for firmware version
- }
- // ***************************************
- // Main Loop
- // ***************************************
- void loop()
- {
- digitalWrite(GPSEnablePin,HIGH); //Ensure gps is enabled at startup for first fix.
- if (GPS.newNMEAreceived()) { //If a sentence is received, we can check the checksum, parse it.
- if (!GPS.parse(GPS.lastNMEA())) //This also sets the newNMEAreceived() flag to false
- return; //We can fail to parse a sentence in which case we should just wait for another.
- }
- if (timer > millis()) timer = millis(); //If millis() or timer wraps around, we'll just reset it.
- if (millis() - timer > 2000) { //Approximately every 2 seconds or so, print out the current stats.
- timer = millis(); //Reset the timer.
- // Serial.print("\nTime: ");
- // Serial.print(GPS.hour, DEC);
- // Serial.print(':');
- // Serial.print(GPS.minute, DEC);
- // Serial.print(':');
- // Serial.print(GPS.seconds, DEC);
- // Serial.print('.');
- // Serial.println(GPS.milliseconds);
- // Serial.print("Date: ");
- // Serial.print(GPS.day, DEC);
- // Serial.print('/');
- // Serial.print(GPS.month, DEC);
- // Serial.print("/20");
- // Serial.println(GPS.year, DEC);
- // Serial.print("Fix: ");
- // Serial.print((int)GPS.fix);
- // Serial.print(" quality: ");
- // Serial.println((int)GPS.fixquality);
- if (GPS.fix) {
- // Serial.print("Location: ");
- // Serial.print(GPS.latitude, 8);
- // Serial.print(GPS.lat);
- // Serial.print(", ");
- // Serial.print(GPS.longitude, 8);
- // Serial.println(GPS.lon);
- // Serial.print("Speed (knots): ");
- // Serial.println(GPS.speed);
- // Serial.print("Angle: ");
- // Serial.println(GPS.angle);
- // Serial.print("Altitude: ");
- // Serial.println(GPS.altitude);
- // Serial.print("Satellites: ");
- // Serial.println((int)GPS.satellites);
- // Serial.print("Google Maps Link: https://maps.google.com/maps?q=");
- // Serial.println(gps2string((String) GPS.lat,GPS.latitude,(String) GPS.lon,GPS.longitude));
- }
- delay(100); //Delay to allow serial to fully print.
- if (GPS.fix && GPS.fixquality) { //If we have a good fix and good fix quality then we record and shut down.
- //Serial.println("WE GOT A QUALITY FIX!");
- //Serial.println("Recording data...");
- //Serial.print("sleeping for ");
- //Serial.println(sleepTime);
- //Serial.print("Counter: ");
- //Serial.println(fix_try_counter);
- fix_try_counter=0; //Reset our fix counter ready for next run.
- delay(100); //Delay to allow serial to fully print before sleep.
- digitalWrite(GPSEnablePin,LOW); //Ensure gps is disabled.
- Narcoleptic.delay(sleepTime);
- }
- else
- {
- 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.
- {
- //Serial.print("Couldnt get a fix so sleeping for ");
- //Serial.println(sleepTime);
- digitalWrite(GPSEnablePin,LOW); //Ensure gps is disabled.
- //Serial.print("Counter: ");
- //Serial.println(fix_try_counter);
- fix_try_counter=0; //Reset our fix try counter.
- delay(100); //Delay to allow serial to fully print before sleep.
- Narcoleptic.delay(sleepTime);
- }
- //Serial.print("Counter: ");
- //Serial.println(fix_try_counter);
- fix_try_counter = fix_try_counter +1; //Fallen through all checks so run loop again and increment our counter.
- }
- }
- }
- // ***************************************
- // Custom Functions
- // ***************************************
- // returns a string of length n (fixed-width)
- String int2fw (int x, int n) {
- String s = (String) x;
- while (s.length() < n) {
- s = "0" + s;
- }
- return s;
- }
- // returns "Ndd mm.mmm, Wddd mm.mmm";
- String gps2string (String lat, float latitude, String lon, float longitude) {
- int dd = (int) latitude/100;
- int mm = (int) latitude % 100;
- int mmm = (int) round(1000 * (latitude - floor(latitude)));
- String gps2lat = lat + int2fw(dd, 2) + " " + int2fw(mm, 2) + "." + int2fw(mmm, 3);
- dd = (int) longitude/100;
- mm = (int) longitude % 100;
- mmm = (int) round(1000 * (longitude - floor(longitude)));
- String gps2lon = lon + int2fw(dd, 3) + " " + int2fw(mm, 2) + "." + int2fw(mmm, 3);
- String myString = gps2lat + ", " + gps2lon;
- return myString;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement