The Rationale:
In some of our grade levels, the color copiers frequently end up broken from users trying to fix paper jams. I wanted to find a way to allow users to quickly report problems that require our assistance. I’ve created a system using Amazon IOT buttons (Developer version of the Amazon Dash), IFTTT Maker Channel, LIFX Led Color Changing Lamps, and Blink(1) usb LED lights to create the following outcome:
A. Printer Jams
B. User Presses Physical Button on Copier
a. A light behind the copier (reflecting off the white wall) turns red alerting this user and others the machine is out of commission
b. An email is sent to our ticketing system informing us that the copier in building A/B/C/etc. needs attention.
c. Another light on a campus map in the IT office pulses red informing us of the outage, and its location.
C. Technician Resolves Error on Copier.
D. Technician Double Taps the Button.
a. Status Light Behind Copier Turns green to alert users visually the copier is available.
b. Status light in IT office pulses green and turns off.
Any device used must connect over wifi as it's the only reliable method across our entire campus. Protocols like Z-Wave or Smartphone dependent Bluetooth would work fine for a small install of a project, but would not scale well beyond a single building. This ruled out Hue Lights, Logitech Pop Buttons, and Flic buttons.
All Materials are available through Amazon, however as of Feb 2017 the LIFX bulbs are backordered. AWS IOT buttons have two generations as of Feb 2, the only difference being that Gen 2 has double the life span.
You'll need to create accounts for these services. You will need a smartphone to set up and manage your IOT devices. I used an iPhone for my setup procedures.
AWS - http://aws.amazon.com
LIFX - Through Mobile App
IFTTT - http://www.ifttt.com
Make an account at IFTTT. I would recommend making a dedicated account rather than reusing a current account. It will get very busy quickly as you scale this out. Once your account is made you will go to services and "Enable the Maker Channel"
Once enabled you will get a "Maker Key" which is the url listed keep this private and save it for later.
This maker key allows you to send web requests as a triggering event on IFTTT. Keep it very private.
LIFX Account Set Up. Follow the instructions noted on packaging to set up your bulb. Remmeber to ensure you have good 2.4 ghz coverage in location where you set up the light, and where it will be located. Don't be stupid like me and put the light inside of a metal desk lamp and expect it to have good wifi reception. Name the lamp and the room something similar, and include the serial # in the lamps name to ease identification if things get shifted around. I also wrote on the base of the lamp with a sharpie which location it was designated for.
You'll need to use the AWS IOT button app. The instructions are here. Basically, open the app, use the phone's camera to scan the barcode on the box and follow the steps to set up your button. Once completed it will let you choose a template to deploy to your button. You'll choose "Trigger IFTTT Maker (nodejs)" Or you can use Python, either works. It will ask you for your "Maker Key" from Step 1.
Set up your LIFX Inegration with IFTTT
Download the Blink(1) Controller Software from ThingM here. I used the Beta version of Version 2 succesfully.
Once completed, and you've spent thirty minutes realizing how awesome the Blink(1) is, you will need to copy your IFTTT key from the status window of the software. and use it to set up your IFTTT blink(1) channel.
Then go back to your Blink1Control software and set up two events. One for Single Click and one for double click. Choose what you want the pattern to be from the premade patterns, or create your own patterns. For simplicity sake, I make any device or rule name with underscores_for_spaces to help denote that they will be used in the IFTTT applets.
You've got all the pieces assembled, you just need to tie them together in an IFTTT Applet. Let's look at the data path flow first.
The AWS IOT Button has three modes which it will send in this format, these can be changed but it involves a bit of digging into AWS which we'll discuss later.
AWS Button will send an "Event Name" to Your IFTTT Maker Channel based off of the device serial # and click type. Practically that looks like this:
Single Click Event: G030JF0511XXLX5J-SINGLE
Double Click Event: G030JF0511XXLX5J-DOUBLE
Long Press Event: G030JF0511XXLX5J-LONG
As you build applets you will associate that event type as a trigger which will fire off an action.
The magic with IFTTT is that you can make multiple applets based off of the same event type. So if you want multiple actions, you just build multiple applets with the same trigger but different actions.
Here's a weirdly grainy image showing the flow of data:
This is where AWS Lambda Comes in. If you sign into your AWS Console click on Services > Lambda You can then edit the Node.Js that is powering your IoT button by clicking on the function that matches the SERIAL # of your IoT button. You can then edit the code inline. It's pretty basic stuff. All I have done to make ifttt cleaner is made this change from:
const makerEvent = `${event.serialNumber}-${event.clickType}`;
to
const makerEvent = `BLDG_A-${event.clickType}`;
By removing the variable of the ${event.serialNumber} I was able to make my IFTTT Applets much cleaner, and I eliminated unneccesary work in the event that I have to replace an IFTTT button. Rather than renaming all of my applets, I only have to change the Event name in once place upon deployment of the IoT button.
by changing it at the const "makerEvent" it's passed along to the maker trigger:
https://maker.ifttt.com/trigger/${makerEvent}/with/key/${makerKey}
Below is the entire code in context, it's pretty straight forward.
I did try to use a switch / call function to make a variable list of serial #s which would pair building names with serial numbers, but Lambda severly disliked every implementation I attempted. When I have more time I'll try it with the python IFTTT code using if, elseif functions.
/**
* This is a sample that connects Lambda with IFTTT Maker channel. The event is
* sent in this format: <serialNumber>-<clickType>.
*
* The following JSON template shows what is sent as the payload:
{
"serialNumber": "GXXXXXXXXXXXXXXXXX",
"batteryVoltage": "xxmV",
"clickType": "SINGLE" | "DOUBLE" | "LONG"
}
*
* A "LONG" clickType is sent if the first press lasts longer than 1.5 seconds.
* "SINGLE" and "DOUBLE" clickType payloads are sent for short clicks.
*
* For more documentation, follow the link below.
* http://docs.aws.amazon.com/iot/latest/developerguide/iot-lambda-rule.html
*/
'use strict';
const https = require('https');
const makerKey = 't4J1j7BkVMXmJxshaeRye'; // change it to your Maker key
exports.handler = (event, context, callback) => {
console.log('Received event:', event);
// make sure you created a receipe for event <serialNumber>-<clickType>
const makerEvent = `BLDG_A-${event.clickType}`;
const url = `https://maker.ifttt.com/trigger/${makerEvent}/with/key/${makerKey}`;
https.get(url, (res) => {
let body = '';
console.log(`STATUS: ${res.statusCode}`);
res.on('data', (chunk) => body += chunk);
res.on('end', () => {
console.log('Event has been sent to IFTTT Maker channel');
callback(null, body);
});
}).on('error', (e) => {
console.log('Failed to trigger Maker channel', e);
callback(`Failed to trigger Maker channel: ${e.message}`);
});
};