Visitors

Sunday, July 2, 2017

Air Quality Monitoring Device Using Arduino



ABOUT THIS PROJECT

So, whilst thinking of a good demonstration for the Opensensors platform, we thought why not see how polluted our workplace is by hooking up a sensor to publish a continuous data stream to the Opensensors messaging broker.


For this we need an easy to pick up and use sensor, we settled on the Shinyei PPD-42. We’ll use this in order to measure the number of potentially hazardous small particulates in the air, with an arduino connected to a linux PC (or Raspberry PI).

To run this mini-project you will need:
  1. Shinyei PPD-42
  2. Arduino UNO
  3. Computer with Linux installed (you can use a Raspberry PI)



We are basing this run-through on a project called DustDuino that uses the Shinyei PPD-42 sensor with an arduino and a wifi module. Check it out here. We used this project as our reference when setting up the sensor and writing the Arduino code.





Firstly we follow step 2 of the instructions for hooking up the sensor to the Arduino. Then we download the code from the projects github repository by opening the link for the code DustDuinoSerial.ino selecting raw and saving that page.

Opening this up in the arduino IDE, we now upload it to our Arduino UNO by connecting the Arduino and pressing upload.
You can check the data is coming in by using the Arduino IDE’s serial monitor.
We then need to figure out how to send the incoming serial message to the Opensensors message broker.

To do this we chose to write a Python script. We used the Mosquitto Python module. I’m going to assume that you already have Python installed, as it comes pre-packaged on most versions of Linux. If you don’t have it already, you’ll need to install pip to download and set up the Mosquitto python module. On Ubuntu or Debian this can be done with the following command:
sudo apt-get install python-pip 
Once pip is installed we can install the Mosquitto python client module using the following command:

sudo pip install paho-mqtt


Final code 

import serial
import paho.mqtt.client as mqtt
import time

mqttc = mqtt.Client(client_id="939")
mqttc.username_pw_set("Louis", password="AbcDEFgH")
mqttc.connect("opensensors.io")

ser = serial.Serial('/dev/ttyACM0')  # open first serial port
while True:
message= ser.readline()
print message
mqttc.publish("/users/Louis/ODI/airquality", payload=message, qos=0, retain=False)
time.sleep(1);


















No comments:

Post a Comment