2022年12月28日 星期三

Paho Python MQTT Client – on_log Examples

Paho Python MQTT Client – on_log  Examples

on_log()

1.  on_log(client, userdata, level, buf)

當用戶端有日誌資訊時調用

  • level
    • 消息嚴重性
      • MQTT_LOG_INFO
      • MQTT_LOG_NOTICE
      • MQTT_LOG_WARNING
      • MQTT_LOG_ERR
      • MQTT_LOG_DEBUG
  • buf
    • 該消息本身就在buf
      可以與標準的Python logging同時使用,通過enable_logger()方法啟用

示例

 

import paho.mqtt.client as mq_tt

import time

import logging


logging.basicConfig(level='DEBUG', format='%(asctime)s [%(name)s:%(lineno)d] [%(levelname)s]- %(message)s')


def on_connect(client, userdata, flags, rc):

    print("Connected with result code "+str(rc))

    client.subscribe("alex9ufo/sensor/#")


def topic_one_callback(client, userdata, message):

    print(message.topic+" "+str(message.payload))



def topic_two_callback(client, userdata, message):

    print(message.topic+" "+str(message.payload))



mq_client = mq_tt.Client(client_id='alex9ufo_pts')

mq_client.enable_logger()

mq_client.on_connect = on_connect

mq_client.message_callback_add("alex9ufo/sensor/temperature", topic_one_callback)

mq_client.message_callback_add("alex9ufo/sensor/humidity", topic_two_callback)

# 連接到EMQX Broker MQTT代理

mq_client.connect("broker.hivemq.com", 1883, 60)


mq_client.loop_start()



>>> %Run Subscribe_4.py

2022-12-28 20:59:15,966 [paho.mqtt.client:2529] [DEBUG]- Sending CONNECT (u0, p0, wr0, wq0, wf0, c1, k60) client_id=b'alex9ufo_pts'

>>> 2022-12-28 20:59:16,271 [paho.mqtt.client:2529] [DEBUG]- Received CONNACK (0, 0)

Connected with result code 0

2022-12-28 20:59:16,271 [paho.mqtt.client:2529] [DEBUG]- Sending SUBSCRIBE (d0, m1) [(b'alex9ufo/sensor/#', 0)]

2022-12-28 20:59:16,570 [paho.mqtt.client:2529] [DEBUG]- Received SUBACK

2022-12-28 20:59:37,690 [paho.mqtt.client:2529] [DEBUG]- Received PUBLISH (d0, q0, r0, m0), 'alex9ufo/sensor/humidity', ...  (2 bytes)

alex9ufo/sensor/humidity b'75'

2022-12-28 20:59:57,767 [paho.mqtt.client:2529] [DEBUG]- Received PUBLISH (d0, q0, r0, m0), 'alex9ufo/sensor/temperature', ...  (2 bytes)

alex9ufo/sensor/temperature b'28'

2022-12-28 21:00:15,917 [paho.mqtt.client:2529] [DEBUG]- Sending PINGREQ

2022-12-28 21:00:16,201 [paho.mqtt.client:2529] [DEBUG]- Received PINGRESP


 

沒有留言:

張貼留言

113 學年度第 1 學期 RFID應用課程 Arduino程式

113 學年度第 1 學期 RFID應用課程 Arduino程式 https://www.mediafire.com/file/zr0h0p3iosq12jw/MFRC522+(2).7z/file 內含修改過後的 MFRC522 程式庫 (原程式有錯誤) //定義MFRC522...