Once the Arduino is setup to be controlled within Eclipse (complete the 10 steps of the Arduino in Eclipse tutorial), one can easily control the Arduino from a single Twitter account by retrieving reply messages. Reply messages are messages sent from one Twitter account to another by using an “at” symbol followed by the recipient’s username; for example, to send the message “Hello” to John who has a username of “JohnDoe”, one would type what is between block brackets into their own Twitter account posting window [@JohnDoe Hello]. I plan to use this method to create an installation piece sometime very soon.
Twuino = Twitter controlled Arduino
I am writing this according to a Mac (I am using Mac OS 10.5.7)
Step 1: Complete the Arduino in Eclipse tutorial.
Complete steps 1-10 of the Arduino in Eclipse tutorial.
Step 2: Import the JTwitter Java Library
Download the jtwitter.jar file from Winterwell Associates. (the following is written as if the jtwitter.jar file is located on the desktop)
Right click on the TestArduino folder under the Package Explorer column on the left side of Eclipse, then click:
IMPORT > GENERAL > FILE SYSTEM
Click next. Click “browse . . . ” and click on the Desktop . . .
Click “Open.” Then, click on the “desktop” folder on the left column to highlight it and put a check next to jtwitter.jar on the right column.
Click “Finish.” You should now see jtwitter.jar under your project in the package explorer:
Right-click on the jtwitter.jar file and select > BUILD PATH > ADD TO BUILD PATH.
The jtwitter.jar files should now appear with a jar icon under “Referenced Libraries.” (Note this right-click option will only show up in the Package Explorer. If you are in the Project Explorer or some other view, switch by going to WINDOW > SHOW VIEW > PACKAGE EXPLORER.):
Step 3: Change up the code.
Replace the code of the TestArduino.java file with the following (Double click on the “TestArduino.java” file under the “testarduino” package or click on the “TestArduino.java” tab in the center editor of Eclipse. Replace whatever is in the file with the following code and FILE–>SAVE (or command+s)):
package testarduino; import java.util.List; //added in order to work with the lists returned from Twitter import processing.core.PApplet; import testarduino.Arduino; import winterwell.jtwitter.Twitter; public class TestArduino extends PApplet { //this is the main of the java program (where the program begins) //this simply tells java to run the PApplet (the Processing applet) //so really Processing is just running inside java (make sure to choose run as Java Application) public static void main(String args[]) { //package.javafilename PApplet.main(new String[] { "testarduino.TestArduino" }); // if you put the following, it will put it in full screen: // PApplet.main(new String[] { "--present", "arduinotest.ArduinoTest" }); } Arduino arduino; //create an arduino object int pin13 = 13; //create a variable for a pin on the arduino //variables needed in order to work with Twitter information List currentreplylist; String currentreplyWithname; String currentreply = "ERROR - no replies were retrieved"; //make new twitter object for a specific account username and password Twitter myTwitter = new Twitter("username", "password"); public void setup() //setup the arduino and anything else for your program { //to set the baud rate rather than go off the default (default is = 115200): //arduino = new Arduino(this, Arduino.list()[0], 57600); //otherwise,this is how to set up using default: arduino = new Arduino(this, Arduino.list()[0]); //setup if the pins are input or output arduino.pinMode(pin13, Arduino.OUTPUT); } public void draw() //this "draw" function; just loops until you shut off the program { //get the last 20 replys from the twitter account in list form currentreplylist = (myTwitter.getReplies()); //remove the first reply off the list (which is the most recent reply sent) currentreplyWithname = (currentreplylist.remove(0)).toString(); //remove the @ and username (and one space) from the begining of the reply in //order to reveal the reply alone currentreply=(currentreplyWithname.replaceAll("@username ","")); //print out the reply just to see if it is working println(currentreply); if (currentreply.equals("LED")) { arduino.digitalWrite(pin13, Arduino.HIGH); //LED will turn on for 5 seconds delay(5000); arduino.digitalWrite(pin13, Arduino.LOW); //then be turned off delay(10); } else if (currentreply.equals("ALLOFF")) { arduino.digitalWrite(pin13, Arduino.LOW); delay(1000); //delay a second before looping through draw() again } else { //do nothing } } //now it will go back and check the Twitter account again } //end of TestArduino.java class
Make sure you replace “username” and “password” on line 29 to whatever Twitter account you are retrieving the reply messages (messages sent TO this account). Also replace “username” on line 50 to the correct name (make sure it has a space after the username before the quotation mark).
Step 4: Hook up the Arduino
The Arduino should already have the Standard Firmata sketch uploaded onto it’s chip (Step 1 of the Arduino in Eclipse tutorial).
Put an LED in pin 13 of the Arduino (put the long leg of the LED in pin 13 and the short leg in the GND right next door to pin 13).
Plug the Arduino in the USB of the computer.
Step 5: Run the program
From the top of the screen click RUN–> RUN
You will be asked if you want to run it as a “Java Applet” or a “Java Application” . . . make sure you choose “Java Application.”
A small applet should show up on the screen like this:
Step 6: Twitter
In order for this to work (since it is set up to retrieve reply messages), you need to send a message from a separate Twitter account than you setup for this program. So, if the account you setup in this program is JohnDoe, then you must send what is between these block brackets [@JohnDoe LED] from a Twitter account other than the JohnDoe account. Once you send the message [@username LED], the LED on the Arduino’s pin 13 should light up for 5 seconds then go off for a little bit while the program checks if another message has been sent. If you reply [@username ALLOFF], the LED will stay off.
Close the small applet window to stop the program – if you close the window when the LED is on, that action will be frozen in the Arduino and the LED will remain on as long as the Arduino is plugged into the computer or some new message is sent to it.
Checkout the Arduino.java file to see how to utilize input and output and PWM (it is not difficult at all to do PWM – you just use analogWrite rather than digitalWrite and use a number between 0 and 255 instead of Arduino.LOW or Arduino.HIGH).




