Project : Measure air quality

04 May

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

Requirements

  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.

 

Resources

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 (as of 3rd May 2018).

  1. Use a standard installation of the RPi and update/upgrade.
  2. Install the Grove Pi software using the guide in the sleeve from the start kit
  3. Upgrade firmware on the Grove Pi
  4. Install the libraries needed for uploading data to Thingspeak:
    1. Paho MQTT
      1. sudo pip3 install paho-mqtt
    2. Psutil
      1. 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)

Sensors:

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

Requirements:

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

Code:

</pre>

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
atexit.register(grovepi.dust_sensor_dis)
HTsensor = 7 #D7
light_sensor = 1 #A2
SoundSensor = 0 #A0
LED_green = 4 #D4
LED_red = 3 #D3
pinMode(LED_green,"OUTPUT")
pinMode(LED_red,"OUTPUT")
grovepi.dust_sensor_en()

#Setting up data to write data to Thingspeak
string.alphanum='1234567890avcdefghijklmnopqrstuvwxyzxABCDEFGHIJKLMNOPQRSTUVWXYZ'
channelID = "489007"
writeAPIKey = "JZSE8ITI04G0P49B"
mqttHost = "mqtt.thingspeak.com"
mqttUsername = "TSMQTTRpiDemo"
mqttAPIKey ="JNDDOO6DJ9N77MUD"
tTransport = "websockets"
tPort = 80
topic = "channels/" + channelID + "/publish/" + writeAPIKey

while True:
try:
#Set green operation
digitalWrite(LED_green,1)
digitalWrite(LED_red,0)

#Gather data from sensors
sound_level = grovepi.analogRead(SoundSensor)
light_intensity = grovepi.analogRead(light_sensor)

[new_val,lowpulseoccupancy] = grovepi.dustSensorRead()

[temp,hum] = grovepi.dht(HTsensor,0)

print ("Light :" , light_intensity, "Sound :", sound_level )

#Prep connection to Thingspeak
clientID=''

# Create a random clientID.
for x in range(1,16):
clientID+=random.choice(string.alphanum)

#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)

# build the payload string for Thingspeak
payload = "field1=" + t + "&field2=" + h + "&field3=" + d + "&field4=" + l + "&field5=" + s

# attempt to publish data to the topic on Teamspeak.
try:
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):
break
except:
print ("There was an error while publishing the data.")
digitalWrite(LED_green,0)
digitalWrite(LED_red,1)

#Set LCD color
setRGB(0,128,64)
setRGB(0,0,255)
#Print on LCD
setText("T:" + t + "C" + " H:" + h + "%" + " Dust:" + d + "psc/L")

#Wait 5 seconds
time.sleep(5)

except (IOError,TypeError) as e:
print("Error")
digitalWrite(LED_green,0)
digitalWrite(LED_red,1)

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.

– SilentHunter.dk –

– Submarines & Rockets…do they mix?!