• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*jslint node:true, vars:true, bitwise:true, unparam:true */
2/*jshint unused:true */
3/*
4* Author: Zion Orent <zorent@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
27var 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.
31var myCO2_obj = new CO2_lib.MHZ16(0);
32
33// make sure port is initialized properly.  9600 baud is the default.
34if (!myCO2_obj.setupTty(CO2_lib.int_B9600))
35{
36	console.log("Failed to setup tty port parameters");
37	process.exit(0);
38}
39
40outputStr = "Make sure that the sensor has had " +
41			"at least 3 minutes to warm up";
42console.log(outputStr);
43outputStr = "or you will not get valid results.";
44console.log(outputStr);
45outputStr = "The temperature reported is not the ambient temperature,";
46console.log(outputStr);
47outputStr = "but rather the temperature of the sensor elements.";
48console.log(outputStr);
49
50var gas = CO2_lib.new_intp();
51var temp = CO2_lib.new_intp();
52
53function 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}
61var myInterval;
62setTimeout(function()
63{
64	myInterval = setInterval(writeCO2data, 2000);
65}, 1000);
66
67
68// Print message, clear memory when exiting
69process.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