Thursday, February 18, 2016

Making a Network Connected Raspberry Pi Display Your Presence State on LEDs

Overview

I work from home full time and at Modality, we use Skype for Business for all of our conference calls and other communications needs.

Being a work from home employee with children, particularly one who doesn’t like to lock himself in his office, I wanted a way to let my family know when I cannot be interrupted.  And being a geek with geek kids, what better way to solve the problem than with legos and a Raspberry Pi?

Items Required

A Raspberry Pi – I used a Model B (non-Plus).  Any Raspberry Pi model should work provided you can get it on the network somehow.  These cost about USD $35.00.  This would likely work with a Banana Pro, as well (about $45.00) and using that would give you wireless out-of-the-box.

A Network Connection – I purchased a RealTek WiFi+Bluetooth Adapter.  Personally, I wouldn’t recommend this product since it required compiling a driver since the kernel didn’t have native support for it.  Refer to the compatibility list for the least effort in getting WiFi working.  If you’re using the Banana Pro, you won’t need to worry about this.

3 LEDs – Green, Yellow, Red if you want to keep with the colors that Skype for Business uses.

3 270-330 ohm resistors – one for each LED.

Wires and a Breadboard – or some other way to connect them to the GPIO pins.

Legos to build an ugly case (optional).

An SD card that works with the platform you’re using (I recommend sticking with 16GB to give you space to play around).

Setting up the Software

I won’t go into the full setup instructions for a Raspberry Pi, but you’ll want to get it up and running.  I used the DietPi operating system, but this will work with Raspbian and I’d recommend using the NOOBS tool to install if you’ve not done an RPi install before.

You’ll need WebIOPi in order to interact with the GPIO via the web.  This came preinstalled with DietPi.

Update the Raspberry Pi OS

I did the installation with the latest bits as of the writing of this post and it’s always a good idea to be up-to-date.  You’ll want to either connect via Secure Shell (PuTTY works well for this on Windows) or plug your RPi into a display and keyboard to get a shell.  Once you’ve gotten a shell up, run these commands (and grab yourself a cup of coffee).

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo rpi-update
Configure WebIOPi

The GPIO pins aren’t configured the way we need them to be, yet, so we’ll edit the WebIOPi configuration to get that going.

$ sudo nano /etc/webiopi/config

Nano is a simple console text editor.  Locate the heading labeled [GPIO] (hint: CTRL+W can be used to find text in Nano, however, GPIO should be at the top).  Anything with a “#” in front of it is ignored (a comment). The configuration under GPIO is “PIN = DIRECTION STATE”.  If you have 17, 18 and 27 already setup in some way, you’ll want to modify those lines.  If they are not configured at all, you can simply add the following lines.

17 = OUT 0
18 = OUT 0
27 = OUT 0

Locate the [HTTP] heading and make sure it is setup as follows

[HTTP]
# HTTP Server configuration
enabled = true
port = 8000

# File containing sha256(base64("user:password"))
# Use webiopi-passwd command to generate it
passwd-file = /etc/webiopi/passwd

# Change login prompt message
prompt = "WebIOPi"

Locate the [REST] heading and make sure it is setup as follows

[REST]
# By default, REST API allows to GET/POST on all GPIOs
# Use gpio-export to limit GPIO available through REST API
gpio-export = 17, 18, 27

# Uncomment to forbid changing GPIO values
#gpio-post-value = false

# Uncomment to forbid changing GPIO functions
#gpio-post-function = false

# Uncomment to disable automatic device mapping
#device-mapping = false

The rest should be fine as is.  Hit CTRL+X and choose “Yes” to save the file.  Finally, we’ll setup a user/password that will let us authenticate.

sudo webiopi-passwd
Set up an ID and password of your choosing.
Wiring it up

The wiring for the project is pretty simple.  Here’s a diagram that explains how to set everything up.  Make sure your resistors are wired correctly! I’m not an EE guy, but in my reading the warnings indicated that failing to install resistors or using the wrong resistors will result in damaging the Raspberry Pi.

It’s also important to note that LEDs are one-way devices.  If you install them backward you won’t break anything, but they won’t light up (which is the first thing to check if your tests don’t work).

Diagram-Cropped

That’s the GPIO arrangement of a B (non plus version).

That’s it, you’re ready for the code.

On Your Windows Box

The code is client-side, so it won’t work if your PC is turned off or asleep, but it’s the easiest way to do it and doesn’t require administrator privileges on the Skype for Business server as would be if we’d done this with the Unified Communications Managed API (though it can be done with a bit more work that way).

You can get the source code from the GitHub Repo.  I’ll provide a compiled version, but because the software is very hastily designed, you’ll need to build it yourself.  You can download Visual Studio 2015 Community Edition to compile the project.

Running the Application

Hit Win+R and type CMD.  Go to the folder that the application compiled to (usually ProjectName\bin\Debug) and run:

Cs-WebIoPi <IPADDRESS> <PORT> <USERID> <PASSWORD> --test

If you’ve configured it following this gude, you’ll put “8000” for the port.  When started in “Test” mode, it’ll blink all of the LEDs and cycle through each one so that you can verify your wiring.  Fix any wiring issues and when you’re all set, run it with the same command above but without the “test”.

No comments: