Sending data from esp8266 to Firebase

Good day, I wish to know how to send data in this format from esp8266 to firebase where the values of my sensors are a child of a key:

image

I have not tested the code due to connection issues but I plan to test it out once our net comes back. However, here it is for reference:

#include <DHT.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <Adafruit_BMP085.h>
#define seaLevelPressure_hPa 1013.25

#include <FirebaseArduino.h>

#define DHT_SENSOR_PIN  D7 // The ESP8266 pin D7 connected to DHT22 sensor
#define DHT_SENSOR_TYPE DHT22
#define WATERLEVEL_SENSOR_PIN A0
#define ON_Board_LED 2

Adafruit_BMP085 bmp;

const char* ssid = "Sky_3DE8_26";
const char* password = "CK9nQQuS";
#define FIREBASE_AUTH "campana-beta-default-rtdb.firebaseio.com"
#define FIREBASE_HOST "AYggFep8b3HKX1ovWhDE7tJ6Qn2jsd9g06nOQulc"

WiFiClientSecure client;

DHT dht(DHT_SENSOR_PIN, DHT_SENSOR_TYPE);

void setup() {
  Serial.begin(115200);
  delay(500);
  dht.begin();  //--> Start reading DHT11 sensors
  bmp.begin();
  delay(500);
  
  WiFi.begin(ssid, password); //--> Connect to your WiFi router
  Serial.println("");
  pinMode(ON_Board_LED,OUTPUT); //--> On Board LED port Direction output
  digitalWrite(ON_Board_LED, HIGH); //--> Turn off Led On Board

  //----------------------------------------Wait for connection
  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    //----------------------------------------Make the On Board Flashing LED on the process of connecting to the wifi router.
    digitalWrite(ON_Board_LED, LOW);
    delay(250);
    digitalWrite(ON_Board_LED, HIGH);
    delay(250);
    //----------------------------------------
  }
  //----------------------------------------
  digitalWrite(ON_Board_LED, HIGH); //--> Turn off the On Board LED when it is connected to the wifi router.
  //----------------------------------------If successfully connected to the wifi router, the IP Address that will be visited is displayed in the serial monitor
  Serial.println("");
  Serial.print("Successfully connected to : ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println();
  //----------------------------------------
  Firebase.begin(FIREBASE_AUTH, FIREBASE_HOST);
  client.setInsecure(); 
}

void loop() {

    // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float p = bmp.readPressure();
  float l = analogRead(WATERLEVEL_SENSOR_PIN);

  float humi  = dht.readHumidity();
  float temperature_C = dht.readTemperature();
  float pressure_Pa = bmp.readPressure();
  float flood_level = analogRead(WATERLEVEL_SENSOR_PIN);

  if ( isnan(temperature_C) || isnan(humi)) {
    Serial.println("Failed to read from DHT sensor!");
  } else {
    Serial.print("Humidity: ");
    Serial.print(humi);
    Serial.print("%");

    Serial.print("  |  ");

    Serial.print("Temperature: ");
    Serial.print(temperature_C);
    Serial.print("°C");

    Serial.print("  |  ");

    Serial.print("Pressure: ");
    Serial.print(pressure_Pa);
    Serial.print("Pa");

    Serial.print("  |  ");

    Serial.print("Flood Level Value: ");
    Serial.print(flood_level);
    Serial.println("");
  }

  Firebase.set("Humidity", h);
  Firebase.set("Temperature", t);
  Firebase.set("Pressure", p);
  Firebase.set("Flood Level", l);
  if(Firebase.failed()) {
    Serial.print("Failed");
    Serial.println(Firebase.error());
    return;
  }
  Serial.println("Sent Data"); 
  delay(5000); 
}

This project is due in a few months, badly need responses as I have other things to fix

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