Air quality monitoring with Slack

November/16/18

Hugo Fiennes |
CEO and co-founder@Electric Imp

As many Californians are now painfully aware, the smoke from the multitude of fires currently raging in the state is making life unpleasant; not as unpleasant as it has been for those who have lost loved ones or homes, but the impact on air quality from the smoke has been particularly hard for  those with respiratory issues.

(The kit checking out how bad the air is on our balcony in Los Altos)

Outside our office, the air quality is very poor. The air seems somewhat better inside, but we wanted to quantify this in order to let people decide whether they'd prefer to work from home rather than making the trip to the office. 

Sounds like a job for IoT!

Here's a very quick project to build an air quality sensor that's connected to Slack, so any user can request the current air quality readings and get an instant reply. There's no soldering involved, and this can be built from parts you can easily get hold of in a day or two.

In slack, you just type "air" in the appropriate channel and the device will reply with the latest reading:

What you'll need

Air Quality Sensor DFrobot PM2.5 laser air quality sensor https://www.amazon.com/DFROBOT-Gravity-Quality-Sensor-Arduino/dp/B073ZDS541  $49.90
Imp air quality kit  Electric Imp April breakout board, imp001 card, USB power cable and jumper wires https://store.electricimp.com/products/april-imp001-bundle?variant=12931066003479 $21.00
USB power supply   I'm sure you have one of these around!  
 Slack admin access   ...if you want to integrate this with Slack  

 

Connection instructions

There are four connections that need to be made between the breakout board and the air quality sensor; two are for power, and two are for serial communication. The wire colors are not important, just that the connections are made between the right pins on each device.

Double-check the connections before plugging power in!

April board DFrobot board
GND GND
VIN VCC
pin1 RX
pin2

TX

 

 

 

 

Loading the code

As the Electric Imp platform takes care of providing both a secure environment to run the device code and a secure endpoint for Slack to access, there's really very little code required to make this work - the code can be found here: https://gist.github.com/hfiennes/8a812b9623ae5d2f32a6fbb7f1eb5153

If you're new to the Electric Imp platform, the first thing to do is to create a developer account, then get your device connected to WiFi. Instructions can be found here https://developer.electricimp.com/gettingstarted/developer/account - once you get to step 4, where you have sent code to the device, you can cut and paste the code from the link above into impCentral and once you press "Build & Run" you should see air quality readings in the logging window once per second.

If you have any issues, post a question on our forums.

Checking out the web interface

Besides interfacing with Slack, the code above serves a simple webpage which will show the current readings; this URL can be accessed from anywhere in the world. Here's our one (you can visit it yourself here if you want).

Your device will have its own unique agent URL, which can be found at the top of the logging window in impCentral. You'll need to copy and paste that for the steps that follow.

Connecting it to Slack

To make readings accessible to Slack users, you need to be a slack administrator. First, pick "administration" from your workspace menu, then "Manage apps"

 

Then, from the window that opens, pick "Custom integrations" and "Outgoing webhooks".

 

Then, click the green "Add configuration" button on the left:

Scroll down to the "Integration settings" section, and pick a channel for the bot to listen on, the trigger words you'd like it to respond to, and then paste the agent URL from above into the URL box, adding "/slack" at the end which tells the agent to format the reply in Slack's preferred JSON format.

Finally, pick a name for your integration and an icon for it to post as (mine's the mask emoji):

Finally, click save.

You're done!

Go to the channel you picked and type your trigger word; you should see the latest air quality reading.

How it works

The air quality sensor pulls air through a cavity with a small fan, and uses a laser to illuminate particles passing through, with the light reflected off them being detected by an optical sensor. A small processor embedded within the module then processes this signal and translates this into a count of different particle sizes, and scales this to a density in micrograms per cubic meter - the generally accepted way to present these readings. The readings are sent over a serial connection to the imp once per second.

The imp code parses these packets, checks that they have not been corrupted, and extracts the readings so that they can be sent to the agent in the cloud. Actually sending the readings to the cloud is just a single line of code, with the Electric Imp system dealing with passing them over the secure & trusted channel to the cloud that is established when the device connects.

The agent code does a few things: formatting the readings into a string, interpreting the PM2.5 levels using the standard "levels of health concern" scale, and providing a tiny webserver which will present these values in two formats.

The first format is just plain text, which is what you get if you visit the agent URL with a browser. The second format, triggered by the URL that the Slack integration uses ("/slack"), is a JSON encoded table that embeds the plain text.

Conclusion

Though this is hopefully just a short-term tool, the reality is that climate change is making these wildfires more likely to occur and chances are there will be fires and smoke next year, so we'll be keeping our sensor online for the foreseeable future.

The really great thing about building on the Electric Imp platform is that once built & working, IoT devices are 100% maintenance free - they will automatically upgrade to the latest and greatest impOS releases when they come out (you literally don't have to lift a finger to click a single button), and will stay online as long as it has a network connection.

Stay safe and healthy everyone!