No Description

mhz16.js 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. var CO2_lib = require('jsupm_mhz16');
  27. // Instantiate a MHZ16 serial CO2 sensor on uart 0.
  28. // This example was tested on the Grove CO2 sensor module.
  29. var myCO2_obj = new CO2_lib.MHZ16(0);
  30. // make sure port is initialized properly. 9600 baud is the default.
  31. if (!myCO2_obj.setupTty(CO2_lib.int_B9600))
  32. {
  33. console.log("Failed to setup tty port parameters");
  34. process.exit(0);
  35. }
  36. outputStr = "Make sure that the sensor has had " +
  37. "at least 3 minutes to warm up";
  38. console.log(outputStr);
  39. outputStr = "or you will not get valid results.";
  40. console.log(outputStr);
  41. outputStr = "The temperature reported is not the ambient temperature,";
  42. console.log(outputStr);
  43. outputStr = "but rather the temperature of the sensor elements.";
  44. console.log(outputStr);
  45. var gas = CO2_lib.new_intp();
  46. var temp = CO2_lib.new_intp();
  47. function writeCO2data()
  48. {
  49. myCO2_obj.getData(gas, temp);
  50. outputStr = "CO2 concentration: " + CO2_lib.intp_value(gas) +
  51. " PPM, " +
  52. "Temperature (in C): " + CO2_lib.intp_value(temp);
  53. console.log(outputStr);
  54. }
  55. var myInterval;
  56. setTimeout(function()
  57. {
  58. myInterval = setInterval(writeCO2data, 2000);
  59. }, 1000);
  60. // Print message, clear memory when exiting
  61. process.on('SIGINT', function()
  62. {
  63. clearInterval(myInterval);
  64. myCO2_obj = null;
  65. CO2_lib.cleanUp();
  66. CO2_lib = null;
  67. console.log("Exiting");
  68. process.exit(0);
  69. });