How to hack a doorbell and connect it to Twitter - Part 5: Using an Arduino to post to Twitter

With the Twitter library for Arduino from the Arduino Playground you can make it post a message to Twitter.

First, follow this link to get a token to post a message using OAuth and to add some Libraries to your Arduino IDE. Then test it with this sketch:

#include  // needed in Arduino 0019 or later
#include 
#include 


char ssid[] = ""; // Your WiFi network name
char pass[] = ""; // Your WiFi network password

// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
Twitter twitter("");

// Message to post
char msg[] = "Hello world! This is your #Arduino tweeting!";

void setup()
{
  delay(1000);
  WiFi.begin(ssid, pass);
  Serial.begin(9600);
  
  int status = -1;
  while (status != 200) {
    Serial.print("Message: ");
    Serial.println(msg);
    Serial.println("Connecting to Twitter...");
    if (twitter.post(msg)) {
      status = twitter.wait(&Serial);
      if (status == 200) {
        Serial.println("OK.");
      } else {
        Serial.print("Failed : code ");
        Serial.println(status);
        waitToContinue(30);
      }
    } else {
      Serial.println("Connection failed.");
    }
  }
}

void waitToContinue(int duration) {
  int step = 5;
  for(int i = duration; i>0; i-=step) {
    Serial.print("Waiting "); Serial.print(i); Serial.println(" seconds...");
    delay(step*1000);
  }
}

void loop()
{
}

Sources:

Share Button

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *

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