• I did something more concise, following your advice:

    const o = {};
    
    function checkvalidity(buff) {
        let sum =  E.sum(buff.slice(2,8));
        sum = sum & 0xFF;
        const validCheckSum = buff[8];
        return sum == validCheckSum;
    }
    
    o.init = function (serial, callback) {
    
        //--- 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 bufferLength = 0;
    
        serial.on('data', function (data) {
    
            for (index in data) {
    
                let charDecimal = data.charCodeAt(index); // chope la valeur decimal du caractère
                bufferDecimal.push(charDecimal);
    
                bufferLength++;
    
                if (bufferLength == 10) {
    
    
                    let validity = checkvalidity(bufferDecimal);
    
                    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
                        });
    
                    } else {
                        console.log("validity false");
                    }
    
                    bufferLength = 0;
                    bufferDecimal=[];
    
                }
            }
        });
    };
    
    module.exports = o;
    
    

    I 've started to build a closure with an soap plastic box, with inside the dust sensor, an ESP8266 or Espruino Pico and a small 128*64 Oled screen.

    I would take data measurements while walking in Paris. I am curious about the results especially at the level of the big automobile hub, and close to the ring road.

    I'll post the full project in the "Project" section of the forum if I do not encounter new problems ! (I have never used the Oled screen)

About

Avatar for larry @larry started