Created
June 22, 2021 02:28
-
-
Save Ajak58a/de86aa2a9199da7f7f22067b41ffaf20 to your computer and use it in GitHub Desktop.
dashboard.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import paho.mqtt.client as mqtt | |
import paho.mqtt.publish as publish | |
import random | |
import time | |
import os | |
from devices import * | |
from fn_callbacks import Device_1,Device_2 | |
welcome_msg = (""" | |
___ _ ___ _ _ _ | |
| \ _____ _(_)__ ___ ___ | \ __ _ __| |_ | |__ ___ __ _ _ _ __| | | |
| |) / -_) V / / _/ -_|_-< | |) / _` (_-< ' \| '_ \/ _ \/ _` | '_/ _` | | |
|___/\___|\_/|_\__\___/__/ |___/\__,_/__/_||_|_.__/\___/\__,_|_| \__,_| | |
_______________________________________________________________________V 1.0 | |
""") | |
device_id = device_1 | |
live_status = "" | |
sensor_values = "" | |
device_msg = "" | |
device_number = 0 | |
# The callback for when the client receives a CONNACK response from the server. | |
def on_connect(client, userdata, flags, rc): | |
print("Connected with result code "+str(rc)) | |
# Subscribing in on_connect() means that if we lose the connection and | |
# reconnect then subscriptions will be renewed. | |
client.subscribe("Dashboard",0) | |
# client.subscribe(devices_names[0],tcount) | |
# tcount = tcount +1 | |
# client.subscribe(devices_names[1],tcount) | |
for topic in devices_names: | |
client.subscribe(topic) | |
# The callback for when a PUBLISH message is received from the server. | |
def on_message(client, userdata, msg): | |
global device_msg | |
message = str(msg.payload) | |
#print("MSG:"+str(count)+str(msg.payload)) | |
#count = count + 1 | |
device_msg = message | |
print message | |
def status(device_id,number): | |
global devices_names_live | |
global devices_names_debug | |
global devices_names_sensor | |
print '\r' + "DEVICE Name:",device_id, '\t', "STATUS:",devices_names_live[number], '\t', "SENSOR DATA:",devices_names_sensor[number], '\t', "MESSAGE:",devices_names_debug[number], | |
def show_screen(): | |
while True: | |
global device_id,device_msg,live_status | |
#print '\r' + "DEVICE ID", '\t', "STATUS", '\t', "SENSOR DATA", '\t', "DEVICE MESSAGE", | |
print welcome_msg | |
for i in devices_names: | |
global device_number | |
status(i,device_number) | |
device_number = device_number + 1 | |
device_number = 0 | |
time.sleep(1) | |
a = 1 | |
os.system("cls") | |
client = mqtt.Client() | |
client.on_connect = on_connect | |
client.on_message = on_message #debug messages | |
client.message_callback_add(devices_names[0],Device_1) | |
client.message_callback_add(devices_names[1],Device_2) | |
client.connect("broker.hivemq.com", 1883, 60) | |
# Blocking call that processes network traffic, dispatches callbacks and | |
# handles reconnecting. | |
# Other loop*() functions are available that give a threaded interface and a | |
# manual interface. | |
client.loop_start() | |
os.system("start python command_control.py") | |
show_screen() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment