ZigBee to Firestore (冲呀) :事情是这样的 ZigBee JN5169 会send message到 一个 usb dongle原本是用一个 C# 的程式观看 ZigBee 传到 USB dongle 的资料于是想到可以用 python 去读 USB dongle COM port 资料 (In time, you will call me master -- Star Wars
python import serial
)python 读取 资料并转成 16进位COM_PORT = 'XXX'BAUD_RATES = xxxxx#initailize connectionser = serial.Serial(COM_PORT, BAUD_RATES, stopbits = 1 ) while ser.in_waiting: # if receive data # byte type raw data data_raw = ser.read() # byte to hex data_hex = data_raw.hex()
处理 资料ZigBee Serial Protocol & Message Characteristics封包开始跟结尾是固定的 ( start
: 0x01 , end
: 0x03 )小于 0x10 的是 special character为了要分清哪里是封包开头跟结尾,只要有小于 0x10 byte 的做 byte stuffing
变成前面 一个0x02 escape character + 小于 0x10 byte xor
0x10for example : xor
回原本的把 1 去掉


cluster
0x0400 : light level, cluster 0x0402 : temperature从 data
栏位 hex to int 我们可以知道 温度, 亮度值python 处理 data 不怎么难 用 dictionary
把 这些栏位 装进去就好连接 firestore 把资料上传上去import firebase_adminfrom firebase_admin import credentialsfrom firebase_admin import firestorecred = credentials.Certificate('./ServiceAccountKey.json')firebase_admin.initailize_app(cred)db = firestore.client()doc_ref = db.collection("database name")# upload data to firestoredoc_ref.document(your key).set(your value)
完成结果
Next blog post : flutter firestore + flutter chart