NodeMCU与Firebase连接异常:调用getInt始终返回0的问题求助
Hey, let's dig into why your NodeMCU keeps returning 0 for Firebase.getInt("LEDStatus")—even with an invalid API key, which tells me the request isn't actually reaching Firebase (or the library is swallowing errors and returning a default value). Here are the key fixes and checks to try:
Fix the Firebase Host Format
YourFIREBASE_HOSTincludeshttps://and a trailing slash, but theFirebaseArduinolibrary expects just the base domain without those extras. Change it to:#define FIREBASE_HOST "*******-*****.firebaseio.com"The library handles the
https://protocol internally, so adding it manually breaks the request. This is likely the biggest culprit—an invalid host means the library can't connect at all, so it returns a default0instead of throwing a clear error.Reorder Your Error Handling
Right now you're printing thegetIntresult before checking if the request failed. When a request fails,Firebase.getInt()returns a default0, which is why you see that value even with a bad auth key. Move the error check immediately after the Firebase call to catch real issues:void loop() { int ledStatus = Firebase.getInt("LEDStatus"); // Check for failure FIRST if (Firebase.failed()) { Serial.print("HATA:"); Serial.println(Firebase.error()); delay(1000); // Add a longer delay to avoid spamming the serial monitor return; } Serial.println(ledStatus); digitalWrite(LED, ledStatus ? HIGH : LOW); delay(100); }This way you'll finally see actual error messages (like invalid host, auth failure, or permission denied) instead of just getting a generic 0.
Check Firebase Database Rules
If your database rules are set to require authentication (the default), even a valid auth key might not work if the library's authentication flow isn't completing. For testing, temporarily set your rules to allow public read/write (don't leave this permanent—it's insecure):{ "rules": { ".read": true, ".write": true } }If this fixes the issue, you'll need to adjust your rules later to properly restrict access while allowing your NodeMCU to authenticate.
Verify Library & Core Compatibility
TheFirebaseArduinolibrary is pretty outdated and might clash with newer ESP8266 core versions. Try updating your ESP8266 core in the Arduino IDE (go to File > Preferences > Additional Boards Manager URLs, addhttp://arduino.esp8266.com/stable/package_esp8266com_index.json, then update the core via Boards Manager). If that doesn't help, consider switching to the Firebase ESP Client Library—it's actively maintained and has better support for ESP8266/ESP32 devices.Double-Check Your Auth Key
Make sure you're using the correct database secret (not the general API key from the Firebase Console). The auth key forFirebaseArduinoshould be the Database Secret found under Project Settings > Service Accounts > Database Secrets (note: this feature is deprecated in newer Firebase projects, which is another good reason to switch to the newer library).
After making these changes, reupload your code and check the serial monitor—you should start seeing either the correct value from Firebase or clear error messages to debug further.
内容的提问来源于stack exchange,提问作者enes yıldız

