• Here is the module for the SDS011, according to the manual to check the checksum and get proper value for PM2.5 and PM10.

    const o = {};
    
    function checkvalidity(bufferHex) {
    
        let  sum = 0;
        for (let i = 2; i < 8; i++) {
            sum = sum + parseInt(bufferHex[i], 16);
        }
    
        sum = sum &0xFF;
        const validCheckSum = parseInt(bufferHex[8],16);
    
        return sum == validCheckSum;
    }
    
    o.init = function (serial, callback) {
        console.log("> mod-dustsensor - init");
    
        //--- Exemple buffer
        //  data <Buffer aa  c0  1b  05  7d  08  9c  eb  2c  ab>
        //               170 192 27  5   125 8   156 235 44  171
    
        let bufferDecimal = [];
        let bufferHex = [];
        let bufferLength = 0;
    
        serial.on('data', function (data) {
    
            for (index in data) {
                let charDecimal = data.charCodeAt(index); // chope la valeur decimal du caractère
                let charHex = data.charCodeAt(index).toString(16); // chope la valeur Hex du caractère
    
                bufferDecimal.push(charDecimal);
                bufferHex.push(charHex);
                bufferLength++;
    
                if (+charDecimal == 171 && bufferLength == 10) {
    
                    let validity = checkvalidity(bufferHex);
    
                    if(validity){
                        // PM2.5 value -  ((PM2.5 High byte[3] *256) + PM2.5 low byte[2] ) / 10
                        // PM10 value -  ((PM10 High byte[5] *256) + PM10 low byte[4] ) / 10
                        let pm2_5 = ((bufferDecimal[3] * 256 ) + bufferDecimal[2])/10;
                        let pm10 = ((bufferDecimal[5] * 256 ) + bufferDecimal[4])/10;
                        callback({
                            pm2_5:pm2_5,
                            pm10:pm10
                        });
                    }
    
                     bufferLength = 0;
                     bufferDecimal=[];
                     bufferHex=[];
    
                }
            }
        });
    };
    
    module.exports = o;
    

    Entry point :

    const modDustSensor = require('mod-sds011');
    
    function onInit() {
    
        Serial1.setup(9600, {rx: B7, tx: B6});
    
        modDustSensor.init(Serial1, function(pmValues){
            console.log(`PM-2.5: ${pmValues.pm2_5} --- PM-10: ${pmValues.pm10} `);
        });
    
    }
    
About

Avatar for larry @larry started