Spaces:
Sleeping
Sleeping
File size: 3,140 Bytes
eb77a04 b598381 eb77a04 725a9ba eb77a04 5aac0d0 eb77a04 0c9d528 b598381 0c9d528 eb77a04 725a9ba b598381 725a9ba eb77a04 b598381 eb77a04 725a9ba eb77a04 b598381 eb77a04 725a9ba eb77a04 725a9ba eb77a04 725a9ba eb77a04 dbf0e29 eb77a04 37f4cc2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | import gradio as gr
import paho.mqtt.client as mqtt
import pandas as pd
import time
from influxdb_client_3 import InfluxDBClient3
import threading
from datetime import datetime
import plotly.express as px
import plotly.graph_objects as go
import os
import logging
logger = logging.getLogger(__name__)
# Global variables
data_points = []
MAX_DATA_POINTS = 10
# InfluxDB configuration
INFLUXDB_URL = "https://us-east-1-1.aws.cloud2.influxdata.com/"
INFLUXDB_TOKEN = os.getenv("INFLUXDB_TOKEN")
INFLUXDB_ORG = "dd7895c825a7c36a"
INFLUXDB_BUCKET = "machine_monitoring"
influx_client = InfluxDBClient3(host=INFLUXDB_URL, token=INFLUXDB_TOKEN, org=INFLUXDB_ORG,database="machine_monitoring")
# MQTT setup
MQTT_BROKER = "broker.hivemq.com"
MQTT_TOPIC = "test_fan/current"
def query_influxdb_gr():
query = '''
SELECT *
FROM "fan"
WHERE time >= now() - interval '1 week'
'''
try:
table = influx_client.query(query=query, language="sql")
df = table.to_pandas().sort_values(by="time")
return df
except:
logger.error('InfluxDB Integration Error')
KeyError("Error with InfluxDB Integration")
def on_connect(client, userdata, flags, reason_code, properties=None):
logger.info(f"Connected with result code {reason_code}")
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe(MQTT_TOPIC)
def on_message(client, userdata, message):
global data_points
try:
payload = message.payload.decode()
value = float(payload) # Assume the payload is a single float value
logger.info(f'received value: {value}')
data_points.append({"timestamp": time.time(), "current": value, "datetime": datetime.now().strftime('%H:%M:%S')})
# Keep only the last MAX_DATA_POINTS
if len(data_points) > MAX_DATA_POINTS:
data_points.pop(0) # Remove the oldest data point
except ValueError:
logger.error(f"Received invalid payload: {payload}")
# Set up MQTT client
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2,protocol=mqtt.MQTTv5,transport='websockets')
client.on_message = on_message
client.on_connect = on_connect
client.connect(MQTT_BROKER, 8000, 60)
# Start MQTT loop in a separate thread
mqtt_thread = threading.Thread(target=client.loop_start)
mqtt_thread.start()
# Gradio components
def update_mqtt_live_data():
# Convert timestamps to formatted time strings
if data_points:
df = pd.DataFrame(data_points)
return df
else:
df = pd.DataFrame([{"timestamp": time.time(), "current": 0, "datetime": datetime.now().strftime('%H:%M:%S')}])
return df
with gr.Blocks() as app:
gr.Markdown("# MQTT Data Visualization")
gr.Markdown(f"Listening to MQTT topic: {MQTT_TOPIC}")
line_plot = gr.LinePlot(update_mqtt_live_data, label="MQTT", x="datetime", y="current", every=1)
gr.Markdown("# InfluxDB Data Visualization")
scatter_plot = gr.ScatterPlot(query_influxdb_gr, label="InfluxDB", x="time", y="current", every=300)
# Launch the app
app.launch()
|