Advertisement
Guest User

erek

a guest
Oct 7th, 2015
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.55 KB | None | 0 0
  1.  
  2. // GPS-based Location Tracker and GSM Transmitter
  3. // by erek (2015)
  4.  
  5. #include <Adafruit_GPS.h>
  6. #include <AltSoftSerial.h>
  7.  
  8. // Include the GSM library
  9. #include <GSM.h>
  10.  
  11. #define PINNUMBER ""
  12.  
  13. // initialize the library instance
  14. GSM gsmAccess;
  15. GSM_SMS sms;
  16.  
  17.  
  18. // modem verification object
  19. GSMModem modem;
  20.  
  21. // IMEI variable
  22. String IMEI = "";
  23.  
  24.  
  25.  
  26. // If you're using a GPS module:
  27. // Connect the GPS Power pin to 5V
  28. // Connect the GPS Ground pin to ground
  29. // If using software serial (sketch example default):
  30. //   Connect the GPS TX (transmit) pin to Digital 3
  31. //   Connect the GPS RX (receive) pin to Digital 2
  32. // If using hardware serial (e.g. Arduino Mega):
  33. //   Connect the GPS TX (transmit) pin to Arduino RX1, RX2 or RX3
  34. //   Connect the GPS RX (receive) pin to matching TX1, TX2 or TX3
  35.  
  36. // If you're using the Adafruit GPS shield, change
  37. // AltSoftSerial mySerial(3, 2); -> AltSoftSerial mySerial(8, 7);
  38. // and make sure the switch is set to SoftSerial
  39.  
  40. // If using software serial, keep this line enabled
  41. // (you can change the pin numbers to match your wiring):
  42. AltSoftSerial mySerial(3, 2);
  43.  
  44. // If using hardware serial (e.g. Arduino Mega), comment out the
  45. // above AltSoftSerial line, and enable this line instead
  46. // (you can change the Serial number to match your wiring):
  47.  
  48. //HardwareSerial mySerial = Serial1;
  49.  
  50.  
  51. Adafruit_GPS GPS(&mySerial);
  52.  
  53.  
  54. // Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
  55. // Set to 'true' if you want to debug and listen to the raw GPS sentences.
  56. #define GPSECHO  true
  57.  
  58. // this keeps track of whether we're using the interrupt
  59. // off by default!
  60. boolean usingInterrupt = false;
  61. void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy
  62.  
  63. void setup()  
  64. {
  65.    
  66.  
  67.  
  68.   // connect at 115200 so we can read the GPS fast enough and echo without dropping chars
  69.   // also spit it out
  70.   Serial.begin(115200);
  71. //Serial.begin(9600);
  72.  
  73.  
  74.   modem.begin();
  75.  
  76.  
  77.   // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
  78.  
  79.   GPS.begin(9600);
  80.  
  81.   // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
  82.   GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  83.   // uncomment this line to turn on only the "minimum recommended" data
  84.   //GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
  85.   // For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since
  86.   // the parser doesn't care about other sentences at this time
  87.  
  88.   // Set the update rate
  89.   GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);   // 1 Hz update rate
  90.   // For the parsing code to work nicely and have time to sort thru the data, and
  91.   // print it out we don't suggest using anything higher than 1 Hz
  92.  
  93.   // Request updates on antenna status, comment out to keep quiet
  94.   GPS.sendCommand(PGCMD_ANTENNA);
  95.  
  96.   // the nice thing about this code is you can have a timer0 interrupt go off
  97.   // every 1 millisecond, and read data from the GPS for you. that makes the
  98.   // loop code a heck of a lot easier!
  99.   useInterrupt(true);
  100.  
  101.   delay(1000);
  102.   // Ask for firmware version
  103.   mySerial.println(PMTK_Q_RELEASE);
  104. }
  105.  
  106.  
  107. // Interrupt is called once a millisecond, looks for any new GPS data, and stores it
  108. SIGNAL(TIMER0_COMPA_vect) {
  109.   char c = GPS.read();
  110.   // if you want to debug, this is a good time to do it!
  111. #ifdef UDR0
  112.   if (GPSECHO)
  113.     if (c) UDR0 = c;  
  114.     // writing direct to UDR0 is much much faster than Serial.print
  115.     // but only one character can be written at a time.
  116. #endif
  117. }
  118.  
  119. void useInterrupt(boolean v) {
  120.   if (v) {
  121.     // Timer0 is already used for millis() - we'll just interrupt somewhere
  122.     // in the middle and call the "Compare A" function above
  123.     OCR0A = 0xAF;
  124.     TIMSK0 |= _BV(OCIE0A);
  125.     usingInterrupt = true;
  126.   } else {
  127.     // do not call the interrupt function COMPA anymore
  128.     TIMSK0 &= ~_BV(OCIE0A);
  129.     usingInterrupt = false;
  130.   }
  131.  
  132.  
  133. }
  134.  
  135. uint32_t timer = millis();
  136.  
  137.  
  138. void loop()                     // run over and over again
  139. {
  140.     IMEI = modem.getIMEI();
  141.  
  142.     // show IMEI in serial monitor
  143.     Serial.println("Modem's IMEI: " + IMEI);
  144.  
  145.  
  146.  
  147.  
  148.   // in case you are not using the interrupt above, you'll
  149.   // need to 'hand query' the GPS, not suggested :(
  150.   if (! usingInterrupt) {
  151.     // read data from the GPS in the 'main loop'
  152.     char c = GPS.read();
  153.     // if you want to debug, this is a good time to do it!
  154.     if (GPSECHO)
  155.       if (c) Serial.print(c);
  156.   }
  157.  
  158.    
  159.   // if a sentence is received, we can check the checksum, parse it...
  160.   if (GPS.newNMEAreceived()) {
  161.     // a tricky thing here is if we print the NMEA sentence, or data
  162.     // we end up not listening and catching other sentences!
  163.     // so be very wary if using OUTPUT_ALLDATA and trytng to print out data
  164.     //Serial.println(GPS.lastNMEA());   // this also sets the newNMEAreceived() flag to false
  165.  
  166.     if (!GPS.parse(GPS.lastNMEA()))   // this also sets the newNMEAreceived() flag to false
  167.       return;  // we can fail to parse a sentence in which case we should just wait for another
  168.   }
  169.  
  170.    
  171.  
  172.   // if millis() or timer wraps around, we'll just reset it
  173.   if (timer > millis())  timer = millis();
  174.  
  175.   // approximately every 2 seconds or so, print out the current stats
  176.   if (millis() - timer > 2000) {
  177.     timer = millis(); // reset the timer
  178.  
  179.     Serial.println("\nTime: ");
  180.  
  181.  
  182.     Serial.print(GPS.hour, DEC); Serial.print(':');
  183.     Serial.print(GPS.minute, DEC); Serial.print(':');
  184.     Serial.print(GPS.seconds, DEC); Serial.print('.');
  185.     Serial.println(GPS.milliseconds);
  186.     Serial.print("Date: ");
  187.     Serial.print(GPS.day, DEC); Serial.print('/');
  188.     Serial.print(GPS.month, DEC); Serial.print("/20");
  189.     Serial.println(GPS.year, DEC);
  190.     Serial.print("Fix: "); Serial.print((int)GPS.fix);
  191.     Serial.print(" quality: "); Serial.println((int)GPS.fixquality);
  192.  
  193.    
  194. /**************
  195.     if (GPS.fix) {
  196.       Serial.print("Location: ");
  197.       Serial.print(GPS.latitude, 4); Serial.print(GPS.lat);
  198.       Serial.print(", ");
  199.       Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);
  200.       Serial.print("Location (in degrees, works with Google Maps): ");
  201.       Serial.print(GPS.latitudeDegrees, 4);
  202.       Serial.print(", ");
  203.       Serial.println(GPS.longitudeDegrees, 4);
  204.      
  205.       Serial.print("Speed (knots): "); Serial.println(GPS.speed);
  206.       Serial.print("Angle: "); Serial.println(GPS.angle);
  207.       Serial.print("Altitude: "); Serial.println(GPS.altitude);
  208.       Serial.print("Satellites: "); Serial.println((int)GPS.satellites);
  209.     }
  210.  
  211.    
  212. **************/
  213.   }
  214.  
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement