Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //https://github.com/mobizt/Firebase-ESP32
- #include <WiFi.h>
- #include <FirebaseESP32.h>
- #include <addons/TokenHelper.h>
- #include <addons/RTDBHelper.h>
- #include <Adafruit_MPU6050.h>
- #include <Adafruit_Sensor.h>
- #include <Wire.h>
- #define WIFI_SSID "network"
- #define WIFI_PASSWORD "123456789"
- #define API_KEY "AIzaSyDrszpkKKAlFS0kCJ1EGW0kSpdl3REq45o"
- #define DATABASE_URL "carcrash-5a006-default-rtdb.firebaseio.com"
- #define USER_EMAIL "projectcarcrash@gmail.com"
- #define USER_PASSWORD "123456789"
- Adafruit_MPU6050 mpu;
- FirebaseData fbdo;
- FirebaseAuth auth;
- FirebaseConfig config;
- #define front_sensor 33
- #define back_sensor 32
- unsigned long sendDataPrevMillis = 0;
- unsigned long crash_front = 0;
- unsigned long crash_back = 0;
- unsigned long car_fliped = 0;
- int flag1 = 0;
- int flag2 = 0;
- int flag3 = 0;
- int count = 0;
- void setup()
- {
- Serial.begin(115200);
- mpu.begin();
- pinMode(front_sensor, INPUT);
- pinMode(back_sensor, INPUT);
- WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
- Serial.print("Connecting to Wi-Fi");
- while (WiFi.status() != WL_CONNECTED)
- {
- Serial.print(".");
- delay(300);
- }
- Serial.println();
- Serial.print("Connected with IP: ");
- Serial.println(WiFi.localIP());
- Serial.println();
- Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
- /* 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;
- /* Assign the callback function for the long running token generation task */
- config.token_status_callback = tokenStatusCallback;
- Firebase.begin(&config, &auth);
- Firebase.reconnectWiFi(true);
- Firebase.setDoubleDigits(5);
- if (Firebase.ready()) {
- Serial.printf("Set int... %s\n", Firebase.setInt(fbdo, F("/test/crash_front"), crash_front) ? "ok" : fbdo.errorReason().c_str());
- Serial.println();
- Serial.printf("Set int... %s\n", Firebase.setInt(fbdo, F("/test/crash_back"), crash_back) ? "ok" : fbdo.errorReason().c_str());
- Serial.println();
- Serial.printf("Set int... %s\n", Firebase.setInt(fbdo, F("/test/car_fliped"), car_fliped) ? "ok" : fbdo.errorReason().c_str());
- Serial.println();
- }
- //setupt motion detection
- mpu.setHighPassFilter(MPU6050_HIGHPASS_0_63_HZ);
- mpu.setMotionDetectionThreshold(1);
- mpu.setMotionDetectionDuration(20);
- mpu.setInterruptPinLatch(true); // Keep it latched. Will turn off when reinitialized.
- mpu.setInterruptPinPolarity(true);
- mpu.setMotionInterrupt(true);
- }
- void loop()
- {
- crash_front = digitalRead(front_sensor);
- crash_back = digitalRead(back_sensor);
- if (mpu.getMotionInterruptStatus()) {
- /* Get new sensor events with the readings */
- sensors_event_t a, g, temp;
- mpu.getEvent(&a, &g, &temp);
- /* Print out the values */
- Serial.print("AccelX:");
- Serial.print(a.acceleration.x);
- Serial.print(",");
- Serial.print("AccelY:");
- Serial.print(a.acceleration.y);
- Serial.print(",");
- Serial.print("AccelZ:");
- Serial.print(a.acceleration.z);
- Serial.print(", ");
- Serial.print("GyroX:");
- Serial.print(g.gyro.x);
- Serial.print(",");
- Serial.print("GyroY:");
- Serial.print(g.gyro.y);
- Serial.print(",");
- Serial.print("GyroZ:");
- Serial.print(g.gyro.z);
- Serial.println("");
- if (a.acceleration.x < 0 && a.acceleration.y < 0 && a.acceleration.z < 0 && flag1 == 0 ) {
- count++;
- Serial.println(count);
- if (count == 2) {
- car_fliped = 1;
- Firebase.ready();
- Serial.printf("Set int... %s\n", Firebase.setInt(fbdo, F("/test/car_fliped"), car_fliped) ? "ok" : fbdo.errorReason().c_str());
- flag1 = 1;
- count = 0;
- }
- }
- }
- if (Firebase.ready() && (millis() - sendDataPrevMillis > 100 || sendDataPrevMillis == 0))
- {
- sendDataPrevMillis = millis();
- /*
- crash_front
- crash_back
- car_fliped
- */
- if (crash_front == 1 && flag2 == 0) {
- Serial.printf("Set int... %s\n", Firebase.setInt(fbdo, F("/test/crash_front"), crash_front) ? "ok" : fbdo.errorReason().c_str());
- Serial.println();
- flag2 = 1;
- }
- if (crash_back == 1 && flag3 == 0) {
- Serial.printf("Set int... %s\n", Firebase.setInt(fbdo, F("/test/crash_back"), crash_back) ? "ok" : fbdo.errorReason().c_str());
- Serial.println();
- flag3 = 1;
- }
- }
- }// end of LOOP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement