From e6dbda6825341ef8ec3ad806677cbcd25204f139 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Fri, 2 Mar 2012 20:01:57 -0500 Subject: [PATCH] Added Twitter client (in progress, not working) --- WiFi/examples/TwitterClient/TwitterClient.ino | 199 ++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 WiFi/examples/TwitterClient/TwitterClient.ino diff --git a/WiFi/examples/TwitterClient/TwitterClient.ino b/WiFi/examples/TwitterClient/TwitterClient.ino new file mode 100644 index 000000000..2e1e9dd6e --- /dev/null +++ b/WiFi/examples/TwitterClient/TwitterClient.ino @@ -0,0 +1,199 @@ +/* + Twitter Client with Strings + + This sketch connects to Twitter using using an Arduino WiFi shield. + It parses the XML returned, and looks for this is a tweet + + This example is written for a network using WPA encryption. For + WEP or WPA, change the Wifi.begin() call accordingly. + + This example uses the String library, which is part of the Arduino core from + version 0019. + + Circuit: + * WiFi shield attached to pins 10, 11, 12, 13 + + created 15 Sep 2011 + modified 2 Mar 2012 + by Tom Igoe + + This code is in the public domain. + + */ +#include +#include + +char ssid[] = "itpsandbox"; // your network SSID (name) +char pass[] = "NYU+s0a!+P?"; // your network password (use for WPA, or use as key for WEP) +int keyIndex = 0; // your network key Index number (needed only for WEP) + +int status = WL_IDLE_STATUS; + +// initialize the library instance: +WiFiClient client; + +long lastConnectionTime = 0; // last time you connected to the server, in milliseconds +boolean lastConnected = false; // state of the connection last time through the main loop +const unsigned long postingInterval = 10* 1000; //delay between updates +//char server[] = "api.twitter.com"; +IPAddress server(199,59,148,20); +//IPAddress server(128,122,151,128); + +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 + +void setup() { + // reserve space for the strings: + currentLine.reserve(256); + tweet.reserve(150); + // initialize serial: + Serial.begin(9600); + Serial.println("Attempting to connect to WPA network..."); + Serial.print("SSID: "); + Serial.println(ssid); + + status = WiFi.begin(ssid, pass); + while ( status != WL_CONNECTED) { + status = WiFi.begin(ssid, pass); + Serial.println("Couldn't get a wifi connection"); + delay(5000); + } + Serial.println("Connected to wifi"); + printWifiData(); + printCurrentNet(); + connectToServer(); +} + + + +void loop() +{ + if (client.connected()) { + Serial.println("requested"); + 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') { + Serial.println(currentLine); + currentLine = ""; + } + // if the current line ends with , it will + // be followed by the tweet: + if ( currentLine.endsWith("")) { + // 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); + // close the connection to the server: + client.stop(); + } + } + } + } + else if (millis() - lastConnectionTime > postingInterval) { + // 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("making HTTP request..."); + // make HTTP GET request to twitter: + delay(10); + client.print("GET /1/statuses/user_timeline.xml?screen_name=arduino&count=1 HTTP/1.1\n"); + client.print("Host:api.twitter.com\n"); + client.print("Connection:close\n\n"); + // client.println(); + } + else { + // if you couldn't make a connection: + Serial.println("connection failed"); + Serial.println(); + Serial.println("disconnecting."); + client.stop(); + } + // note the time of this connect attempt: + lastConnectionTime = millis(); + lastConnected = client.connected(); +} + + +void printWifiData() { + // print your WiFi shield's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); + + // print your MAC address: + byte mac[6]; + WiFi.macAddress(mac); + Serial.print("MAC address: "); + Serial.print(mac[5],HEX); + Serial.print(":"); + Serial.print(mac[4],HEX); + Serial.print(":"); + Serial.print(mac[3],HEX); + Serial.print(":"); + Serial.print(mac[2],HEX); + Serial.print(":"); + Serial.print(mac[1],HEX); + Serial.print(":"); + Serial.println(mac[0],HEX); + +} + +void printCurrentNet() { + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print the MAC address of the router you're attached to: + byte bssid[6]; + WiFi.BSSID(bssid); + Serial.print("BSSID: "); + Serial.print(bssid[5],HEX); + Serial.print(":"); + Serial.print(bssid[4],HEX); + Serial.print(":"); + Serial.print(bssid[3],HEX); + Serial.print(":"); + Serial.print(bssid[2],HEX); + Serial.print(":"); + Serial.print(bssid[1],HEX); + Serial.print(":"); + Serial.println(bssid[0],HEX); + + // print the received signal strength: + long rssi = WiFi.RSSI(); + Serial.print("signal strength (RSSI):"); + Serial.println(rssi); + + // print the encryption type: + byte encryption = WiFi.encryptionType(); + Serial.print("Encryption Type:"); + Serial.println(encryption,HEX); + Serial.println(); +} + +