ronny_azazel

Untitled

Jan 16th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SoftwareSerial.h>
  2. #include <TinyGPS++.h>
  3.  
  4. SoftwareSerial simSerial(5, 6);
  5. SoftwareSerial gpsSerial(4, 3); // RX, TX
  6. TinyGPSPlus gps;
  7.  
  8. char incomingByte;
  9. String inputString;
  10. String phone;
  11. double latitude, longitude;
  12. String link = "GPS masih proses";
  13.  
  14. //long interval = 3000; // the time we need to wait
  15. //long previousMillis = 0; // millis() returns an unsigned long.
  16.  
  17. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  18. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  19. void setup() {
  20.   Serial.begin(9600);
  21.  
  22.   gpsSerial.begin(9600);
  23.   simSerial.begin(9600);
  24.  
  25.   simSerial.listen();
  26.  
  27.   while (!simSerial.available()) {
  28.     simSerial.println("AT");
  29.     delay(1000);
  30.   }
  31.  
  32.   simSerial.println("AT+CMGF=1");  //Set SMS to Text Mode
  33.   simSerial.println("AT+CNMI=1,2,0,0,0");  //Procedure to handle newly arrived messages(command name in text: new message indications to TE)
  34.   simSerial.println("AT+CMGL=\"REC UNREAD\""); // Read Unread Messages
  35.   delay(1000);
  36.   simSerial.println("AT+CMGDA=\"DEL ALL\"");
  37.   delay(1000);
  38.  
  39. }
  40. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  41. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  42. void loop() {
  43.   /*//Loop Read GPS
  44.       gpsSerial.listen();
  45.  
  46.     while (gpsSerial.available()) {
  47.       gps.encode(gpsSerial.read());
  48.  
  49.       if (gps.location.isUpdated()) {
  50.         latitude = gps.location.lat();
  51.         longitude = gps.location.lng();
  52.         link = "www.google.com/maps/place/" + String(latitude, 6) + "," + String(longitude, 6);
  53.         Serial.println(link);
  54.       }
  55.     }*/
  56.  
  57.   if (simSerial.available()) {
  58.     delay(100);
  59.     while (simSerial.available()) {
  60.       incomingByte = simSerial.read();
  61.       inputString += incomingByte;
  62.     }
  63.  
  64.     delay(10);
  65.     Serial.println(inputString);
  66.  
  67.     if (inputString.indexOf("gps") > -1) {
  68.       if (inputString.indexOf("CMT:") > 0) {
  69.         phone = inputString.substring(inputString.indexOf("+CMT: ") + 7,  inputString.indexOf(",") - 1);
  70.       }
  71.       gpsLink();
  72.       delay(1000);
  73.       sendSMS();
  74.       delay(1000);
  75.       simSerial.println("AT+CMGDA=\"DEL ALL\"");//Delete Messages & Save Memory
  76.     }
  77.     inputString = "";
  78.   }
  79.  
  80. }
  81. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  82. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  83. void sendSMS() {
  84.   simSerial.listen();
  85.   Serial.println("Sending message........");
  86.   simSerial.println("AT+CMGS=\"" + phone + "\"\r"); //Mobile phone number to send message
  87.   delay(1000);
  88.   simSerial.println(link);
  89.   delay(1000);
  90.   simSerial.println((char)26);// ASCII code of CTRL+Z
  91. }
  92. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  93. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  94. void gpsLink() {
  95.   gpsSerial.listen();
  96.  
  97.   while (gpsSerial.available()) {
  98.     gps.encode(gpsSerial.read());
  99.  
  100.     if (gps.location.isUpdated()) {
  101.       latitude = gps.location.lat();
  102.       longitude = gps.location.lng();
  103.       link = "www.google.com/maps/place/" + String(latitude, 6) + "," + String(longitude, 6);
  104.       Serial.println(link);
  105.     }
  106.   }
  107. }
  108. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  109. ////////////////////////////////////////////////////////////////////////////////////////////////////////
Add Comment
Please, Sign In to add comment