0
0
Iot-protocolsHow-ToBeginner · 4 min read

How to Use Google Firebase with Raspberry Pi: Simple Guide

To use Google Firebase with a Raspberry Pi, install the Firebase Python SDK and configure your Firebase project credentials on the Pi. Then, use Python code to read and write data to Firebase's real-time database or Firestore from your Raspberry Pi.
📐

Syntax

Here is the basic syntax to connect Raspberry Pi with Firebase using Python:

  • import firebase_admin: Imports Firebase admin SDK.
  • from firebase_admin import credentials, db: Imports credentials and database modules.
  • cred = credentials.Certificate('path/to/serviceAccountKey.json'): Loads your Firebase service account key.
  • firebase_admin.initialize_app(cred, {'databaseURL': 'https://your-db-url.firebaseio.com'}): Initializes Firebase app with credentials and database URL.
  • ref = db.reference('your/data/path'): Creates a reference to a location in the database.
  • ref.set({'key': 'value'}): Writes data to Firebase.
  • data = ref.get(): Reads data from Firebase.
python
import firebase_admin
from firebase_admin import credentials, db

cred = credentials.Certificate('path/to/serviceAccountKey.json')
firebase_admin.initialize_app(cred, {
    'databaseURL': 'https://your-db-url.firebaseio.com'
})

ref = db.reference('your/data/path')
ref.set({'key': 'value'})
data = ref.get()
print(data)
Output
{'key': 'value'}
💻

Example

This example shows how to write and read a simple message to Firebase Realtime Database from a Raspberry Pi using Python.

python
import firebase_admin
from firebase_admin import credentials, db
import time

# Load Firebase credentials
cred = credentials.Certificate('serviceAccountKey.json')

# Initialize Firebase app
firebase_admin.initialize_app(cred, {
    'databaseURL': 'https://your-db-url.firebaseio.com'
})

# Reference to 'messages' node
ref = db.reference('messages')

# Write data
ref.set({'message': 'Hello from Raspberry Pi!'})

# Wait a moment
time.sleep(1)

# Read data
data = ref.get()
print('Data from Firebase:', data)
Output
Data from Firebase: {'message': 'Hello from Raspberry Pi!'}
⚠️

Common Pitfalls

Common mistakes when using Firebase with Raspberry Pi include:

  • Not downloading the correct serviceAccountKey.json from Firebase console.
  • Using the wrong database URL or forgetting to enable Realtime Database in Firebase.
  • Not installing the firebase-admin Python package with pip install firebase-admin.
  • Incorrect file paths for the service account key.
  • Trying to use Firebase client SDKs instead of admin SDK on Raspberry Pi.

Always use the Firebase Admin SDK for server-side or device-side scripts like on Raspberry Pi.

python
## Wrong way (using client SDK, will fail):
# import pyrebase
# config = {...}
# firebase = pyrebase.initialize_app(config)

## Right way (admin SDK):
import firebase_admin
from firebase_admin import credentials, db

cred = credentials.Certificate('serviceAccountKey.json')
firebase_admin.initialize_app(cred, {'databaseURL': 'https://your-db-url.firebaseio.com'})
📊

Quick Reference

Tips for using Firebase with Raspberry Pi:

  • Use firebase-admin Python SDK, not client SDKs.
  • Download and securely store your serviceAccountKey.json.
  • Enable Realtime Database or Firestore in Firebase console.
  • Use correct database URL from Firebase project settings.
  • Install dependencies with pip install firebase-admin.
  • Test connection with simple read/write operations before complex logic.

Key Takeaways

Use Firebase Admin SDK for Python on Raspberry Pi to connect securely.
Always download and use your Firebase project's service account key JSON file.
Initialize Firebase with correct database URL to read/write data.
Install firebase-admin package using pip before running your code.
Avoid client SDKs; they are not designed for Raspberry Pi environments.