Skip to content

Instantly share code, notes, and snippets.

# -----------------------------------------------------------------------------
# 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"
import time
import paho.mqtt.client as mqtt
import Adafruit_DHT
# MQTT Client
mqttc = mqtt.Client()
mqttc.connect("mqtt.eclipseprojects.io", 1883, 60)
# DHT11 sensor data
dht11_sensor = Adafruit_DHT
# -----------------------------------------------------------------------------
# Main function
# -----------------------------------------------------------------------------
if __name__ == "__main__":
app.run_server(debug=True)
# -----------------------------------------------------------------------------
# Callback for updating temperature data
# -----------------------------------------------------------------------------
@app.callback(
Output('temperature', 'children'),
Input('update', 'n_intervals')
)
def update_temperature(timer):
return ("Temperature: " + str(current_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))
]
# -----------------------------------------------------------------------------
# Temperature card
# -----------------------------------------------------------------------------
card = dbc.Card(
html.H4(id="temperature")
)
# -----------------------------------------------------------------------------
# Defining Dash app
# -----------------------------------------------------------------------------
app = dash.Dash(external_stylesheets=[dbc.themes.DARKLY])
# -----------------------------------------------------------------------------
# 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")
# -----------------------------------------------------------------------------
# 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"
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
mqttc.subscribe("myroom/temperature")
def on_message(client, userdata, msg):
print("Received:", msg.payload.decode())
mqttc = mqtt.Client()