Last active
July 3, 2018 16:17
-
-
Save cheton/3c6a801be689638326d506f1167c585f to your computer and use it in GitHub Desktop.
A Particulate Matter Detector Using PM 2.5 Sensor by PLANTOWER
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// http://aqicn.org/air/view/sensor/spec/pms7003.pdf | |
// | |
const SerialPort = require('serialport'); | |
const port = new SerialPort('/dev/ttyUSB0', { | |
baudRate: 9600 | |
}); | |
port.on('data', (data) => { | |
const payload = data.toString('hex'); | |
const start = payload.slice(0, 4); | |
if (start !== '424d') { | |
return; | |
} | |
const frameLength = data.readUInt16BE(2); | |
const offset = 4; | |
if ((frameLength + offset) < data.length) { | |
console.error(`frame length exceeds ${data.length - offset}`); | |
return; | |
} | |
const c0 = data.readUInt16BE(offset + ((frameLength / 2 - 1) << 1)); | |
const c1 = data.slice(0, -2).reduce((acc, val) => acc + val, 0) | |
if (c0 !== c1) { | |
console.error('checksum error'); | |
return; | |
} | |
// Standard Particles or CF-1, bytes 4-9 | |
const standardParticles = [ | |
data.readUInt16BE(offset + ((1 - 1) << 1)), // PM 1.0 | |
data.readUInt16BE(offset + ((2 - 1) << 1)), // PM 2.5 | |
data.readUInt16BE(offset + ((3 - 1) << 1)), // PM 10 | |
]; | |
console.log('Standard Particles (CF-1):'); | |
console.log(` PM 1.0: ${standardParticles[0]} (ug/m3), PM 2.5: ${standardParticles[1]} (ug/m3), PM 10: ${standardParticles[2]} (ug/m3)`); | |
// Atmospheric Environment, bytes 10-15 | |
const atmosphericEnvironment = [ | |
data.readUInt16BE(offset + ((4 - 1) << 1)), // PM 1.0 | |
data.readUInt16BE(offset + ((5 - 1) << 1)), // PM 2.5 | |
data.readUInt16BE(offset + ((6 - 1) << 1)), // PM 10 | |
]; | |
console.log('Atmosphere Environment:'); | |
console.log(` PM 1.0: ${atmosphericEnvironment[0]} (ug/m3), PM 2.5: ${atmosphericEnvironment[1]} (ug/m3), PM 10: ${atmosphericEnvironment[2]} (ug/m3)`); | |
// Particles | |
if ((frameLength / 2) > 12) { | |
const particles = [ | |
data.readUInt16BE(offset + ((7 - 1) << 1)), // 0.3um | |
data.readUInt16BE(offset + ((8 - 1) << 1)), // 0.5um | |
data.readUInt16BE(offset + ((9 - 1) << 1)), // 1.0um | |
data.readUInt16BE(offset + ((10 - 1) << 1)), // 2.5um | |
data.readUInt16BE(offset + ((11 - 1) << 1)), // 5.0um | |
data.readUInt16BE(offset + ((12 - 1) << 1)), // 10um | |
]; | |
console.log('Particles (under 0.1L):'); | |
console.log(` * 0.3um: ${particles[0]}`); | |
console.log(` * 0.5um: ${particles[1]}`); | |
console.log(` * 1.0um: ${particles[2]}`); | |
console.log(` * 2.5um: ${particles[3]}`); | |
console.log(` * 5.0um: ${particles[4]}`); | |
console.log(` * 10um: ${particles[5]}`); | |
} | |
}); |
Author
cheton
commented
Jul 3, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment