@Arduino based twitter-enabled candy machine – all working and monitoring twitter

I got my twitter monitoring candy machine working today!

If you wish to give me a candy simply send a tweet with the hashtag #candy4greg and I (or my children!) get a healthy nutritious snack. Chewing gum, skittles and tic tacs are healthy, right?

Here is the code I am using. Note you will need to get the TextFinder library from the arduino site:

#include <SPI.h>
#include <Ethernet.h>
#include <TextFinder.h>

int treatPin = 9; // The pin the transistor is connected to

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xE4 };
byte ip[] =  { 192,168,0,228}; //change this
EthernetClient client;
char TwitterHashtag[] = "#candy4greg"; //change this to your own twitter hashtag, or follow arduino ;-)
char tweet[140] = "", oldTweet[140] = "";
char serverName[] = "search.twitter.com";  // twitter URL


void setup() {
  pinMode(treatPin, OUTPUT);
  // initialize serial:
  Serial.begin(9600);
  if(!Ethernet.begin(mac))
    Ethernet.begin(mac, ip);
  // connect to Twitter:
  delay(3000);
  for(int i = 0; i < 140; i++)
  {
    oldTweet[i] = 0;
    tweet[i] = 0;
  }
}


void loop(){
  int i;
  Serial.println("connecting to server...");
  if (client.connect(serverName, 80)) {
    TextFinder  finder( client,2 );
    Serial.println("making HTTP request...");
    // make HTTP GET request to twitter:
    client.print("GET /search.atom?q=%23");
    client.print(TwitterHashtag);
    client.println("&count=1 HTTP/1.1");
    client.println("HOST: search.twitter.com");
    client.println();
    Serial.println("sended HTTP request...");
    while (client.connected()) {
      if (client.available()) {
        Serial.println("looking for tweet...");
        if((finder.find("<published>")&&(finder.getString("<title>","</title>",tweet,140)!=0)))
        {
          for(i = 0; i < 140; i++)
              if(oldTweet[i] != tweet[i])
                break;
          if(i != 140)
          {
            Serial.println(tweet);
            feedKids();
            for(i = 0; i < 140; i++)
              oldTweet[i] = tweet[i];
            break;
          }
        }
      }
    }
    delay(1);
    client.stop();
  }
  Serial.println("delay...");
  delay (60000); 
  // don't make this less than 30000 (30 secs), because you can't connect to the twitter servers faster (you'll be banned)
  // off course it would be better to use the "Blink without delay", but I leave that to you.
}

void feedKids()
{
  Serial.println("Time to feed the kids!");
	digitalWrite(treatPin, HIGH);
	delay(2000); // time in ms to run the motor
	digitalWrite(treatPin, LOW);
}

The code searches for the hashtag #candy4greg and then compares it with the previous time it checked, if there is a difference I get fed :)

The below images and above code should point you in the right direction.

Wiring to arduino

Wiring to arduino

Arduino motor driver using TIP120

Arduino motor driver using TIP120

An endnote: The actual candy machine is from Maplin, here is a link to the product page: www.maplin.co.uk/search?criteria=n05nb. They have a larger model, I want that now I know the idea works. That is at www.maplin.co.uk/large-treat-ball-the-motion-activated-treat-dispenser-662062. Both are on sale at the moment, grab them before they become popular now us arduino lovers have discovered them!

A note for parents: Candy like chewing gum balls are not an acceptable substitute for square meals. Stick to giving the kids drumstick lollies and mojo's and you'll be fine.
Drumstick lolly

23 thoughts on “@Arduino based twitter-enabled candy machine – all working and monitoring twitter”

  1. Wow awesome project. Well done. Gives others great ideas too, what a bonus.

    Safari 534.57.2 Mac OS
    Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2
    1. Thanks for taking the time to reply Andrew :)

      Google Chrome 22.0.1229.79 GNU/Linux 64 bits
      Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4
    1. Thanks for taking the time to visit and make your own blog post about my work :)

      Google Chrome 22.0.1229.79 GNU/Linux 64 bits
      Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4
  2. Hilarious – I’m almost tempted to join Twitter just to douse you in candy :-)

    Firefox 18.0 Windows 7
    Mozilla/5.0 (Windows NT 6.1; rv:18.0) Gecko/20100101 Firefox/18.0
    1. Douse away Phil – I just bought another big bag of Skittles :)

      Google Chrome 22.0.1229.79 GNU/Linux 64 bits
      Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4
  3. It took a careful read of your review of the large sweet dispenser before I convinced myself that you had not posted a review for something else against the wrong product :-) Amazing!

    Internet Explorer 9.0 Windows Vista
    Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0; BOIE9;ENGB)
    1. Thanks for visiting Tony.. they are reduced again from when I bought mine – I may have to buy a few :)

      Google Chrome 22.0.1229.79 GNU/Linux 64 bits
      Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4
  4. Hi Greg, thanks for posting this!

    Completely by chance I picked one of these up and plan to do a similar thing and found your article!

    I’m actually just trying to get the Arduino to act the manual switch on the dispenser itself, so that I can activate it but still have the requirement to activate the sensor.

    I was hoping I could achieve this simply by hooking up the internal switch to a digital pinout and ground on the Arduino… but having tried that and a number of approaches I’ve had no luck. Would you mind sharing how you might approach doing that? I’ve tried using both a transistor and 4N35 to switch the dispenser’s own circuit but I’m struggling to “debug” the problem any further.

    Thanks!

    Google Chrome 26.0.1410.65 Mac OS
    Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31
    1. If I understand you correctly:
      you want to use the arduino in place of the main switch? Maybe some kind of time activated power for example?

      That should be simple using my original circuit and fitting the batteries

      —battery-transistor in place of switch–

      So the batteries power the transistor instead of the 5v rail.

      The arduino code will need some rewriting but basically you set the pin high when you want the sensor to work (“office hours”) and low when you don’t want it to work (“closed period”).

      You would still need to trigger the sensor manually to get the treats.

      If my understanding is completely wrong I am happy to look at what you are trying and help in any way I can.

      Best regards

      Greg

      Google Chrome 26.0.1410.43 GNU/Linux
      Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31
  5. Unfortunately does not work for me. I think the problem is TextFinder library. Could you send me your library? Please help me fix problem.
    TNX

    Google Chrome 28.0.1500.95 Windows 7
    Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36
  6. Hi Greg,
    have you had any luck updating this to work with the Twitter 1.1. API?
    If you manage it, I’d love to see the new code.
    I tried myself, thought I had PHO echoing the tweets but Arduino is picking them up :(

    Safari 536.30.1 Mac OS
    Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/536.30.1 (KHTML, like Gecko) Version/6.0.5 Safari/536.30.1
    1. Hi Martin

      I have recently been playing with the 1.1 API to get it working from a bash script.

      The code in there should help with your PHP issues.

      At some point I will update the code and redo the arduino stuff.

      Check my latest posts for the bash script.

      Thanks for visiting my site

      Greg

      Google Chrome 22.0.1229.79 GNU/Linux 64 bits
      Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4
  7. Have you had any advise about how ican make it work with API 1.1 ?

    Google Chrome 33.0.1750.146 Windows XP
    Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CommentLuv badge