Arduino controlled by Twitter commands


#1

Hi,

I’m trying to make a twitter app that uses tweets to control lights on an arduino micro controller. The arduino is linked with a wifi receiver and so I want the whole device to work without python or Ruby as its a stand alone project and won’t be used with a computer. So far I am able to control them via my own Tweets using this GET command:

“GET /1/statuses/user_timeline.xml?screen_name=”+twitterUsername + “&count=1 HTTP/1.1”

However, I would like other people to be able to control them too, using a hashtag or maybe by mentioning my username. I tried changing my GET command to this:

“GET /1/statuses/user_timeline.xml?screen_name=”+twitterUsername + “&count=1 HTTP/1.1”

but it didn’t work. I understand that I need to use OAuth to access this information. I have registered my twitter account as an app but am unsure as to how to integrate the OAuth information with my arduino code. Which numbers do I need and where?

DOes anyone have any experience of this?

Thanks,

Nicky


#2

Here’s my current code:

#define yellow 3 #define blue 4 #define white 5 #include "WiFly.h" String twitterUsername = "NickysLights"; //set the twitter user you'd like to get here.

char ssid[] = “…”; //enter your ssid here.
char passphrase[] = “…”; //enter your passphrase here.

const unsigned long requestInterval = 1000; // delay between requests
boolean requested; // whether you’ve made a request since connecting
unsigned long lastAttemptTime = 0; // last time you connected to the server, in milliseconds

String currentLine = “”; // string to hold the text from server
String tweet = “”; // string to hold the tweet
boolean readingTweet = false; // if you’re currently reading the tweet

Client client(“api.twitter.com”, 80);

void setup() {
currentLine.reserve(256);
tweet.reserve(150);

Serial.begin(9600);

WiFly.begin();

//Define LED pin Modes
pinMode(yellow, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(white, OUTPUT);

//set initial values for leds as low

digitalWrite(yellow, LOW);
digitalWrite(blue, LOW);
digitalWrite(white, LOW);

if (!WiFly.join(ssid,passphrase)) {
Serial.println(“Association failed.”);
while (1) {
// Hang on failure.
}
}

Serial.println(“connecting…”);

}

void loop() {
if(client.connected()){
if (client.available()) {
char inChar = client.read();

  // add incoming byte to end of line:
  currentLine += inChar; 

  // if you get a newline, clear the line:
  if (inChar == '\n') {
    currentLine = "";
  }
  
  if ( currentLine.endsWith("<text>")) {
    // tweet is beginning. Clear the tweet string:
    readingTweet = true; 
    tweet = "";
  }
  // if you're currently reading the bytes of a tweet,
  // add them to the tweet String:
  if (readingTweet) {
    if (inChar != '<') {
      tweet += inChar;
    } 
    else {
      // if you got a "<" character,
      // you've reached the end of the tweet:
      readingTweet = false;
      Serial.println(tweet);  
     
      char tweet = Serial.read();

checkAction();
// close the connection to the server:
client.stop();
}

  }
}

}
else if (millis() - lastAttemptTime > requestInterval) {
// if you’re not connected, and two minutes have passed since
// your last connection, then attempt to connect again:
connectToServer();
}
}

void connectToServer() {
// attempt to connect, and wait a millisecond:
Serial.println(“connecting to server…”);
if (client.connect()) {
Serial.println(“making HTTP request…”);
// make HTTP GET request to twitter:

client.println(“GET /1/statuses/user_timeline.xml?screen_name=”+twitterUsername + “&count=1 HTTP/1.1”);

client.println(“HOST: api.twitter.com”);
client.println();
}
// note the time of this connect attempt:
lastAttemptTime = millis();
}
void checkAction() {
if (tweet == “>yellow on”)
{
digitalWrite(yellow, HIGH);
digitalWrite(blue, LOW);
digitalWrite(white, LOW);

}
else if (tweet == “>blue on”)
{
digitalWrite(blue, HIGH);
digitalWrite(yellow, LOW);
digitalWrite(white, LOW);
}
else if (tweet == “>white on”)
{
digitalWrite(white, HIGH);
digitalWrite(blue, LOW);
digitalWrite(yellow, LOW);
}
else if (tweet == “>all off”)
{
digitalWrite(yellow, LOW);
digitalWrite(blue, LOW);
digitalWrite(white, LOW);
}

else if (tweet == “>all on”)
{
digitalWrite(yellow, HIGH);
digitalWrite(blue, HIGH);
digitalWrite(white, HIGH);
}
}


Did the oAuth update break Arduino code for reading tweets?
#3

and How to get mentions or hastag from twitter using arduino…?

HTTP req…>>
user tweet = GET /1/statuses/user_timeline.xml?screen_name="+twitterUsername + "&count=1 HTTP/1.1

user mentions == can u give me example for my question??


#4

hi~~


#5

Hello,

i’ve got the same Problem as @NickysLights.
The GET-Question doesn’t work without oAuth and i don’t know how to implement the oAuth with my Arduino. I tried several examples (e.g. http://hofmannsven.com/2013/laboratory/arduino-twitter-library/ ) but the method with such an access token doesn’t work too.

Please, could anybody help us?