How to Turning ON/OFF LED with twitter?
- You must have twitter acount.
- Prepare Ethernet Shield + Arduino Uno. (or etc).
- LED, UTP Cable with RJ45.
- Connect Anode LED to PIN DIGITAL 8 in Arduino UNO, and Katode LED to GND.
- You must connect Ethernet Shield to Internet with UTP Cable. You can conncet internet with internet sharing in your PC.
- Let's Code Here.
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(31,220,16,46);Â // numeric IP for apkit (no DNS)
char server[] = "apkit.id";Â Â // name address for apkit (using DNS)
IPAddress ip(192,168,137,177);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
const unsigned long requestInterval = 10000;
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
String lasttweet = "";
boolean readingTweet = false;
int led=8; //LED on PIN 8;
String username="username"; //twitter username to read target status.
String access_token="Your access Token";//access token your account can be found in http://apkit.esy.es/twitteroauth/request/
String access_token_secret="Your Access Token Secret";//access token secret your account can be found in http://apkit.id/twitteroauth/request/
String count_status="1";
void setup() {
 //declare led or pin 3 to OUTPUT
 pinMode(led,OUTPUT);
 digitalWrite(led,LOW);
 // Open serial communications and wait for port to open:
 Serial.begin(9600);
 // start the Ethernet connection:
 Serial.println("Autoconfigure Ethernet using DHCP");
 if (Ethernet.begin(mac) == 0) {
  Serial.println("Failed to configure Ethernet using DHCP");
  // no point in carrying on, so do nothing forevermore:
  // try to congifure using IP address instead of DHCP:
  Ethernet.begin(mac, ip);
 }
 Serial.print("My address:");
 Serial.println(Ethernet.localIP());
 // give the Ethernet shield a second to initialize:
 delay(1000);
 Serial.println("connecting...");
 // if you get a connection, report back via serial:
 connectToServer();
}
void loop()
{
 if (client.connected()) {
  if (client.available()) {
   // read incoming bytes:
   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 the current line ends with [text] => , it will
   // be followed by the tweet:
   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("Your Status:");Â
     tweet.trim();
     Serial.println(tweet);
     if (tweet=="LED TURN ON"){
      Serial.println("Action :");
      Serial.println("LED ON");
      digitalWrite(led,HIGH);
     }
     if (tweet=="LED OFF){
      Serial.println("Action :");
      Serial.println("LED TURN OFF");
      digitalWrite(led,LOW);
     }
     // 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(server, 80)) {
  Serial.println("connected");
  // Make a HTTP request:
  client.println("GET /twitteroauth/status.php?username=" + username + "&access_token=" + access_token + "&access_token_secret=" + access_token_secret + "&count=" + count_status + " HTTP/1.1");
  client.println("Host: apkit.id");
  client.println("Connection: close");
  client.println();
 }
 else {
  // kf you didn't get a connection to the server:
  Serial.println("connection failed");
 }
 // note the time of this connect attempt:
 lastAttemptTime = millis();
}
You must put your twitter access token in that code.
You can get your twitter acces acount in this link http://apkit.id/twitteroauth/request/.
- Compile and Upload in Your Arduino.
- You tweet LED ON or LED OFF in twitter. And LED will be turn on or turn off.
Thanks.
I hope this is useful
LEDAndTwitter.ino (4.55 KB)