This example shows you how to answer a HTTP request using a 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.

Hardware Required

Arduino WiFi Shield

Shield-compatible Arduino board

Circuit

The WiFi shield uses pins 10, 11, 12, and 13 for the SPI connection to the HDG104 module. Digital pin 4 is used to control the slave select pin on the SD card.

You should have access to a 802.11b/g wireless network that connects to the internet for this example. You will need to change the network settings in the sketch to correspond to your particular networks SSID.

For networks using WPA/WPA2 Personal encryption, you need the SSID and password. The shield will not connect to networks using WPA2 Enterprise encryption.

WEP network passwords are hexadecimal strings known as keys. A WEP network can have 4 different keys; each key is assigned a "Key Index" value. For WEP encrypted networks, you need the SSID, the key, and key number.

// 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(199,59,149,200); // numeric IP for api.twitter.comchar server[]="api.twitter.com";// name address for twitter API

boolean requested;// whether you've made a request since connectingunsignedlong 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 tweetboolean readingTweet =false;// if you're currently reading the tweet

voidsetup(){// reserve space for the strings:
currentLine.reserve(256);
tweet.reserve(150);//Initialize serial and wait for port to open:Serial.begin(9600);while(!Serial){;// wait for serial port to connect. Needed for Leonardo only}

// check for the presence of the shield:if(WiFi.status()== WL_NO_SHIELD){Serial.println("WiFi shield not present");// don't continue:while(true);}

// attempt to connect to Wifi network:while( status != WL_CONNECTED){Serial.print("Attempting to connect to SSID: ");Serial.println(ssid);// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);

// 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 ="";// break out of the loop so this character isn't added to the tweet:return;}// 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();}}}}elseif(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("making HTTP request...");// make HTTP GET request to twitter:
client.println("GET /1/statuses/user_timeline.xml?screen_name=arduino HTTP/1.1");
client.println("Host: api.twitter.com");
client.println("Connection: close");
client.println();}// note the time of this connect attempt:
lastAttemptTime =millis();}