Air quality monitoring [Workshop]
Air quality monitoring [Workshop]
1. Overview
This codelab is part of the Viam for educators program, and provides a suggested delivery plan and resources for instructors interested in delivering a hands-on workshop. Take these resources, and make it your own!
Workshop Overview
- Goal: Introduce sensors and actuators as concepts and build a simple air quality monitoring system
- Audience: Beginner developers with basic programming and hardware skills
- Duration: ~2-3 hours
What You'll Teach
- How to build an air quality monitor that triggers an air filtration system when detected values of particulate (PM2.5) exceed a certain threshold
Watch the Video
See a demonstration of the air quality monitoring system in this video.
2. What participants will need
Review the list of required components (hardware, software), and determine which materials are provided or required for participants to bring on their own. Provide instructions to participants, giving them ample time to prepare and complete prerequisites.
Hardware and supplies requirements
- 1 - Raspberry Pi 5
- 1 - USB flash drive or microSD card to use with your Pi
- 1 - USB cable to power your Pi
- Make sure you are using a 5V 5A (25W) power supply. USB boot is disabled by default when connected to a 3A power supply, so adequate amperage is required for optimal performance of your Raspberry Pi 5.
- 1 - Store-bought or 3D-printed Raspberry Pi case (like this one here) that provides access to GPIO pins
- 1 - PM2.5 air quality sensor (with G7 transfer board)
- 4 - jumper wires to connect the air sensor to the Pi
- 1 - Kasa smart plug (supported Kasa models: EP10, EP25, HS100, HS103, HS105, HS110, KP100, KP105, KP115, KP125, KP125M, KP401)
- 1 – 20" Box Fan
- 1 - 20" x 20" x 1" MERV 13 electrostatic air filter (most effective 11-13)
- 4 - 3D-printed clips (or duct tape to attach the air filter to the box fan)
Software and other device requirements
- Laptop running MacOs, Linux, or Windows with required software
- Preferred IDE, such as VS Code
- Python3
- Raspberry Pi Imager
- Sign up for a free Viam account, and then sign in to the Viam app
- A smartphone to download and use the Kasa mobile app
3. Pre-workshop setup for instructors
Learning Objectives
Review the suggested learning objectives, and adjust it according to your goals and audience.
- How to use modules from the Viam registry
- How to set up a control logic module with your Viam machine
- How to adjust
Agenda
Review the suggested agenda, and adjust it according to your goals and audience.
- Introduction (10 mins)
- Hardware Assembly (30 mins)
- Software Setup (30 mins)
- Hands-On Experiment (40 mins)
- Q&A/Wrap-Up (10 mins)
Pre-workshop checklist ✅
Review these setup steps as you're planning the workshop to help prepare for it.
Planning and Logistics
- Verify equipment inventory: Decide which materials are provided or required for participants to bring on their own. Order any materials and supplies that are provided to participants.
- Confirm workstation setup: Verify the workshop room layout supports hardware work, such as tables. Ensure participants have power outlets and internet access. Have spare supplies, such as Raspberry Pis, sensors, and cables, in case of failures. For virtual workshops, confirm logistics for breakout sessions, providing support, and checkpoints.
- Communicate prerequisites: Share a participant guide prior to the workshop with learning objectives and instructions for installing software and preparing laptops. Provide a checklist for participants to confirm readiness.
- Establish support channels: Determine how participants can get support for real-time troubleshooting (e.g. Slack or Discord channel for virtual workshops, additional helpers in the room for on-site workshops)
- Request Viam stickers: Don't forget to request Viam stickers for the workshop.
Content Preparation
- Prepare workshop teaching materials: Review the Delivery Plan, sample slide deck, and provided resources. Tailor these materials to suit your participants' needs. Decide whether to present them to the group or provide them for independent reference, such as through a web browser or printed handouts.
- Prepare code and documentation: Review the provided code and add comments as necessary.
- Review discussion topics: Review the suggested group discussion topics and adjust for your participants.
- Review quiz questions: Review the suggested quiz questions and adjust for your participants. Determine if knowledge assessment will be completed throughout the workshop or afterwards.
Dry Run and Testing
- Test the workshop project: Run through the build following the prepared documentation to ensure it works as expected. Simulate common issues and prepare troubleshooting tips for these scenarios. If physical space is limited, consider sharing a few box fans or use a different corded appliance such as a table lamp to demonstrate the effect.
4. Delivery Plan - overview
Feel free to make a copy of this sample slide deck, customize it, and make it your own.
During the workshop, instructors can present this customizable slide deck (see above) tailored to your specific workshop needs. Alternatively, learners can follow step-by-step instructions and checkpoints independently by referencing the pages linked below in a web browser.
-
- Set up your air monitoring device
- Set up your air filter
- Set up your smart plug
- Set up your Raspberry Pi
-
- Configure your machine
- Add your Raspberry Pi
- Add your air sensor
- Add your smart plug
-
- Test the air sensor
- Test the smart plug
- Program your air monitoring device
- Configure and run a Viam control module
5. Delivery Plan - Hardware Assembly
Set up your air monitoring device
The PMS7003 particulate sensor measures the air quality and transmits data in a serial stream from the transmitter pin (TX) to the receiver pin (RX pin) on the Raspberry Pi.
Refer to the following wiring diagram to connect the Raspberry Pi to the PMS7003 air monitoring device.
- Pin 4 (3V) to VCC (Power)
- Pin 6 (GND) to GND (Ground)
- Pin 8 (TX) to RX (receiver)
- Pin 10 (RX) to TX (transmitter)
To power the Raspberry Pi, you can use the USB cord from earlier to continue providing power from your computer, or use a separate USB power supply.
How does the air sensor transmit data to your Raspberry Pi?
WiFi I2C Serial MQTT
Why are TX and RX pins crossed between the Raspberry Pi and the PMS7003 device in the wiring diagram?
To prevent power surges To enable proper communication between the Raspberry Pi and the device To match the voltage levels of both devices To provide a common ground
Set up your air filter
Make your own air purifier by combining a box fan and air filter to effectively clean the air in a small to medium sized, closed room.
- Attach the filter to the back of the fan, ensuring the airflow arrow points toward the fan. The fan should pull air through the filter when operating. If the filter will block access to the fan's power knob, now is a good time to switch it to an
onposition. - Secure the filter using 3D-printed clips or duct tape. Write the date when the filter is first used directly on the filter.
Set up your smart plug
Plug your Kasa smart plug into a power outlet.
Set up your smart plug and connect it to your local Wifi using the Kasa mobile app from the App Store or Google Play.
From the command line of your terminal app, install the
python-kasalibrary, and enter the commandkasa discoverto locate the IP address of the connected device. Make a note of the IP address since we'll need it again soon.Plug the box fan into the Kasa smart plug. Toggle the box fan power with the Kasa app to make sure it's working as expected.
Set up your Raspberry Pi
The Raspberry Pi boots from a USB flash drive (or microSD card). You need to install Raspberry Pi OS on a USB flash drive that you will use with your Pi. For more details about alternative methods of setting up your Raspberry Pi, refer to the Viam docs.
Install Raspberry Pi OS
- Connect the USB flash drive (or microSD card) to your computer.
- Download the Raspberry Pi Imager and launch it.
- Click CHOOSE DEVICE. Select your model of Pi, which is Raspberry Pi 5.
- Click CHOOSE OS. Select Raspberry Pi OS (64-bit) from the menu.
- Click CHOOSE STORAGE. From the list of devices, select the USB flash drive you intend to use in your Raspberry Pi.
- Configure your Raspberry Pi for remote access. Click Next. When prompted to apply OS customization settings, select EDIT SETTINGS.
- Check Set hostname and enter the name you would like to access the Pi by in that field, for example,
air. - Select the checkbox for Set username and password and set a username (for example, your first name) that you will use to log into the Pi. If you skip this step, the default username will be
pi(not recommended for security reasons). And specify a password. - Connect your Pi to Wi-Fi so that you can run
viam-serverwirelessly. Check Configure wireless LAN and enter your wireless network credentials. SSID (short for Service Set Identifier) is your Wi-Fi network name, and password is the network password. Change the sectionWireless LAN countryto where your router is currently being operated. - Select the SERVICES tab, check Enable SSH, and select Use password authentication.
- Save your updates, and confirm
YESto apply OS customization settings. ConfirmYESto erase data on the USB flash drive. You may also be prompted by your operating system to enter an administrator password. After granting permissions to the Imager, it will begin writing and then verifying the Linux installation to the USB flash drive. - Remove the USB flash drive from your computer when the installation is complete.
Connect with SSH
Place the USB flash drive into your Raspberry Pi and boot the Pi by plugging it in to an outlet. A red LED will turn on to indicate that the Pi is connected to power.
Once the Pi is started, connect to it with SSH. From a command line terminal window, enter the following command. The text in <> should be replaced (including the < and > symbols themselves) with the user and hostname you configured when you set up your Pi.
ssh <USERNAME>@<HOSTNAME>.local
- If you are prompted "Are you sure you want to continue connecting?", type "yes" and hit enter. Then, enter the password for your username. You should be greeted by a login message and a command prompt.
- Update your Raspberry Pi to ensure all the latest packages are installed
sudo apt update
sudo apt upgrade
Enable communication protocols
- Launch the Pi configuration tool by running the following command
sudo raspi-config
- Use your keyboard to select "Interface Options", and press return.
- Enable the relevant protocols to support our hardware. Since you are using a sensor that communicates over the serial port, enable Serial Port.
- Confirm the options to enable the serial login shell and serial interface. And reboot the Pi when you're finished.
6. Delivery Plan - Software Setup
Configure your machine
- In the Viam app under the LOCATIONS tab, create a machine by typing in a name and clicking Add machine.
- Click View setup instructions.
- Install
viam-serveron the Raspberry Pi device that you want to use to communicate with and control your air sensor. Select theLinux / Aarch64platform for the Raspberry Pi to control the air sensor, and leave your installation method asviam-agent. - Use the
viam-agentto download and installviam-serveron your Raspberry Pi. Follow the instructions to run the command provided in the setup instructions from the SSH prompt of your Raspberry Pi. The setup page will indicate when the machine is successfully connected.
Add your Raspberry Pi
- In the Viam app, find the CONFIGURE tab. It's time to configure your hardware.
- Click the + icon in the left-hand menu and select Component.
- Select
board, and find thepi5module. This adds the module for working with the Raspberry Pi 5's GPIO pins. Leave the default nameboard-1for now. - Notice adding this module adds the board hardware component called
board-1. The collapsible card on the right corresponds to the part listed in the left sidebar. - Click Save in the top right to save and apply your configuration changes.
Add your air sensor
- In the Viam app, click the + icon and select Component. Select
sensor, find theair:pms7003module, and click Add module. This module provides the sensor model that supports the specific hardware we are using for this tutorial. Leave the default namesensor-1for now. - Notice adding this module adds the sensor hardware component called
sensor-1as well as a module calledpms7003which contains the logic for how our Raspberry Pi and air sensor work with each other. - Save your updates.
Add your smart plug
- In the Viam app, let's configure the rest of our hardware so that your machine can communicate with the smart plug over wifi. Click the + icon in the left-hand menu and select Component. Select
generic, and find thekasa:switchmodule. This is a generic module that's been configured to use thepython-kasalibrary. In other words, it's a layer of abstraction that lets us use the Kasa manufacturer's APIs with Viam. - Notice adding this module adds the generic hardware component called
generic-1as well as a module calledkasasmartplugwhich contains the logic for how our Raspberry Pi and smart plug work with each other. - Under the CONFIGURE section of the
generic-1card, add your own smart plug's IP address that you found in a previous step, formatted like the following.
{
"plug_ip": "192.168.1.169"
}
- Save your updates.
7. Delivery Plan - Hands-on Experiment
Test the air sensor
- In the Viam app under the CONTROL tab, you can see two hardware components called
generic-1andsensor-1. They are your smart plug and air sensor that you configured on the CONFIGURE tab. - Find the
sensor-1card, and expand the TEST accordion. The module we added has aGetReadingsfunction to get the readings from our air sensor. SelectManual refresh, and then click the refresh icon to manually get the readings.
Test the smart plug
- Next, test that your smart plug is working properly. Make sure the fan is still plugged in to the powered smart plug. Also make sure the fan knob is set to on.
- In the Viam app under the CONTROL tab, find the
generic-1card, and expand the DOCOMMAND accordion. The module we added has a few functions to control the smart plug. Under the Input section, add the following JSON object (if your fan is still running), and then click Execute to toggle off the smart plug.
{
"toggle_off": []
}
- Now update `