Advertisement
safwan092

Untitled

Oct 30th, 2023
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.92 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 DHTPIN 22
  11. #define vibration_Sensor_Pin 34
  12.  
  13. #define WIFI_SSID "Saud"
  14. #define WIFI_PASSWORD "saud112233"
  15. #define API_KEY "AIzaSyADNmTRkiRXx1lV0lnK3uH6z-BAfjG8NrY"
  16. #define USER_EMAIL "MedicalTruckSafety@gmail.com"
  17. #define USER_PASSWORD "123456789"
  18. #define DATABASE_URL "medicaltrucksafety-32e85-default-rtdb.firebaseio.com"
  19. FirebaseData fbdo;
  20. FirebaseAuth auth;
  21. FirebaseConfig config;
  22. FirebaseJson json;
  23. #define DHTTYPE DHT11
  24. DHT dht(DHTPIN, DHTTYPE);
  25. TinyGPSPlus gps;
  26. String stringURL = "";
  27. String gpsSpeed;
  28. String gpsLon;
  29. String gpsLat;
  30. int vibrationValue = 0;
  31. float h;
  32. float t;
  33. String databasePath;
  34. String uid;
  35. String currentDate;
  36. String currentTime;
  37. String parentPath;
  38. String temperaturePath = "/temperature";
  39. String humidityPath = "/humidity";
  40. String vibrationPath = "/vibration";
  41. String speedPath = "/Speed";
  42. String mapURLPath = "/Location";
  43. String timePath = "/Time";
  44. String datePath = "/Date";
  45.  
  46. unsigned long dataMillis = 0;
  47.  
  48. void setup() {
  49.  
  50. Setup_Sensors_And_Connect_To_WiFi_And_Firebase();
  51. initTime("<+03>-3");
  52. read_Local_Time_And_Date();
  53. Serial.println(currentTime);
  54. Serial.println(currentDate);
  55. delay(5000);
  56. }
  57.  
  58. void loop() {
  59. read_Local_Time_And_Date();
  60. DHT11_Sensor_Read();
  61. Vibration_Sensor_Read();
  62. GPS_Modem_Read();
  63. ////////////////////////////////////////////////////////////////////////////
  64. stringURL = "http://www.google.com/maps/place/" + gpsLat + "," + gpsLon;
  65. ////////////////////////////////////////////////////////////////////////////
  66.  
  67. if (millis() - dataMillis > 5000 && Firebase.ready())
  68. {
  69. dataMillis = millis();
  70. parentPath = databasePath + "/" + currentDate + "/" + currentTime;
  71.  
  72. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  73. json.set(temperaturePath.c_str() , String(t));
  74. json.set(humidityPath.c_str() , String(h));
  75. json.set(vibrationPath.c_str() , String(vibrationValue));
  76. json.set(speedPath.c_str() , String(gpsSpeed));
  77. json.set(mapURLPath.c_str() , String(stringURL));
  78. json.set(timePath.c_str() , String(currentTime));
  79. json.set(datePath.c_str() , String(currentDate));
  80. Serial.printf("Set json... %s\n", Firebase.RTDB.setJSON(&fbdo, parentPath.c_str(), &json) ? "ok" : fbdo.errorReason().c_str());
  81. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  82.  
  83. }
  84. }
  85.  
  86.  
  87.  
  88. void Setup_Sensors_And_Connect_To_WiFi_And_Firebase() {
  89. Serial.begin(115200);
  90. Serial2.begin(9600);//Serial Channel for GPS with ESP32 [TX2+RX2]
  91. dht.begin();
  92. pinMode(vibration_Sensor_Pin, INPUT);
  93.  
  94. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  95. Serial.print("Connecting to WiFi ..");
  96. while (WiFi.status() != WL_CONNECTED) {
  97. Serial.print('.');
  98. delay(1000);
  99. }
  100. Serial.println(WiFi.localIP());
  101. Serial.println();
  102. // Assign the api key (required)
  103. config.api_key = API_KEY;
  104.  
  105. // Assign the user sign in credentials
  106. auth.user.email = USER_EMAIL;
  107. auth.user.password = USER_PASSWORD;
  108.  
  109. // Assign the RTDB URL (required)
  110. config.database_url = DATABASE_URL;
  111.  
  112. Firebase.reconnectWiFi(true);
  113. fbdo.setResponseSize(4096);
  114.  
  115. // Assign the callback function for the long running token generation task */
  116. config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h
  117.  
  118. // Assign the maximum retry of token generation
  119. config.max_token_generation_retry = 5;
  120.  
  121. // Initialize the library with the Firebase authen and config
  122. Firebase.begin(&config, &auth);
  123.  
  124. // Getting the user UID might take a few seconds
  125. Serial.println("Getting User UID");
  126. while ((auth.token.uid) == "") {
  127. Serial.print('.');
  128. delay(1000);
  129. }
  130. // Print user UID
  131. uid = auth.token.uid.c_str();
  132. Serial.print("User UID: ");
  133. Serial.println(uid);
  134.  
  135. // Update database path
  136. databasePath = "/UsersData/" + uid + "/readings";
  137.  
  138. }
  139.  
  140. void initTime(String timezone) {
  141. struct tm timeinfo;
  142.  
  143. Serial.println("Setting up time");
  144. configTime(0, 0, "pool.ntp.org"); // First connect to NTP server, with 0 TZ offset
  145. if (!getLocalTime(&timeinfo)) {
  146. Serial.println(" Failed to obtain time");
  147. return;
  148. }
  149. Serial.println(" Got the time from NTP");
  150. // Now we can set the real timezone
  151. setTimezone(timezone);
  152. }
  153.  
  154. void setTimezone(String timezone) {
  155. Serial.printf(" Setting Timezone to %s\n", timezone.c_str());
  156. setenv("TZ", timezone.c_str(), 1); // Now adjust the TZ. Clock settings are adjusted to show the new local time
  157. tzset();
  158. }
  159.  
  160.  
  161. void read_Local_Time_And_Date() {
  162. struct tm timeinfo;
  163. if (!getLocalTime(&timeinfo)) {
  164. Serial.println("Failed to obtain time 1");
  165. return;
  166. }
  167. // Format the date
  168. char formattedDate[20];
  169. sprintf(formattedDate, "%04d-%02d-%02d", timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday);
  170. currentDate = String(formattedDate);
  171. // Format the time
  172. char formattedTime[20];
  173. sprintf(formattedTime, "%02d:%02d:%02d", timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec);
  174. currentTime = String(formattedTime);
  175. }
  176.  
  177. void DHT11_Sensor_Read() {
  178. h = dht.readHumidity();
  179. t = dht.readTemperature();
  180. if (isnan(h) || isnan(t)) {
  181. Serial.println(F("Failed to read from DHT sensor!"));
  182. return;
  183. }
  184. Serial.print(F("Humidity: "));
  185. Serial.print(h);
  186. Serial.print(F("% Temperature: "));
  187. Serial.print(t);
  188. Serial.println(F("°C "));
  189. }
  190.  
  191. void Vibration_Sensor_Read() {
  192. vibrationValue = analogRead(vibration_Sensor_Pin);//0-4095
  193. if (vibrationValue > 0) {
  194. Serial.print("Vibration Detected !! = ");
  195. Serial.println(vibrationValue + " [VoltageLevel]");
  196. delay(100);
  197. }
  198. }
  199.  
  200. void GPS_Modem_Read() {
  201. if (Serial2.available() > 0) {
  202. if (gps.encode(Serial2.read())) {
  203. if (gps.location.isValid()) {
  204. float gpslat_float = gps.location.lat();
  205. gpsLat = String(gpslat_float, 6);
  206. Serial.print(F("- latitude: "));
  207. Serial.println(gpsLat);
  208. float gpslon_float = gps.location.lng();
  209. gpsLon = String(gpslon_float, 6);
  210. Serial.print(F("- longitude: "));
  211. Serial.println(gpsLon);
  212. } else {
  213. Serial.println(F("- location: INVALID"));
  214. }
  215. Serial.print(F("- speed: "));
  216. if (gps.speed.isValid()) {
  217. float speedInKmph = gps.speed.kmph();
  218. gpsSpeed = String(speedInKmph, 2);//1.12
  219. Serial.print(gpsSpeed);
  220. Serial.println(F(" km/h"));
  221. } else {
  222. Serial.println(F("INVALID"));
  223. }
  224. Serial.println();
  225. }
  226. }
  227. if (millis() > 5000 && gps.charsProcessed() < 10)
  228. Serial.println(F("No GPS data received: check wiring"));
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement