Project : Measure air quality

04 May

The purpose is to build a system which can measure some elements of air quality


  1. Measure dust
  2. Measure temperature
  3. Measure humidity
  4. Measure gases like CO2, methane etc
  5. Report realtime measurement on a local display
  6. Upload data to an external site, where data can be shown in graphs
  7. Log data locally, so the program can collect data without an internet connection.
  8. LEDs to indicate a healthy operation (green) or errors (red)
  9. Button to dim LCD (change color to black)

Due to requirements to keep it simple to put together and code, I chose to do this on the Grove platform, which sits on top of a Raspberry Pi and has a wide range of sensors, which plugs straight in. Further more most of the sensors has a RPi library making coding much simpler.



Links to sources of information for the components needed beside the RPi:
Grove Pi start kit 
Grove Pi Dust sensor 
Grove Pi Multichannel gas sensor  (does not have a RPi library, so I will deal with this last)
Grove Pi MQ2 gas sensor
Grove Pi light sensor
Grove Pi sound sensor


Preparation of Raspberry Pi & Grove Pi

***as of 2nd of April 2020***

  1. Use a standard installation of the RPi and update/upgrade.
  2. Install the Grove Pi software using the guide 
  3. Upgrade firmware on the Grove Pi
  4. Install the libraries needed for uploading data to Thingspeak:
    1. Paho MQTT
      sudo pip3 install paho-mqtt
    2. Psutil
      sudo pip3 install psutil
  5. Connect sensors to ports as mentioned in the sleeve with the Grove Pi start kit or as you see fit, just remember to connect digital to digital, analog to analog etc


Solution to upload data

Thingspeak  will be used to upload data to. I did search for examples, but most used httplib and urllib, which has changed with Python3, so the examples didn’t work.
So I decided on using another method.Example used for uploading data 


Current build and requirements developed (1.4)


  1. Temperature
  2. Humidity
  3. Dust
  4. light
  5. Sound


  1. Present data on LCD
  2. Upload data to Thingspeak


from __future__ import print_function
import paho.mqtt.publish as publish
import psutil
import grovepi
import string
import random
import time
import atexit
from grove_rgb_lcd import *
from grovepi import *
#Setting up sensors
HTsensor = 7 #D7
light_sensor = 1 #A1
SoundSensor = 0 #A0
gas_sensor = 2 #A2
LED_green = 4 #D4
LED_red = 3 #D3
#Setting up data to write data to Thingspeak
channelID = "489007"
writeAPIKey = "JZSE8ITI04G0P49B"
mqttHost = ""
mqttUsername = "TSMQTTRpiDemo"
tTransport = "websockets"
tPort = 80
topic = "channels/" + channelID + "/publish/" + writeAPIKey
while True:
        #Set green operation
        #Gather data from sensors
        sound_level = grovepi.analogRead(SoundSensor)
        light_intensity = grovepi.analogRead(light_sensor)
        sensor_value = grovepi.analogRead(gas_sensor)
        # Calculate gas density - large value means more dense gas
        density = (float)(sensor_value / 1024.0)
        [new_val,other,lowpulseoccupancy] = grovepi.dust_sensor_read()
        print ("new_val=" , new_val, " other=", other, "      lowpulseoccupancy=",lowpulseoccupancy)
        [temp,hum] = grovepi.dht(HTsensor,0)
        print ("Light :" , light_intensity, "Sound :", sound_level )
        print ("Gas sensor value =", sensor_value, " Gas density =",                                                     density)
        #Prep connection to Thingspeak
        # Create a random clientID.
        for x in range(1,16):
        #To avoid posting empty data as "0" from the dust sensor. New_val means there is new data
        if new_val:
        #Show data
            print ("Temp =", temp, "C & Humidity =", hum,"% & Dust consentrantion =",lowpulseoccupancy," & Light =",light_intensity," & Sound =",sound_level)
        #Prepare data for use on LCD & Thingspeak
        t = str(temp)
        h = str(hum)
        d = str(lowpulseoccupancy)
        l = str(light_intensity)
        s = str(sound_level)
        g = str(density)
        # build the payload string for Thingspeak
        payload = "field1=" + t + "&field2=" + h + "&field3=" + d + "&field4=" + l + "&field5=" + s + "&field6=" + g
        # attempt to publish data to the topic on Teamspeak.
            publish.single(topic, payload, hostname=mqttHost, transport=tTransport, port=tPort,auth={'username':mqttUsername,'password':mqttAPIKey})
            print ("Published to Teamspeak to host:" ,mqttHost , " clientID=" ,clientID)
        except (KeyboardInterrupt):
            print ("There was an error while publishing the data.")
        #Set LCD color for day and night time, so day is Blue and night is dark Red
        if (light_intensity>80):
            print ("Daytime")
            print ("Nighttime")
        #Print on LCD
        setText("T:" + t + "C" + " H:" + h + "%" + " Dust:" + d + "psc/L")
        #Wait 5 seconds
    except (IOError,TypeError) as e:

Issues noted and worked around

  1. For some reason the data from the light sensor and sound sensor has to be collected before the Temperature & Humidity sensor, otherwise the data is invalid and the same.
  2. Most examples of uploading data to Thingspeak use libraries not working with Python 3
  3. There were mismatches between what sensors were supported on the RPi and which were not in the Seeedstudio documentation. Check online and on Dexterindustries website & Github

Live data from a sensor

Comments are closed.

– –

– Submarines & Rockets…do they mix?!