Kamaljit Chahal
Published © GPL3+

Air Quality Monitor

This is a project addendum to a larger project called "Scalable Intelligent Air Quality Monitoring and Response"

BeginnerShowcase (no instructions)1 hour1,910
Air Quality Monitor

Things used in this project

Hardware components

Intel Edison
Intel Edison
+ all other components in the Main Project except the GPS module
×1

Software apps and online services

Intel XDK IoT Edition

Story

Read more

Schematics

Fritzing image

GPS is not used

Code

Code

JavaScript
//Isolated node: local viewing of data only

// we want mraa to be at least version 0.6.1
var mraa = require('mraa');
var version = mraa.getVersion();
if (version >= 'v0.6.1') {
    console.log('mraa version (' + version + ') ok');
}
else {
    console.log('meaa version(' + version + ') is old - this code may not work');
}
var Gas = require("jsupm_gas"); // Load UPM gas sensor
var MQ2 = new Gas.MQ2(0); // Analog input 0
var MQ5 = new Gas.MQ5(1); // Analog input 1
var MQ9 = new Gas.MQ9(3); // Analog input 3
var TP401 = new Gas.TP401(2); // Analog input 2 // Winsen MP503 Gas Sensor or Air Quality Sensor v1.3

useUpm();

function useUpm() {
    var lcd = require('jsupm_i2clcd');
    var display = new lcd.Jhd1313m1(0, 0x3E, 0x62);
    display.setCursor(1, 1);
    display.write('hi there');
    var lcdMessage = " ";
    var read_MQ2 = 0;
    var read_MQ5 = 0;
    var read_TP401 = 0;
    var read_MQ9 = 0;
    rotateColors(display);
}
/**
 * Rotate through a color pallette and display the sensor readings locally
 */
function rotateColors(display) {
    var red = 0;
    var green = 0;
    var blue = 0;
    var i = 0;
    display.setColor(red, green, blue);
    setInterval(function() {
        read_MQ2 = MQ2.getSample(); // 300 - 10k ppm
	    read_MQ5 = MQ5.getSample();
	    read_MQ9 = MQ9.getSample();
	    read_TP401 = TP401.getSample();
        var lcdMessage = ["MQ2 = "+read_MQ2+" ", "MQ5 = "+read_MQ5+" ", "MQ9 = "+read_MQ9+" ", "CO level = "+read_TP401+" "];
        blue += 64;
        if (blue > 255) {
            blue = 0;
            green += 64;
            if (green > 255) {
                green = 0;
                red += 64;
                if (red > 255) {
                    red = 0;
                }
            }
        }
        display.setColor(red, green, blue);
        display.setCursor(0,0);
        if (i==4){i=0;} // reset array if reaches end
        display.write(lcdMessage[i] + '      '); i++; // Pad extra space to clear out old display data
    }, 5000);
}

// toggle the entire data set that appears (1. MQ2 value, etc.) every 0.5s
loop();

function loop() {
    var i;
    read_MQ2 = MQ2.getSample(); // 300 - 10k ppm
    read_MQ5 = MQ5.getSample();
	read_MQ9 = MQ9.getSample();
	read_TP401 = TP401.getSample();
    var lcdMessage = ["MQ2 = " + read_MQ2 + " ", "MQ5 = " + read_MQ5 + " ", "MQ9 = " + read_MQ9 + " ", "CO level = " + read_TP401 + " "];
    for (i = 0; (i < lcdMessage.length); i++ ) {
        console.log(lcdMessage[i]); // Print element of the string array
    }  
}

setInterval(loop,500); // call the loop function every 0.5s

Credits

Kamaljit Chahal

Kamaljit Chahal

2 projects • 3 followers
Electrical engineering recent graduate
Thanks to IoT Warriors and Intel IoT Innovator Lab @ WSU.

Comments