Intro: Arduino/Twitter Mets Apple
Whenever I go to Citi Field with my kids to see the Mets, they go crazy for the Home Run Apple.To me, the Apple is part of what makes baseball exciting for kids. Even though my kids may not watch most of the games, I still enjoy bringing them along.
The Home Run Apple started popping out of a top hat at Shea Stadium in 1980 and now Citi Field has an updated version of the Apple. The apple pops up when a Met hits a home run. The old apple from Shea Stadium was retired and now rests outside of the Jackie Robinson Rotunda of Citi Field.
There are a few other teams around the majors that have their own home run gimmicks as well, and I have written this project in a way that it could be easily adapted to another team. The other teams that come to mind are the Angels (fireworks), Astros (Train), Brewers (Bernie Brewer sliding down a yellow slide), Marlins (clockwork fish & fireworks), Phillies (Liberty bell) and Reds (fireworks). Feel free to let me know if I have missed any in this list.
I thought I would like to have a little piece of Citi Field in my house that would react when the Mets hit a homerun. I thought of a few different options, but all of them would be difficult to implement reliably on such a small platform as the Arduino. The main problem is that while baseball scores are available, you have to pay for most data feeds. To make things worse, I didn't really want a box score or any other form of sports score that I could think of. I wanted a notification of a current home run on a timeline that I could acknowledge and ignore until another home run notification came in. The only possibility that I could think of was Twitter.
First working prototype...
Final working apple!
Step 1: Lego Prototypes
Originally I was just planning to temporarily make the apple and top hat out of Legos until I could find a more suitable solution. My daughters have a growing collection of Legos, but unfortunately almost all of theirs are pink, white and light green. I stopped by a Lego store and bought a small container (about $8) of red and black bricks from the pick-a-brick bins and built the first model which was very boxy.
I wanted something that had a little more curve to it, but I had used up all of the Legos I bought. I made another trip to a different Lego store, hoping to find different sizes of bricks and hopefully some plates as well. I didn't have much luck finding plates, but I did get a good variety of black and red bricks, so I bought the large container (roughly $15) and filled it up.
I still wasn't satisfied with the curves I was able to make, so I made another trip to the Lego store and bought another large container (I know, I can't help myself). I figured I could make the curves smoother if I just made it big enough. Unfortunately, the smooth horizontal curves that I can get by making it bigger don't help with the vertical curves of the apple, so I needed to find some plates.
I looked on Lego.com and a few brick resale sites online, but it seems like you have to buy everything separately and it can get very expensive, very quickly. I looked on eBay and found a user selling 1/4lb bags of color separated Legos with a buy it now auction. I bought 2 red and 2 black and hoped that they had a good selection of plates. When the bags came they had everything I needed and there was plenty of excess that I gave to my kids to improve their collection.
Step 2: Servo
I bought a micro-servo from Sparkfun because it was cheap ($9) and I thought space might be an issue. I've never had a reason to use servos with Arduino, but this worked very easily and without too much trouble.
I first tested the servo with the basic servo sweep sketch on the Arduino and moved the arm back and forth. I had to adjust the arm so I kept the movement that I needed within the range of the servo. It seems like the servo strains a bit when it gets up to the outer limits of it's movement. Avoid the urge to force the servo to a particular place because you may break something inside the servo.
I used a drinking straw as a push rod to pop the apple up and down. The straw was just the right size to fit into the circles on the bottom of the Legos so it would stay in place. The other end I trimmed short, poked a hole in the end, pushed a piece of UTP wire I had laying around through the straw and servo arm and twisted it up. I had found a spot to rest the servo in the side of the top hat and I used hot glue to stick it in place.
Step 3: Meet the Mets
I wanted to play some sound as well when I triggered the apple mechanism, so since I don't have a way to play crowd sounds or baseball bat cracking sounds, I decided to make the Arduino play the Mets theme song "Meet the Mets".
I found 2 different sets of sheet music for the song, and I pieced together the part I needed from the 2 sites. You have to click around an awful lot to get to the right instrument part, but I think I got it pretty close.
I won't go into it again here, but I wrote another Instructable that outlines my process for turning sheet music into Arduino code.
Step 4: Arduino Mini-Shields
A few months ago I posted a project about my Arduino Mini-Shields. When I started making the apple rise with the servo, I thought of using a motor controller shield that I have, which has a few connectors for servos, but the wiring is very easy for a servo so I though I could simplify everything and avoid future pin conflicts by making it into a new mini-shield.
Step 5: Twitter Code - PHP
Part of the reason I decided to make this model was that I seen so many Twitter reactive Arduino projects over last few years. I had no idea until I started digging deeper that Twitter had discontinued their RSS feeds with the introduction of version 1.1 of their API. Initially I thought I could still parse the HTML of the Mets twitter page for the information I wanted, but it seems like the Arduino's memory is too small to read a fully formatted website's page source.
I didn't want to have to sign up to use the new API and deal with their authentication tokens, so I tried to parse the Mets Twitter page as a visitor (not logged in or authenticated). I tried writing something in Perl and PHP, but the class structure was a little too hard for me to predict, so I tried a different approach.
I found this guide on StackOverflow that was easily the best guide I found on how to do this. I signed up for the new Twitter API and spent some time creating a PHP page that would boil down a team's Twitter feed to just a text list of the tweets containing "HR", "Home Run", "Homerun", or "Slam". The PHP script worked out better than I thought and works fairly seamlessly with the Arduino. I like it because I can have my web host do the heavy lifting of parsing out the important details and leave the Arduino to just request the consolidated data feed every 30 seconds or so. The PHP script will only request the Twitter feed when it's called by the Arduino sketch so there is no trouble with syncing the timing of the script and Arduino.
I decided to be nice and make the PHP script universal for any baseball team you want. When the Arduino calls the URL for the PHP script, you can just call the script, or you can append some GET variables to the end of the URL and that will affect the output.
You can see an example of the attached PHP code here. If you don't have a webhost, there are a bunch of free webhosts around that offer PHP script support.
Step 6: *Arduino Code
I used the Textfinder and Ethernet examples included with those libraries, along with the help pages on the Arduino site to parse the simplified Twitter feed from my PHP script. The GET variables that I pass to the PHP script tell it to show a different team or only tweets since the last one it reported. This prevents the apple from popping up multiple times for the same home run. I am really happy with the way these scripts work together.
Step 7: Finished Apple & Top Hat
Once I had everything working and connected, I upgraded the apple to my newer, rounder model. This one doesn't pop up as quickly or as high, probably because it is a lot heavier and there are more bricks in the sleeve and collar to rub against each other. I may still upgrade to a lighter apple model when I finally get around to buying a 3d printer, but for now this works. The Arduino/Twitter connection turned out to be more of a hassle than I expected, but I was able to put all of this together without spending too much money on parts. In the future I might try to use an Electric Imp for this.
Here is a test when it was looking at my Twitter account:
Here is a test with the servo installed in the new and improved apple: