mobizt / FirebaseClient

🔥Async Firebase Client for Arduino. Supports Realtime Database, Cloud Firestore Database, Firebase Storage, Cloud Messaging, Google Cloud Functions and Google Cloud Storage.
MIT License
134 stars 8 forks source link

i think something wrong in this lib or in my code #133

Closed jamsyogendra closed 3 months ago

jamsyogendra commented 3 months ago
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <FirebaseClient.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>

// Replace with your Wi-Fi credentials
#define WIFI_SSID "........"
#define WIFI_PASSWORD "......................................."

// Replace with your Firebase details
#define DATABASE_SECRET ".................................................................."
#define DATABASE_URL "..................................................................................."

// Define the relay pin
#define RELAY_PIN D1

WiFiClientSecure ssl;
DefaultNetwork network;
AsyncClientClass client(ssl, getNetwork(network));

FirebaseApp app;
RealtimeDatabase Database;
AsyncResult result;
LegacyToken dbSecret(DATABASE_SECRET);

void printError(int code, const String &msg)
{
    Serial.printf("Error, msg: %s, code: %d\n", msg.c_str(), code);
}

void setup()
{
    Serial.begin(115200);
    pinMode(RELAY_PIN, OUTPUT);
    digitalWrite(RELAY_PIN, LOW); // Ensure relay is off initially

    // Connect to Wi-Fi
    Serial.print("Connecting to Wi-Fi");
    WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
    while (WiFi.status() != WL_CONNECTED)
    {
        Serial.print(".");
        delay(300);
    }
    Serial.println();
    Serial.print("Connected with IP: ");
    Serial.println(WiFi.localIP());
    Serial.println();

    // Initialize Firebase
    Serial.println("Initializing Firebase...");
    Firebase.printf("Firebase Client v%s\n", FIREBASE_CLIENT_VERSION);
    ssl.setInsecure();
    ssl.setBufferSizes(1024, 1024);
    initializeApp(client, app, getAuth(dbSecret));
    app.getApp<RealtimeDatabase>(Database);
    Database.url(DATABASE_URL);
    client.setAsyncResult(result);
}

void loop()
{
    Serial.print("Fetching relay state from Firebase... ");
    bool relayState = Database.get<bool>(client, "/relayState");
    if (client.lastError().code() == 0)
    {
        Serial.print("Relay State: ");
        Serial.println(relayState);

        // Control the relay based on the Firebase value
        digitalWrite(RELAY_PIN, relayState ? HIGH : LOW);
    }
    else
    {
        printError(client.lastError().code(), client.lastError().message());
    }

    delay(10000); // Wait 10 seconds before the next read
}

no any error in showing in my serial monitor but my code stacked ,

i am geeting realtime data or changes in data from my app but my relay not going to change state

can anyone solve or any suggestions.

mobizt commented 3 months ago

Read the library documentation thoroughly for the concepts and proper usage.

jamsyogendra commented 3 months ago

can you tell me whats wrong in my code and can you make changes for me please

On Sun, Aug 18, 2024 at 8:54 PM Suwatchai K. @.***> wrote:

Read the library documentation thoroughly for the concepts and proper usage.

— Reply to this email directly, view it on GitHub https://github.com/mobizt/FirebaseClient/issues/133#issuecomment-2295300256, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALXAOEIWGXM4MLAC6FF2T33ZSC4ETAVCNFSM6AAAAABMWOM45WVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEOJVGMYDAMRVGY . You are receiving this because you authored the thread.Message ID: @.***>

mobizt commented 3 months ago

Read and follow the examples comments.

jamsyogendra commented 3 months ago

include

if defined(ESP32) || defined(ARDUINO_RASPBERRY_PI_PICO_W)

include

elif defined(ESP8266)

include

endif

include

include

include // Include ArduinoJson library

define WIFI_SSID "Jamsariyaji"

define WIFI_PASSWORD @.***@"

define DATABASE_URL "https://fir-8266-458c8-default-rtdb.firebaseio.com/"

define DATABASE_SECRET "xbaiaVfrgiJWI4yG3DtwLN681P4RbbCGhH7pjyxX"

// Define the relay pin

define RELAY_PIN D1

WiFiClientSecure ssl; DefaultNetwork network; AsyncClientClass client(ssl, getNetwork(network));

FirebaseApp app; RealtimeDatabase Database; AsyncResult result; LegacyToken dbSecret(DATABASE_SECRET);

void printError(int code, const String &msg) { Serial.printf("Error, msg: %s, code: %d\n", msg.c_str(), code); }

void setup() { Serial.begin(115200); pinMode(RELAY_PIN, OUTPUT); digitalWrite(RELAY_PIN, LOW); // Ensure relay is off initially

// Connect to Wi-Fi
Serial.print("Connecting to Wi-Fi");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED)
{
    Serial.print(".");
    delay(300);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();

// Initialize Firebase
Serial.println("Initializing Firebase...");
Firebase.printf("Firebase Client v%s\n", FIREBASE_CLIENT_VERSION);

ssl.setInsecure();

if defined(ESP8266)

ssl.setBufferSizes(1024, 1024);

endif

// Initialize the authentication handler.
initializeApp(client, app, getAuth(dbSecret));

// Binding the authentication handler with your Database class object.
app.getApp<RealtimeDatabase>(Database);

// Set your database URL
Database.url(DATABASE_URL);

// In sync functions, we have to set the operating result for the

client that works with the function. client.setAsyncResult(result);

// Control the relay based on the Firebase value
controlRelay();

}

void controlRelay() { // Read the relay state from Firebase Serial.print("Fetching relay state from Firebase... "); if (Database.get(client, "/relayState")) { bool relayState = Database.get(client, "/relayState"); Serial.print("Relay State: "); Serial.println(relayState);

    // Control the relay based on the Firebase value
    digitalWrite(RELAY_PIN, relayState ? HIGH : LOW);
}
else
{
    printError(client.lastError().code(), client.lastError().message());
}

}

void loop() { // Fetch and update relay state every 10 seconds controlRelay(); delay(10000); // Wait 10 seconds before the next read } // again this code written by me i am using esp8266 latest board version with arduino json 6.21.2 and latest version of your lib , getting data from database but again relay not going to change his state i also change the firebase rule as true

On Sun, Aug 18, 2024 at 9:33 PM Suwatchai K. @.***> wrote:

Read and follow the examples comments.

— Reply to this email directly, view it on GitHub https://github.com/mobizt/FirebaseClient/issues/133#issuecomment-2295310991, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALXAOENGS52KMP23V53MMZ3ZSDATXAVCNFSM6AAAAABMWOM45WVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEOJVGMYTAOJZGE . You are receiving this because you authored the thread.Message ID: @.***>

mobizt commented 3 months ago

You didn't even try to read the documentation.

mobizt commented 3 months ago

I never give user advice and never wrote the examples and documentation about using delay in all my Firebase libraries in almost 6 years of development.

jamsyogendra commented 3 months ago

Sorry 😐 i apologize for it from bottom of my heart, I read it then get back to you, I hope will always here for my help

On Sun, Aug 18, 2024, 9:40 PM Suwatchai K. @.***> wrote:

You don't even try to read the documentation.

— Reply to this email directly, view it on GitHub https://github.com/mobizt/FirebaseClient/issues/133#issuecomment-2295313361, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALXAOENMNTUPBTGN66VV2FDZSDBPTAVCNFSM6AAAAABMWOM45WVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEOJVGMYTGMZWGE . You are receiving this because you authored the thread.Message ID: @.***>