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 DHTPIN 22
- #define vibration_Sensor_Pin 34
- #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;
- #define DHTTYPE DHT11
- DHT dht(DHTPIN, DHTTYPE);
- TinyGPSPlus gps;
- String stringURL = "";
- String gpsSpeed;
- String gpsLon;
- String gpsLat;
- int vibrationValue = 0;
- float h;
- float t;
- String databasePath;
- String uid;
- String currentDate;
- String currentTime;
- String parentPath;
- String temperaturePath = "/temperature";
- String humidityPath = "/humidity";
- String vibrationPath = "/vibration";
- String speedPath = "/Speed";
- String mapURLPath = "/Location";
- String timePath = "/Time";
- String datePath = "/Date";
- unsigned long dataMillis = 0;
- void setup() {
- Setup_Sensors_And_Connect_To_WiFi_And_Firebase();
- initTime("<+03>-3");
- read_Local_Time_And_Date();
- Serial.println(currentTime);
- Serial.println(currentDate);
- delay(5000);
- }
- void loop() {
- read_Local_Time_And_Date();
- DHT11_Sensor_Read();
- Vibration_Sensor_Read();
- GPS_Modem_Read();
- ////////////////////////////////////////////////////////////////////////////
- stringURL = "http://www.google.com/maps/place/" + gpsLat + "," + gpsLon;
- ////////////////////////////////////////////////////////////////////////////
- if (millis() - dataMillis > 5000 && Firebase.ready())
- {
- dataMillis = millis();
- parentPath = databasePath + "/" + currentDate + "/" + currentTime;
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- json.set(temperaturePath.c_str() , String(t));
- json.set(humidityPath.c_str() , String(h));
- json.set(vibrationPath.c_str() , String(vibrationValue));
- json.set(speedPath.c_str() , String(gpsSpeed));
- json.set(mapURLPath.c_str() , String(stringURL));
- json.set(timePath.c_str() , String(currentTime));
- json.set(datePath.c_str() , String(currentDate));
- 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]
- dht.begin();
- pinMode(vibration_Sensor_Pin, INPUT);
- 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 = "/UsersData/" + uid + "/readings";
- }
- void initTime(String timezone) {
- struct tm timeinfo;
- Serial.println("Setting up time");
- configTime(0, 0, "pool.ntp.org"); // First connect to NTP server, with 0 TZ offset
- if (!getLocalTime(&timeinfo)) {
- Serial.println(" Failed to obtain time");
- return;
- }
- Serial.println(" Got the time from NTP");
- // Now we can set the real timezone
- setTimezone(timezone);
- }
- void setTimezone(String timezone) {
- Serial.printf(" Setting Timezone to %s\n", timezone.c_str());
- setenv("TZ", timezone.c_str(), 1); // Now adjust the TZ. Clock settings are adjusted to show the new local time
- tzset();
- }
- void read_Local_Time_And_Date() {
- struct tm timeinfo;
- if (!getLocalTime(&timeinfo)) {
- Serial.println("Failed to obtain time 1");
- return;
- }
- // Format the date
- char formattedDate[20];
- sprintf(formattedDate, "%04d-%02d-%02d", timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday);
- currentDate = String(formattedDate);
- // Format the time
- char formattedTime[20];
- sprintf(formattedTime, "%02d:%02d:%02d", timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec);
- currentTime = String(formattedTime);
- }
- void DHT11_Sensor_Read() {
- h = dht.readHumidity();
- t = dht.readTemperature();
- if (isnan(h) || isnan(t)) {
- Serial.println(F("Failed to read from DHT sensor!"));
- return;
- }
- Serial.print(F("Humidity: "));
- Serial.print(h);
- Serial.print(F("% Temperature: "));
- Serial.print(t);
- Serial.println(F("°C "));
- }
- void Vibration_Sensor_Read() {
- vibrationValue = analogRead(vibration_Sensor_Pin);//0-4095
- if (vibrationValue > 0) {
- Serial.print("Vibration Detected !! = ");
- Serial.println(vibrationValue + " [VoltageLevel]");
- delay(100);
- }
- }
- 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);//1.12
- 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