Search

@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[] = "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.
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.

arduino_bb_pot_transistor_motor2-1024x560

acm17

Related posts

Leave a Comment