我有一个Firebase数据库,我是唯一的用户。我正在从通过UDP触发的设备读取一个值,我使用time.sleep函数每5秒触发一次该设备,然后想在每次time.sleep刷新时将测量值写入我的数据库。 “时间”是我的文档,“值”是我的收藏。但是我的代码仅在第一次运行python代码时更新数据库。我希望它每次time.sleep()更新时都更新FireBase数据库。我要去哪里错了?
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
import datetime
import time
import socket
import csv
import datetime
from decimal import Decimal
now = datetime.datetime.now()
Time = str(now)
topsenspan = 50
mA = 16
appname = int(0)
cred = credentials.Certificate('/myfirebase certificate.json')
default_app = firebase_admin.initialize_app(cred)
while True:
ctu = True
if ctu == True:
appname = appname + 1
num = str(appname)
actappname = str("actapp" + num)
cred = credentials.Certificate('/myfirebase certificate.json')
other_app = firebase_admin.initialize_app(cred, name=actappname)
db = firestore.client()
ctu = False
def some_job(val):
now = datetime.datetime.now()
UDP_IP = "169.254.84.122"
UDP_PORT = 1025
MESSAGE = "#010\r"
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE.encode(encoding='utf-8'), (UDP_IP, UDP_PORT))
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
# print ("received message:", str(data))
actdata = str(data)
renddata = float(actdata[5:13])-4
scaledval = (topsenspan/mA)*renddata
output = round(scaledval,2)
finval = (output, now.strftime("%Y-%m-%d %H:%M"))
val = finval
return (finval)
data = some_job(1)
value = str(data)
u = value
#date = (finval[1])
#t = unicode(value, "utf-8")
doc_ref = db.collection(u'iot sens').document(Time)
doc_ref.set({
u'value': u,
})
print(value+" successfully written to database")
time.sleep(5)