Tuesday, October 26, 2010

Automatic Halloween Candy Dispenser

This year my Halloween project is the Automatic Halloween Candy Dispenser® triggered by TextforTreat®, CallforTreat® (powered by Twilio)and @TweetforTreat® technologies. Of course it can also be triggered from a Big Red push button, but where is the fun in that?

When trick-or-treaters show up at my house they can text, call, or tweet a code displayed on an LCD screen to get their Halloween candy. They can also push the Big Red button.

Once the candy request is made a few "special" effects are triggered by X10 modules. A low laying fog machine is activated and lights turn on while the candy shoots down from my front deck on the second floor.

* Check my Instructable write up for more detailed information.




Candy Dispenser
Control Box
Main Box
Communication Bridge
Fog Machine
Lights and accesories


Candy Dispenser


The first thing was to figure out a good way to hold the candy and dispense it. I used a plastic bucket, a metal ventilation pipe and a CD attached to a servo motor. I figured that by having the candy dispense from the side as opposed to the bottom would allow the candy to move more freely and avoid candy jams.

The ventilation pipe comes in a "U" shape, so I made a "U" shaped cut in the bottom center of the bucket. Notice I did't cut the whole "U", instead I left a piece of plastic in the middle to hold the servo motor that rotates the CD.

After I inserted the ventilation pipe I did a few cuts to allow me to expand the pipe into a sheet form. I used a couple screws and bolts to keep the sheet attached to the bucket.  The rest of the space was covered by the mighty duck tape.

I cut the CD to allow the candy flow when rotated. Also I duck taped a continuous servo inside to function as a "shaker."


Control Box

The control box is pretty simple. I drilled/dremelled a few holes to attach the big red button, ioBridge LCD screen,  power jack and the RJ45 jack.

I really like the use of CAT 5 cable to make things more modular.

Main Box

The main program runs in the Arduino Duemilanove.  When the board is initialized it immediately makes a Web call to check for the current code and displays it on the LCD screen.

The main loop is waiting for either a physical push of the big red button or to receive a remote serial message. The attached XBee module communicates with another XBee module connected to the ioBridge IO-204 module serial board.

These are the digital I/O pins configuration:
  • XBee Module (with adafruit XBee kit)
    • Pin 13 - TX 
    • Pin 12 - RX
    • Gnd and 5v
  • Big Dome Push Button
    • Pin  8 - Button NO(normally open) +  GND + 100K. 
    • 5v - COM
  • Servo 1 (continuous rotating servo used for shaking)
    • Pin  7 - Data
    • Gnd and 5v
  • Servo 2 (used to rotate the CD)
    • Pin 6 - Data
    • Gnd and 5v
  • X10 C17A (x10 devices controller)
    • Pin  3 - DTR line for C17A - DB9 pin 4
    • Pin  2 - RTS line for C17A - DB9 pin 7
    • Gnd  - DB9 pin 5
  • ioBridge 16x2 serial LCD display
    • Pin 1 - LCD Data pin
    • Gnd and 5v
Attached is the Arduino Sketch, it also includes a library to control the ioBridge LCD screen.

Code Generator

I realized that someone with mischievous intentions could just tweet-spam the @Tweetfortreat account (or less likely someone could text or call my Twilio number numerous times) and I would end up with tons of candy on the floor.

Instead, I created a REST web service using of course my favorite web development tool, Oracle Application Express http://apex.oracle.com/ , to make sure a unique and random code was generated each time candy drop request was made from text, call or Twitter.

The REST application is really simple and I recognize it can also be created using any other technologies such as LAMP or even a GCI, or Perl application.

Application Objects:
  • 1 Table (ID,CODE,SELECTED)
  • 1 Numerical Sequence to generate primary key (ID)
  • 1 Trigger before insert record that will get the ID value and append a random number to the CODE column
  • 1 public procedure to retrieve the code (where selected = 1)
  • 1 public procedure to update the code once is used and generate a new one.
So I ended up with these two URL
http:/my.apexserver.com/pls/apex/getcode
http:/my.apexserver.com/pls/apex/updatecode?code=XXXX
*If you would like to implement something with a static file instead, I would recommend using http://www.random.org/clients/http/ to get a random number and store it in file, memory, or even the arduino itself.

ioBridge Setup

The ioBridge IO-204 is used as bridge (duh!) between the arduino and the internet. I am using the ioBridge serial board to connect an XBee module.

There are two main functions that the ioBridge was used for:
  1. Request current code
     I do a GET call from the arduino to retrieve the current code from my Code Generator service using ioBridge Serial Web Services API.
     Arduino code:
     mySerial.println("[[[get|http:/my.apexserver.com/pls/apex/getcode]]]");

      
  2. Start candy dropping sequence by text, call or tweet.
     An ioBridge widget will send a start character via serial message and the candy dropping sequence will start. This widget can be called using the ioBridge Static Widget API.
This URL  is called by Twilio and by my custom Twitter alert script.

http://www.iobridge.com/widgets/static/id=[widgetID]


These are the digital I/O pins configuration:
  • ioBridge Serial Board
  • TX - RX XBee
  • RX - TX XBee
  • GND and 5v

Twilio Setup


Twilio is a web-service API for communication apps. Twilio is as simple as it gets. It only  took me a short time to get my  SMS and Voice applications up and running! I had tried in the past to create similar applications with other technologies (VoiceXML and even Google Voice) and let me tell you is not that easy.

The voice application answers the phone, plays an intro sound file and asks for the code followed by the # sign. If a code is detected Twilio will do a "GET" action to a PHP file that will lookup the current code from my Code Generator Web Service. If it matches it will call the ioBridge static widget URL and candy will be dispensed.

The SMS application has almost the same PHP code except this time instead of looking for the digits dialed  it will look for body of the SMS message.

Check the attached XML and PHP files that generate the TwiML XML.

All in all Twilio is a winner and I have plenty of other ideas for the future.

Twitter Setup


No modern internet enabled project is complete without Twitter integration, right? :) Here is mine: This time i used a bash script, twurl and a crontab job to check for replies to @tweetfortreat. This bash script is running on my Sheevaplug, but it can be run in any linux/mac machine or it could be adapted to run as .bat in windows (using scheduled tasks).

Twurl is almost like curl but for Twitter. I did some ugly parsing of the twitter RSS feed to retrieve the tweet sender, but it works!

Here is my bash script
#!/bin/bash
mycode=`curl --silent http:/my.apexserver.com/pls/apex/getcode`
twitter=`twurl /1/statuses/mentions.rss | grep "@tweetfortreat $mycode" | cut -d ':' -f1 | tail -n 1 | cut -d '>' -f2`
echo $mycode $twitter
if [ -n "$twitter" ]; then
iobridge=`curl --silent http://www.iobridge.com/widgets/static/id=[widgetID]`
newkey=`curl --silent -d "code=$mycode" http:/my.apexserver.com/pls/apex/updatecode`
update=`twurl -d "status=@$twitter The code $mycode is correct. Happy Halloween!" /1/statuses/update.xml`
echo $newkey $iobridge
fi

Crontab will only allow you to execute a script every minute and I think is fine for now. Alternatively a while loop could be used instead.
Here is my crontab entry

*/1 * * * * /script_location/HalloweenCandy.sh > /dev/null 2>&1

Conclussion and Alternatives
If my Google skills (http://lmgtfy.com/?q=automatic+halloween+candy+dispenser)serve me well there are just not many documented Automatic Halloween Candy Dispensers out there. I did run into this one which I really like but is limited to M&Ms sized candy.I found some inspiration from the dog/cat feeders built using the internet enabled ioBridge http://support.iobridge.com/projects/tag/dog-feeder/.

I hope to see more iterations of the Automatic Halloween Candy Dispenser next year. Please make sure you share all your ideas/improvements in the comments section.


Happy Halloween!

13 comments:

  1. Good blog, where did you come up with the knowledge in this piece of content? I’m glad I found it though, ill be checking back soon to see what other articles you have.
    Thanks
    nilyvn

    ReplyDelete
  2. Thanks for this information.I really appreciate your work, keep it up.The only difference is that websites allow you greater access to the world.
    web design company

    ReplyDelete
  3. I am also interested in latest news, sometimes i posted on my blog.....Web Design Company

    ReplyDelete
  4. Well, very good post with informative information. I really appreciate the fact that you approach these topics from a stand point of knowledge and information. joomla websites | joomla development

    ReplyDelete
  5. Such talented content! It's very meaningful and well worthy input. Get more details in this issue. Thanks!
    webdesign

    ReplyDelete
  6. If you are one of those people who hate Halloween and dislike having to hand out sweets to pesky kids, you definitely need a solution for that. Automatic Halloween Candy Dispenser by Noel Portugal is built for people like you. It’s extremely handy for those spooky visitors who want sweets and won’t leave until they get their share. They can use any number modern technologies like TextforTreat, CallforTreat and @TweetforTreat to release some sweets, or they can just press a big red button. It’s perfect for grumpy old people who can’t be bothered to entertain the little ones. I think I personally need it in my house, so that I can have easy access to delicious candy. There is a guide on how to make it here. It’s fairly complicated, so you might need some help.
    jogos de moto
    mensagens de carinho

    ReplyDelete
  7. I really like your post.It’s really informative and interesting.I really appreciate that.
    Schaumburg Boot Camp

    ReplyDelete
  8. Now you don't have to wait to collect the investment amount to start your own business in a really affordable prices and more 75 features you can have your own on-line store.
    Ecommerce Cart

    ReplyDelete
  9. It’s a pleasure to visit this blog because it has a detailed structure. Simply Excellent. Visited so many blogs, I find this a very unique and interesting.
    Business IT Support London

    ReplyDelete
  10. Thank you very much for posting this blog. I really appreciate the effort to design the blog.

    Packers and movers pune

    ReplyDelete
  11. Thank you for sharing valuable information. Nice post. I enjoyed reading this post. The whole blog is very nice found some good stuff and good information here Thanks..Also visit my page the best wordpress themes.

    ReplyDelete
  12. This year my Halloween project is the Automatic Halloween Candy ... fogmachine.blogspot.com

    ReplyDelete
  13. A low laying fog machine is activated and lights turn on while the candy shoots ... fogmachineliquid.blogspot.com

    ReplyDelete