{"id":2575,"date":"2017-07-25T21:55:31","date_gmt":"2017-07-25T20:55:31","guid":{"rendered":"http:\/\/www.robertprice.co.uk\/robblog\/?p=2575"},"modified":"2017-07-25T23:46:28","modified_gmt":"2017-07-25T22:46:28","slug":"running-sds011-particulate-sensor-mac-using-php","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/running-sds011-particulate-sensor-mac-using-php\/","title":{"rendered":"Running a SDS011 particulate sensor on a Mac using PHP"},"content":{"rendered":"<p>The Novafit <a href=\"https:\/\/github.com\/opendata-stuttgart\/meta\/blob\/master\/files\/SDS011-V1.3.pdf\">SDS011<\/a> particulate sensor is an a very affordable sensor for detecting particulate pollution. It is capable of detecting both PM2.5 and PM10 with a relative error margin of +\/- 10\u00b5g\/m3.<\/p>\n<p>It can output data via it&#8217;s serial port. The one I bought came with a serial to USB adaptor, allowing it to be plugged into my Mac. It did need a driver, and I used <a href=\"https:\/\/github.com\/adrianmihalko\/ch340g-ch34g-ch34x-mac-os-x-driver\">ch340g-ch34g-ch34x-mac-os-x-driver<\/a>.<\/p>\n<p>Data is sent at 9600 baud, with 8 data bits, no parity bit, and 1 stop bit. 10 bytes are sent at a time.<\/p>\n<table>\n<thead>\n<tr>\n<th>Byte<\/th>\n<th>Name<\/th>\n<th>Content<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>0<\/td>\n<td>Message Header<\/td>\n<td>AA<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>Commander No<\/td>\n<td>C0<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>DATA 1<\/td>\n<td>PM2.5 Low byte<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>DATA 2<\/td>\n<td>PM2.5 High byte<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>DATA 3<\/td>\n<td>PM10 Low byte<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>DATA 4<\/td>\n<td>PM10 High byte<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>DATA 5<\/td>\n<td>ID byte 1<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>DATA 6<\/td>\n<td>ID byte 2<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td>Check-sum<\/td>\n<td>DATA 1+DATA 2+..+DATA 6<\/td>\n<\/tr>\n<tr>\n<td>9<\/td>\n<td>Message tail<\/td>\n<td>AB<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>PM2.5 (\u03bcg \/m3) = ((PM2.5 High byte *256) + PM2.5 low byte)\/10<br \/>\nPM10 (\u03bcg \/m3) = ((PM10 high byte*256) + PM10 low byte)\/10<\/p>\n<p>We can read this using PHP. The following PHP script can be run on the command line and outputs the PM2.5 and PM10 levels every 2 seconds to a terminal window.<\/p>\n<pre>\r\n&lt;?php\r\nexec('stty -f \/dev\/cu.wchusbserial1420 9600 raw');\r\nwhile (true) {\r\n    $handle = @fopen( '\/dev\/cu.wchusbserial1420', 'r' ); # Open device for Read access\r\n    if ($handle) {\r\n        $binarydata = fread( $handle, 10 ); # Read data from device\r\n        $data = unpack('H2header\/H2commander\/vpm25\/vpm10\/Sid\/H2checksum\/H2tail', $binarydata);\r\n        echo sprintf(\"PM2.5: %d\u00b5g\/m\u00b3\\nPM10:  %d\u00b5g\/m\u00b3\\n\", $data['pm25']\/10, $data['pm10']\/10);\r\n        fclose ($handle); # Close device file\r\n    } else {\r\n        echo \"Unable to connect to pollution sensor\\n\";\r\n    }\r\n    sleep(2);\r\n}\r\n<\/pre>\n<p>The device is used by reading the 10 bytes it returns each message from the USB device. We can use PHP&#8217;s <a href=\"http:\/\/php.net\/manual\/en\/function.unpack.php\">unpack<\/a> function to break the binary data returned into an associative array for ease of use. We do have to do divide the PM2.5 and PM10 results by 10 to get their real values though. This can then be echo&#8217;d to the terminal.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Novafit SDS011 particulate sensor is an a very affordable sensor for detecting particulate pollution. It is capable of detecting both PM2.5 and PM10 with a relative error margin of +\/- 10\u00b5g\/m3. It can output data via it&#8217;s serial port. The one I bought came with a serial to USB adaptor, allowing it to be &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/running-sds011-particulate-sensor-mac-using-php\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Running a SDS011 particulate sensor on a Mac using PHP&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":2577,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2,84],"tags":[50,95,88],"class_list":["post-2575","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dev","category-hardware","tag-php","tag-sds011","tag-sensors"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Running a SDS011 particulate sensor on a Mac using PHP - Robert Price<\/title>\n<meta name=\"description\" content=\"How to run an SDS011 particulate pollution sensor on a Mac. Includes working examples of processing the returned data using PHP.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.robertprice.co.uk\/robblog\/running-sds011-particulate-sensor-mac-using-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Running a SDS011 particulate sensor on a Mac using PHP - Robert Price\" \/>\n<meta property=\"og:description\" content=\"How to run an SDS011 particulate pollution sensor on a Mac. Includes working examples of processing the returned data using PHP.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/running-sds011-particulate-sensor-mac-using-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2017-07-25T20:55:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-07-25T22:46:28+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/assets\/sds011.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"600\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/running-sds011-particulate-sensor-mac-using-php\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/running-sds011-particulate-sensor-mac-using-php\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Running a SDS011 particulate sensor on a Mac using PHP\",\"datePublished\":\"2017-07-25T20:55:31+00:00\",\"dateModified\":\"2017-07-25T22:46:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/running-sds011-particulate-sensor-mac-using-php\\\/\"},\"wordCount\":253,\"image\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/running-sds011-particulate-sensor-mac-using-php\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/assets\\\/sds011.jpg\",\"keywords\":[\"PHP\",\"SDS011\",\"sensors\"],\"articleSection\":[\"Dev\",\"Hardware\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/running-sds011-particulate-sensor-mac-using-php\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/running-sds011-particulate-sensor-mac-using-php\\\/\",\"name\":\"Running a SDS011 particulate sensor on a Mac using PHP - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/running-sds011-particulate-sensor-mac-using-php\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/running-sds011-particulate-sensor-mac-using-php\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/assets\\\/sds011.jpg\",\"datePublished\":\"2017-07-25T20:55:31+00:00\",\"dateModified\":\"2017-07-25T22:46:28+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"description\":\"How to run an SDS011 particulate pollution sensor on a Mac. Includes working examples of processing the returned data using PHP.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/running-sds011-particulate-sensor-mac-using-php\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/running-sds011-particulate-sensor-mac-using-php\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/running-sds011-particulate-sensor-mac-using-php\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/assets\\\/sds011.jpg\",\"contentUrl\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/assets\\\/sds011.jpg\",\"width\":1200,\"height\":600,\"caption\":\"SDS011 particulate sensor\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/running-sds011-particulate-sensor-mac-using-php\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Running a SDS011 particulate sensor on a Mac using PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Running a SDS011 particulate sensor on a Mac using PHP - Robert Price","description":"How to run an SDS011 particulate pollution sensor on a Mac. Includes working examples of processing the returned data using PHP.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.robertprice.co.uk\/robblog\/running-sds011-particulate-sensor-mac-using-php\/","og_locale":"en_GB","og_type":"article","og_title":"Running a SDS011 particulate sensor on a Mac using PHP - Robert Price","og_description":"How to run an SDS011 particulate pollution sensor on a Mac. Includes working examples of processing the returned data using PHP.","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/running-sds011-particulate-sensor-mac-using-php\/","og_site_name":"Robert Price","article_published_time":"2017-07-25T20:55:31+00:00","article_modified_time":"2017-07-25T22:46:28+00:00","og_image":[{"width":1200,"height":600,"url":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/sds011.jpg","type":"image\/jpeg"}],"author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/running-sds011-particulate-sensor-mac-using-php\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/running-sds011-particulate-sensor-mac-using-php\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Running a SDS011 particulate sensor on a Mac using PHP","datePublished":"2017-07-25T20:55:31+00:00","dateModified":"2017-07-25T22:46:28+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/running-sds011-particulate-sensor-mac-using-php\/"},"wordCount":253,"image":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/running-sds011-particulate-sensor-mac-using-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/sds011.jpg","keywords":["PHP","SDS011","sensors"],"articleSection":["Dev","Hardware"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/running-sds011-particulate-sensor-mac-using-php\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/running-sds011-particulate-sensor-mac-using-php\/","name":"Running a SDS011 particulate sensor on a Mac using PHP - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/running-sds011-particulate-sensor-mac-using-php\/#primaryimage"},"image":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/running-sds011-particulate-sensor-mac-using-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/sds011.jpg","datePublished":"2017-07-25T20:55:31+00:00","dateModified":"2017-07-25T22:46:28+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"description":"How to run an SDS011 particulate pollution sensor on a Mac. Includes working examples of processing the returned data using PHP.","breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/running-sds011-particulate-sensor-mac-using-php\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/running-sds011-particulate-sensor-mac-using-php\/"]}]},{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/www.robertprice.co.uk\/robblog\/running-sds011-particulate-sensor-mac-using-php\/#primaryimage","url":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/sds011.jpg","contentUrl":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/sds011.jpg","width":1200,"height":600,"caption":"SDS011 particulate sensor"},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/running-sds011-particulate-sensor-mac-using-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Running a SDS011 particulate sensor on a Mac using PHP"}]},{"@type":"WebSite","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website","url":"https:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/2575","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=2575"}],"version-history":[{"count":2,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/2575\/revisions"}],"predecessor-version":[{"id":2578,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/2575\/revisions\/2578"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media\/2577"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=2575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=2575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=2575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}