mobizt / Firebase-Arduino-WiFiNINA

Firebase Arduino Library for ARM/AVR WIFI Dev Boards based on WiFiNINA
MIT License
65 stars 12 forks source link

Couldn't get my Arduino to update my Firebase #50

Closed Mukesh-Kalikaya closed 7 months ago

Mukesh-Kalikaya commented 7 months ago

Hello. I am currently using an Arduino MKR Wifi 1010 Board along with an MKR Env Shield. Here is the code I currently have:

#include "Firebase_Arduino_WiFiNINA.h"
#include <WiFiNINA.h>
#include <Arduino_MKRENV.h>

#define FIREBASE_URL "https://nasahunch-174c5-default-rtdb.firebaseio.com/"
#define FIREBASE_SECRET ""
#define WIFI_SSID ""
#define WIFI_PASSWORD ""

//Define Firebase data object
FirebaseData fbdo;

void setup() {
  Serial.begin(9600);
  Serial.println("Connecting to Wi-Fi");

  int status = WL_IDLE_STATUS;

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

  if (status == WL_CONNECTED) {
    Serial.println("\nConnected to Wi-Fi");
    Serial.println("Initializing Firebase");
  } else {
    Serial.println("\nFailed to connect to Wi-Fi");
  }

  // Initialize Firebase
  Firebase.begin(FIREBASE_URL, FIREBASE_SECRET, WIFI_SSID, WIFI_PASSWORD);

  // Initialize environmental sensor
  if (!ENV.begin()) {
    Serial.println("Failed to initialize ENV sensor!");
    while (1);
  }
  Serial.println("ENV sensor initialized");
}

void loop() {
  // Read sensor data
  bool temperature_C = ENV.readTemperature();
  bool temperature_F = ENV.readTemperature(FAHRENHEIT);
  bool humidity = ENV.readHumidity();
  bool pressure = ENV.readPressure();
  bool luminosity = ENV.readIlluminance();

  if (Firebase.setBool(fbdo, "/temperature_C", temperature_C)) {
    Serial.println("Temperature_C updated successfully");
  } else {
    Serial.println("Failed to update Temperature_C");
  }

  if (Firebase.setBool(fbdo, "/temperature_F", temperature_F)) {
    Serial.println("Temperature_F updated successfully");
  } else {
    Serial.println("Failed to update Temperature_F");
  }

  if (Firebase.setBool(fbdo, "/humidity", humidity)) {
    Serial.println("Humidity updated successfully");
  } else {
    Serial.println("Failed to update Humidity");
  }

  if (Firebase.setBool(fbdo, "/pressure", pressure)) {
    Serial.println("Pressure updated successfully");
  } else {
    Serial.println("Failed to update Pressure");
  }

  if (Firebase.setBool(fbdo, "/luminosity", luminosity)) {
    Serial.println("Luminosity updated successfully");
  } else {
    Serial.println("Failed to update Luminosity");
  }

  // Print data to serial monitor
  Serial.print("Temperature_C: ");
  Serial.print(temperature_C);
  Serial.println(" °C");

  Serial.print("Temperature_F: ");
  Serial.print(temperature_F);
  Serial.println(" °F");

  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.println(" %");

  Serial.print("Pressure: ");
  Serial.print(pressure);
  Serial.println(" Pa");

  Serial.print("Luminosity: ");
  Serial.print(luminosity);
  Serial.println(" lux");

  delay(1000); // Wait for 1 second before sending data again
}

Does the FIREBASE_SECRET part cause the problem? I did define it properly but I didn't want to put it on GitHub. I am currently stuck on where the problem might be. Just in case, here are the steps I took to get the FIREBASE_SECRET:

  1. Went to project settings after clicking on the gear in the top left corner.
  2. Headed over to Service Accounts
  3. Clicked on Database Secrets and grabbed the thing from there. I only have one secret key so there's no possibility for me to mix it with something else.

Can anyone please help me?

mobizt commented 7 months ago

The database secret is deprecated for years but if you take it correctly as you described it's not the cause of your error.

You can get the error info from fbdo.errorReason().

This library works normally but it lags of feature and supports only database secret which is not recommended to use.

In addition. this library cannot include any more features as it designs to support low memory AVR device as UNO WiFi Rev.2 which is end of life now.

If you try this library for the first time and you use Arduino MKR device. I recommended using this new FirebaseClient library instead.

With the new library, you have to read the documentation and follow the examples to use it correctly.

Mukesh-Kalikaya commented 7 months ago

I will try the new FirebaseClient library just like you mentioned and I'll return back to you with the results. Thank you for the quick and helpful response!

mobizt commented 7 months ago

You can post the question or issue there.

I recommend you read the documentation (Readme) first (important), and follow the examples by reading the comments in the examples carefully.