Peter Wessel
Published © GPL3+

Air Quality Reader Using nanoFramework

If you've wanted to measure the air quality at your workplace, look no further.

BeginnerProtip1 hour1,232
Air Quality Reader Using nanoFramework

Things used in this project

Hardware components

Nova PM Sensor SDS011
×1
Netduino
Wilderness Labs Netduino
×1

Software apps and online services

Visual Studio extension
.NET nanoFramework Visual Studio extension
.NET nanoFramework nanoFramework.Windows.Devices.Gpio
.NET nanoFramework nanoFramework.Windows.Devices.SerialCommunication
Microsoft Visual Studio 2017 Community Edition

Story

Read more

Schematics

Not a fritzing but should be clear enough

How to connect the SDS011 to the Netduino 3 Wifi

Code

The serial data received logic

C#
        private static void _sds011_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (e.EventType == SerialData.Chars)
            {
                // Ignore the in between noise
            }
            else if (e.EventType == SerialData.WatchChar)
            {
                SerialDevice serDev = (SerialDevice)sender;

                using (DataReader dr = new DataReader(serDev.InputStream))
                {
                    dr.InputStreamOptions = InputStreamOptions.Partial;
                    uint bytesRead = dr.Load(serDev.BytesToRead);

                    if (bytesRead > 0)
                    {
                        byte[] rawData = new byte[bytesRead];
                        dr.ReadBytes(rawData);

                        // If rawData.Length == 10 and rawData[0] = 0xAA and rawData[9] = 0xAB and rawData[1] = 0xC0
                        // this means we have a valid measure package from the sensor
                        // and byte[2] = low byte, byte[3] = high byte of uint representing the AQI for PM 2.5
                        if (rawData.Length >= 10)
                        {
                            if ((rawData[0] == 0xAA) && (rawData[1] == 0xC0) && (rawData[9] == 0xAB))
                            {
                                // Need to do checksum
                                byte crc = 0;
                                for (int i = 0; i < 6; i++)
                                {
                                    crc += rawData[i + 2];
                                }
                                if (crc == rawData[8])
                                {
                                    // All right, we have a go !!!!
                                    float pm25 = 0, pm10 = 0;

                                    pm25 = (float)((int)rawData[2] | (int)(rawData[3] << 8)) / 10;
                                    pm10 = (float)((int)rawData[4] | (int)(rawData[5] << 8)) / 10;

                                    Console.WriteLine(String.Format("Air quality index: {0}\tPM 10\t{1} µg / m3\tPM 2.5\t{2} µg / m3\tSensor: {3}",
                                        DateTime.UtcNow.ToString("yyyy'-'MM'-'dd'\t'HH':'mm':'ss"),
                                        pm10.ToString("N1"),
                                        pm25.ToString("N1"),
                                        "SDS011"));
                                }
                            }
                        }
                    }
                }
            }
        }

Air Quality Index

Simple demo how to read the partical matter values in PM10 and PM2.5

Credits

Peter Wessel
3 projects • 5 followers
Contributor for nanoFramework organisation, long time developer, started with a Motorola 6800 in machine code.
Contact

Comments

Please log in or sign up to comment.