Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Antonio Villanueva Segura Tkinter MQTT IPX800V5 CTRL relai
- https://youtu.be/rt0O_GNc1sA
- """
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import tkinter as tk
- from tkinter import ttk
- import paho.mqtt.client as mqtt
- import time , json,threading
- import threading
- #import queue as Queue
- LOGIN="tony"
- PWD="icaro"
- TOPIC = "/strac/out/" #Topic
- address ="192.168.6.99" # Broker address UP with mosquitto MQTT server
- port =1883 # port
- class Automate(tk.Tk):
- def __init__(self):
- super().__init__()
- self.title('IPX800')
- self.resizable( False, False )
- #Créer une copie de l'état des relais dans l'IPX800 V5
- self.relais=[False] *9 #liste status relais ... [0]
- # Cree 8 buttons-relais
- for b in range (1,9):
- name =str(b)
- self.button = tk.Button(self, text=name, activebackground="red",
- command=lambda name=name :self.button_relai(name)) #Cree bouton num.
- """
- self.button = ttk.Button(self, text=name, activebackground="red",
- command=lambda name=name :self.button_relai(name)) #Cree bouton num.
- """
- self.button.pack(side=tk.LEFT)
- #self.queue = Queue.Queue()
- #MQTT Thread parallèle al thread principal tkinter
- self.running = 1
- self.thread1 = threading.Thread(target=self.Thread)
- self.thread1.start( )
- def button_relai(self,relai):
- """ click bouton-relai """
- if ( self.relais[ int(relai) ]==True):
- status= '{"state":false}' #dictionary
- else:
- status= '{"state":true}' #dictionary
- #Save new status
- #self.relais[int (relai)]= not self.relais[int (relai)]
- #Make TOPIC MQTT
- newTopic=TOPIC+str(relai) #Make TOPIC
- print ("Debug ",newTopic,status)
- while (not self.client_connect):
- print (" ... waiting client thread ... ")
- #publish PUB MQTT
- self.client.publish(newTopic,status) #Publish
- def on_connect(self,client,userdata,flags,rc):
- """ When the client receives a CONNACK message from the broker
- in response to the connect it generates an on_connect() callback. """
- self.client_connect=True
- print ("Connected to ",client._host,"port :",client._port)
- client.subscribe(TOPIC+'#',qos=0)
- def on_disconnect(self,client, userdata, rc):
- self.client_connect=False
- """ Called when the client disconnects from the broker. """
- def on_message(self,client, userdata, message):
- """ Called when a message has been received on a topic """
- self.listStateRelais(message) #mise à jour des états des relais
- #print(message.topic+" "+str(message.payload.decode("utf-8")))
- def on_subscribe(self,client, userdata, mid, granted_qos):
- """ Called when the broker responds to a subscribe request. """
- #print ("on_subscribe userdata ",userdata)
- def creeClient (self,name="Icarvs",login=LOGIN,pwd=PWD):
- self.client =mqtt.Client (name,
- clean_session=True,
- userdata=None,
- protocol=mqtt.MQTTv311,
- transport="tcp")
- """ calls backs"""
- self.client.on_connect=self.on_connect #Attach function to callback
- self.client.on_message=self.on_message #Attach function to callback
- self.client.on_subscribe=self.on_subscribe #Attach function to callback
- """ login & pwd """
- self.client.username_pw_set(login, password=pwd) #Login & Pwd
- """ Connect """
- self.client.connect (address,port,keepalive=60) #Connet to broker (host, port,keepalive, bind_address="")
- #self.client.loop_start() #start loop to process received messages
- def listStateRelais(self,message):
- """Écrire l'état d'un relais dans la liste relais local """
- self.relais[ int (message.topic[-1])]=all ( json.loads( (message.payload.decode("utf-8") ) ).values() )
- def Thread(self):
- """ MQTT Thread"""
- while self.running:
- self.creeClient()
- self.client.loop_start() #start loop to process received messages
- time.sleep( 0.1)
- #print ("Thread Debug")
- #msg = "un mensaje"
- #self.queue.put(msg)
- self.client.loop_stop()
- def endApplication(self):
- self.running = 0
- if __name__ == "__main__":
- app = Automate() #Instance automate tkinter
- #app.creeClient() #Client
- app.mainloop() #tkinter main loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement