Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

How add new data #569

Open
abozozzaher opened this issue Jan 25, 2024 · 0 comments
Open

How add new data #569

abozozzaher opened this issue Jan 25, 2024 · 0 comments

Comments

@abozozzaher
Copy link

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();
  

}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant