• @Robin

    Here my small Node script, I've just connect de SDS011 to an USB to my computer (port COM8):

    ///////////////////////////////////
    // Hello, SDS011
    ///////////////////////////////////
    const SDS011Wrapper = require("sds011-wrapper");
    
    const sensor = new SDS011Wrapper("COM8"); // Use your system path of SDS011 sensor.
    
    console.log("process.env " , process.env);
    
    sensor
        .setReportingMode('active')
        .then(() => {
            console.log("Sensor is now working in active mode.");
            return sensor.setWorkingPeriod(0); // Sensor will send data as soon as new data is available.
        })
        .then(() => {
            console.log("Working period set to 0 minutes.");
            console.log("\nSensor readings:");
    
            // Since working period was set to 0 and mode was set to active, this event will be emitted as soon as new data is received.
            sensor.on('measure', (data) => {
                console.log(`[${new Date().toISOString()}] ${JSON.stringify(data)}`);
            });
        });
    

    I've added a console.log to know what the serialPort npm Node module serial output is like (I've edited the file wrapper.js in the node module directory : node_modules\sds011-wrapper\wrapper.js)

    Here a sample view of wrapper.js where I've put my console.log:

    const SerialPort = require('serialport');
    const EventEmitter = require('events');
    
    const SensorState = require("./core/sensor-state.js");
    const SensorCommand = require("./core/sensor-command.js");
    
    const addChecksumToCommandArray = require("./core/packet-utils.js").addChecksumToCommandArray;
    const verifyPacket = require("./core/packet-utils.js").verifyPacket;
    
    const PacketHandlers = require("./core/packet-handlers.js");
    
    const ALLOWED_RETRIES = 10; // Number of retries allowed for single command request. 
    const COMMAND_RETRY_INTERVAL = 150; // Time between sequential retries.
    
    class SDS011Wrapper extends EventEmitter {
    
        /**
         * Open sensor.
         *
         * @param {string} portPath - Serial port path
         */
        constructor(portPath) {
            super();
    
            this._port = new SerialPort(portPath, { baudRate: 9600 });
            this._state = new SensorState();
    
            this._commandQueue = [];
            this._isCurrentlyProcessing = false;
            this._retryCount = 0;
    
            this._port.on('error', function (err) {
                console.log('Error: ', err.message);
            });
    
            this._port.on('close', () => {
                console.log('SDS011Wrapper port closed');
                this.close();
            });
    
            /**
              * Listen for incoming data and react: change internal state so queued commands know that they were completed or emit data.
              */
            this._port.on('data', (data) => {
                console.log("///// data ////// " , data);
                if (verifyPacket(data)) {
                ...
                ...
    

    now the output :

    ///// data //////  <Buffer aa c0 f3 1b 1f 4e 9c eb 02 ab>
    [2019-06-22T16:29:46.134Z] {"PM2.5":715.5,"PM10":1999.9}
    
    

    you can see that the BaudRate is default to 9600 (wrapper.js), and I've set the same value when I use the sensor connected to Espruino Pico, here is my code :

    function onInit() {
      
            Serial1.setup(9600, {rx: B7, tx: B6});
            Serial1.on('data', function (data) {
                 console.log("> data  : " , data);
            }
    }
    
    

    with result like :

    > data  :  ­
    > data  :  ¶
    > data  :  
    > data  :  «
    > data  :  =
    > data  :  ¼
    > data  :  Œ
    > data  :  ¶
    > data  :  
    > data  :  ¯
    > data  :  ¾
    > data  :  ä
    > data  :  q
    > data  :  =
    > data  :  }
    > data  :  þ
    > data  :  ú
    > data  :  N
    > data  :  œ
    > data  :  ë
    > data  :  Ï
    > data  :  ÿ
    > data  :  
    > data  :  
    > data  :  N
    > data  :  Ü
    > data  :  ÿ
    > data  :  ü
    > data  :  ¡
    > data  :  
    > data  :  
    > data  :  Î
    > data  :  œ
    > data  :  ÿ
    
About

Avatar for larry @larry started