F
主程式
import network
import json
import time
import ujson
import ssl
import os
import gc
import urequests
#import firebase_admin
#from firebase_admin.firebase_admin import credentials
# WiFi 網路設定
#ssid = "Zouyk"
#password = "zouyk0219"
# Firebase 儲存庫的網址
FIREBASE_URL = **************************
ssid = "Zouyk"
password = "zouyk0219"
# 連接 WiFi
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
# 等待 WiFi 連線成功
while not wlan.isconnected():
time.sleep(1)
# 印出 IP 位置
print(wlan.ifconfig()[0])
設置 Firebase 認證信息
cred = credentials.Certificate("openmv-test-45904-firebase-adminsdk-lupkk-6d46339002.json")
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://openmv-test-45904-default-rtdb.firebaseio.com/'
})
儲存一個字串到 Firebase
my_string = 'Hello, Firebase!'
ref = db.reference('messages')
ref.set({'message': my_string})
# 定義一個函式,用來上傳資料到 Firebase
try:
def save_data_to_firebase(data):
headers = {'Content-type': 'application/json'}
response = urequests.put(FIREBASE_URL, data=ujson.dumps(data), headers=headers)
print('Firebase 回應:', response.content)
except Exception as e:
print('發生錯誤:', e)
# 儲存一個字串到 Firebase
my_string = 'Hello, Firebase!'
data = {'message': my_string}
save_data_to_firebase(data)