2022年12月9日 星期五

TKinter + MQTT Pusbish Topic

 


import tkinter as tk

import paho.mqtt.client as mqtt


def on_subscribe(client, userdata, mid, granted_qos):

    nomTopic = zoneTopic.get()

    labelAbonnement.config(text = "" + nomTopic, fg = "blue")

    boutonMessage.config(state = "active")


def on_message(client, userdata, message):

    labelMessageRecu.config(text = "" + str(message.payload.decode("utf-8")), fg = "blue")


def fonctionIp():

    ip = zoneIP.get()

    client = mqtt.Client("alex9ufo")

    client.connect(ip)

    zoneIP.config(state = "disable")

    boutonIP.config(state = "disable")

    zoneTopic.config(state = "normal")

    boutonTopic.config(state = "active")


def souscrire():

    ip = zoneIP.get()

    client = mqtt.Client("alex9ufo")

    client.connect(ip)

    nomTopic = zoneTopic.get()

    client.on_message = on_message

    client.on_subscribe = on_subscribe

    client.loop_start()

    client.subscribe(nomTopic)

    zoneMessage.config(state = "normal")


def publier():

    ip = zoneIP.get()

    client = mqtt.Client("alex9ufo")

    client.connect(ip)

    nomTopic = zoneTopic.get()

    client.on_message = on_message

    client.on_subscribe = on_subscribe

    client.loop_start()

    client.subscribe(nomTopic)

    client.publish(nomTopic, zoneMessage.get())


win = tk.Tk()

win.geometry("800x450")

win.title("MQTT")

win.iconbitmap('icon.ico')

win.resizable(False, False)


label = tk.Label(win, text = "MQTT communication application", font = "size 14 underline", fg = "red")

label.grid(column = 0, row = 0, columnspan = 3, pady = 15)

label = tk.Label(win, text = "https://github.com/PouletEnSlip/MQTT", font = "size 8 underline", fg = "red")

label.grid(column = 0, row = 1, columnspan = 3, pady = 5)



label = tk.Label(win, text = "Broker IP:", font = "size 12", fg = "blue")

label.grid(column = 0, row = 2, sticky = 'w', padx =5)


zoneIP = tk.Entry(win, width = "30", font = "size 12")

zoneIP.insert(0, "broker.mqtt-dashboard.com")

zoneIP.grid(column = 0, row = 3, sticky = 'w', padx = 5, pady = 5)


boutonIP = tk.Button(win, text = "Validate IP", font = "size 12", command = fonctionIp)

boutonIP.grid(column = 2, row = 3, sticky = 'e', padx = 5, pady = 5)


label = tk.Label(win, text = "Topic name:", font = "size 12", fg = "blue")

label.grid(column = 0, row = 4, sticky = 'w', padx = 5)


zoneTopic = tk.Entry(win, width = "30", font = "size 12", state = "disable")

zoneTopic.grid(column = 0, row = 5, sticky = 'w', padx = 5, pady = 5)


boutonTopic = tk.Button(win, text = "Subscribe to topic", font = "size 12", command = souscrire, state = "disable")

boutonTopic.grid(column = 2, row = 5, sticky = 'e', padx = 5, pady = 5)


label = tk.Label(win, text = "")

label.grid(column = 0, row = 6)


label = tk.Label(win, text = "Subscription to topic:", font = "size 10")

label.grid(column = 0, row = 7, sticky = 'e', pady = 5)


labelAbonnement = tk.Label(win, text = "No topic...", font = "size 10", fg = "grey", width = "30")

labelAbonnement.grid(column = 1, row = 7)


label = tk.Label(win, text = "Message(s) received:", font = "size 10")

label.grid(column = 0, row = 8, sticky = 'e', pady = 5)


labelMessageRecu = tk.Label(win, text = "No message...", font = "size 10", fg = "grey", width = "30")

labelMessageRecu.grid(column = 1, row = 8)


label = tk.Label(win, text = "")

label.grid(column = 0, row = 9)


label = tk.Label(win, text = "Enter your message:", font = "size 12", fg = "blue")

label.grid(column = 0, row = 10, sticky = 'w', padx = 5)


zoneMessage = tk.Entry(win, width = "30", font = "size 12", state = "disable")

zoneMessage.grid(column = 0, row = 11, sticky = 'w', padx = 5, pady = 5)


boutonMessage = tk.Button(win, text = "Send message", font = "size 12", command = publier, state = "disable")

boutonMessage.grid(column = 2, row = 11, sticky = 'e', padx = 5, pady = 5)


boutonQuitter = tk.Button(text = "eXit", font = "size 12", bg = '#FF5020', width = 6, command = win.destroy)

boutonQuitter.grid(column = 2, row = 12, sticky = 'e', padx = 5)


label = tk.Label(text = "alex9ufo © 2022", font = "size 8")

label.grid(column = 0, row = 12, sticky = 'w')


win.mainloop()


沒有留言:

張貼留言

2024產專班 作業2 (純模擬)

2024產專班 作業2  (純模擬) 1) LED ON,OFF,TIMER,FLASH 模擬 (switch 控制) 2)RFID卡號模擬 (buttom  模擬RFID UID(不從ESP32) Node-Red 程式 [{"id":"d8886...