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.





Wow awesome project. Well done. Gives others great ideas too, what a bonus.
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
Thanks for taking the time to reply Andrew :)
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4
Pingback: Control a motorised food dispenser with twitter « freetronicsblog
Thanks for taking the time to visit and make your own blog post about my work :)
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4
Hilarious – I’m almost tempted to join Twitter just to douse you in candy :-)
Mozilla/5.0 (Windows NT 6.1; rv:18.0) Gecko/20100101 Firefox/18.0
Douse away Phil – I just bought another big bag of Skittles :)
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4
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!
Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0; BOIE9;ENGB)
Thanks for visiting Tony.. they are reduced again from when I bought mine – I may have to buy a few :)
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.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!
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
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
Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31