Home | History | Annotate | Download | only in javascript
      1 /*
      2  * Author: Mihai Tudor Panu <mihai.tudor.panu (at) intel.com>
      3  * Copyright (c) 2014 Intel Corporation.
      4  *
      5  * Permission is hereby granted, free of charge, to any person obtaining
      6  * a copy of this software and associated documentation files (the
      7  * "Software"), to deal in the Software without restriction, including
      8  * without limitation the rights to use, copy, modify, merge, publish,
      9  * distribute, sublicense, and/or sell copies of the Software, and to
     10  * permit persons to whom the Software is furnished to do so, subject to
     11  * the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be
     14  * included in all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
     20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     23  */
     24 
     25 var upmTP401 = require('jsupm_gas');
     26 //var time = require('sleep');
     27 
     28 //give a qualitative meaning to the value from the sensor
     29 function airQuality(value)
     30 {
     31     if(value < 50) return "Fresh Air";
     32     if(value < 200) return "Normal Indoor Air";
     33     if(value < 400) return "Low Pollution";
     34     if(value < 600) return "High Pollution - Action Recommended";
     35     return "Very High Pollution - Take Action Immediately";
     36 }
     37 
     38 function loop()
     39 {
     40     //read values (consecutive reads might vary slightly)
     41     var value = airSensor.getSample();
     42     var ppm = airSensor.getPPM();
     43 
     44     //write the sensor values to the console
     45     console.log("raw: " + value + " ppm: " + (" " + ppm.toFixed(2)).substring(-5, 5) + "   " + airQuality(value));
     46 
     47     //wait 2.5 s then call function again
     48     setTimeout(loop, 2500);
     49 }
     50 
     51 //setup sensor on Analog pin #0 (A0)
     52 var airSensor = new upmTP401.TP401(0);
     53 
     54 //warm up sensor
     55 console.log("Sensor is warming up for 3 minutes..");
     56 var i = 1;
     57 
     58 //print a message every passing minute
     59 var waiting = setInterval(function() {
     60         console.log(i++ + " minute(s) passed.");
     61         if(i == 3) clearInterval(waiting);
     62     }, 60000);
     63 
     64 //start loop in 3 minutes
     65 setTimeout(function(){
     66     console.log("Sensor is ready!");
     67     loop();
     68     }, 180000);
     69