import paho.mqtt.client as mqtt
import random
import json
import datetime
import time
# 設置日期時間的格式
ISOTIMEFORMAT = '%m/%d %H:%M:%S'
# 連線設定
# 初始化地端程式
client = mqtt.Client()
# 設定登入帳號密碼
client.username_pw_set("alex9ufo","alex9981")
# 設定連線資訊(IP, Port, 連線時間)
client.connect("broker.mqtt-dashboard.com", 1883, 60)
while True:
t0 = random.randint(0,30)
t = datetime.datetime.now().strftime(ISOTIMEFORMAT)
payload = {'Temperature' : t0 , 'Time' : t}
print (json.dumps(payload))
#要發布的主題和內容
client.publish("alex9ufo/MQTT/Test", json.dumps(payload))
time. Sleep(5)
>>> %Run test2.py
{"Temperature": 29, "Time": "12/09 19:51:10"}
{"Temperature": 26, "Time": "12/09 19:51:15"}
{"Temperature": 20, "Time": "12/09 19:51:20"}
{"Temperature": 17, "Time": "12/09 19:51:25"}
{"Temperature": 15, "Time": "12/09 19:51:30"}
{"Temperature": 23, "Time": "12/09 19:51:35"}
{"Temperature": 16, "Time": "12/09 19:51:40"}
{"Temperature": 7, "Time": "12/09 19:51:45"}
{"Temperature": 28, "Time": "12/09 19:51:50"}
{"Temperature": 6, "Time": "12/09 19:51:55"}
{"Temperature": 27, "Time": "12/09 19:52:00"}
{"Temperature": 8, "Time": "12/09 19:52:05"}
{"Temperature": 26, "Time": "12/09 19:52:10"}
{"Temperature": 23, "Time": "12/09 19:52:15"}
{"Temperature": 2, "Time": "12/09 19:52:20"}
{"Temperature": 11, "Time": "12/09 19:52:25"}
{"Temperature": 16, "Time": "12/09 19:52:30"}
{"Temperature": 22, "Time": "12/09 19:52:35"}
{"Temperature": 28, "Time": "12/09 19:52:40"}
{"Temperature": 23, "Time": "12/09 19:52:45"}
{"Temperature": 5, "Time": "12/09 19:52:50"}
{"Temperature": 22, "Time": "12/09 19:52:55"}
{"Temperature": 25, "Time": "12/09 19:53:00"}
{"Temperature": 26, "Time": "12/09 19:53:05"}
import paho.mqtt.client as mqtt
import random
import json
import datetime
import time
# 設置日期時間的格式
ISOTIMEFORMAT = '%m/%d %H:%M:%S'
# 連線設定
# 初始化地端程式
client = mqtt.Client()
# 設定登入帳號密碼
client.username_pw_set("alex9ufo","alex9981")
# 設定連線資訊(IP, Port, 連線時間)
client.connect("broker.mqtt-dashboard.com", 1883, 60)
while True:
t0 = random.randint(0,30)
t = datetime.datetime.now().strftime(ISOTIMEFORMAT)
payload = {'Temperature' : t0 , 'Time' : t}
print (json.dumps(payload))
#要發布的主題和內容
client.publish("alex9ufo/MQTT/Test", json.dumps(payload))
time. Sleep(5)
>>> %Run test3.py
{"Temperature": 14, "Time": "12/09 19:55:19"}
{"Temperature": 12, "Time": "12/09 19:55:24"}
{"Temperature": 7, "Time": "12/09 19:55:29"}
{"Temperature": 12, "Time": "12/09 19:55:34"}
{"Temperature": 15, "Time": "12/09 19:55:39"}
{"Temperature": 26, "Time": "12/09 19:55:44"}
{"Temperature": 15, "Time": "12/09 19:55:49"}
import paho.mqtt.client as mqtt
import random
import json
import datetime
import time
# 設置日期時間的格式
ISOTIMEFORMAT = '%m/%d %H:%M:%S'
# 連線設定
# 初始化地端程式
client = mqtt.Client()
# 設定登入帳號密碼
client.username_pw_set("alex9ufo","alex9981")
# 設定連線資訊(IP, Port, 連線時間)
client.connect("broker.mqtt-dashboard.com", 1883, 60)
while True:
t0 = random.randint(0,30)
t = datetime.datetime.now().strftime(ISOTIMEFORMAT)
payload = {'Temperature' : t0 , 'Time' : t}
print (json.dumps(payload))
#要發布的主題和內容
client.publish("alex9ufo/MQTT/Test", json.dumps(payload))
time. Sleep(5)
>>> %Run Publish_MQTT.py
{"Temperature": 3, "Time": "12/09 19:56:32"}
{"Temperature": 24, "Time": "12/09 19:56:37"}
{"Temperature": 22, "Time": "12/09 19:56:42"}
{"Temperature": 15, "Time": "12/09 19:56:47"}
{"Temperature": 24, "Time": "12/09 19:56:52"}
{"Temperature": 11, "Time": "12/09 19:56:57"}
{"Temperature": 4, "Time": "12/09 19:57:02"}
{"Temperature": 7, "Time": "12/09 19:57:07"}
{"Temperature": 0, "Time": "12/09 19:57:12"}
{"Temperature": 17, "Time": "12/09 19:57:17"}
{"Temperature": 16, "Time": "12/09 19:57:22"}
{"Temperature": 0, "Time": "12/09 19:57:27"}
{"Temperature": 7, "Time": "12/09 19:57:32"}
{"Temperature": 18, "Time": "12/09 19:57:37"}
{"Temperature": 2, "Time": "12/09 19:57:42"}
{"Temperature": 29, "Time": "12/09 19:57:47"}
{"Temperature": 17, "Time": "12/09 19:57:52"}
import paho.mqtt.client as mqtt
# 當地端程式連線伺服器得到回應時,要做的動作
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# 將訂閱主題寫在on_connet中
# 如果我們失去連線或重新連線時
# 地端程式將會重新訂閱
client.subscribe("alex9ufo/MQTT/Test")
# 當接收到從伺服器發送的訊息時要進行的動作
def on_message(client, userdata, msg):
# 轉換編碼utf-8才看得懂中文
print(msg.topic+" "+ msg.payload.decode('utf-8'))
# 連線設定
# 初始化地端程式
client = mqtt.Client()
# 設定連線的動作
client.on_connect = on_connect
# 設定接收訊息的動作
client.on_message = on_message
# 設定登入帳號密碼
client.username_pw_set("alex9ufo","alex9981")
# 設定連線資訊(IP, Port, 連線時間)
client.connect("broker.mqtt-dashboard.com", 1883, 60)
# 開始連線,執行設定的動作和處理重新連線問題
# 也可以手動使用其他loop函式來進行連接
client.loop_forever()
Python 3.10.1 (tags/v3.10.1:2cd268a, Dec 6 2021, 19:10:37) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
================== RESTART: D:\TKinter\MQTT\Subscribe_MQTT.py ==================
Connected with result code 0
alex9ufo/MQTT/Test {"Temperature": 7, "Time": "12/09 19:57:32"}
alex9ufo/MQTT/Test {"Temperature": 18, "Time": "12/09 19:57:37"}
沒有留言:
張貼留言