使用Python检索Firebase数据



我正在尝试制作一个由android应用程序控制的门锁系统。我已经安装了firebase,并给出了一个条件,根据锁定/解锁,将我的firebase值从0更改为1,从1更改为0,0用于锁定,1用于解锁。

现在我想从Python中检索数据,并将其发送到Ardiuno。我已经将Ardiuno连接到树莓派的串行端口。

我面临的问题是,下面的Python脚本只获取/检索之前存储在Firebase中的数据,就像如果我有0,它会连续打印0一样。我想在Firebase中每次更改时都对其进行更改。每次单击按钮时,Firebase数据都会发生变化,但Python脚本没有获得更新的数据。

from firebase import firebase
import serial
firebase = firebase.FirebaseApplication('https://smartdoor-922ad.firebaseio.com/smartdoor-922ad',)
result = firebase.get('smartdoor-922ad', '')
while True:
print(result)

更新数据后,您必须再次检索数据。现在,您正在一次又一次地打印检索到的数据。

from firebase import firebase
import serial
firebase = firebase.FirebaseApplication('https://smartdoor-922ad.firebaseio.com/smartdoor-922ad',)
while True:
result = firebase.get('smartdoor-922ad', '')
print(result)

而上面的脚本应该可以工作。当你不断呼叫防火基地时,会出现节流问题。要解决这个问题,请更新您的代码以使用Firebase触发器,这样您的代码只会在数据更新时运行。

https://firebase.google.com/docs/functions/database-events

使用firebase管理员来听取更改:

import firebase_admin
def listener(event):
print(event.event_type)  # can be 'put' or 'patch'
print(event.path)  # relative to the reference, it seems
print(event.data)  # new data at /reference/event.path. None if deleted
firebase_admin.db.reference('smartdoor-922ad').listen(listener)

最新更新