In my town, the streets are cleaned once a week. I've observed that some streets accumulate a significant amount of dust just two or three days after cleaning, while others remain relatively clean even after six days. As a pedestrian, you can feel the difference in air quality between these areas.
I've noticed a correlation between the dust levels on the streets and the air quality, specifically regarding the presence of 10-micrometer particles. I believe that if we can monitor the status of each street, we can clean them as needed. Some streets may require cleaning every two or three days, while others may only need attention every two or three weeks. This approach would optimize our cleaning efforts and ensure that all streets are consistently clean.
To achieve this, I propose using an AVR IoT Mini Cellular board in conjunction with a PMS5003 sensing module. This setup will allow us to accurately monitor air quality levels. Additionally, integrating a BME280 digital sensor will provide further environmental data. By streaming this data to an MQTT server (hivemq) and the ThingSpeak platform, we can analyze and visualize the air quality status of each street in real-time.
In this tutorial, I will demonstrate how to connect the PMS5003 sensing module and the BME280 digital sensor to the AVR IoT Mini Cellular board. I will also provide instructions on streaming the data to both an MQTT server and the ThingSpeak platform.
Step 1: Hivemq account setupTo set up our MQTT connection, we'll be using HiveMQ due to their free plan, which suits our project's requirements perfectly. Follow the steps below to create an account:
- Visit the HiveMQ website at www.hivemq.cloud.
- On the homepage, you'll find multiple options to create an account. Choose the option that best suits your preferences.
- Once you've selected your preferred method and finalized the account creation process, log in to your newly created account.
After logging in, you'll see a menu on the left side of the window. Follow these steps to create a new cluster:
- In the menu, locate and click on the plus sign next to 'Clusters'. This action will prompt you to create a new cluster for your project.
- Now select the free plan
- Next step is to select the provider. For out project the provider do not matter so chose what you like and press create.
Once you've created the new cluster, it should appear on the left side of the window under the 'Clusters' menu. If it's not already selected, click on the cluster, and then navigate to the top menu and press 'Overview'. Here, you'll find the Cluster URL and the port that we'll use to connect to the MQTT server.
In addition to the Cluster URL and port, we'll need a username and a password for authentication. Follow these steps to create them:
- From the top menu, select the 'Access Management' page.
- Now, add a username and a password.
- Choose the permission level "Publish and Subscribe" from the options.
- Finally, press the 'Create credentials' button to generate the username and password.
Now we have all the information in order to establish a mqtt connection.
Step 2: Thingspeak account setupThingSpeak is an Internet of Things (IoT) platform that allows users to collect, analyze, and visualize data from various IoT devices or sensors. It provides tools for data logging, processing, and visualization, enabling users to create IoT applications and projects easily. In order to use it we will need to create an account. Go to thingspeak.com and press the "Get started for free" button in order to create an account. Please follow the instructions from thingspeak, should be quite straight forward.
Once you've completed the account creation process, navigate to the 'Channels' tab in the menu and select 'My Channel'. On the new page, click on the 'New Channel' button to begin creating your channel.
Start by providing a name and description for your channel. Next, ensure that the first six fields are checked and add the corresponding field names as shown in the picture below.
It's important to note that data from the AVR board will be transmitted based on the field ID rather than the field name. Once you've filled in the necessary information, press the 'Save' button located at the bottom of the form.
While there are numerous settings available for customizing the charts, we won't delve into them in this tutorial. For further information, please refer to the documentation provided by ThingSpeak.
The next step is to obtain the credentials required for the connection. Navigate to your newly created channel and locate the 'API Keys' section. Here, you'll find both a Write API Key and a Read API Key. For our AVR-ThingSpeak connection, we'll specifically need the Write API Key.
Step 3: Hardware setupThe next diagram explain the needed connections.
Connect PMS5003 to AVR board
To establish the connection between the sensor and the board, you'll need to connect just two pins. The TX (transmit) pin from the AVR board should be connected to the RX (receive) pin on the PMS5003 module, and vice versa, the RX pin from the AVR board should be connected to the TX pin of the PMS5003 module.
The PMS5003 module typically comes with a small cable. For a convenient solution, you can purchase an adapter. However, if you don't have an adapter available, you can simply cut the cable and solder some pins from jumper wires, as I did, for an easy connection.
Connect BME280 to AVR board
We will connect the BME280 sensor using I2C communication. For this, you'll need to connect the SCL (Serial Clock) pin of the AVR board to the SCL pin of the BME280 sensor, and the SDA (Serial Data) pin of the AVR board to the SDA pin of the BME280 sensor. This connection can be easily achieved using standard jumper wires.
Connect allcomponents to supply
The AVR board, the PMS5003 particle sensing module, and the BME280 sensor all need to be connected to a 5V power supply. Please follow the connections as illustrated in the picture below.
If you prefer to power the AVR board through a USB cable, you can omit the connection with the external power supply. In this case, all components will be powered through the GND and BAT pins of the AVR board.
Add all components in to the electric box
To ensure proper functionality of the new device outdoors, it's essential to enclose all the components within an electric box. The specific type of electric box used is not critical; however, certain key considerations must be taken into account.
Firstly, it's imperative to position the PMS5003 module adjacent to a hole in the electric box to allow for the intake of outside air. This ensures accurate monitoring of outdoor air quality. Additionally, it's crucial to orient the hole in a downward position to prevent the ingress of rainwater, which could potentially damage the components.
When placing the components inside the electric box, ensure that the PMS5003 module is positioned appropriately next to the intake hole, and all other components, including the AVR board and BME280 sensor, are securely mounted to prevent movement or damage. PMS5003 can be fixed with double tape or hot glue or any glue that will not affect the sensor.
Prepare the softwareenvironment:
To build the software for this project, you can use either Visual Studio Code (VSC) or the Arduino IDE. However, I prefer using Visual Studio Code due to its versatility, especially when paired with the PlatformIO extension.
Here's how to set up your project in Visual Studio Code:
- Open Visual Studio Code and navigate to the Extensions tab.
- Search for "PlatformIO IDE" and install the extension.
- Once installed, you'll see the PlatformIO icon in the left-hand menu. Click on it to open the PlatformIO menu.
- Within the PlatformIO menu, navigate to "Open" to access the PlatformIO page.
- In the PlatformIO page, go to "Projects" and click on the "Create new project" button on the newly opened page.
- In the opened window please add a name for the project, select the board and the framework and press finish.
- In the explorer you shall have the structure like in the next picture:
Now replace the platformio.ini file with the file provided in this tutorial.
Provisioning for MQTT connection:
To connect to the HiveMQ server, we need to provision the board. Follow these steps:
- Access all the necessary information for provisioning on the Microchip IoT website at https://iot.microchip.com/docs/arduino/userguide/provisioning.
- To build the provision code mentioned in the user guide from the above link, copy the content of the provision.ino file into the main.cpp file and build the project.
- To build the project, click on the build icon located in the footbar.
- Once the build is complete, locate the firmware.hex file in the.pio/build/AVR128DB48 folder.
- Drag the firmware.hex file to the AVR board drive.
- Follow the instructions provided in the Microchip user guide to complete the provisioning process.
- After you've finished provisioning, you can proceed to the next step.
Build the code for the actual project:
The fallowing steps need to be performed:
- Copy the.h files to 'include' folder and all the.cpp files to 'src' folder
- Go to credentials.h file and add your credentials that you obtained in the previous chapters.
- Build the project
- Flash the firmware.hex file (copy the file to the AVR drive)
I recommend to take a look on the code files to understand exactly how is working. Also you fill find some parameters (variables with 'p_' prefix) that you can change based on your needs.
Verifying Data Transmission:To confirm whether the data from the AVR board is successfully reaching the HiveMQ server and ThingSpeak platform, follow these steps:
HiveMQ Server:
- Access HiveMQ Account: Log in to your HiveMQ account and select the appropriate Cluster.
- Navigate to Web Client: From the top menu, navigate to the Web Client.
- Subscribe to Topics: Press the "Subscribe to All Topics" button to subscribe to all topics.
- Monitor Messages: Wait for some time (based on the p_publish_intervals parameter) to allow data transmission. If the board is connected and sending data, you should receive messages containing sensor data.
ThingSpeak Platform:
- Access ThingSpeak Account: Log in to your ThingSpeak account.
- Check Charts: Look at the charts on your ThingSpeak account to see if they are updated periodically. Data should be reflected on the charts after some time, indicating successful transmission from the AVR board.
By following these steps, you can verify that the data from the AVR board is being transmitted to both the HiveMQ server and ThingSpeak platform as expected.
Debugging:
If you're not seeing any data updates on the HiveMQ or ThingSpeak platforms, don't worry. There are steps you can take to troubleshoot and identify the issue.
1. Check Terminal Messages:
The first step in debugging is to check the terminal for any error messages or debug output. Many times, these messages can provide valuable insights into what might be causing the problem. Look for any errors or warnings related to data transmission or connectivity issues.
2. Verify Connection Settings:
Ensure that the connection settings, including the MQTT broker address, port number, and authentication credentials, are correctly configured. Even a small mistake in these settings can prevent data from being transmitted successfully.
3. Review Code and Configuration:
Take a closer look at your code and configuration files to ensure that everything is set up correctly. Double-check any variables or parameters related to data transmission and make sure they match the specifications provided by the platform documentation.
5. Test with Sample Data:
If possible, try sending sample data or test messages to the HiveMQ or ThingSpeak platforms manually. This can help isolate whether the issue lies with the device or the data transmission process itself.
Comments