Controlling Arduino With twitter

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
greggawatt
 
Posts: 2
Joined: Tue Dec 28, 2010 12:14 am

Controlling Arduino With twitter

Post by greggawatt »

I'm trying to do a simple control of an arduino using twitter. The arduino would be connected to a laptop and be able to make and led go high or low with the simple tweeting of "led on" or "led off"

What is the best way to go about this? I've been looking at all kinds of options, code examples are much appreciated.

User avatar
richms
 
Posts: 558
Joined: Tue Jan 20, 2009 3:05 am

Re: Controlling Arduino With twitter

Post by richms »

I briefly tried using twitter to go the other way, and something I kept running into was twitters refusal to take something if I had already tweeted it recently, Oops you already said that from the web interface and something else from the API. Adding a timestamp solved it.

Something to keep in mind if you are planning on simple commands like LED on and LED off.

greggawatt
 
Posts: 2
Joined: Tue Dec 28, 2010 12:14 am

Re: Controlling Arduino With twitter

Post by greggawatt »

yeah there are tons of projects on how to post TO twitter, but i want to read FROM twitter

User avatar
jdubrow
 
Posts: 32
Joined: Tue Dec 21, 2010 1:49 pm

Re: Controlling Arduino With twitter

Post by jdubrow »

I would write a piece of software on the computer* to query twitter peridically for the twitter 'channel', then serer that up on your webserver, then put an ehternet connection on your arduino then have it query your web server.

* I would do it this way so if twitter updates you don't have to reprogram your device to understand how twitter works (or does it have an RSS feed?)

pdxNat
 
Posts: 16
Joined: Wed Apr 28, 2010 8:34 am

Re: Controlling Arduino With twitter

Post by pdxNat »

Accepting commands from Twitter is a great idea. In general, you'll need to use a program on your computer to listen to Twitter, to parse the status updates, to scan those updates for keywords, and to pass those keywords to Arduino through Serial port write commands. I've got two projects that do all that except for the final step of writing back to Arduino. I have other projects written in Processing that do it, and I can dig 'em out if you like, but you may have already found examples.

User avatar
jchristensen
 
Posts: 111
Joined: Mon Jan 03, 2011 11:45 am

Re: Controlling Arduino With twitter

Post by jchristensen »

An Arduino/Twitter project was featured in the June issue of IEEE Spectrum:

http://spectrum.ieee.org/geek-life/hand ... ice-door/0

pdxNat
 
Posts: 16
Joined: Wed Apr 28, 2010 8:34 am

Re: Controlling Arduino With twitter

Post by pdxNat »

And here is the Tweet-a-Pot, an Arduino Python project that accepts commands from Twitter to control a power supply.

It uses Python on the computer to interact w/ Twitter, though you could do this with Processing, Java, etc.

pdxNat
 
Posts: 16
Joined: Wed Apr 28, 2010 8:34 am

Re: Controlling Arduino With twitter

Post by pdxNat »

greggawatt wrote:I'm trying to do a simple control of an arduino using twitter. The arduino would be connected to a laptop and be able to make and led go high or low with the simple tweeting of "led on" or "led off"

What is the best way to go about this? I've been looking at all kinds of options, code examples are much appreciated.
Here is your answer:

I've posted an example I call the Twitter Mention Mood Light up on instructables that allows Twitter to command the LED on your Arduino. I even wrote it twice, once in Processing and once in Python.

Specifically, it uses the Processing or Python code to check Twitter ever 15 seconds for any mention of @yourUsername. Upon finding a mention, the Processing or Python code sends a message to Arduino through the Serial.write() command. Arduino evaluates the message and if the message meets certain criteria then Arduino tells the led light to blink.

Visit Twitter Mention Mood Light for complete details, but here are a few highlights:

Arduino code for receiving messages over Serial:

Code: Select all

void setup(){ // begin
  Serial.begin(9600);
}
void loop(){
  listenToSerial();
}
void listenToSerial(){ // Twitter commands enter here
  int serialMsg = 0;
  if (Serial.available()){
    serialMsg = Serial.read();
    if (serialMsg == 1) state = "mention"; // processing 
    if (serialMsg == 49) state = "mention"; // python 
  }
}
Processing code for sending a message to Arduino over Serial looks something like this:

Code: Select all

import processing.serial.*; // this appears near top
Serial arduino; // near top, sets "arduino" equal to action known as Serial
void setup() {
  println(Serial.list()); // do this to learn what ports to use
  String arduinoPort = Serial.list()[0]; // Set the Serial Port here
  arduino = new Serial(this, arduinoPort, 9600); // Activate the Serial Port connection here
  writeToArduino();
}
void writeToArduino(){
  arduino.write(1); // The Arduino receives a 1
}
Python code for sending a message to Arduino over Serial looks something like this:

Code: Select all

 import serial
arduino = serial.Serial('COM4', 9600, timeout=1)
def writeToArduino():
  arduino.write(1)
while 1:
  writeToArduino()
Notice that Processing and Python both send the same value arduino.write(1) but Arduino receives different values. I think Python's message is turned into Ascii or something, I'm not really sure how or why, but I know it's reliable. I don't know how to pass a string value (like the word "mention") from Python to Arduino, but that'd be ideal because then the Arduino code could be more generic. Instead of two if-statements (one for Processing and one for Python) it could have one if-statement if (serialMsg == "mention") state = "mention"; If anyone knows how to do this, please comment with the answer below.

Hope that helps!

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”