Sending ultrasonic data to firebase

Can someone help me to this code on how can I send ultrasonic sensor data to firebase. I’ve looked up many tutorial but didn’t seem to help me :confused:

#include <WiFi.h>
#include <FirebaseArduino.h>


#define FIREBASE_HOST "abcd.firebaseio.com"
#define FIREBASE_AUTH "B4Uujjjjjjjjdsfdsfjjjjjjjvfgfjjjjjjj""
#define WIFI_SSID "Huhu"
#define WIFI_PASSWORD "888888"
#define trigPin 13
#define echoPin 12

void setup() {
  
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
    
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("connecting");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println();
  Serial.print("connected: ");
  Serial.println(WiFi.localIP());
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
  Firebase.setInt("Distance",0);
}


void loop() {
 
  if (distance < 5) {  
 Firebase.setString("message", "Detected");
  
  if (Firebase.failed()) // Check for errors {
  {
    Serial.print("setting /number failed:");
    Serial.println(Firebase.error());
    return;
  }
  
   long duration, distance;
  digitalWrite(trigPin, LOW)
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  Firebase.setFloat("Distance", 5.0);

}
  // handle error
  if (Firebase.failed()) {
      Serial.print("setting /number failed:");
      Serial.println(Firebase.error()); 
      return;
  }
    delay(1000);
///////
    Firebase.setFloat("Distance", 4.0);
  // handle error
  if (Firebase.failed()) {
      Serial.print("setting /number failed:");
      Serial.println(Firebase.error());  
      return;
  }
  delay(1000);

  /////////

      Firebase.setFloat("Distance", 4.0);
  // handle error
  if (Firebase.failed()) {
      Serial.print("setting /number failed:");
      Serial.println(Firebase.error());  
      return;
  }
  delay(1000);

  ///
      Firebase.setFloat("Distance", 3.0);
  // handle error
  if (Firebase.failed()) {
      Serial.print("setting /number failed:");
      Serial.println(Firebase.error());  
      return;
  }
  delay(1000)
  ////
      Firebase.setFloat("Distance", 2.0);
  // handle error
  if (Firebase.failed()) {
      Serial.print("setting /number failed:");
      Serial.println(Firebase.error());  
      return;
  }
  delay(1000);
  ////////////

      Firebase.setFloat("Distance", 1.0);
  // handle error
  if (Firebase.failed()) {
      Serial.print("setting /number failed:");
      Serial.println(Firebase.error());  
      return;
  }
  delay(1000);
  ///
  {
  
  delay(1000);
  ///
   // set bool value
  Firebase.setBool("truth", false);
  // handle error
  if (Firebase.failed()) {
      Serial.print("setting /truth failed:");
      Serial.println(Firebase.error());  
      return;
  }
  delay(1000);

  // append a new value to /logs
  String name = Firebase.pushInt("logs", n++);
  // handle error
  if (Firebase.failed()) {
      Serial.print("pushing /logs failed:");
      Serial.println(Firebase.error());  
      return;
  }
  Serial.print("pushed: /logs/");
  Serial.println(name);
  delay(1000);
}
}
}

THE CODE REALLY MESSED UP :’( :’(
Where will I place this one? I’m really confused to that part and the condition.

long duration, distance;
  digitalWrite(trigPin, LOW)
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;

Using NODEMCU ESP8266

THE CODE REALLY MESSED UP

The programmer bears absolutely no responsibility?

  Firebase.setInt("Distance",0);

What is this supposed to do? Why are you setting the value to 0?

  Firebase.setFloat("Distance", 5.0);

Make up your mind. Is Distance a float or an int?

Why 5.0? You measured the distance, or you should have.

Posting some code that actually compiles would have been better. Then, we could have asked just what the code does. But, since it won't compile, there is no need to ask that.

Sir, I already sent data to firebase. My problem now is how can I choose what data to send in firebase?

#include <FirebaseArduino.h>
#include <ESP8266WiFi.h>
#define FIREBASE_HOST "aaaaaaaaaaaaa.firebaseio.com"
#define FIREBASE_AUTH "BdsadsadsadsarpiyxffqY87MbgvfRtk5"
#define WIFI_SSID "1234"
#define WIFI_PASSWORD "abcd"

int TRIG_PIN = 5; //d1
int ECHO_PIN = 4; //d2

// Anything over 400 cm (23200 us pulse) is "out of range"
const unsigned int MAX_DIST = 23200;
void setup() {
   pinMode(TRIG_PIN, OUTPUT);
  digitalWrite(TRIG_PIN, LOW);
  
  Serial.begin(9600);

  // connect to wifi.
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("connecting");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println();
  Serial.print("connected: ");
  Serial.println(WiFi.localIP());
  
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
}

int n = 0;

void loop() {
   unsigned long t1;
  unsigned long t2;
  unsigned long pulse_width;
  int cm;
  int inches;
  //
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  // Wait for pulse on echo pin
  while ( digitalRead(ECHO_PIN) == 0 );

   // Measure how long the echo pin was held high (pulse width)
  // Note: the micros() counter will overflow after ~70 min
  t1 = micros();
  while ( digitalRead(ECHO_PIN) == 1);
  t2 = micros();
  pulse_width = t2 - t1;

  // Calculate distance in centimeters and inches. The constants
  // are found in the datasheet, and calculated from the assumed speed 
  //of sound in air at sea level (~340 m/s).
  cm = pulse_width / 58.0;
  inches = pulse_width / 148.0;
  
// Print out results
  if ( pulse_width > MAX_DIST ) {
    Serial.println("Out of range");
  } else {
    Serial.print(cm);
    Serial.print(" cm \t");
    Serial.print(inches);
    Serial.println(" in");
  }
  
  // Wait at least 60ms before next measurement
  delay(60);

Firebase.setFloat("distance", pulse_width);
   if (Firebase.failed()) {
      Serial.print("setting /distance failed:");
      Serial.println(Firebase.error());  
      return;
}
      delay(1000);

       // update value
  if ( pulse_width < 5) 
  {
   Firebase.setString("message", "Mouse Detected");
  // handle error
  if (Firebase.failed()) {
      Serial.print("setting /distance failed:");
      Serial.println(Firebase.error());  
      return;
  }
  delay(1000);
 
 Serial.print("distance: ");
  Serial.println(Firebase.getInt("distance"));
  delay(1000);
  
Firebase.remove("distance");
  delay(1000);


 // set bool value
  Firebase.setBool("truth", false);
  // handle error
  if (Firebase.failed()) {
      Serial.print("setting /truth failed:");
      Serial.println(Firebase.error());  
      return;
  }
  delay(1000);

  // append a new value to /logs
  String name = Firebase.pushInt("logs", n++);
  // handle error
  if (Firebase.failed()) {
      Serial.print("pushing /logs failed:");
      Serial.println(Firebase.error());  
      return;
  }
  Serial.print("pushed: /logs/");
  Serial.println(name);
  delay(1000);

  }  }

My problem now is how can I choose what data to send in firebase?

Well, what I'd like to see is the average bust size on beaches in Florida versus day of week and day of year.

You might want to see other things. How are we supposed to know what you want to see?

You SHOULD create a function to get the distance from the ultrasonic sensor. You should call that function once in a while. How often is entirely up to you. You COULD then send real data to firebase.

PaulS: Well, what I'd like to see is the average bust size on beaches in Florida versus day of week and day of year.

You might want to see other things. How are we supposed to know what you want to see?

You SHOULD create a function to get the distance from the ultrasonic sensor. You should call that function once in a while. How often is entirely up to you. You COULD then send real data to firebase.

You helped me once again! I did it. Thanks!

sir? can you help me? can you send your full codes of your project in sending data to firebase? i have the same problem.. please?