BLE网关V4 MQTT和使用python3获取信息msgpack.unpackb的问题

时间:2019-07-09 07:22:21

标签: python-3.x mqtt paho msgpack

我正在尝试将来自AB BLE网关V4的信息通过MQTT服务器用于python3。通过msgpack.unpack(有效负载)解压缩后,devices数组似乎被部分弄乱了:

import time
import paho.mqtt.client as mqtt
import msgpack

def on_subscribe(mosq, obj, mid, granted_qos):
    print("Subscribed: " + str(mid))

def on_message(mosq, obj, msg):
    for d in msgpack.unpackb(msg.payload)[b'devices']:
      print("=============================================")
      # adv type
      print(d)
      print("type:", ord(d[0]))
      print("mac:{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}".format(ord(d[1]), ord(d[2]), ord(d[3]), ord(d[4]), ord(d[5]), ord(d[6])))
      print("rssi:", ord(d[7]) - 256)
      hex_chars = map(hex,map(ord,d))
      del hex_chars[:8]
      print("adv:", hex_chars)

def on_connect(mosq, obj,flags, rc):
    print("Connected with result code "+str(rc))
    mqttc.subscribe("/test", 0)
    print("Connected")


mqttc = mqtt.Client()
mqttc.on_connect = on_connect
mqttc.on_subscribe = on_subscribe
mqttc.on_message = on_message
mqttc.connect("mqtt.bconimg.com", 1883, 60)
mqttc.loop_forever()

我希望输出类似

"type: 3
mac:2026698D97F1
rssi: -66"

但是我得到的输出是打击

 "Connected with result code 0
Connected
Subscribed: 1
=============================================
b'\x03\x16*)\x82|\xa7\xa7\x1e\xff\x06\x00\x01\t \x02\x1b6.\xe7P\xdf\xe9\xc3\xe6r\xca\x04H\x96!\xc0z~+\xd9\x13\x91?'
=============================================
b'\x037~M(\x83A\xaa\x1e\xff\x06\x00\x01\t \x02L\x90Ps\xb8\x9e\x80\x996Vx\xe3N\\\x1f\x0c\x8eHE\x0eN\xaf\xbd'"

1 个答案:

答案 0 :(得分:0)

从此行删除ord,您会很方便。

要更改的行

print("mac:{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}".format(ord(d[1]), ord(d[2]), ord(d[3]), ord(d[4]), ord(d[5]), ord(d[6])))