iT邦幫忙

2

IoT Project ( Flutter + Python + ZigBee ) 冒險闖關記 -- Part 2

In time, you will call me master -- Star Wars

  • ZigBee to Firestore (衝呀) :
    • 事情是這樣的 ZigBee JN5169 會send message到 一個 usb dongle
    • 原本是用一個 C# 的程式觀看 ZigBee 傳到 USB dongle 的資料
    • 於是想到可以用 python 去讀 USB dongle COM port 資料 (python import serial)
      • python 讀取 資料並轉成 16進位
        COM_PORT = 'XXX'
        BAUD_RATES = xxxxx
        #initailize connection
        ser = 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 0x10
          • for example : xor 回原本的把 1 去掉
            https://ithelp.ithome.com.tw/upload/images/20200716/20116751wPaKpQZmKS.jpg
        • 現在有了byte stuffing 前的正確資料
          • 開始解析 msg type = 0x8102 然後用data type 解析 看每個attribute
          • https://ithelp.ithome.com.tw/upload/images/20200716/20116751UhRlkfsX5T.jpg
          • for example
            https://ithelp.ithome.com.tw/upload/images/20200716/20116751UolIuCZCq2.jpg
          • cluster 0x0400 : light level, cluster 0x0402 : temperature
          • data 欄位 hex to int 我們可以知道 溫度, 亮度值
        • python 處理 data 不怎麼難 用 dictionary 把 這些欄位 裝進去就好
        • 連接 firestore 把資料上傳上去
          import firebase_admin
          from firebase_admin import credentials
          from firebase_admin import firestore
          cred = credentials.Certificate('./ServiceAccountKey.json')
          firebase_admin.initailize_app(cred)
          db = firestore.client()
          doc_ref = db.collection("database name")
          # upload data to firestore
          doc_ref.document(your key).set(your value)
          
        • 完成結果
          https://ithelp.ithome.com.tw/upload/images/20200716/201167517QaPiTC8Ap.png

Next blog post : flutter firestore + flutter chart


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言