How to display a string value in Google Firebase?

I managed to display my ultrasonic sensor on the firebase
image

When it reaches a certain level, I need to display "HIGH", "LOW", and "MODERATE" like what I did before but on LCD.

I use this as my code in the firebase

if(r < 60.96){
   String s = "HIGH";
    delay(100);}
  
  if(r >60.96 && r<152.4){
   String s = "MODERATE";
    delay(100);}
  
  if(r >152.4){
   String s = "LOW";
    delay(100);}

    
 Firebase.setFloat(firebaseData, "Flood/cm", r);
 Firebase.setFloat(firebaseData, "/Flood/level", s);
 delay(200);

How can I display on the "level" in firebase the High, Moderate, and Low? Thankyou!

I'm not familiar with the firebase database.
But the functionsname Firebase.setFloat
says what it does

set a float-value.

So I guess the firebase-data-library has a similar function but with a different name that expresses "string"

You should always post your complete sketch. With just these few lines nobody knows if you are using an ESP or something different which have all different firebase-libraries

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan

Hello sir, here is the full code that i am using :slight_smile:

I just cant seem to include the string value to firebase but they said it supports string data

#include <WiFi.h>
#include <FirebaseESP32.h>
#include<UltraDistSensor.h>

//Ultrasonic Pins
int trigPin = 13;    // TRIG pin
int echoPin = 12;    // ECHO pin

float d, r, s;

//Earthquake perimeters


int xsample=0;
int ysample=0;
int zsample=0;

#define samples 50
int maxVal = 20;
int minVal = -20;

String H = "HIGH";
String M = "MOD ";
String L = "LOW ";


FirebaseData firebaseData;

//Firebase
#define FIREBASE_HOST "cloud-based-multialarm-default-rtdb.firebaseio.com"
#define FIREBASE_AUTH "sqJSyzl1QWPGTt3ujv0mE3gdGrZS6bnt6sPiy7ft"

//ESP8266
#define WIFI_SSID "Epis"
#define WIFI_PASSWORD "tp6bqCR2"

void setup() {

  Serial.begin(9600);

  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("Attempting Connection");
  while(WiFi.status() !=WL_CONNECTED)
  {Serial.print("...");
    delay(500);}

    Serial.println();
    Serial.print("Connected Successfully");
    Serial.print(WiFi.localIP());


  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
  delay(500);

  
        
  pinMode(trigPin, OUTPUT); 
  pinMode(echoPin, INPUT);  

  for(int i=0; i<samples;i++){
    xsample+=analogRead(25);
    ysample+=analogRead(26);
    zsample+=analogRead(27);
  }

  xsample/=samples;
  ysample/=samples;
  zsample/=samples;
  
}

void loop() {

  //Flood
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  d = pulseIn(echoPin, HIGH);
  r = d*0.034/2;

  Serial.print(r);
  Serial.print(" cm");

  if (isnan(r)){
    Serial.print("error");
    return;
  }
  
  if(r < 60.96){
   String s = "HIGH";
    delay(100);}
  
  if(r >60.96 && r<152.4){
   String s = "MODERATE";
    delay(100);}
  
  if(r >152.4){
   String s = "LOW";
    delay(100);}

  int value1=analogRead(25);
  int value2=analogRead(26);
  int value3=analogRead(27);
  
  int xValue=xsample-value1;
  int yValue=ysample-value2;
  int zValue=zsample-value3;

 Firebase.setFloat(firebaseData, "/Earthquake/X", xValue);
 Firebase.setFloat(firebaseData, "/Earthquake/Y", yValue);
 Firebase.setFloat(firebaseData, "/Earthquake/Z", zValue); 
 Firebase.setFloat(firebaseData, "Flood/cm", r);
 Firebase.setFloat(firebaseData, "/Flood/level", s);
 delay(200);
  


    
  //Earthquake

  Serial.print("x=");
  Serial.println(xValue);
  Serial.print("y=");
  Serial.println (yValue);
  Serial.print("z=");
  Serial.println(zValue);
  Serial.println("  ");


}

maybe this will assist:

https://firebase.google.com/docs/database/admin/retrieve-data

do you understand your variable xValue contains a comma-number which is a different dataTYPE than a string

and for variables that hold a comma-number which is dataTYPE "float" the command

setFloat is used

So a command

Firebase.setFloat(firebaseData, "/Flood/level", s);

with your variable "s" which is dataTYPE String does not work
You have to find the command
which might be

setString

or

setStr

or something similar to handover variables of dataTYPE String

using setFloat for a variable of datatype String is like trying to drill a hole
with a toothbrush. Does not work.

For drilling a hole you need a drill.

For finding example-codes that demonstrate how to use strings do a google-search with this keywords

GitHub firebase "your word of interest"

best regards Stefan

thankyou sir! I used different way and got the data, I insert the setstring on my conditions like this

if(r < 60.96){
    Firebase.setString(firebaseData, "/Flood/level", "HIGH");
    delay(100);}
  
  if(r >60.96 && r<152.4){
   Firebase.setString(firebaseData, "/Flood/level", "MODERATE");
    delay(100);}
  
  if(r >152.4){
   Firebase.setString(firebaseData, "/Flood/level", "LOW");
    delay(100);}

and it worked thankyou so much

glad I could help.

You wrote "thank you sir". To me using the word "sir" expresses a lot of respect.
Thank you very much for that. But I'm not working in the military-section or the police where "sir" is the common term. I'm just an Arduino-cc-user-forum-user. So a simple thank you is enough or if you think the help was really great a "thank you very much". The biggest thank you for me is if you contribute or help with anything anywhere anybody (not nescessarily in the forum).
The big circle of helping each other will close somewhere some day so that I will receive some help too.

best regards Stefan

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.