Created
May 13, 2022 19:25
-
-
Save tolgahancepel/ccd7245530dd3a80d823b52968295080 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# ----------------------------------------------------------------------------- | |
# Importing the modules | |
# ----------------------------------------------------------------------------- | |
import dash | |
from dash import html, dcc, Input, Output | |
import dash_bootstrap_components as dbc | |
import paho.mqtt.client as mqtt | |
global current_temperature | |
current_temperature = "NaN" | |
# ----------------------------------------------------------------------------- | |
# MQTT Subscribe | |
# ----------------------------------------------------------------------------- | |
mqttc = mqtt.Client() | |
mqttc.connect("mqtt.eclipseprojects.io", 1883, 60) | |
def on_connect(client, userdata, flags, rc): | |
print("Connected with result code "+str(rc)) | |
mqttc.subscribe("myroom/temperature") | |
def on_message(client, userdata, msg): | |
global current_temperature | |
current_temperature = msg.payload.decode() | |
mqttc.on_connect = on_connect | |
mqttc.on_message = on_message | |
mqttc.loop_start() | |
# ----------------------------------------------------------------------------- | |
# Defining Dash app | |
# ----------------------------------------------------------------------------- | |
app = dash.Dash(external_stylesheets=[dbc.themes.DARKLY]) | |
# ----------------------------------------------------------------------------- | |
# Temperature card | |
# ----------------------------------------------------------------------------- | |
card = dbc.Card( | |
html.H4(id="temperature") | |
) | |
# ----------------------------------------------------------------------------- | |
# Application layout | |
# ----------------------------------------------------------------------------- | |
app.layout = dbc.Container( | |
[ | |
dcc.Interval(id='update', n_intervals=0, interval=1000*3), | |
html.H1("Monitoring IoT Sensor Data with Plotly Dash"), | |
html.Hr(), | |
dbc.Row(dbc.Col(card, lg=4)) | |
] | |
) | |
# ----------------------------------------------------------------------------- | |
# Callback for updating temperature data | |
# ----------------------------------------------------------------------------- | |
@app.callback( | |
Output('temperature', 'children'), | |
Input('update', 'n_intervals') | |
) | |
def update_temperature(timer): | |
return ("Temperature: " + str(current_temperature)) | |
# ----------------------------------------------------------------------------- | |
# Main function | |
# ----------------------------------------------------------------------------- | |
if __name__ == "__main__": | |
app.run_server(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment