PM2.5 Monitor

Log Data with Authentication

5.1 Set up a home MQTT server

As mention earlier, to send data with authentication, or with password and username, you need to set up a home computer with MQTT server, rent a cloud equivalent. This setup allow server to accept certain pair of username and password.

  • A computer to install MQTT server package (around one dozen of MB), preferable running Linux. Raspberry Pi is one excellent choice. Want to set up one?
  • A Python script to capture and store data from the sensor. The data can be saved a database or a file
Make sure that after setting up MQTT, you have an IP of the MQTT server (192.168.1.100, for example), a pair of user name and password to transfer data to MQTT with authentication.

A few things changes included: (1) Specify MQTT server, username and password in .ino files

//right below setup WiFi
//#define mqtt_server "192.168.1.100"
//#define mqtt_user "mqtt_username" 
//#define mqtt_password "mqtt_password"

and (2) pass the user and password to MQTT object

// connected with authenication
client.connect(SENSORNAME, mqtt_user, mqtt_password)
// connect to open server
// client.connect(SENSORNAME)

Here is the full sketch for ESP8266 on GitHub.

Below are some screen captures to compare. First, place MQTT IP, user and password in Credentials tab

mqtt.fx password>
            <p>And we can see the same data format as with HiveMQ server</p>
            <img class="img-fluid" src="/tutorial/pm25-monitor/pw-working.jpg" alt=
5.2 How to capture data

Similar to listening to MQTT server using MQTT.fx, a python library called mqtt-paho allows you to capture data stream and save it to a local file in addition to pre-processing.

On the GitHub, inside py folder, you will find Python script called collector.py. This script will listen to a topic and capture the and save them in a folder with current month, and customized name.

When excuting this script, it will log data and save it.

terminal data

Adding & in the end of the line will let the script runs in the background. If you want no printing on terminal, you can direct the stream to a null device such as python collector.py >> /dev/null 2>&1 &

Remember to change to authentication data to fit your setup.

auth = {
    'topic': "sensors/pms7003",
    'password': 'mqtt_password',
    'user': 'mqtt_user', 
    'ip': '192.168.1.100'

If you open Oct2020/pms7003_20201008_112242.txt file, you will see a list of dictionary. This is surely convenient to log and store, but not for reading for processing. There is a second script with a simple job converting a list of dictionary to a CSV file.

$ python3 convert2csv.py Oct2020/pms7003_20201008_112242.txt 
Processing: Oct2020/pms7003_20201008_112242.txt
First line:
{'sensor': 'pms7003', 'uptime': 1023, 'pm1_0': 13, 'pm2_5': 17, 'pm10': 17, 'pm1_0_ac': 13, 'pm2_5_ac': 17, 'pm10_ac': 17, 'um03': 2133, 'um05': 645, 'um1': 98, 'um2_5': 0, 'um5': 0, 'um10': 0, 'type': 'json', 'time': '2020-10-08 11:23:13'}
Save Oct2020/pms7003_20201008_112242.csv

Open Oct2020/pms7003_20201008_112242.csv to see the end result. Now it is up to use how to visualize the data in CSV file.