Nothing annoys me more at work than finding all the conference rooms booked in the online scheduler but then walking around the building and finding a large percentage are actually unoccupied. If I were President, I would institute a policy where by people would lose vacation days if they didn't cancel their conference room reservation when they canceled their meeting or ended up doing a conference call from their office. Me becoming President of the company - very unlikely; so on to plan B - creating a conference room monitor so that I know when a room is unoccupied regardless of what the online scheduler shows. (This project isn't limited to the office; I initially set it up at home as my daughter isn't allowed in my home office when I am not there.)
The projects uses a simple PIR motion sensor connected to a Raspberry Pi running Windows 10 IoT Core. The Universal Windows App (UWA) on the Pi reports the status of the conference room to an Azure IoT Hub - "occupied" or "not occupied". IoT Hub allows me to easily collect data from several devices; once the data is in Azure, the possibilities are endless - including just simply showing whether a room is empty.
This is a fairly easy project to get started with IoT. The hardware and the app are both quite simple. Creating an IoT Hub in Azure was even easier (this is the first time I have used Azure). I am using a Free Trial subscription so nothing to worry about there. Additionally, I have created a series of step-by-step build videos for the project.
If you haven't already upgraded your development machine to Windows 10 and installed Visual Studio 2015, do that first (here are some instructions); this could take up to an hour - most of which is unattended while the bits download and get installed - so plan accordingly. Then, put the Windows 10 IoT Core image on your SD card using the Windows 10 IoT Core Dashboard. Insert the SD card into the Pi and power it.
The easiest way to verify that Windows 10 IoT Core loads properly is to plug in an HDMI capable monitor as the Pi's status will be displayed throughout the boot up process. Alternatively, use the Windows 10 IoT core Dashboard to verify that the Pi is ready to go; the Dashboard will discover the Pi once it has booted and connected to the network (you can plug a network cable between the Pi and your network's router or directly into your development machine's Ethernet port if you turn on Internet connection sharing).
You'll also need an Azure account. I only have a Free Trial subscription and was still able to use all the services described in this project. If you don't already have an Azure account, sign up for the free trial.
- Raspberry Pi 2 and standard accessories: 5v 2A power supply, 8GB class 10 micro SD card, case, and network cable
- PIR motion sensor
- LED and 200 ohm resistor
- Male/female jumper wires
- Mini breadboard
Time: 15 mins
Parts: PIR motion sensor, mini breadboard, LED, resistor, jumper wires
The hardware component of this project is fairly straightforward. You'll use the Raspberry Pi's GPIO pins to read the input from the PIR motion sensor and to turn on the LED when motion is detected. (You may do without the LED when you deploy the final solution but I find it helpful to have a visual indicator during development and testing.) The application running on the Raspberry Pi will also send conference room status - occupied (motion detected) or unoccupied - data to the Azure IoT Hub.
First, connect the PIR motion sensor's pins to the Raspberry Pi using jumper wires - 5V on the sensor to 5V PWR on the Pi (I used pin 2), GND on the sensor to GND on the Pi (I used pin 6), and signal on the sensor to a GPIO pin on the Pi (I used pin 31 / GPIO 6). Double check the data sheet for your sensor to make sure you correctly identify the sensor's 5V, GND, and signal pin as their order varies by manufacturer. (I bent the pins on a female header so that I could mount the sensor upright on the mini breadboard.)
My PIR sensor operates at 5 volts which is why it's power pin needs to be connected to a 5V power pin on the Pi. But, it's signal pin operates at only 3.3V which is why it can be connected directly to a GPIO pin. Again, double check the data sheet for your sensor to ensure that the signal's voltage is 3.3V. If it's 5V, you'll have to divide the voltage before connecting it to the Raspberry Pi's GPIO pin.
Next, connect the Pi's 3.3V power (I used pin 1) to a 200 - 220 ohm resistor and the resistor to the longer leg on LED. Connect the LED's shorter leg to a GPIO pin (I used pin 29 / GPIO 5).
There is jumper on the back of the PIR sensor that lets you select "Single Trigger" or "Repeat Trigger". In these pictures, the jumper is set on Single Trigger. But, after deployment, I switched it to Repeat Trigger. On Single Trigger, the sensor would detect me when I entered the room and then trigger the application to indicate the motion was detected as expected but, after that, another trigger wasn't being sent to the app even though I was still in the room and moving. Only after I left and returned to the conference room, would another trigger be sent to the app to indicate that motion was detected. The Single Trigger setting basically remembers that I am in the room and therefore doesn't count my continuous motion as a new detection. The Repeat Trigger on the other hand sends a trigger to the app whenever it detects motion regardless of whether or not it detected my motion the last time it took a reading. Since I want to know when people are in the room - not when the come and go - I have sent the jumper to Repeat Trigger.
Time: 15 minutes
Tools: Device Explorer app
Go to the Preview Portal and sign in to your Azure account. I followed the first section of the Get started with Azure tutorial to create an IoT Hub. The only difference between what I did and what is outlined in the tutorial is that I chose the “F1 – Free” pricing tier.
Instead of following the second section of the tutorial which uses a console app created in Visual Studio to create a device identity, I downloaded the Device Explorer app and created my device identity using the app's UI in just a few clicks. Once you enter your IoT Hub Connection String in the Configuration tab - refer to Step 6 in the Get started with Azure tutorial if needed - go to the management tab, click Create, enter your device ID, and click Create in the Create Device pop up; your device keys will be generated for you (you'll need the device ID and it's Primary Key in the next step). You should now see your device listed.
Voila, you now have a cloud service to which to connect your device.
Time: 15 minutes
Parts: Assembled Raspberry Pi with PIR motion sensor and LED
Tools: Visual Studio 2015
Download the code from the github repository and open the solution in Visual Studio 2015. You'll first need to update three variables in order to be able to connect your device to your instance of Azure IoT Hub. First, insert your IoT Hub's URI into the iotHubUri string - refer to Step 5 in the Get started with Azure tutorial if needed. Then, insert your device ID into the deviceName string - the device ID you specified when you created the device in Device Explorer in the previous step. Finally, insert the device key into the deviceKey string - the primary device key Device Explorer generated when you created the device in the previous step. Now build the app and deploy it to your Raspberry Pi.
Every 60 seconds (the interval defined for the timer), the Raspberry Pi will read the signal pin on the PIR sensor. If it's "HIGH", the LED will turn on to indicate that motion was detected or turn off to indicate that no motion was detected. The app will also then send a message to the IoT Hub. The message contains the device ID, the time, and the room status - either "Motion detected." or "No motion detected."
The easiest way to see that the messages are being received in the IoT Hub is via the Data tab in Device Explorer. Select the appropriate device from the Device ID drop down list if you created more than one. Select the start time - Device Explorer will display all messages received by IoT Hub after this time. Then click Monitor. The messages sent from the device will be displayed.
Additionally, you can see the number of devices connected and the number of messages sent to the IoT Hub in the Preview Portal. After logging in, click on the tile for your IoT Hub. At the bottom of the first blade, you will see these top level statistics.
I set up the sensor in the back corner of the conference room so that it could "see" the entire room while not getting in people way at the same time. There are trim pots on the back of the PIR sensor to adjust its sensitivity and the time delay. You'll need to adjust these to suit your deployment. I have set the sensitivity fairly high since there is little to trigger false alarms - i.e. traffic in the hallway is easily visible when the conference room door is open but I don't get false alarms because the sensor is positioned on the same side of the room as the door and facing away from it. The time delay specifies how long motion needs to occur in order to be considered a detection - i.e. if you have a long time delay, motion that lasts a fraction of the second won't be reported. I just left my time delay at the factory set midpoint as it made very little difference for this project (no one runs in and then out of the conference room extremely quickly for the heck of it).