Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "DHT.h"
- #include <Arduino.h>
- #include <WiFi.h>
- #include <Firebase_ESP_Client.h>
- #include <TinyGPSPlus.h>
- #include "time.h"
- #include "addons/TokenHelper.h"
- #include "addons/RTDBHelper.h"
- #define WIFI_SSID "Saud"
- #define WIFI_PASSWORD "saud112233"
- #define API_KEY "AIzaSyADNmTRkiRXx1lV0lnK3uH6z-BAfjG8NrY"
- #define USER_EMAIL "MedicalTruckSafety@gmail.com"
- #define USER_PASSWORD "123456789"
- #define DATABASE_URL "medicaltrucksafety-32e85-default-rtdb.firebaseio.com"
- FirebaseData fbdo;
- FirebaseAuth auth;
- FirebaseConfig config;
- FirebaseJson json;
- TinyGPSPlus gps;
- String stringURL = "";
- String gpsSpeed;
- String gpsLon;
- String gpsLat;
- String databasePath;
- String uid;
- String parentPath;
- String speedPath = "/Speed";
- String mapLatPath = "/Lat";
- String mapLonPath = "/Lon";
- unsigned long dataMillis = 0;
- void setup() {
- Setup_Sensors_And_Connect_To_WiFi_And_Firebase();
- delay(5000);
- }
- void loop() {
- GPS_Modem_Read();
- ////////////////////////////////////////////////////////////////////////////
- stringURL = "http://www.google.com/maps/place/" + gpsLat + "," + gpsLon;
- ////////////////////////////////////////////////////////////////////////////
- if (millis() - dataMillis > 5000 && Firebase.ready())
- {
- dataMillis = millis();
- parentPath = databasePath;
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- json.set(speedPath.c_str() , String(gpsSpeed));
- json.set(mapLatPath.c_str() , String(gpsLat));
- json.set(mapLonPath.c_str() , String(gpsLon));
- Serial.printf("Set json... %s\n", Firebase.RTDB.setJSON(&fbdo, parentPath.c_str(), &json) ? "ok" : fbdo.errorReason().c_str());
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- }
- }
- void Setup_Sensors_And_Connect_To_WiFi_And_Firebase() {
- Serial.begin(115200);
- Serial2.begin(9600);//Serial Channel for GPS with ESP32 [TX2+RX2]
- WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
- Serial.print("Connecting to WiFi ..");
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print('.');
- delay(1000);
- }
- Serial.println(WiFi.localIP());
- Serial.println();
- // Assign the api key (required)
- config.api_key = API_KEY;
- // Assign the user sign in credentials
- auth.user.email = USER_EMAIL;
- auth.user.password = USER_PASSWORD;
- // Assign the RTDB URL (required)
- config.database_url = DATABASE_URL;
- Firebase.reconnectWiFi(true);
- fbdo.setResponseSize(4096);
- // Assign the callback function for the long running token generation task */
- config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h
- // Assign the maximum retry of token generation
- config.max_token_generation_retry = 5;
- // Initialize the library with the Firebase authen and config
- Firebase.begin(&config, &auth);
- // Getting the user UID might take a few seconds
- Serial.println("Getting User UID");
- while ((auth.token.uid) == "") {
- Serial.print('.');
- delay(1000);
- }
- // Print user UID
- uid = auth.token.uid.c_str();
- Serial.print("User UID: ");
- Serial.println(uid);
- // Update database path
- databasePath ="/Data";
- }
- void GPS_Modem_Read() {
- if (Serial2.available() > 0) {
- if (gps.encode(Serial2.read())) {
- if (gps.location.isValid()) {
- float gpslat_float = gps.location.lat();
- gpsLat = String(gpslat_float, 6);
- Serial.print(F("- latitude: "));
- Serial.println(gpsLat);
- float gpslon_float = gps.location.lng();
- gpsLon = String(gpslon_float, 6);
- Serial.print(F("- longitude: "));
- Serial.println(gpsLon);
- } else {
- Serial.println(F("- location: INVALID"));
- }
- Serial.print(F("- speed: "));
- if (gps.speed.isValid()) {
- float speedInKmph = gps.speed.kmph();
- gpsSpeed = String(speedInKmph, 2);
- Serial.print(gpsSpeed);
- Serial.println(F(" km/h"));
- } else {
- Serial.println(F("INVALID"));
- }
- Serial.println();
- }
- }
- if (millis() > 5000 && gps.charsProcessed() < 10)
- Serial.println(F("No GPS data received: check wiring"));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement