FirebaseExtended / firebase-arduino

Arduino samples for Firebase.
Apache License 2.0
943 stars 494 forks source link

How add new data #569

Open abozozzaher opened 5 months ago

abozozzaher commented 5 months ago

I have this code that writes the status of the lamp, whether it is working or not, and writes the date But the problem is that every time he writes the history, he deletes the previous one. How do I make it record again?

image

#include <NTPClient.h>
#include <WiFiUdp.h>

#include <ESP8266WiFi.h>
#include <ESP8266Firebase.h>

#define _SSID "Blue Technology LLC_2.4GHz"
#define _PASSWORD "blue2023"
#define REFERENCE_URL "arduino-37530-default-rtdb.firebaseio.com"
#define FIREBASE_AUTH "3dfNNfs3TmdormzkVhsEfGsFnUBZU5mAe8vuojV4"
#define FIREBASE_HOST "arduino-37530-default-rtdb.firebaseio.com"

Firebase firebase(REFERENCE_URL);

//const int lampPin = D0;
#define RELAY_PIN 0  // تعيين رقم دبوس التحكم بالريلي على الـ GPIO0
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
void setup() {

Serial.begin(115200);

  // Connect to WiFi
  WiFi.begin(_SSID, _PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");
  Serial.println(_SSID);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  pinMode(RELAY_PIN, OUTPUT);
    timeClient.begin();

  // Run the main code every 5 seconds
 // while (true) {    mainCode();

    // Wait for 5 seconds before running again
 //   delay(5000);  }

}

void loop() {

  // }void mainCode() {

   String data = firebase.getString("Controller/setString");
  Serial.print("LampaController:\t");
  Serial.println(data);

  if (data == "1") {
    // Turn on the lamp if the value is "true"
    digitalWrite(RELAY_PIN, HIGH);
  } else if (data == "0") {
    // Turn off the lamp if the value is "false"
    digitalWrite(RELAY_PIN, LOW);
    Serial.println("Lamp is OFF");
  }
  // Read the current state of the relay (ON/OFF)
  int relayState = digitalRead(RELAY_PIN);
  if (relayState == HIGH) {

   firebase.setString("Time/turnOffTime", getCurrentTime());

      Serial.println("Turned OFF at: " + getCurrentTime());

  } else {
    firebase.setString( "Time/turnOnTime", getCurrentTime());
      Serial.println("Turned ON at: " + getCurrentTime());

  }

  delay(1000); 
}
String getCurrentTime() {
  timeClient.update();  
  Serial.println(timeClient.getFormattedTime());
  return timeClient.getFormattedTime();

}