SHARE
TWEET

TweetLED Code

a guest Jan 11th, 2013 1,243 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. TweetLED Code
  3.  
  4. This code is based on the code made by Tom Igoe and is used in episode 3 of the Mike's Lab Series on Youtube.
  5.  
  6.  */
  7. #include <SPI.h>
  8. #include <Ethernet.h>
  9.  
  10.  
  11. // Enter a MAC address and IP address for your controller below.
  12. // The IP address will be dependent on your local network:
  13. byte mac[] = {
  14.   0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };
  15. IPAddress ip(192,168,1,20);
  16.  
  17. // initialize the library instance:
  18. EthernetClient client;
  19.  
  20. const int requestInterval = 30000;  // delay between requests
  21.  
  22. char serverName[] = "api.twitter.com";  // twitter URL
  23.  
  24. boolean requested;                   // whether you've made a request since connecting
  25. long lastAttemptTime = 0;            // last time you connected to the server, in milliseconds
  26.  
  27. String currentLine = "";            // string to hold the text from server
  28. String tweet = "";                  // string to hold the tweet
  29. boolean readingTweet = false;       // if you're currently reading the tweet
  30.  
  31. void setup() {
  32.   // reserve space for the strings:
  33.   pinMode(7,OUTPUT);
  34.   pinMode(8,OUTPUT);
  35.   currentLine.reserve(256);
  36.   tweet.reserve(150);
  37.  
  38. // initialize serial:
  39.   Serial.begin(9600);
  40.   // attempt a DHCP connection:
  41.   if (!Ethernet.begin(mac)) {
  42.     // if DHCP fails, start with a hard-coded address:
  43.     Ethernet.begin(mac, ip);
  44.   }
  45.   // connect to Twitter:
  46.   connectToServer();
  47. }
  48.  
  49.  
  50.  
  51. void loop()
  52. {
  53.   if (client.connected()) {
  54.     if (client.available()) {
  55.       // read incoming bytes:
  56.       char inChar = client.read();
  57.  
  58.       // add incoming byte to end of line:
  59.       currentLine += inChar;
  60.  
  61.       // if you get a newline, clear the line:
  62.       if (inChar == '\n') {
  63.         currentLine = "";
  64.       }
  65.       // if the current line ends with <text>, it will
  66.       // be followed by the tweet:
  67.       if ( currentLine.endsWith("<text>")) {
  68.         // tweet is beginning. Clear the tweet string:
  69.         readingTweet = true;
  70.         tweet = "";
  71.       }
  72.       // if you're currently reading the bytes of a tweet,
  73.       // add them to the tweet String:
  74.       if (readingTweet) {
  75.         if (inChar != '<') {
  76.           tweet += inChar;
  77.         }
  78.         else {
  79.           // if you got a "<" character,
  80.           // you've reached the end of the tweet:
  81.           readingTweet = false;
  82.           Serial.println(tweet);  
  83.           // close the connection to the server:
  84.           client.stop();
  85.         }
  86.       }
  87.     }  
  88.   }
  89.   else if (millis() - lastAttemptTime > requestInterval) {
  90.     // if you're not connected, and two minutes have passed since
  91.     // your last connection, then attempt to connect again:
  92.     connectToServer();
  93.   }
  94.   if(tweet==">ledon")
  95.    {
  96.       digitalWrite(8,HIGH);
  97.    }
  98.    else if(tweet==">ledoff"){
  99.  
  100.     digitalWrite(8,LOW);
  101.    }
  102. }
  103.  
  104. void connectToServer() {
  105.   // attempt to connect, and wait a millisecond:
  106.   Serial.println("Connecting to server");
  107.   if (client.connect(serverName, 80)) {
  108.     Serial.println("Making HTTP Request");
  109.   // make HTTP GET request to twitter:
  110.     client.println("GET /1/statuses/user_timeline.xml?screen_name=mpieters95&count=1 HTTP/1.1");
  111.     client.println("HOST: api.twitter.com");
  112.     client.println();
  113.   }
  114.   // note the time of this connect attempt:
  115.   lastAttemptTime = millis();
  116. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top