Hacking Halloween: Using Arduino and Twilio To Build An Interactive Haunted House

Talk about this post on twitter: #hauntedHack

When I was a young trick-or-treater I never missed the house on the hill that gave full-size candy bars; that was the Halloween destination on our street. But these days setting up the ideal trick-or-treating destination requires thinking outside the Snickers bar. I want kids to remember our house. I want parents to tell other parents about it. But more importantly I want a “surprise factor” when people discover our house. So this year I amped up the “trick” and put together an interactive haunted house using Twilio, Arduino and some cheap electronic components.

Haunted Hack: Pumpkins That Change Color When You Text Them

My initial goal was simple. When people text a color to my app I wanted the pumpkins in our lawn to change colors.

Here’s a video of the final product.


 

First my wife texts “red” to the Twilio number, which turns the pumpkins red. Then she texts the word “chaos”, which unleashes a world of terror. Can’t wait to see how the trick-or-treaters react when their parents are texting chaos as they walk up! Let’s walk through how I built these interactive pumpkins using Arduino and Twilio.

Here’s what you’ll need to make this happen:

Extras:

  • Fog Machine
  • Speakers
  • Bats
  • Other spooky business

Step 1: Building The Circuit

 
This is my exact circuit with one change. Instead of a 9V battery I used a 12V AC to DC converter. A 9V battery WILL work it just won’t be as bright.

This circuit is based off of this make guide tutorial and is relatively simple. The only trick here is that we are using MOSFETs (a type of transistor) in order to amplify the voltage going to the LEDs. Because the LEDs require more than the 5V the arduino puts out, we have to use MOSFETs to amplify the power. The way it works is the Arduino sends a digital signal (PWM) to the MOSFET through the (G) gate pin. The signal switches the transistor passing the higher current at 12 V, gathered through the (S) source, out through the (D) drain to the LED strip. This is a smart way of amplifying a small PWM signal to a device that requires a higher source of power.


Safety Warning!!

Remember whenever working with power to be extremely careful. Always build circuits with the power unplugged. The last thing we want is for you to fry your Arduino or yourself, leave the shocking to Dr. Frankenstein.
Once you’ve got the circuit looking like this, we should be ready to go on to Step 2.

The Arduino Software

Now let’s get the Arduino piece working. Before we look at the code let’s make sure you are all setup.

First you need to:

Now that you’re all set to go, let’s set up a sketch to make the Arduino talk to Pusher and trigger some fun LED colors.

Once you have the Arduino app (IDE) open, create a new file and paste the following code into it.

Let’s walkthrough this sketch together. First we are including the two libraries we are going to need for this project; the Ethernet and Pusher libraries. Then all we are doing is setting up our three PWM pins to prepare them to manipulate our three LED strip leads. In this case we are using pins 5, 6 and 9. Pin 5 controls the RED signal, 6 is GREEN and 9 is BLUE.

Now that we have prepped the PINS to do their jobs, we can create complex colors by sending RGB values through each pin. Notice in the setup() function that we are writing different values to each pin. Pin 5 (RED) is sending 255, pin 6 (GREEN) is sending 102, and pin 9 (BLUE) is sending 0. This of course matches the RGB values for Orange.

Next we need to connect to our Pusher channel via the ethernet connection and setup up our handlers for the different events will trigger the pumpkins to change colors.

Paste this next snippet within the setup() function where I’ve indicated in the code.

Once our Pusher client has connected to the internet, it authorizes with our app key and subscribes to the channel that we will eventually be sending our events through. Then our client listens for certain event names, in this case colors, and binds those events to event handlers.

Sweet, we’re almost there. Now all we have to do is write the loop and the callback functions that will change the color. Here is the next piece of code.

This last piece is straightforward. Inside the loop we tell the Pusher client to constantly monitor the event channel.

Then we create the callback functions for the Pusher event listeners which change the color of the LED strip lights by pulsing new values through the RGB leads. This could be simplified by writing a helper function that takes an RGB array as an argument and loops through the pins. Since we only have a few colors this is fine for now.

Finally we can upload our sketch to the Arduino. You can find the full sketch here for reference.

Next we are going to write a simple Sinatra app and connect all the pieces to create some jacked-up Jack O Lanterns!

Step 3: The Sinatra App

The server piece of this haunted hack is a very basic Sinatra app that we will host on heroku. The first thing to do is set up basic configuration with two new files in your project directory; the Gemfile and the Procfile:

We’re just going to start with the whole Ruby file since it’s pretty simple. Now you can go ahead and create haunted-hack.rb and add the following code:

Let’s walk through this file. At the top we are setting up our Pusher configuration variables and including necessary gems. We will set up the environment variables later when we push to Heroku.

Next we set up our /trick/ route, which receives incoming SMS messages and triggers a Pusher event that the Arduino is already listening for. Once the event has been fired, we send a text back to the user using the Twiml.Message verb.

Lastly we throw up our root route which renders a simple index.erb file. Here is the index file.

Other than displaying some instructions this index file does one other thing. The template sets up a client-side Pusher listener so that we can listen for Pusher events within the website and handle it on the client side. In this case, I wanted to add some flair to the pumpkin hack, so our webpage listens for the Pusher event called ‘chaos’ and redirects the page to ‘/chaos/’.

Here is the erb template for /chaos/

Now that we have the server set up lets configure our deploy process, which in this case will involve Twilio and Heroku.

Step 4: Do It Live

Next step:

    • Install Heroku
    • Setup Heroku environment variables
    • Set up your Twilio number

Before we can configure your Twilio phone number we need to host the haunted-hack Sinatra server somewhere. Heroku allows us to host the Sinatra app for free and with very little overhead.

First install the Heroku Toolbelt which comes with git and heroku tools. If you’ve never used Heroku before take 5 minutes to go through this starter guide.

Next open terminal and cd into the directory you are working in. Run these commands to initialize a git repo, add your first commit and create a heroku app.
cd hauntedHackDirectory/git initgit add .git commit -am "Initial commit"heroku create --stack cedargit push heroku master
When your git push is finished executing, you should see a line similar to this: “http://blazing-galaxy-997.herokuapp.com deployed to Heroku” – that gives you the URL of your new Heroku application. Remember this for the next step.

Before we move on we need to set some heroku environment variables. You can find these tokens on your Pusher dashboard. Inside Terminal run these commands:
 
heroku config:set PUSHER_APP_ID=12345 PUSHER_KEY=abc123456789 PUSHER_SECRET=abc123456789

Your output should be similar to this: “Setting config vars and restarting haunted-hack… done”

Now we need to point our Twilio phone number to our Message endpoint. If you are new to Twilio you can get your free number here.

Once you’re logged in to Twilio go to your number for the haunted hack and add your heroku app url to the Message Request URL field. Make sure to add ‘trick/ to the end of the url.

Holy pumpkin Batman! I think we are done so let’s hook it up and test it out. Feel free to checkout or fork the repo for this whole project here.

Checklist

  • Put the Arduino + LED strips in some foam pumpkins. (I carved mine to be friendly and not too scary).
  • Plug in Arduino 12 V converter
  • Plug in LED 12 V converter. (LED strip should turn on and glow orange, if not check your circuit)
  • Go to http://yourhauntedapp.herokuapp.com/
  • Point some speakers out your window
  • Text ‘blue’ to your Twilio number.
  • Text ‘chaos’ to your Twilio number.
  • You’ve just hacked halloween!

 

Add some flair to your scare!

In my haunted hack, if someone texts chaos a series of events is triggered. I load ‘/chaos/’ on my webapp, which begins playing scary music. The Arduino tells the LED’s to begin strobing and it flips a PowerSwitch to turn on the fog machine. But it could be even scarier with a little more hardware magic.

Why not trigger some servos to drop bats on your unsuspecting trick-or-treaters! Or have ‘chaos’ trigger a PowerSwitch that makes zombies come out of the ground! Now you have the building blocks to truly hack Halloween. Let us know what #hauntedHack you’ve built on Twitter.

Happy Halloween!