Advertisement
safwan092

Untitled

Apr 24th, 2022
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. //https://github.com/mobizt/Firebase-ESP32
  2. #include <WiFi.h>
  3. #include <FirebaseESP32.h>
  4. #include <addons/TokenHelper.h>
  5. #include <addons/RTDBHelper.h>
  6. #include <Adafruit_MPU6050.h>
  7. #include <Adafruit_Sensor.h>
  8. #include <Wire.h>
  9.  
  10. #define WIFI_SSID "network"
  11. #define WIFI_PASSWORD "123456789"
  12. #define API_KEY "AIzaSyDrszpkKKAlFS0kCJ1EGW0kSpdl3REq45o"
  13. #define DATABASE_URL "carcrash-5a006-default-rtdb.firebaseio.com"
  14. #define USER_EMAIL "projectcarcrash@gmail.com"
  15. #define USER_PASSWORD "123456789"
  16.  
  17. Adafruit_MPU6050 mpu;
  18. FirebaseData fbdo;
  19. FirebaseAuth auth;
  20. FirebaseConfig config;
  21.  
  22. #define front_sensor 33
  23. #define back_sensor 32
  24.  
  25. unsigned long sendDataPrevMillis = 0;
  26. unsigned long crash_front = 0;
  27. unsigned long crash_back = 0;
  28. unsigned long car_fliped = 0;
  29.  
  30. int flag1 = 0;
  31. int flag2 = 0;
  32. int flag3 = 0;
  33. int count = 0;
  34.  
  35. void setup()
  36. {
  37.  
  38. Serial.begin(115200);
  39. mpu.begin();
  40. pinMode(front_sensor, INPUT);
  41. pinMode(back_sensor, INPUT);
  42. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  43. Serial.print("Connecting to Wi-Fi");
  44. while (WiFi.status() != WL_CONNECTED)
  45. {
  46. Serial.print(".");
  47. delay(300);
  48. }
  49. Serial.println();
  50. Serial.print("Connected with IP: ");
  51. Serial.println(WiFi.localIP());
  52. Serial.println();
  53.  
  54. Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
  55.  
  56. /* Assign the api key (required) */
  57. config.api_key = API_KEY;
  58. /* Assign the user sign in credentials */
  59. auth.user.email = USER_EMAIL;
  60. auth.user.password = USER_PASSWORD;
  61. /* Assign the RTDB URL (required) */
  62. config.database_url = DATABASE_URL;
  63. /* Assign the callback function for the long running token generation task */
  64. config.token_status_callback = tokenStatusCallback;
  65. Firebase.begin(&config, &auth);
  66. Firebase.reconnectWiFi(true);
  67. Firebase.setDoubleDigits(5);
  68. if (Firebase.ready()) {
  69. Serial.printf("Set int... %s\n", Firebase.setInt(fbdo, F("/test/crash_front"), crash_front) ? "ok" : fbdo.errorReason().c_str());
  70. Serial.println();
  71. Serial.printf("Set int... %s\n", Firebase.setInt(fbdo, F("/test/crash_back"), crash_back) ? "ok" : fbdo.errorReason().c_str());
  72. Serial.println();
  73. Serial.printf("Set int... %s\n", Firebase.setInt(fbdo, F("/test/car_fliped"), car_fliped) ? "ok" : fbdo.errorReason().c_str());
  74. Serial.println();
  75. }
  76. //setupt motion detection
  77. mpu.setHighPassFilter(MPU6050_HIGHPASS_0_63_HZ);
  78. mpu.setMotionDetectionThreshold(1);
  79. mpu.setMotionDetectionDuration(20);
  80. mpu.setInterruptPinLatch(true); // Keep it latched. Will turn off when reinitialized.
  81. mpu.setInterruptPinPolarity(true);
  82. mpu.setMotionInterrupt(true);
  83.  
  84. }
  85.  
  86. void loop()
  87. {
  88. crash_front = digitalRead(front_sensor);
  89. crash_back = digitalRead(back_sensor);
  90. if (mpu.getMotionInterruptStatus()) {
  91. /* Get new sensor events with the readings */
  92. sensors_event_t a, g, temp;
  93. mpu.getEvent(&a, &g, &temp);
  94.  
  95. /* Print out the values */
  96. Serial.print("AccelX:");
  97. Serial.print(a.acceleration.x);
  98. Serial.print(",");
  99. Serial.print("AccelY:");
  100. Serial.print(a.acceleration.y);
  101. Serial.print(",");
  102. Serial.print("AccelZ:");
  103. Serial.print(a.acceleration.z);
  104. Serial.print(", ");
  105. Serial.print("GyroX:");
  106. Serial.print(g.gyro.x);
  107. Serial.print(",");
  108. Serial.print("GyroY:");
  109. Serial.print(g.gyro.y);
  110. Serial.print(",");
  111. Serial.print("GyroZ:");
  112. Serial.print(g.gyro.z);
  113. Serial.println("");
  114. if (a.acceleration.x < 0 && a.acceleration.y < 0 && a.acceleration.z < 0 && flag1 == 0 ) {
  115. count++;
  116. Serial.println(count);
  117. if (count == 2) {
  118. car_fliped = 1;
  119. Firebase.ready();
  120. Serial.printf("Set int... %s\n", Firebase.setInt(fbdo, F("/test/car_fliped"), car_fliped) ? "ok" : fbdo.errorReason().c_str());
  121. flag1 = 1;
  122. count = 0;
  123. }
  124. }
  125. }
  126. if (Firebase.ready() && (millis() - sendDataPrevMillis > 100 || sendDataPrevMillis == 0))
  127. {
  128. sendDataPrevMillis = millis();
  129. /*
  130. crash_front
  131. crash_back
  132. car_fliped
  133. */
  134. if (crash_front == 1 && flag2 == 0) {
  135. Serial.printf("Set int... %s\n", Firebase.setInt(fbdo, F("/test/crash_front"), crash_front) ? "ok" : fbdo.errorReason().c_str());
  136. Serial.println();
  137. flag2 = 1;
  138. }
  139. if (crash_back == 1 && flag3 == 0) {
  140. Serial.printf("Set int... %s\n", Firebase.setInt(fbdo, F("/test/crash_back"), crash_back) ? "ok" : fbdo.errorReason().c_str());
  141. Serial.println();
  142. flag3 = 1;
  143. }
  144. }
  145. }// end of LOOP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement