From ad0cfd530515925d87aec4ec3d0c6a0ee5b1bd8c Mon Sep 17 00:00:00 2001 From: dpro Date: Mon, 2 Oct 2017 07:42:54 +0300 Subject: [PATCH 1/2] Create mhz19.js var mhz19 = require('@amperka/mhz19.js').connect({ rx: P0, tx: P1, speed: 9600, port: Serial3 }); console.log( mhz19.read() ); --- modules/@amperka/mhz19.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 modules/@amperka/mhz19.js diff --git a/modules/@amperka/mhz19.js b/modules/@amperka/mhz19.js new file mode 100644 index 0000000..3949f2c --- /dev/null +++ b/modules/@amperka/mhz19.js @@ -0,0 +1,22 @@ +var MHZ19 = function(opts) { + opts = opts || {}; + this._tx = opts.tx || P1; + this._rx = opts.rx || P0; + this._serial = opts.port || Serial3; + this._serial.setup(opts.speed || 9600); +}; + +MHZ19.prototype.calibrate = function() { this._serial.write("\xFF\x01\x87\x00\x00\x00\x00\x00\x78"); } +MHZ19.prototype.abc_on = function() { this._serial.write("\xFF\x01\x79\xA0\x00\x00\x00\x00\xE6"); } +MHZ19.prototype.abc_off = function() { this._serial.write("\xFF\x01\x79\x00\x00\x00\x00\x00\x86"); } +MHZ19.prototype.read = function() { this._serial.write("\xFF\x01\x86\x00\x00\x00\x00\x00\x79"); + var data = this._serial.read(9); + var a = []; + for (var i=0; i < data.length; i++) { a.push(data.charCodeAt(i)); } + var status = (256 - (a[1] + a[2] + a[3] + a[4] + a[5] + a[6] + a[7])%256) == a[8]; + var co2 = a[2] * 256 + a[3]; + var temp = a[4]-40; + return { status: status, co2: co2, temp: temp }; + }; + +exports.connect = function(opts) { return new MHZ19(opts) }; From d3bdb82ec6c337f8e11852c7833c4f162fb5b5c7 Mon Sep 17 00:00:00 2001 From: dpro Date: Wed, 4 Oct 2017 09:01:49 +0300 Subject: [PATCH 2/2] Update mhz19.js var mhz19 = require('@amperka/mhz19.js').connect({tx: P1, rx: P0, port: Serial3, speed: 9600}); --- modules/@amperka/mhz19.js | 48 ++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/modules/@amperka/mhz19.js b/modules/@amperka/mhz19.js index 3949f2c..7606448 100644 --- a/modules/@amperka/mhz19.js +++ b/modules/@amperka/mhz19.js @@ -1,22 +1,34 @@ var MHZ19 = function(opts) { - opts = opts || {}; - this._tx = opts.tx || P1; - this._rx = opts.rx || P0; - this._serial = opts.port || Serial3; - this._serial.setup(opts.speed || 9600); + opts = opts || {}; + this._serial = opts.port; + this._serial.setup(opts.speed, {rx: opts.rx, tx: opts.tx}); }; -MHZ19.prototype.calibrate = function() { this._serial.write("\xFF\x01\x87\x00\x00\x00\x00\x00\x78"); } -MHZ19.prototype.abc_on = function() { this._serial.write("\xFF\x01\x79\xA0\x00\x00\x00\x00\xE6"); } -MHZ19.prototype.abc_off = function() { this._serial.write("\xFF\x01\x79\x00\x00\x00\x00\x00\x86"); } -MHZ19.prototype.read = function() { this._serial.write("\xFF\x01\x86\x00\x00\x00\x00\x00\x79"); - var data = this._serial.read(9); - var a = []; - for (var i=0; i < data.length; i++) { a.push(data.charCodeAt(i)); } - var status = (256 - (a[1] + a[2] + a[3] + a[4] + a[5] + a[6] + a[7])%256) == a[8]; - var co2 = a[2] * 256 + a[3]; - var temp = a[4]-40; - return { status: status, co2: co2, temp: temp }; - }; +MHZ19.prototype.calibrate = function() { + this._serial.write('\xFF\x01\x87\x00\x00\x00\x00\x00\x78'); +}; + +MHZ19.prototype.abcOn = function() { + this._serial.write('\xFF\x01\x79\xA0\x00\x00\x00\x00\xE6'); +}; + +MHZ19.prototype.abcOff = function() { + this._serial.write('\xFF\x01\x79\x00\x00\x00\x00\x00\x86'); +}; + +MHZ19.prototype.read = function() { + this._serial.write('\xFF\x01\x86\x00\x00\x00\x00\x00\x79'); + var data = this._serial.read(9); + var a = []; + for (var i=0; i < data.length; i++) { + a.push(data.charCodeAt(i)); + } + var status = (256 - (a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7])%256) === a[8]; + var co2 = a[2] * 256 + a[3]; + var temp = a[4]-40; + return {status: status, co2: co2, temp: temp}; +}; -exports.connect = function(opts) { return new MHZ19(opts) }; +exports.connect = function(opts) { + return new MHZ19(opts); +};