In this project, you will be connecting your system to a sensor network via the MQTT protocol. This will involve creating a python script that communicates the current temperature, pressure, and accelerometer state to the mqtt broker.
You will need to install a couple of packages on your raspberry pi. These packages are as follows:
sudo apt-get install python3-paho-mqtt mosquitto-clients
Once you have installed the packages above on your raspberry pi, you will be able to run mosquitto_pub and mosquitto_sub. You can find notes on how to use these commands in the reference materials above.
One of the items that you are required to demonstrate is you receiving and transmitting messages from silo/the desktop workstations in the lab. Mosquitto_pub and mosquitto_sub libararies are already installed by default on silo.
The following code is a template to get you started on your own version of sensor_node.py. This code writes static variables to the mqtt broker.
The first thing that you should do in modifying this code is to change sensor_id. This is the 6 digit number on the white IU tag on your pi. This tag has a barcode on it.
#!/usr/bin/env python3
import paho.mqtt.client as mqtt
import time
# White Bar Code Label Number on Each Raspberry Pi
sensor_id = None #YOUR PI SERIAL NUMBER
temperature = 21
pressure = 31
x_acceleration = 0.001
y_acceleration = 0.002
z_acceleration = 1.001
def on_message(client, userdata, message):
# print("topic:", message.topic)
print("message:", message.payload.decode('UTF-8'))
def on_connect(client,userdata,flags,rc):
pass
client = mqtt.Client()
client.on_message=on_message
client.on_connect=on_connect
client.connect("pivot.iuiot.org")
client.loop_start()
while(1):
print("Publish Temperature, Pressure, and Accelerometer Data")
client.publish(f"sensors/{sensor_id}/temperature",f"{temperature}")
client.publish(f"sensors/{sensor_id}/pressure",f"{pressure}")
client.publish(f"sensors/{sensor_id}/accel/x",f"{x_acceleration}")
client.publish(f"sensors/{sensor_id}/accel/y",f"{y_acceleration}")
client.publish(f"sensors/{sensor_id}/accel/z",f"{z_acceleration}")
time.sleep(5)
In labs 1 and 2, you created classes that enable access to the pressure/temperature sensor and the accelerometer. In this lab, you will be using these classes to get the data needed to pass to the mqtt broker.
You should copy your lps331ap.py and adxl343.py files to the same directory as sensor_node.py.
From your raspberry pi, you will be writing the temperature, pressure, and accelerometer values to the mqtt topics shown in the template code. You should send the data every 5 seconds.
1) Working code:
2) Individual Contributions:
List down the accomplishments of each member of the group. You should use a MarkDown
table for this purpose. The table should be formatted similar to the following:
Date | Time | Contributor | Accomplishment |
---|---|---|---|
2/1/2024 | 3:15 pm | Nicole | Assisted with writing mqtt code |
2/1/2024 | 4:30 pm | Aidan | Assisted with writing mqtt code |
2/1/2024 | 4:45 pm | Caleb | Implemented real sensor data code |
2/1/2024 | 5:45 pm | Jesus | Tested results on silo |
Both team members should submit to the canvas.