Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python
- # -*- coding: utf-8 -*-
- #
- # Support module generated by PAGE version 4.13
- # In conjunction with Tcl version 8.6
- # May 22, 2018 07:56:40 AM
- # ======================================================
- # mqttclient_support.py
- # ------------------------------------------------------
- # Written by G.D. Walters for Full Circle Magazine #134
- # Python in the REAL World # 84 June 2018
- # ======================================================
- import sys
- import paho.mqtt.client as mqtt
- import datetime
- import locale
- try:
- from Tkinter import *
- except ImportError:
- from tkinter import *
- try:
- import ttk
- py3 = False
- except ImportError:
- import tkinter.ttk as ttk
- py3 = True
- def set_Tk_var():
- global LastMessage
- LastMessage = StringVar()
- global HumidityValue
- HumidityValue = StringVar()
- global TempValue
- TempValue = StringVar()
- def on_btnQuit():
- global client
- print('mqttclient_support.on_btnQuit')
- sys.stdout.flush()
- client.loop_stop()
- destroy_window()
- def init(top, gui, *args, **kwargs):
- global w, top_level, root
- w = gui
- top_level = top
- root = top
- lang = "en_US.utf8"
- locale.setlocale(locale.LC_ALL, lang)
- set_our_globals()
- start_up()
- def set_our_globals():
- global MQTT_SERVER, MQTT_PATH1, MQTT_PATH2
- MQTT_SERVER = '192.168.1.224' # Enter the IP address of your broker
- MQTT_PATH1 = 'greghouse/dht22/humidity'
- MQTT_PATH2 = 'greghouse/dht22/temperature'
- def start_up():
- global client
- client = mqtt.Client()
- client.on_connect = on_connect
- client.on_message = on_message
- client.connect(MQTT_SERVER, 1883, 60)
- subscribe_to_topics()
- client.loop_start() # Start the client loop. Replaces loop_forever()
- def subscribe_to_topics():
- global MQTT_PATH1, MQTT_PATH2, client
- client.subscribe(MQTT_PATH1, qos=1)
- client.subscribe(MQTT_PATH2, qos=1)
- # Our Code Starts Here ...
- # Callbacks for MQTT
- def on_connect(client, userdata, flags, rc):
- print('on_connect: rc={0}'.format(rc))
- def on_message(client, userdata, message):
- tim = datetime.datetime.now().strftime('%x %X')
- print('Topic={0} Message={1}'.format(message.topic, message.payload))
- if 'humidity' in message.topic:
- HumidityValue.set(message.payload)
- elif 'temperature' in message.topic:
- TempValue.set(message.payload)
- else:
- print('unknown topic - {0} - {1}'.format(message.topic,
- message.payload))
- LastMessage.set(tim)
- def destroy_window():
- # Function which closes the window.
- global top_level
- top_level.destroy()
- top_level = None
- if __name__ == '__main__':
- import mqttclient
- mqttclient.vp_start_gui()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement