Skip to content

Commit

Permalink
add HCHO
Browse files Browse the repository at this point in the history
  • Loading branch information
lsongdev committed Sep 9, 2018
1 parent e39f6be commit 1d94184
Show file tree
Hide file tree
Showing 10 changed files with 1,228 additions and 91 deletions.
26 changes: 26 additions & 0 deletions HCHO.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const SerialPort = require('serialport');
const createReader = require('./reader');

class HCHO extends SerialPort {
constructor(dev){
super(dev);
const reader = createReader({
LEN_LENGTH: 1
}, message => {
console.log(message);
});
this.on('data', reader);
}
send(cmd, data){
let checksum = 0;
const packet = Buffer.alloc(7);
packet.writeUIntBE(0x42, 0); checksum+=0x42;
packet.writeUIntBE(0x4d, 1); checksum+=0x4d;
packet.writeUIntBE(cmd, 2); checksum+=cmd;
packet.writeUInt16BE(data, 3); checksum+=data;
packet.writeUInt16BE(checksum, 5);
return this.write(packet);
}
}

module.exports = HCHO;
55 changes: 55 additions & 0 deletions PMS5003.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const fs = require('fs');
const assert = require('assert');
const EventEmitter = require('events');
const createReader = require('./reader');

/**
* parse sensor data
*/
function parse(buffer){
var data = {
Head : buffer.readUInt16BE(00),
Length : buffer.readUInt16BE(02),
PM1_0 : buffer.readUInt16BE(04),
PM2_5 : buffer.readUInt16BE(06),
PM10 : buffer.readUInt16BE(08),
PM1_0_ : buffer.readUInt16BE(10),
PM2_5_ : buffer.readUInt16BE(12),
PM10_ : buffer.readUInt16BE(14),
UM0_3 : buffer.readUInt16BE(16),
UM0_5 : buffer.readUInt16BE(18),
UM1_0 : buffer.readUInt16BE(20),
UM2_5 : buffer.readUInt16BE(22),
UM5_0 : buffer.readUInt16BE(24),
UM10 : buffer.readUInt16BE(26),
VERSION : buffer.readUInt8(28),
ERRCODE : buffer.readUInt8(29),
CHECKSUM: buffer.readUInt16BE(30)
};
var checksum = 0;
for(var i=0;i<buffer.length - 2;i++){
checksum += buffer[i];
}
assert.equal(data.Head, 0x424d, 'Invalid data header');
assert.equal(checksum, data.CHECKSUM, 'Checksum error');
return data;
}

/**
* DSensor
*/
class DSensor extends EventEmitter {
constructor(dev){
super();
if(typeof dev === 'undefined') dev = process.stdin;
if(typeof dev === 'string') dev = fs.createReadStream(dev);
const reader = createReader(data => {
const message = parse(data);
this.emit('data', message);
});
dev.on('data', reader);
return this;
}
}

module.exports = DSensor;
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ sensor.on('data', function(data){
});
```

![](docs/IMG_1705.JPG)
![](docs/PMS5003.jpg)

### Contributing
- Fork this Repo first
Expand Down
File renamed without changes
11 changes: 3 additions & 8 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
const { HCHO } = require('..');

const DSensor = require('..');

var sensor = new DSensor('/dev/cu.usbserial');

sensor.on('data', function(data){
console.log(data);
});

const hc = new HCHO("/dev/xxx");

hc.send(0xe2, 0xaabb);
4 changes: 1 addition & 3 deletions example/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ function Linear(AQIhigh, AQIlow, Conchigh, Conclow, Concentration){
return linear;
}

var sensor = new DSensor('/dev/cu.usbserial')
.on('data', function(data){

new DSensor('/dev/cu.usbserial').on('data', function(data){
var pm25 = data[ 'PM2_5' ];
console.log(JSON.stringify({
PM25: pm25,
Expand Down
82 changes: 4 additions & 78 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,10 @@
const fs = require('fs');
const util = require('util');
const EventEmitter = require('events');

const STATE_HEADER_1 = 0x42;
const STATE_HEADER_2 = 0x4d;
const STATE_LENGTH = 2;
const STATE_DATA = 3;
class DSensor extends EventEmitter {

const LEN_HEADER = 2;
const LEN_LENGTH = 2;
const LEN_CHECKSUM = 2;

/**
* DSensor
*/
function DSensor(dev){
EventEmitter.call(this);
if(!(this instanceof DSensor)) return new DSensor(dev);
var flag = STATE_HEADER_1, index, len, buffer = new Buffer([]);
if(typeof dev === 'undefined') dev = process.stdin;
if(typeof dev === 'string') dev = fs.createReadStream(dev);
dev.on('data', function(chunk){
buffer = Buffer.concat([ buffer, chunk ]);
for(var i = 0; i < buffer.length; i++){
if(flag === STATE_HEADER_1 && buffer[i] === STATE_HEADER_1){
flag = STATE_HEADER_2;
continue;
}
if(flag === STATE_HEADER_2 && buffer[i] === STATE_HEADER_2){
flag = STATE_LENGTH;
index = ++i;
continue;
}
if(flag === STATE_LENGTH && buffer.length - index >= LEN_LENGTH){
len = buffer.readUInt16BE(index);
flag = STATE_DATA;
continue;
}
if(flag === STATE_DATA && buffer.length - index >= len + LEN_CHECKSUM){
this.parse(buffer.slice(index - LEN_HEADER, index + len + LEN_CHECKSUM));
buffer = buffer.slice(index + len + LEN_CHECKSUM);
flag = STATE_HEADER_1;
}
}
}.bind(this));
return this;
}

util.inherits(DSensor, EventEmitter);
/**
* parse sensor data
*/
DSensor.prototype.parse = function(buffer){
var data = {
Head : buffer.readUInt16BE(00),
Length : buffer.readUInt16BE(02),
PM1_0 : buffer.readUInt16BE(04),
PM2_5 : buffer.readUInt16BE(06),
PM10 : buffer.readUInt16BE(08),
PM1_0_ : buffer.readUInt16BE(10),
PM2_5_ : buffer.readUInt16BE(12),
PM10_ : buffer.readUInt16BE(14),
UM0_3 : buffer.readUInt16BE(16),
UM0_5 : buffer.readUInt16BE(18),
UM1_0 : buffer.readUInt16BE(20),
UM2_5 : buffer.readUInt16BE(22),
UM5_0 : buffer.readUInt16BE(24),
UM10 : buffer.readUInt16BE(26),
VERSION : buffer.readUInt8(28),
ERRCODE : buffer.readUInt8(29),
CHECKSUM: buffer.readUInt16BE(30)
};
var checksum = 0;
for(var i=0;i<buffer.length - 2;i++){
checksum += buffer[i];
}
if(data.Head === 0x424d && data.CHECKSUM === checksum){
this.emit('data', data);
}
return this;
}
DSensor.HCHO = require('./HCHO');
DSensor.PMS5003 = require('./PMS5003');

module.exports = DSensor;
module.exports = DSensor;
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
"type": "git"
},
"author": "Lsong <song940@gmail.com>",
"license": "MIT"
"license": "MIT",
"dependencies": {
"serialport": "^7.0.2"
}
}
39 changes: 39 additions & 0 deletions reader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

function createReader({
LEN_HEADER = 2,
LEN_LENGTH = 2,
LEN_CHECKSUM = 2
}, fn){
const STATE_HEADER_1 = 0x42;
const STATE_HEADER_2 = 0x4d;
const STATE_LENGTH = 2;
const STATE_DATA = 3;
var flag = STATE_HEADER_1, index, len, buffer = Buffer.alloc(0);
return function(chunk){
buffer = Buffer.concat([ buffer, chunk ]);
for(var i = 0; i < buffer.length; i++){
if(flag === STATE_HEADER_1 && buffer[i] === STATE_HEADER_1){
flag = STATE_HEADER_2;
continue;
}
if(flag === STATE_HEADER_2 && buffer[i] === STATE_HEADER_2){
flag = STATE_LENGTH;
index = ++i;
continue;
}
if(flag === STATE_LENGTH && buffer.length - index >= LEN_LENGTH){
len = buffer.readUInt16BE(index);
flag = STATE_DATA;
continue;
}
if(flag === STATE_DATA && buffer.length - index >= len + LEN_CHECKSUM){
fn(buffer.slice(index - LEN_HEADER, index + len + LEN_CHECKSUM));
buffer = buffer.slice(index + len + LEN_CHECKSUM);
flag = STATE_HEADER_1;
}
}
};
}


module.exports = createReader;
Loading

0 comments on commit 1d94184

Please sign in to comment.