Merge branch 'master' of github.com:arduino/wifishield

This commit is contained in:
Mimmo La Fauci 2012-03-04 11:47:53 +01:00
commit 15d7873d24
1 changed files with 199 additions and 0 deletions

View File

@ -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 <text>this is a tweet</text>
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 <SPI.h>
#include <WiFi.h>
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 <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(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();
}