Advertisement
gregwa

FCM 134 - mqttclient_support.py

May 22nd, 2018
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.82 KB | None | 0 0
  1. #! /usr/bin/env python
  2. #  -*- coding: utf-8 -*-
  3. #
  4. # Support module generated by PAGE version 4.13
  5. # In conjunction with Tcl version 8.6
  6. #    May 22, 2018 07:56:40 AM
  7.  
  8. # ======================================================
  9. #             mqttclient_support.py
  10. # ------------------------------------------------------
  11. # Written by G.D. Walters for Full Circle Magazine #134
  12. # Python in the REAL World # 84 June 2018
  13. # ======================================================
  14. import sys
  15. import paho.mqtt.client as mqtt
  16. import datetime
  17. import locale
  18.  
  19. try:
  20.     from Tkinter import *
  21. except ImportError:
  22.     from tkinter import *
  23.  
  24. try:
  25.     import ttk
  26.     py3 = False
  27. except ImportError:
  28.     import tkinter.ttk as ttk
  29.     py3 = True
  30.  
  31.  
  32. def set_Tk_var():
  33.     global LastMessage
  34.     LastMessage = StringVar()
  35.     global HumidityValue
  36.     HumidityValue = StringVar()
  37.     global TempValue
  38.     TempValue = StringVar()
  39.  
  40.  
  41. def on_btnQuit():
  42.     global client
  43.     print('mqttclient_support.on_btnQuit')
  44.     sys.stdout.flush()
  45.     client.loop_stop()
  46.     destroy_window()
  47.  
  48.  
  49. def init(top, gui, *args, **kwargs):
  50.     global w, top_level, root
  51.     w = gui
  52.     top_level = top
  53.     root = top
  54.     lang = "en_US.utf8"
  55.     locale.setlocale(locale.LC_ALL, lang)
  56.     set_our_globals()
  57.     start_up()
  58.  
  59.  
  60. def set_our_globals():
  61.     global MQTT_SERVER, MQTT_PATH1, MQTT_PATH2
  62.     MQTT_SERVER = '192.168.1.224'  # Enter the IP address of your broker
  63.     MQTT_PATH1 = 'greghouse/dht22/humidity'
  64.     MQTT_PATH2 = 'greghouse/dht22/temperature'
  65.  
  66.  
  67. def start_up():
  68.     global client
  69.     client = mqtt.Client()
  70.     client.on_connect = on_connect
  71.     client.on_message = on_message
  72.     client.connect(MQTT_SERVER, 1883, 60)
  73.     subscribe_to_topics()
  74.     client.loop_start()  # Start the client loop.  Replaces loop_forever()
  75.  
  76.  
  77. def subscribe_to_topics():
  78.     global MQTT_PATH1, MQTT_PATH2, client
  79.     client.subscribe(MQTT_PATH1, qos=1)
  80.     client.subscribe(MQTT_PATH2, qos=1)
  81.  
  82.  
  83. # Our Code Starts Here ...
  84. # Callbacks for MQTT
  85. def on_connect(client, userdata, flags, rc):
  86.     print('on_connect: rc={0}'.format(rc))
  87.  
  88.  
  89. def on_message(client, userdata, message):
  90.     tim = datetime.datetime.now().strftime('%x %X')
  91.     print('Topic={0} Message={1}'.format(message.topic, message.payload))
  92.     if 'humidity' in message.topic:
  93.         HumidityValue.set(message.payload)
  94.     elif 'temperature' in message.topic:
  95.         TempValue.set(message.payload)
  96.     else:
  97.         print('unknown topic - {0} - {1}'.format(message.topic,
  98.                                                  message.payload))
  99.     LastMessage.set(tim)
  100.  
  101.  
  102. def destroy_window():
  103.     # Function which closes the window.
  104.     global top_level
  105.     top_level.destroy()
  106.     top_level = None
  107.  
  108.  
  109. if __name__ == '__main__':
  110.     import mqttclient
  111.     mqttclient.vp_start_gui()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement