safwan092

Project_10746_Code

Feb 1st, 2023
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. #include <TinyGPSPlus.h>
  2. #include <WiFi.h>
  3. #include <FirebaseESP32.h>
  4. #include <addons/TokenHelper.h>
  5. #include <addons/RTDBHelper.h>
  6.  
  7. #define seat_no_1 36
  8. #define seat_no_2 39
  9. #define seat_no_3 34
  10. #define seat_no_4 35
  11. #define seat_no_5 32
  12. #define seat_no_6 33
  13.  
  14. int seat_no_1_State;
  15. int seat_no_2_State;
  16. int seat_no_3_State;
  17. int seat_no_4_State;
  18. int seat_no_5_State;
  19. int seat_no_6_State;
  20.  
  21. #define WIFI_SSID "network"
  22. #define WIFI_PASSWORD "123456789"
  23. #define API_KEY "AIzaSyAEPYl4Pgt9mA1LDMPObokuEdigGTGLz2Q"
  24. #define USER_EMAIL "[email protected]"
  25. #define USER_PASSWORD "123456789"
  26. #define DATABASE_URL "esp32-project-9ae45-default-rtdb.firebaseio.com"
  27. #define DATABASE_SECRET "h4EezjpVFqGVowB3a2Y8E0XX0CUCzSQJegn5nyXV"
  28.  
  29. char google_maps_link[70];
  30. String google_maps_link_string = "";
  31. String Longitude = "1";
  32. String Latitude = "1";
  33.  
  34. FirebaseData fbdo;
  35. FirebaseAuth auth;
  36. FirebaseConfig config;
  37.  
  38. unsigned long dataMillis = 0;
  39. int count = 0;
  40.  
  41.  
  42. TinyGPSPlus gps;
  43.  
  44. void setup() {
  45. Serial.begin(9600);
  46. Serial2.begin(9600);
  47. connect_WiFi_and_Firebase();
  48. pinMode(seat_no_1,INPUT);
  49. pinMode(seat_no_2,INPUT);
  50. pinMode(seat_no_3,INPUT);
  51. pinMode(seat_no_4,INPUT);
  52. pinMode(seat_no_5,INPUT);
  53. pinMode(seat_no_6,INPUT);
  54. }
  55.  
  56. void loop() {
  57.  
  58. seat_no_1_State = digitalRead(seat_no_1);
  59. seat_no_2_State = digitalRead(seat_no_2);
  60. seat_no_3_State = digitalRead(seat_no_3);
  61. seat_no_4_State = digitalRead(seat_no_4);
  62. seat_no_5_State = digitalRead(seat_no_5);
  63. seat_no_6_State = digitalRead(seat_no_6);
  64.  
  65. read_GPS_receiver();
  66. send_data_Firebase();
  67. }
  68.  
  69. void read_GPS_receiver() {
  70. while (Serial2.available() > 0) {
  71. if (gps.encode(Serial2.read())) {
  72. if (gps.location.isValid()) {
  73. Latitude = String(gps.location.lat(), 6);
  74. Longitude = String(gps.location.lng(), 6);
  75. Serial.print(Latitude);
  76. Serial.print(",");
  77. Serial.print(Longitude);
  78. Serial.println();
  79. google_maps_link_string = "http://maps.google.com/maps?&z=15&mrt=yp&t=k&q=" + Latitude + "+" + Longitude;
  80. google_maps_link_string.toCharArray(google_maps_link, 70);
  81. }
  82. else {
  83. Serial.println(F("INVALID"));
  84. }
  85. }
  86. }
  87. if (millis() > 5000 && gps.charsProcessed() < 10) {
  88. Serial.println(F("No GPS detected: check wiring."));
  89. while (true);
  90. }
  91. }
  92.  
  93. void connect_WiFi_and_Firebase() {
  94. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  95. Serial.print("Connecting to Wi-Fi");
  96. while (WiFi.status() != WL_CONNECTED)
  97. {
  98. Serial.print(".");
  99. delay(300);
  100. }
  101. Serial.println();
  102. Serial.print("Connected with IP: ");
  103. Serial.println(WiFi.localIP());
  104. Serial.println();
  105. Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
  106. config.api_key = API_KEY;
  107. auth.user.email = USER_EMAIL;
  108. auth.user.password = USER_PASSWORD;
  109. config.database_url = DATABASE_URL;
  110. Firebase.reconnectWiFi(true);
  111. fbdo.setResponseSize(4096);
  112. config.token_status_callback = tokenStatusCallback;
  113. config.max_token_generation_retry = 5;
  114. Firebase.begin(&config, &auth);
  115. }
  116.  
  117.  
  118. void send_data_Firebase() {
  119. if (millis() - dataMillis > 5000 && Firebase.ready())
  120. {
  121. dataMillis = millis();
  122.  
  123. Serial.printf("Set String... %s\n", Firebase.setString(fbdo, "/Golf_Cart/Latitude/",Latitude.c_str()) ? "ok" : fbdo.errorReason().c_str());
  124. Serial.printf("Set String... %s\n", Firebase.setString(fbdo, "/Golf_Cart/Longitude/",Longitude.c_str()) ? "ok" : fbdo.errorReason().c_str());
  125. Serial.printf("Set Bool... %s\n", Firebase.setInt(fbdo, "/Golf_Cart/Seat_No_1/", seat_no_1_State) ? "ok" : fbdo.errorReason().c_str());
  126. Serial.printf("Set Bool... %s\n", Firebase.setInt(fbdo, "/Golf_Cart/Seat_No_2/", seat_no_2_State) ? "ok" : fbdo.errorReason().c_str());
  127. Serial.printf("Set Bool... %s\n", Firebase.setInt(fbdo, "/Golf_Cart/Seat_No_3/", seat_no_3_State) ? "ok" : fbdo.errorReason().c_str());
  128. Serial.printf("Set Bool... %s\n", Firebase.setInt(fbdo, "/Golf_Cart/Seat_No_4/", seat_no_4_State) ? "ok" : fbdo.errorReason().c_str());
  129. Serial.printf("Set Bool... %s\n", Firebase.setInt(fbdo, "/Golf_Cart/Seat_No_5/", seat_no_5_State) ? "ok" : fbdo.errorReason().c_str());
  130. Serial.printf("Set Bool... %s\n", Firebase.setInt(fbdo, "/Golf_Cart/Seat_No_6/", seat_no_6_State) ? "ok" : fbdo.errorReason().c_str());
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment