programowanie:python:mqtt

Różnice

Różnice między wybraną wersją a wersją aktualną.

Odnośnik do tego porównania

Poprzednia rewizja po obu stronach Poprzednia wersja
Nowa wersja
Poprzednia wersja
programowanie:python:mqtt [2024/10/18 15:56] – [Subskrypcja] sasesprogramowanie:python:mqtt [2024/10/18 15:58] (aktualna) – [Publikacja] sases
Linia 9: Linia 9:
 <code python> <code python>
 import random import random
 +import logging
 from paho.mqtt import client as mqtt_client from paho.mqtt import client as mqtt_client
 +
 +logging.basicConfig(level=logging.INFO)
  
 broker = '127.0.0.1' broker = '127.0.0.1'
Linia 21: Linia 24:
     def on_connect(client, userdata, flags, rc):     def on_connect(client, userdata, flags, rc):
         if rc == 0:         if rc == 0:
-            print("Connected to MQTT Broker!")+            logging.info("Connected to MQTT Broker!")
         else:         else:
-            print(f"Failed to connect, return code {rc}")+            logging.error(f"Failed to connect, return code {rc}")
             # Attempt to reconnect             # Attempt to reconnect
             client.reconnect()             client.reconnect()
Linia 40: Linia 43:
 def subscribe(client: mqtt_client, topics): def subscribe(client: mqtt_client, topics):
     def on_message(client, userdata, msg):     def on_message(client, userdata, msg):
-        print(f"Received `{msg.payload.decode()}` from `{msg.topic}` topic")+        logging.info(f"Received `{msg.payload.decode()}` from `{msg.topic}` topic")
  
     for topic in topics:     for topic in topics:
Linia 56: Linia 59:
 status = result[0] status = result[0]
 if status == 0: if status == 0:
-   print(f"Send `{msg}` to topic `{topic}`")+   logging.info(f"Send `{msg}` to topic `{topic}`")
 else: else:
-   print(f"Failed to send message to topic {topic}")+   logging.error(f"Failed to send message to topic {topic}")
 </code> </code>
 \\ \\
  • programowanie/python/mqtt.1729259771.txt.gz
  • ostatnio zmienione: 2024/10/18 15:56
  • przez sases