No matter where your niche is within the startup world, I guarantee you have a use for the Raspberry Pi—that fancy little $35 computer board everybody and their robot dog is talking about these days. Your application may be as simple as turning an old TV into an arcade machine for the break room or as complicated as tracking the energy use of every electronic device at your office. Maybe that application is a marketable product you want to develop. Maybe it’s just automating some time-consuming task that otherwise zaps your productivity. Whatever the case, I’m determined to show you that you need the Raspberry Pi in your life.
If you ever feel that fear or anxiety begin to rise, never forget there is an army of nerds like myself who want you to succeed and provide the tools and assistance in an open manner. Grab a Raspberry Pi kit and get started.
Among all the cool stuff we could do with the Raspberry Pi, I’ve decided to start with a project that is centered on you. Technically, it’s centered on me, but plenty of this should translate to your own work life. Your work environment most certainly affects your ability to operate, so let’s create a “Connected Office” to give us some critical information about our work environment and add some “smart” functionality to make our lives easier. That is to say, let’s make a Raspberry Pi-based system that measures some key environmental properties in our office, displays those values in a platform-independent manner, and reacts to our needs without a whole lot of effort. That’s a very highfalutin’ way of saying we want occupancy and environmental sensors but we don’t want to pay a lot of money for it.
The Connected Office
Below is a diagram of my objective for this connected office build that has a Raspberry Pi Zero (any Raspberry Pi will work, but this one is adorable), a door sensor, environmental sensors, Bluetooth and plenty of room to grow. Early on, this type of diagram likely has little meaning to you (aside from some inevitable anxiety) but I’d like to show how we can go from a basic problem statement to this complex electronic device. Hold on tight, y’all.
That’s quite a lot to throw at you, I know, but I want to start with the end-goal in mind and show you all how we can dissect this into manageable chunks. We begin with the simplest portion of the connected office system—the door sensor. This is a simple switch of sorts mounted to a door frame that controls the flow of electricity through the circuit based on the presence of a magnet that’s mounted to the door itself. In short, I want a little bit of current flowing through the switch whenever the door is closed and none when the door is open.
This device that’s either OFF or ON (i.e., digital) is the simplest of Inputs/Outputs we can deal with for the Raspberry Pi. We simply need to connect one end of the switch to one of our General Purpose Input/Output (GPIO) pins on the Pi. I’ve chosen GPIO #17, but you can choose any of the available digital GPIO pins.
To help sort through this rat’s nest of wires in the connected office prototype, let’s look at the door sensor in isolation. Here’s a schematic that shows the available electrical connections on the Raspberry Pi and how the door switch is connected. I want the Raspberry Pi’s 3.3 V to flow to GPIO #17 when the switch is closed and I want GPIO #17 to be grounded when the switch is open. I’ve added a resistor between GPIO #17 and ground for that purpose. (Note the resistor needs to be more resistive than the switch or else it’s the path of least resistance.) Without grounding that pin, that is said to be “floating” and can return nonsensical data as it’ll flip between 1s and 0s.
Now that I’ve wired just this one part of the connected office system together, we’re ready to interface with the device. I’ll leave the task of setting up the Raspian operating system and prerequisite packages on the Raspberry Pi for you; there are numerous tutorials available through the Raspberry Pi Foundation and Adafruit that can guide you through the setup. I’ll be skipping a lot of the basic software installation portions here and focus on the end results.
I want to show you a few different ways of connecting this door sensor to the Internet of Things, so let’s walk through it the hard way and some much easier ways.
The Hard Way: Linux Command Line Interface
Like it or not, you’ll need to get comfortable with the command line interface (CLI) in Linux if you’re interested in the Raspberry Pi. With the coming era of Windows on the Raspberry Pi, I see a general shift towards graphical interface, but never forget the practicality of the command line. Nowadays, we need this simple user interface to at least install the fancy applications and functionality we desire from the RasPi. Don’t think of it like going back to 1990s-era DOS; think of it as an exceptionally practical means of computer interfacing that just happens to look like 1990s-era DOS…
Presuming you have the Pi booted up and this circuit constructed, let’s read the state of that sensor via the command line interface. You’ll likely not use this much (if ever) but it’s useful to see it at least once; it helps put things into perspective at least.
Navigate to /sys/class/gpio
List the folder contents with:
You may or may not see entries of the form gpio##. You will see them if they are active in this device tree. To do so, try:
echo 17 > export
Another ls and you should now see gpio17. Change the directory to gpio17 with:
One more ls and we now see the properties of gpio17. We now want to issue the following command:
echo in > direction
This sets the direction to input. We can read the state of gpio17 by simply reading value with the cat command:
Try opening the door switch and re-run cat value. (Note it helps to simply press the up key to load the prior command. Another way is to issue the “bang bang” command: !! ) Your screen should look something like the following but will most likely be pi@raspberrypi as I named my Pi Zero nihil:
There you have it. A door sensor reader that’s not terribly useful at all unless you’re stuck in 1995. We could create a simple script that repeats the process of reading gpio17 and logging that value to the console at a set interval. Better than the manual mode, but I want to be able to see a plot of this over time on any device I choose.
I think we can all agree that it’s time to look at some easier ways of interfacing with this door sensor.
The Easy Way: Adafruit.IO
Here’s an example of an Adafruit.io Dashboard that contains both gauges and timeplots of sensor data. I show a day in the life of my office told from the temperature & humidity sensor we will be exploring in a later part of the connected office project. Without a whole lot of effort, I walked through the basic adafruit.io tutorials and had myself this fancy dashboard ready within an afternoon.
This is all great for data logging, but the Internet of Things is ideally more than just data logging; there should be an element of reactivity or context-dependent action. Practically, I don’t care what the specific temperature is down to the hundredth decimal place. I do however care if that temperature goes outside of a certain range of temperatures. With this adafruit.io tool plus If This Then That, we’ll get just what we need to suit this IOT application to our everyday lives.
If you’re working along with this tutorial, now’s the time to try out the adafruit.io tutorials for Raspberry Pi: https://learn.adafruit.com/adafruit-io-basics-digital-input/overview. For part 1 of this Connected Office build, I’ll simply adapt the canonical digital input application for a magnetic door sensor but I’d like some reactivity; if the door is opened or closed, I want a push notification on my phone. We already have our switch circuit wired and by now should note that’s essentially the same as the button circuit in the adafruit.io tutorial. The application we’ll be using on our Pi is as follows.
If you’ve worked though the digital_in.js example from adafruit, then you’ll see my code here is barely different. I don’t really need new functionality at the moment, so I’ve changed the feed name to ‘door’ and added a snarky message to the console log. The original program connected a toggle button to a power strip, but the same underlying functionality is identical. We can add more sensors simply by adding more GpioStream.readable() commands and piping that data to additional adafruit.io feeds.
We begin by using the gpio-stream package that does all the heavy lifting for reading GPIO #17 and the adafruit-io package that takes care of communicating with adafruit.io. (Make sure you put your own Adafruit IO key and username in the program.) The core functionality we want is the state of the door sensor to be written to the ‘door’ feed on adafruit.io every time the state of GPIO #17 changes. All of this is handled by the door.pipe(aio.feeds(‘door’)) function. This starts the “pipe” that looks for changes on door (note we set that to GPIO #17 in the first command); if a change occurs, this will immediately update the ‘door’ feed with that new value.
Give this a test while the program is running and you’ll see that value updated on your adafruit.io feed every time the door is open. If you set up a dashboard with a gauge and time-plot of the door sensor feed, you should see something like the following:
Adafruit.io has a few basic type of triggers: reactive and scheduled. With reactive triggers, you can send an email, issue a webhook, or modify the value of another adafruit.io feed. Not a whole lot going on in the responsiveness realm however they are integrated with ifttt.com—perhaps one of the coolest IOT apps I’ve encountered so far. If This Then Thatconnects a vast array of devices and webapps to each other. Want to get a text message every time a human goes into space? ifttt.com can do it. Want to set your thermostat to 68 degrees every time someone mentions you on Twitter? ifttt.com can do it.
Here I’ll use ifttt.com to connect this door sensor value on adafruit.io to the IF Notification channel so I can receive notifications on my cellphone. Although we can set this up to send notifications by SMS, I would rather avoid a heap of text messages so I’ll set up a push notification from the IF app for when the ‘door’ feed changes.
The ifttt.com process is an absolute breeze. Following the seven step process:
Create a new ifttt recipe and choose what “this” is.
We have an incredible number of channels we can connect to with ifttt.com, but here we’ll stick to the Adafruit channel.
Now we can see the types of interactions allowed by Adafruit. We want a trigger every time new data is posted to a feed, so choose “Any new data”.
Select the appropriate feed from the drop-down menu.
That completes the “this” part, so now we choose our “that” (the reaction to the trigger.)
Again, we find an incredible number of possibilities. Let’s pick “IF Notification”
Not a whole lot of available actions with this channel, but all we need is a push notification.
Now we get to set the details of our notification. Sticking with the default will give us a notification that looks like “door is currently 1.” Or “door is currently 0.” That doesn’t exactly tell us opened or closed, but we’ll leave that complication for later.
There you have it. If you’ve followed along, you’ve gotten a nice exposure to the wide range of capabilities the Raspberry Pi possesses and see how we can leverage additional resources to make a “useful” Internet of Things application.
This method, however, is lacking a critical element of convenience. I’ll get a notification every time my office door is opened or closed. I only really care about logging the data in the background and getting a notice if the door is left open while I’m not around. For that, we’ll need to start down a new path. Next time, I’ll introduce you to a neat IOT platform that makes use of a convenient graphical programming language and is suited for a wide variety of applications.
This has covered quite a lot in a short span so I’ll leave this part of the connected office build there. Hopefully I didn’t scare you away from the Raspberry Pi; it’s OK, we’ll get through this together. In Part 2, we’ll add some extra intelligence to this office IOT project with Structure and use the knowledge gained in this exercise to connect more sophisticated sensors such as temperature/humidity modules and light meters. Maybe a door sensor is something you want, but I’m sure you can find some use for even this simplest of Internet of Things applications in your professional and personal life.
Tweet your project pictures, questions and comments to @RossMakes. If you’re an experienced Raspberry Pi user, how do you use this in your professional life?