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

@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[] = "";  // twitter URL

void setup() {
  pinMode(treatPin, OUTPUT);
  // initialize serial:
    Ethernet.begin(mac, ip);
  // connect to Twitter:
  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.println("&count=1 HTTP/1.1");
    Serial.println("sended HTTP request...");
    while (client.connected()) {
      if (client.available()) {
        Serial.println("looking for tweet...");
          for(i = 0; i < 140; i++)
              if(oldTweet[i] != tweet[i])
          if(i != 140)
            for(i = 0; i < 140; i++)
              oldTweet[i] = tweet[i];
  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.
An endnote: The actual candy machine is from Maplin, here is a link to the product page: They have a larger model, I want that now I know the idea works. That is at 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.



Related posts

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.