Students will measure temperature, humidity, pressure from a fully integrated sensor over an I2C bus. This will require programming the data collection. Once the data is available it shall be published to an MQTT broker. The instructor and staff will display data posted from each student on a central screen and record that as successful submission of the assignment..
Learning outcome: Understanding how measured data can be collected by central data server.
MQTT, which stands for Message Queuing Telemetry Transport, is a lightweight and efficient messaging protocol designed for small sensors and mobile devices. It's particularly well-suited for the "Internet of Things" (IoT) applications where bandwidth and battery power are limited. MQTT operates on a publish/subscribe model, making it highly scalable and enabling devices to communicate with each other and with servers across networks with minimal network bandwidth.
An MQTT system consists of clients and a broker. Clients, which can be devices or software applications, connect to the MQTT broker. The broker is responsible for distributing messages to interested clients based on the topics of the messages. When a client publishes a message to a topic, the broker forwards this message to all clients subscribed to that topic.
You will need to install paho a python MQTT client:
cd ~/pythonBME210
source env/bin/activate
pip3 install install paho-mqtt
Chat GPT tells me that publishing to MQTT broker needs the code below. The broker address, username and password is listed below.
import paho.mqtt.client as mqtt
import time
# MQTT settings
MQTT_BROKER = "150.135.157.30"
MQTT_PORT = 1883
MQTT_TOPIC = "Urs_Utzinger"
# username is mqtt
# password is the same as the wifi password
# Sensor settings
sensor_HTU = ...
sensor_BMP = ...
# Create MQTT client and connect to broker
client = ... # figure out the command
client.connect(...) # figure out the command
while True:
# Read sensor data
humidity, temperature = sensorHTU.read() # or similar
pressure = sensorBMP.read() # or similar
# Publish sensor data
# payload = "Temperature={:0.1f}, Humidity={:0.1f}, Pressure={:d}".format(temperature, humidity, pressure)
# client.publish(MQTT_TOPIC, payload)
client.publish(MQTT_TOPIC+"/Temperature", "{:0.1f}".format(temperature))
client.publish(MQTT_TOPIC+"/Humidity", "{:0.1f}".format(humidity))
client.publish(MQTT_TOPIC+"/Pressure", "{:d}".format(pressure))
time.sleep(0.5)
# Disconnect from broker
client.disconnect()