Advertisement
safwan092

Untitled

Nov 23rd, 2023
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. #include "DHT.h"
  2. #include <Arduino.h>
  3. #include <WiFi.h>
  4. #include <Firebase_ESP_Client.h>
  5. #include <TinyGPSPlus.h>
  6. #include "time.h"
  7. #include "addons/TokenHelper.h"
  8. #include "addons/RTDBHelper.h"
  9.  
  10. #define WIFI_SSID "Saud"
  11. #define WIFI_PASSWORD "saud112233"
  12. #define API_KEY "AIzaSyADNmTRkiRXx1lV0lnK3uH6z-BAfjG8NrY"
  13. #define USER_EMAIL "MedicalTruckSafety@gmail.com"
  14. #define USER_PASSWORD "123456789"
  15. #define DATABASE_URL "medicaltrucksafety-32e85-default-rtdb.firebaseio.com"
  16. FirebaseData fbdo;
  17. FirebaseAuth auth;
  18. FirebaseConfig config;
  19. FirebaseJson json;
  20.  
  21. TinyGPSPlus gps;
  22. String stringURL = "";
  23. String gpsSpeed;
  24. String gpsLon;
  25. String gpsLat;
  26. String databasePath;
  27. String uid;
  28. String parentPath;
  29. String speedPath = "/Speed";
  30. String mapLatPath = "/Lat";
  31. String mapLonPath = "/Lon";
  32.  
  33. unsigned long dataMillis = 0;
  34.  
  35. void setup() {
  36.  
  37. Setup_Sensors_And_Connect_To_WiFi_And_Firebase();
  38. delay(5000);
  39. }
  40.  
  41. void loop() {
  42. GPS_Modem_Read();
  43. ////////////////////////////////////////////////////////////////////////////
  44. stringURL = "http://www.google.com/maps/place/" + gpsLat + "," + gpsLon;
  45. ////////////////////////////////////////////////////////////////////////////
  46.  
  47. if (millis() - dataMillis > 5000 && Firebase.ready())
  48. {
  49. dataMillis = millis();
  50. parentPath = databasePath;
  51.  
  52. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  53. json.set(speedPath.c_str() , String(gpsSpeed));
  54. json.set(mapLatPath.c_str() , String(gpsLat));
  55. json.set(mapLonPath.c_str() , String(gpsLon));
  56. Serial.printf("Set json... %s\n", Firebase.RTDB.setJSON(&fbdo, parentPath.c_str(), &json) ? "ok" : fbdo.errorReason().c_str());
  57. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  58.  
  59. }
  60. }
  61.  
  62.  
  63.  
  64. void Setup_Sensors_And_Connect_To_WiFi_And_Firebase() {
  65. Serial.begin(115200);
  66. Serial2.begin(9600);//Serial Channel for GPS with ESP32 [TX2+RX2]
  67.  
  68. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  69. Serial.print("Connecting to WiFi ..");
  70. while (WiFi.status() != WL_CONNECTED) {
  71. Serial.print('.');
  72. delay(1000);
  73. }
  74. Serial.println(WiFi.localIP());
  75. Serial.println();
  76. // Assign the api key (required)
  77. config.api_key = API_KEY;
  78.  
  79. // Assign the user sign in credentials
  80. auth.user.email = USER_EMAIL;
  81. auth.user.password = USER_PASSWORD;
  82.  
  83. // Assign the RTDB URL (required)
  84. config.database_url = DATABASE_URL;
  85.  
  86. Firebase.reconnectWiFi(true);
  87. fbdo.setResponseSize(4096);
  88.  
  89. // Assign the callback function for the long running token generation task */
  90. config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h
  91.  
  92. // Assign the maximum retry of token generation
  93. config.max_token_generation_retry = 5;
  94.  
  95. // Initialize the library with the Firebase authen and config
  96. Firebase.begin(&config, &auth);
  97.  
  98. // Getting the user UID might take a few seconds
  99. Serial.println("Getting User UID");
  100. while ((auth.token.uid) == "") {
  101. Serial.print('.');
  102. delay(1000);
  103. }
  104. // Print user UID
  105. uid = auth.token.uid.c_str();
  106. Serial.print("User UID: ");
  107. Serial.println(uid);
  108.  
  109. // Update database path
  110. databasePath ="/Data";
  111.  
  112. }
  113.  
  114.  
  115.  
  116.  
  117. void GPS_Modem_Read() {
  118. if (Serial2.available() > 0) {
  119. if (gps.encode(Serial2.read())) {
  120. if (gps.location.isValid()) {
  121. float gpslat_float = gps.location.lat();
  122. gpsLat = String(gpslat_float, 6);
  123. Serial.print(F("- latitude: "));
  124. Serial.println(gpsLat);
  125. float gpslon_float = gps.location.lng();
  126. gpsLon = String(gpslon_float, 6);
  127. Serial.print(F("- longitude: "));
  128. Serial.println(gpsLon);
  129. } else {
  130. Serial.println(F("- location: INVALID"));
  131. }
  132. Serial.print(F("- speed: "));
  133. if (gps.speed.isValid()) {
  134. float speedInKmph = gps.speed.kmph();
  135. gpsSpeed = String(speedInKmph, 2);
  136. Serial.print(gpsSpeed);
  137. Serial.println(F(" km/h"));
  138. } else {
  139. Serial.println(F("INVALID"));
  140. }
  141. Serial.println();
  142. }
  143. }
  144. if (millis() > 5000 && gps.charsProcessed() < 10)
  145. Serial.println(F("No GPS data received: check wiring"));
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement