Home | History | Annotate | Download | only in javascript
      1 /*jslint node:true, vars:true, bitwise:true, unparam:true */
      2 /*jshint unused:true */
      3 /*
      4 * Author: Zion Orent <zorent (at) ics.com>
      5 * Copyright (c) 2015 Intel Corporation.
      6 *
      7 * Permission is hereby granted, free of charge, to any person obtaining
      8 * a copy of this software and associated documentation files (the
      9 * "Software"), to deal in the Software without restriction, including
     10 * without limitation the rights to use, copy, modify, merge, publish,
     11 * distribute, sublicense, and/or sell copies of the Software, and to
     12 * permit persons to whom the Software is furnished to do so, subject to
     13 * the following conditions:
     14 *
     15 * The above copyright notice and this permission notice shall be
     16 * included in all copies or substantial portions of the Software.
     17 *
     18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
     22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     25 */
     26 
     27 var CO2_lib = require('jsupm_mhz16');
     28 
     29 // Instantiate a MHZ16 serial CO2 sensor on uart 0.
     30 // This example was tested on the Grove CO2 sensor module.
     31 var myCO2_obj = new CO2_lib.MHZ16(0);
     32 
     33 // make sure port is initialized properly.  9600 baud is the default.
     34 if (!myCO2_obj.setupTty(CO2_lib.int_B9600))
     35 {
     36 	console.log("Failed to setup tty port parameters");
     37 	process.exit(0);
     38 }
     39 
     40 outputStr = "Make sure that the sensor has had " +
     41 			"at least 3 minutes to warm up";
     42 console.log(outputStr);
     43 outputStr = "or you will not get valid results.";
     44 console.log(outputStr);
     45 outputStr = "The temperature reported is not the ambient temperature,";
     46 console.log(outputStr);
     47 outputStr = "but rather the temperature of the sensor elements.";
     48 console.log(outputStr);
     49 
     50 var gas = CO2_lib.new_intp();
     51 var temp = CO2_lib.new_intp();
     52 
     53 function writeCO2data()
     54 {
     55 	myCO2_obj.getData(gas, temp);
     56 	outputStr = "CO2 concentration: " + CO2_lib.intp_value(gas) +
     57 	" PPM, " +
     58 	"Temperature (in C): " + CO2_lib.intp_value(temp);
     59 	console.log(outputStr);
     60 }
     61 var myInterval;
     62 setTimeout(function()
     63 {
     64 	myInterval = setInterval(writeCO2data, 2000);
     65 }, 1000);
     66 
     67 
     68 // Print message, clear memory when exiting
     69 process.on('SIGINT', function()
     70 {
     71 	clearInterval(myInterval);
     72 	myCO2_obj = null;
     73 	CO2_lib.cleanUp();
     74 	CO2_lib = null;
     75 	console.log("Exiting");
     76 	process.exit(0);
     77 });
     78