safwan092

ESP32_GPS

Feb 2nd, 2023
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. #include <TinyGPSPlus.h>
  2.  
  3. String lon;
  4. String latt;
  5.  
  6. TinyGPSPlus gps;
  7.  
  8. void setup() {
  9. Serial.begin(9600);
  10. Serial2.begin(9600);
  11. delay(2000);
  12. }
  13.  
  14.  
  15. void loop() {
  16. readGPS_Data();
  17. }
  18.  
  19.  
  20. void readGPS_Data() {
  21. while (Serial2.available() > 0)
  22. if (gps.encode(Serial2.read()))
  23. displayInfo();
  24. if (millis() > 5000 && gps.charsProcessed() < 10) {
  25. Serial.println(F("No GPS detected: check wiring."));
  26. while (true);
  27. }
  28. }
  29.  
  30. void displayInfo() {
  31. Serial.print(F("Location: "));
  32. if (gps.location.isValid()) {
  33. lon = String(gps.location.lng(), 6);
  34. latt = String(gps.location.lat(), 6);
  35. Serial.print("Lat: ");
  36. Serial.print(latt);
  37. Serial.print(F(","));
  38. Serial.print("Lng: ");
  39. Serial.print(lon);
  40. Serial.println();
  41. }
  42. else {
  43. Serial.println(F("INVALID"));
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment