Internet of Things - make Raspberry PI interact with Slack

About IoT

One of the most exciting applications of technology is the possibility of connecting a vast array of devices, assign them unique alias ( like us humans having names ) and allow them to be individuals in a “Machinapolis”, a society of devices. They can talk to each other, ask for things from each other and report stuff back to us human overlords. Thanks to the Internet, communication between each one of these device is made easy.

Imagine that you have a fridge capable of scanning its content, learn exactly what products you like to have and in what quantity, and do something about it. If you are running out of milk, the fridge then can send you an SMS telling you that you need to buy milk, otherwise you’ll have to eat your cereals with water next morning. Even better, the fridge could connect to an online grocery store and order copious amounts of milk for you. The only work you’d have to do is unpack your grocery. Eventually little robot slaves could do that for you too!

The best part is that it is not sci-fi, it is real life - and it is one of those things where the only limit is imagination ( and technical prowess, to be honest ). As we keep moving into the future, it is expected that almost anything will be connected to the Internet of Things. Experts put a number at 50 billion objects connected by 2020!

This is what the Internet if Things ( IoT ) is all about:

The Internet of Things (IoT) is a system of interrelated computing devices, mechanical and digital machines, objects, animals or people that are provided with unique identifiers and the ability to transfer data over a network without requiring human-to-human or human-to-computer interaction.

One key concept of IoT is to make these devices as independent of human supervision as possible. Kevin Ashton, cofounder and executive director of the Auto-ID Center at MIT, said it best:

Today computers -- and, therefore, the Internet -- are almost wholly dependent on human beings for information. Nearly all of the roughly 50 petabytes of data available on the Internet were first captured and created by human beings by typing, pressing a record button, taking a digital picture or scanning a bar code.

People have limited time, attention and accuracy -- all of which means they are not very good at capturing data about things in the real world. If we had computers that knew everything there was to know about things -- using data they gathered without any help from us -- we would be able to track and count everything and greatly reduce waste, loss and cost. We would know when things needed replacing, repairing or recalling and whether they were fresh or past their best.

Here at Cloudoki

As was explained in this post, our Hackfridays are pretty much holy to us here at Cloudoki. It is the best chance we have to play with the latest technologies and toys on the market. And obviously to try things that are outside our comfort zone, not only in terms of of business but also when it comes to technology.

So we took the chance to try something new. “He have some Arduinos and Raspberry PIs laying around… what can we do with those?”. After some brainstorming we came up with the idea of creating some kind of poker. Why? Because sometimes we get so immersed in our work that we forget there are people around us, in need of us … and if we had some kind of device whose only purpose is to get their attention when we need them… and boom! A hackfriday project was born.

The plan was to use the Slack API to listen to whatever is said on a particular channel and trigger a response in our IoT device when a specific word is typed. Therefore we decided to use Raspberry PI over Arduino due to its independent network connectivity. Raspberry PI is one interesting little fella: a fully functional computer with a dedicated processor, memory, and a graphics driver for output through HDMI. It even runs a specially designed version of the Linux operating system, the Raspbian. The triggered event can be anything from playing a song, send an insult, or use an attached grenade launcher and point it in the direction of the person being poked, all with the sole purpose of getting said person’s attention.

Since we don’t have any grenade launcher (for now), we decided in favor of something simpler - playing an annoying song whenever some person was mentioned in a specific slack channel, with the keyword “poke” before that person’s name. Also, a small LED light will flash for a few seconds, for effect.

For instructions on how to setup your Raspberry, please refer to the official documentation:

How we did it

Using Slack’s API, we registered a bot, and from our Raspberry the bot checks each second for the last message that was sent. If it contains the words “poke ”, the bot will poke that person.

The Slack API is long, but well documented. Read it here (link)

We used Node.js to write a simple app to connect to our slack, but first we need to register our bot.

Once you’ve registered your Slack Bot, a token is provided. This token is what is used to allow connection with the slack team chat. After getting our Raspberry up and running, the first thing was to install a decent text editor. I opted for Geany (geany link)

To install node in your Raspberry:
sudo apt-get install nodejs”

There are several Node helper libraries available to make a developer’s life easier. For this project I needed Node libraries for the Slack API, GPIO,lame and speakers ( these last two for playing sounds ).

slack-node was used to handle the Slack API:

We need a GPIO (general purpose input/output) library to help access to our Raspberry functionalities. I used pi-gpio, a very simple node library for GPIO. Full instructions here:



We also need d Node FS (File System) extension to access system files:

The Code

In our main file, we need to import our Node libraries:

var slack = require('slack-node'); var gpio = require("pi-gpio");
var libs = require("./libs/libs");
var fs = require('fs');
var lame = require('lame');
var speaker = require(‘speaker’);

To connect to our Slack chat:

var token = "<your slack token>";

var slack = new Slack(token);

I needed a list of all available slack users, so that the bot knows if the person I want to poke actually exists:

var users = [];

slack.api("users.list", function(err, response){...

if ( response.ok == true ){

    var members = response.members;
    for ( x in members ){

        if ( members[x].is_bot === false ){


All users will be pushed to the “members” array.

Them, using a time interval of one second, I sniff a particular channel’s history for the latest messages. In this case the very last one:

slack.api('channels.history', { channel: ‘<channel ID>', count: 1 } ,function(err, response){ …

Slack will return a response object containing the last message. Them you can use regular expressions to look for a key word.

If that keyword is found, we play a song and flash a LED light

fs.createReadStream(“<path/to/soundfile>”) .pipe(new lame.Decoder()) .on('format', function (format) { this.pipe(new Speaker(format)); });

for the LED, it is imperative that you understand how the Raspberry PI pin ports work. Take a look here:

first we need to indicate which pin we will use. In our case its the pin 13 (GPIO port 17)
var gpioPin = 13;

Then we flash the LED each 200 milliseconds:, "output", function(){ var on = 1; console.log("GPIO pin " +gpioPin+"is open: toggling LED"); intervalId = setInterval(function(){ gpio.write(gpioPin, on, function(){ //toggle pin between high (1) and low (0) on = (on+1)%2; }); },200);

Then after 3 seconds we clear the interval and close the pin, otherwise the LED will remain turned on.

setTimeout(function(){ clearInterval(intervalId); gpio.close(gpioPin); }, 3000);

Thats basically it. There isn't that much going on here, but if you take you time to read through the Slack API and the node library used here, there are many other things you can do.

This was our first step into the world of IoT, and hopefully more projects and ideas will come along for us the explore its possibilities.

Here is a picture of our setup:

  • Audio speaker to deliver the noise
  • Rasperry pi to listening to slack
  • Breadboard to deal with the LED
  • LED to (humbly) enlighten our office
  • Connecting MF cables to connect
  • Motivational lighter to motivate
  • PEZ dispenser to dispense

See you soon!

comments powered by Disqus