Python Forum
How to use an Emulator and a MttQ broker?
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use an Emulator and a MttQ broker?
#6
Hello,

I did some change.
In Def() Histo: why ct variable wont appear?
Lumière salon fermée:-", ct, but Lumière salon fermée: will.
Thank you

from tkinter import *
import datetime
ct = datetime.datetime.now()
from RPiSim import GPIO
from time import sleep
import RPi.GPIO as GPIO
import paho.mqtt.client as mqtt 
from random import randrange, uniform
from time import sleep
#import pymongo
Ent= 22
Sal=18

#client = pymongo.MongoClient("localhost")
#db = client.Timestamp.py

#collection = db.alarmes


mqttBroker ="mqtt.eclipseprojects.io"
client = mqtt.Client("Alarme_Interieur")
client.connect(mqttBroker) 
  
root = Tk()
root.title('Ma console')  
root.geometry('430x800')
  
title = Label(root, text="Alarme", bg="grey", font=("bold", 20))
title.pack()
c = Canvas(root, width=330, height=160, bg="grey")
c.place(x=50, y=50)

def AlarmeOn():
    Label(root, text="On", bg="red", font=("bold", 20)).place(x = 190, y = 210)
    print("Alarme armée:-", ct)
    client.publish("ALARME", str(ct))
    print("Publication message" + str(ct) + "Alarme armée")
    #time = {"Alarme armée": str(ct)}
    #print(collection.insert_one(time).inserted_id)

btn = Button(root, text='On', width=10,
             height=5, bd='10', command = AlarmeOn)

def AlarmeOff():
   Label(root, text="Off", bg="red", font=("bold", 20)).place(x = 190, y = 210)
   print("Alarme désarmée:-", ct)
   client.publish("ALARME", str(ct))
   print("Publication message" + str(ct) + "Alarme déssarmée")
    
btn1 = Button(root, text='Off', width=10,
             height=5, bd='10', command = AlarmeOff)
 
btn.place(x=65, y=70)
btn1.place(x=225, y=70)


#2e paire de boutons

c1 = Canvas(root, width=330, height=200, bg="grey")
c1.place(x=50, y=250)

def EntOn():
   Label(root, text="On", bg="red", font=("bold", 20)).place(x = 190, y = 420)
   GPIO.setmode(GPIO.BCM)
   GPIO.setup(Ent,GPIO.OUT)
   GPIO.setwarnings(False)
   GPIO.output(Ent,GPIO.HIGH)
   print("Pin 22 - HIGH")
   GPIO.cleanup()
    #print("Lumière entrée allumée:-", ct)
    #client.publish("ALARME", str(ct))
    #print("Publication message" + str(ct) + "Alarme entrée")

btn2 = Button(root, text='On', width=10,
             height=5, bd='10', command = EntOn)

def EntOff():
   Label(root, text="Off", bg="red", font=("bold", 20)).place(x = 190, y = 420)
   print("Lumière entrée fermée:-", ct)
   client.publish("ALARME", str(ct))
   print("Publication message" + str(ct) + "Alarme entrée off")

btn3 = Button(root, text='Off', width=10,
             height=5, bd='10', command = EntOff)
 
btn2.place(x=65, y=300)
btn3.place(x=225, y=300)
title1 = Label(root, text="Lumière entrée", bg="grey", font=("bold", 20)).place(x = 110, y = 255)
#title1.pack() 
  
#3e paire de boutons

c2 = Canvas(root, width=330, height=280, bg="grey")
c2.place(x=50, y=450)

def SalonOn():
   Label(root, text="On", bg="red", font=("bold", 20)).place(x = 190, y = 620)
   GPIO.setmode(GPIO.BCM)
   GPIO.setup(Sal,GPIO.OUT)
   GPIO.setwarnings(False)
   GPIO.output(Sal,GPIO.HIGH)
   print("Pin 22 - HIGH")
   GPIO.cleanup()
   print("Lumière salon allumée:-", ct)
   client.publish("ALARME", str(ct))
   print("Publication message" + str(ct) + "Alarme Salon On")

btn4 = Button(root, text='On', width=10,
             height=5, bd='10', command = SalonOn)

def SalonOff():
    Label(root, text="Off", bg="red", font=("bold", 20)).place(x = 190, y = 620)
    print("Lumière salon fermée:-", ct)
    client.publish("ALARME", str(ct))
    print("Publication message" + str(ct) + "Alarme Salon Off")

btn5 = Button(root, text='Off', width=10,
             height=5, bd='10', command = SalonOff)
 
btn4.place(x=65, y=500)
btn5.place(x=225, y=500)
title2 = Label(root, text="Lumière salon", bg="grey", font=("bold", 20)).place(x = 110, y = 460)
#title2.pack()
  
  
#3e Bouton historique

c3 = Canvas(root, width=330, height=280, bg="grey")
c3.place(x=50, y=650)

def Histo():
     root = Toplevel()
     root.title('Historique')  
     root.geometry('630x450')
     resultat = Text(root)
     #resultat.config(width=630, height = 450)
     resultat.insert(INSERT, "Date\tHeure\tÉvènement\n===========================\n")
     #db.name
     #db.list_collection_names()
     #for item in db.alarmes.find():
     #if EntOn():
     resultat.insert(INSERT, "Lumière salon fermée:-",  ct)
     resultat.grid(row = 0, column = 0, columnspan = 2)
     bouton = Button(root, text = "Quitter", command = root.destroy)
     bouton.grid(row = 1, column = 1)
     
     
     
btn6 = Button(root, text='Afficher', width=28,
             height=3, bd='10', command = Histo)
 
btn6.place(x=65, y=700)
title3 = Label(root, text="Historique", bg="grey", font=("bold", 20)).place(x = 150, y = 660)
#title3.pack()
    
 
root.mainloop()
Reply


Messages In This Thread
RE: How to use an Emulator and a MttQ broker? - by Frankduc - Jul-02-2022, 06:45 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pyautogui with a display emulator? gumby4231 0 2,641 Jul-30-2020, 02:46 PM
Last Post: gumby4231
  running python script from shell invoked with os.system("x-terminal-emulator -e /bin/ markhaus 2 3,129 Feb-21-2019, 11:55 PM
Last Post: markhaus

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020