Air Quality Index on a Raspberry Pi
Running AI and ML models on an 'Edge' device like a Raspberry Pi is easy with Modzy! Let's walk through a simple example by building an Air Quality Index sensor to detect current air quality and use that data to generate a next-hour prediction, on a Raspberry Pi.
Raspberry Pi Setup
First let's build and configure an edge computing device. In this example we'll use a Raspberry Pi 3B+ – but the specific model isn't too important so long as it is 64-bit ARM and has sufficient RAM to run your model (The 3B+ has 1GB, more than enough for the AQI model).
Hardware
The Bill of Materials for the AQI sensor includes:
- Raspberry Pi 3B+
- Adafruit PMSA003I Air Quality
- Adafruit SCD-40
- 2x Qwiic JST SH 4-pin Header Cable
- warning light
- jumper wires: M-F, M-M
- breadboard
- battery, if desired
Connect the sensors and light to the Pi following this diagram:
Software
Set up the Pi by following the Raspberry Pi getting started guide.
Important
When setting up your image, select "Raspberry Pi OS (64-bit)". Enabling SSH and preconfiguring your WiFi and timezone information in the Advanced Menu are highly recommended.
When the initial setup is complete and you have logged in, first task is to update the system and install a few libraries to help with development. In a terminal, execute the following commands:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python3-pip
sudo pip3 install --upgrade setuptools
sudo apt-get install -y i2c-tools libgpiod-dev
Now prepare the Pi for use with Modzy.
- Install Docker, following these simple instructions, then allow non-sudo users to use docker by running
sudo groupadd docker, sudo usermod -aG docker $USER
- Install CircuitPy and the libraries to interact with the AQI sensors (note some users will want to use a Python Virtual Environment if you are using this Pi for multiple projects) by running:
-pip3 install --upgrade adafruit-python-shell
-wget <https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/raspi-blinka.py
>
-sudo python3 raspi-blinka.py
-pip3 install adafruit-circuitpython-scd4x
-pip3 install adafruit-circuitpython-pm25
- Finally, install the Python Modzy-SDK:
-pip install modzy-sdk
(or, if you have an unsupported processorpip install git+<https://github.com/modzy/sdk-python.git
>)
AQI Sensing and Prediction
Congratulations, you've prepared the Pi to begin logging and processing sensor data! All that remains is to grab the logging code and pull the AQI model from Modzy to begin processing and predicting locally.
Logger code
To begin logging the data, we run a simple script available in Modzy's GitHub repo:
wget <https://raw.githubusercontent.com/modzy/edge_AQI/main/raspi/logger.py
>
To verify that all sensors are correctly wired and the Pi is properly configured, run the script:
python logger.py
You should see data printing every 5 seconds to the terminal:
1651252442, 0, 0, 31.519775390625, 22.2503662109375, 610, 0, 0, 0, 0, 0, 0, 114, 32, 6, 0, 0, 0
1651252447, 0, 0, 31.525115966796875, 22.27325439453125, 605, 0, 0, 1, 0, 0, 1, 90, 26, 8, 2, 0, 0
1651252452, 0, 0, 31.493072509765625, 22.247314453125, 598, 0, 0, 1, 0, 0, 1, 78, 24, 8, 2, 0, 0
1651252457, 0, 0, 31.522445678710938, 22.20458984375, 598, 0, 0, 1, 0, 0, 1, 87, 27, 4, 2, 0, 0
- If you see an error, verify all wiring and required libraries are correct.
- Enable the logging script to run in the background on startup by creating
/etc/systemd/system/aqi_logger.service
and set it to autorun with these commands:
sudo systemctl daemon-reload
sudo systemctl start aqi_logger.service
sudo systemctl enable aqi_logger.service
Running the model
Step away from the Pi for a moment to set up Modzy Edge Management in your Modzy instance.
- In a web browser, log in to your Modzy instance and select Operations>Edge Devices
- Click
New Device Group
and name your device group. - Under Models, click
Add Model
and search for "AQI Sensor (ARM)", or your desired model. Select it. - Configure Processing Engine limit as desired
- Click
Create New Device Group
- Find your newly created device group and click it, then click
Create Device Token
- Choose your time limit and number of uses – for development large numbers are recommended, you can revoke the token when complete.
- Select
Linux with 64-bit Arm chip
and click Generate Commands
- Execute the commands from the previous step in a Pi terminal. This will retrieve the model container and will take a few minutes depending on the speed of your connection and size of the model.
- While the model download is happening, you may click
Download Token and Close
in the web browser. - You should see a new
TOKEN ACCESSOR
under Device Tokens, and if your Pi has successfully completed the wget command from above you will see your device listed under Devices. - If desired, select your device under Devices, give it a nickname and location, and make sure GPU, CPU and Memory have been correctly identified.
- In the terminal window, you should see an output:
{"level":"info","ts":1652396163.4500823,"caller":"runtime/docker.go:107","msg":"creating container..."}
{"level":"info","ts":1652396169.9438648,"caller":"runtime/docker.go:165","msg":"container is ready: running"}
{"level":"info","ts":1652396225.816962,"caller":"logger/logger.go:41","msg":"Modzy Core server is starting..."}
{"level":"info","ts":1652396225.8208668,"caller":"logger/logger.go:41","msg":"Server is starting..."}
{"level":"info","ts":1652396225.82189,"caller":"logger/logger.go:41","msg":"Server is listening at :55000"}
The Pi is now ready to run the prediction algorithm! Retrieve the basic script from Modzy's GitHub:
wget <https://raw.githubusercontent.com/modzy/edge_AQI/main/raspi/aq_predictor.py
>
and execute: python aq_predictor.py
You should begin to see output from the model, such as:
{'next_hour': 589.5541489468759, 'current': 730, 'timestamp': 1651253299}
{'next_hour': 589.5541489439528, 'current': 735, 'timestamp': 1651253303}
{'next_hour': 589.5541489415464, 'current': 735, 'timestamp': 1651253308}
{'next_hour': 589.5541489396047, 'current': 735, 'timestamp': 1651253313}
{'next_hour': 589.5541489379096, 'current': 733, 'timestamp': 1651253318}
Note this model requires at least 1 hour of data before it can begin to make accurate predictions.
That's it! You now have a device logging a variety of air quality indicators and running a model to predict what the air quality will be 1 hour in the future!
Updated 2 months ago