How to hack a doorbell and connect it to Twitter - Part 6: Using an Arduino to search on Twitter

Besides detecting the signal with the "sniffer" sketch, this part took me the most time to figure out. It turns out that Twitter just recently decided that you can't use a HTTP GET command of the REST API v1.0 anymore. Developers must now use v1.1, which means you have to use OAuth even if you want to search for tweets. Since just recently, Twitter offers applications the ability to issue authenticated requests on behalf of the application itself (as opposed to on behalf of a specific user). This is called Application-only authentication, but the drawback of this is that you have to connect to the Twitter servers through SSL. Unfortunately, an Arduino isn't capable of doing this because it hasn't enough recources to do so.

So this is what I came up with: I created a PHP script on my own server which makes the request, parses the JSON response and outputs the search result. I now make the Arduino connect to my own server instead of the Twitter servers.

This is the PHP script:

 "",
    'oauth_access_token_secret' => "",
    'consumer_key' => "",
    'consumer_secret' => ""
);

$url = 'https://api.twitter.com/1.1/search/tweets.json';
$getfield = '?q=%23ArduinoDoorbell&count=1&include_entities=false'; // Change this GET field BEFORE calling buildOauth();
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$json = $twitter->setGetfield($getfield)
                ->buildOauth($url, $requestMethod)
                ->performRequest();
//echo $json;
$obj = json_decode($json, true);
$tweet = $obj["statuses"][0]["text"];
echo "$tweet";
$user = $obj["statuses"][0]["user"]["screen_name"];
echo "@$user";
?>

As you can see, this script echoes the most recent tweet with hashtag #ArduinoDoorbell between tags and the screen name of the user who tweeted between tags.

This is the accompanying Arduino sketch which connects to the PHP script. It uses the TextFinder library to search for the text between the <tweet> tags on the page on my server.

#include 
#include 
#include 

char ssid[] = "";
char pass[] = ""; 

WiFiClient client;
char tweet[140] = "", oldTweet[140] = "";

char host[] = "doorbell.crutzen.eu"; // replace this with your own hostname

void foundHashtag()
{
  Serial.println("Found the hashtag!");
}

void setup() {
  WiFi.begin(ssid, pass);
  Serial.begin(9600);

  for(int i = 0; i < 140; i++) {
    oldTweet[i] = 0;
    tweet[i] = 0;
  }
}

void loop() {

  int i;
  
  Serial.println("Connecting to server...");
  if (client.connect(host, 80)) { // replace this 
  
    client.println("GET /index.php HTTP/1.1");
    client.print("Host: ");
    client.println(host);
    client.println("User-Agent: Arduino/1.0");
    client.println();

    TextFinder finder(client);
    while (client.connected()) {
      if (client.available()) {
        if (finder.getString("", "", tweet, 140) != 0) {
          for (i = 0; i < 140; i++)
              if(oldTweet[i] != tweet[i])
                break;
          if (i != 140) {
            Serial.println(tweet);
            foundHashtag();
            for (i = 0; i < 140; i++)
              oldTweet[i] = tweet[i];
            break;
          }
        }
      }
    }
    delay(1);
    client.stop();
  }
  Serial.println("delay...");
  delay (40000);   // don't make this less than 30000 (30 secs), because you can't connect to the twitter servers faster (you'll be banned)
}

Sources:

Share Button

3 gedachten over “How to hack a doorbell and connect it to Twitter - Part 6: Using an Arduino to search on Twitter”

    1. Your code was very useful, although it didn't work anymore because of Twitter switching to API v1.1. But it was a really good starting point; thanks very much!

      By the way: your candy machine is a brilliant idea!!! - Wish I had it 🙂

Geef een antwoord

Het e-mailadres wordt niet gepubliceerd.

Deze site gebruikt Akismet om spam te verminderen. Bekijk hoe je reactie-gegevens worden verwerkt.