Advertisement
banyucenter

Untitled

Jan 3rd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.70 KB | None | 0 0
  1. #include <TinyGPS++.h>
  2. #include <SoftwareSerial.h>
  3. #include <ESP8266WiFi.h>
  4. #include <FirebaseArduino.h>
  5.  
  6. /*
  7.    This sample sketch demonstrates the normal use of a TinyGPS++ (TinyGPSPlus) object.
  8.    It requires the use of SoftwareSerial, and assumes that you have a
  9.    4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
  10. */
  11. static const int RXPin = 6, TXPin = 7;
  12. static const uint32_t GPSBaud = 9600;
  13.  
  14. // repace your wifi username and password
  15. // repace your wifi username and password
  16. const char* ssid     = "Banyustudio";
  17. const char* password = "4llahMahaKaya9900*#";
  18.  
  19.  
  20. #define FIREBASE_HOST "landslidepad-6c940.firebaseio.com"
  21. #define FIREBASE_AUTH "c7dsqqiinizfMQAkkqBIMfqkmQ9iskIhaDiiEC1f"
  22.  
  23.  
  24. // The TinyGPS++ object
  25. TinyGPSPlus gps;
  26. WiFiClient  client;
  27.  
  28. // The serial connection to the GPS device
  29. SoftwareSerial ss(RXPin, TXPin);
  30.  
  31. void setup()
  32. {
  33.   Serial.begin(115200);
  34.   ss.begin(GPSBaud);
  35.   Serial.println(F(":App Started"));
  36.   Serial.println(F("Realtime GPS data IoT"));
  37.   Serial.print(F("GPS Started"));
  38.   Serial.println(TinyGPSPlus::libraryVersion());
  39.   Serial.println();
  40.  
  41.   Serial.print("Connecting to ");
  42.   Serial.println(ssid);
  43.   WiFi.begin(ssid, password);
  44.   while (WiFi.status() != WL_CONNECTED) {
  45.     delay(500);
  46.     Serial.print(".");
  47.   }
  48.    Serial.println("");
  49.   Serial.println("WiFi connected");  
  50.   Serial.println("IP address: ");
  51.   Serial.println(WiFi.localIP());
  52.   Serial.print("Netmask: ");
  53.   Serial.println(WiFi.subnetMask());
  54.   Serial.print("Gateway: ");
  55.   Serial.println(WiFi.gatewayIP());
  56.   Firebase.begin(FIREBASE_HOST,FIREBASE_AUTH);
  57.  
  58.  
  59. }
  60.  
  61. void loop()
  62. {
  63.   // This sketch displays information every time a new sentence is correctly encoded.
  64.  
  65.  
  66.       displayInfo();
  67.       yield();
  68.       smartDelay(5000);
  69.  
  70. }
  71.  
  72. static void smartDelay(unsigned long ms) {               // This custom version of delay() ensures that the gps object is being "fed".
  73.   unsigned long start = millis();
  74.   do {
  75.     while (ss.available())
  76.       gps.encode(ss.read());
  77.   } while (millis() - start < ms);
  78. }
  79.  
  80. void displayInfo()
  81. {
  82. // Serial.print(F("Location: "));
  83.   if (gps.location.isValid())
  84.   {
  85.  
  86.     //set default lat lng
  87.     const double TOWER_LAT = -7.441299;
  88.     const double TOWER_LNG = 109.211704;
  89.  
  90.     double latitude = (gps.location.lat());
  91.     double longitude = (gps.location.lng());
  92.  
  93.     //get distance
  94.     double distanceM = gps.distanceBetween(TOWER_LAT,TOWER_LNG,latitude,longitude)/10;
  95.  
  96.     Serial.print("Jarak titik: ");
  97.     Serial.println(distanceM);
  98.     Serial.print(" meter");
  99.     Serial.println("");
  100.    
  101.     String latbuf;
  102.     latbuf += (String(latitude, 6));
  103.     Serial.print("lat : ");
  104.     Serial.print(latbuf);
  105.  
  106.     Serial.println();
  107.  
  108.     String lonbuf;
  109.     lonbuf += (String(longitude, 6));
  110.     Serial.print("lon : ");
  111.     Serial.print(lonbuf);Serial.println();
  112.  
  113.     if (Firebase.failed()) {
  114.       Serial.println("Firebase get failed");
  115.       Serial.println(Firebase.error());
  116.       return;
  117.     }else {
  118.       Firebase.setFloat("Lat",latitude);
  119.     Serial.println("mengirim data latitude : ");
  120.     Serial.println();
  121.     Firebase.setFloat("Lon",longitude);
  122.     Serial.println("mengirim data longitude : ");
  123.     Firebase.setFloat("Distance",distanceM);
  124.     Serial.println("mengirim data distance : ");
  125.  
  126.     if(distanceM >= 0.3){
  127.       Serial.println("status bahaya : ");
  128.       Firebase.set("Status","Bahaya");
  129.     }else if(distanceM >= 0.2){
  130.       Serial.println("status waspada : ");
  131.       Firebase.set("Status","Waspada");
  132.     }
  133.     else if(distanceM >= 0.1){
  134.       Serial.println("status hati-hati : ");
  135.       Firebase.set("Status","Hati-hati");
  136.     }else {
  137.       Serial.println("status aman : ");
  138.       Firebase.set("Status","Aman");
  139.     }
  140.     }
  141.  
  142.     delay(5000);
  143.    
  144.   }
  145.   else
  146.   {
  147.     Serial.print(F("INVALID"));
  148.   }
  149.  
  150.   Serial.print(F("  Date/Time: "));
  151.   if (gps.date.isValid())
  152.   {
  153.     Serial.print(gps.date.month());
  154.     Serial.print(F("/"));
  155.     Serial.print(gps.date.day());
  156.     Serial.print(F("/"));
  157.     Serial.print(gps.date.year());
  158.   }
  159.   else
  160.   {
  161.     Serial.print(F("INVALID"));
  162.   }
  163.  
  164.   Serial.print(F(" "));
  165.   if (gps.time.isValid())
  166.   {
  167.     if (gps.time.hour() < 10) Serial.print(F("0"));
  168.     Serial.print(gps.time.hour());
  169.     Serial.print(F(":"));
  170.     if (gps.time.minute() < 10) Serial.print(F("0"));
  171.     Serial.print(gps.time.minute());
  172.     Serial.print(F(":"));
  173.     if (gps.time.second() < 10) Serial.print(F("0"));
  174.     Serial.print(gps.time.second());
  175.     Serial.print(F("."));
  176.     if (gps.time.centisecond() < 10) Serial.print(F("0"));
  177.     Serial.print(gps.time.centisecond());
  178.   }
  179.   else
  180.   {
  181.     Serial.print(F("INVALID"));
  182.   }
  183.  
  184.   Serial.println();
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement