Pages: [1]   Go Down
Author Topic: Controlling Arduino Uno with Twitter Posts  (Read 948 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I am trying to control 3 coloured LED's (blue, white, yellow) via twitter posts such as "blue on" etc.

I am using an Arduino Uno with Wifly shield and my code is based on this:

 https://github.com/dalequark/Arduino-WiFly-Twitter/blob/master/arduino_wifly_twitter.ino

So far I have managed to control the lights via posts on my own twitter account, but I would like others to be able to control the lights too, by mentioning my twitter name, then writing a command.

Here is my code:

#
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 = 100;  // 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);
  }
}


I tried changing the GET command to include "home_timeline", rather than "user_timeline" to include other peoples posts but that stopped the serial monitor from reading any posts,it just kept printing:

connecting to server...
making HTTP request...


on repeat.

Anyone got any suggestions?

thanks,

Nicky
« Last Edit: October 30, 2012, 01:40:34 pm by Nhume » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8975
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I think that only looks at tweets FROM 'username'.  You want mentions.

https://dev.twitter.com/docs/api/1/get/statuses/mentions

You're going to have to authenticate to get access to your mentions.

You might be able to use the search api to look for your username:
https://dev.twitter.com/docs/api/1.1/get/search/tweets
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks John,

I thought it might be something to do with authentication. I have registered my twitter as an app at

https://dev.twitter.com/apps

but am unsure as to how to enter this information in to the code. There are so many different numbers I'm not sure which to use. Have you ever seen an example of someone using authentication for a similar application? I can only find examples for projects that use arduino to tweet, rather than using twitter to control an arduino.

Thanks,

Nicky
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8975
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I did a quick Google search and found only Python and Ruby programs that run on a PC and forward data to the Arduino.

I guess you have to study the Twitter API and figure out how to authenticate.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Pages: [1]   Go Up
Jump to: