diff --git a/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino b/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino index 20cf86bc0..2b0e3ae11 100644 --- a/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino +++ b/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino @@ -4,6 +4,9 @@ This sketch connects an analog sensor to Pachube (http://www.pachube.com) using an Arduino Wifi shield. + This example is written for a network using WPA encryption. For + WEP or WPA, change the Wifi.begin() call accordingly. + This example has been updated to use version 2.0 of the Pachube.com API. To make it work, create a feed with a datastream, and give it the ID sensor1. Or change the code below to match your feed. @@ -12,7 +15,7 @@ * Analog sensor attached to analog in 0 * Wifi shield attached to pins 10, 11, 12, 13 - created 2 March 2012 + created 4 March 2012 by Tom Igoe This code is in the public domain. @@ -22,6 +25,7 @@ #include #include + #define APIKEY "YOUR API KEY GOES HERE" // replace your pachube api key here #define FEEDID 00000 // replace your feed ID #define USERAGENT "My Project" // user agent is the project name @@ -38,15 +42,20 @@ boolean lastConnected = false; // state of the connection last time through const int postingInterval = 10000; //delay between updates to Pachube.com void setup() { - // start serial port: Serial.begin(9600); - status = WiFi.begin(ssid, pass); + Serial.println("Attempting to connect to Wifi network..."); + Serial.print("SSID: "); + Serial.println(ssid); + + status = WiFi.begin(ssid, pass); if ( status != WL_CONNECTED) { Serial.println("Couldn't get a wifi connection"); + // don't do anything else: while(true); } else { Serial.println("Connected to wifi"); + printWifiStatus(); } } @@ -86,7 +95,7 @@ void sendData(int thisData) { // if there's a successful connection: if (client.connect("www.pachube.com", 80)) { Serial.println("connecting..."); - // send the HTTP PUT request: + // send the HTTP PUT request: client.print("PUT /v2/feeds/"); client.print(FEEDID); client.println(".csv HTTP/1.1"); @@ -107,20 +116,20 @@ void sendData(int thisData) { client.println("Connection: close\n"); // here's the actual content of the PUT request: - client.print("sensor1,"); + client.print("sensor1,"); client.println(thisData); // note the time that the connection was made: lastConnectionTime = millis(); } else { - // if you couldn't make a connection: + // if you couldn't make a connection: Serial.println("connection failed"); Serial.println(); Serial.println("disconnecting."); client.stop(); - lastConnected = client.connected(); } + lastConnected = client.connected(); } @@ -144,3 +153,23 @@ int getLength(int someValue) { return digits; } +void printWifiStatus() { + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your WiFi shield's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + + // print the received signal strength: + long rssi = WiFi.RSSI(); + Serial.print("signal strength (RSSI):"); + Serial.print(rssi); + Serial.println(" dBm"); +} + + + + + diff --git a/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino b/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino index 0f0697e0b..8c655d9d2 100644 --- a/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino +++ b/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino @@ -4,6 +4,13 @@ This sketch connects an analog sensor to Pachube (http://www.pachube.com) using a Arduino Wifi shield. + This example is written for a network using WPA encryption. For + WEP or WPA, change the Wifi.begin() call accordingly. + + This example has been updated to use version 2.0 of the Pachube.com API. + To make it work, create a feed with a datastream, and give it the ID + sensor1. Or change the code below to match your feed. + This example uses the String library, which is part of the Arduino core from version 0019. @@ -11,7 +18,7 @@ * Analog sensor attached to analog in 0 * Wifi shield attached to pins 10, 11, 12, 13 - created 2 March 2012 + created 4 March 2012 by Tom Igoe This code is in the public domain. @@ -39,7 +46,7 @@ const int postingInterval = 10000; //delay between updates to Pachube.com void setup() { // start serial port: Serial.begin(9600); - status = WiFi.begin(ssid, pass); + status = WiFi.begin(ssid, pass); if ( status != WL_CONNECTED) { Serial.println("Couldn't get a wifi connection"); while(true); @@ -53,9 +60,9 @@ void loop() { // read the analog sensor: int sensorReading = analogRead(A0); // convert the data to a String to send it: - + String dataString = "sensor1,"; - dataString += sensorReading; + dataString += sensorReading; // you can append multiple readings to this String if your // pachube feed is set up to handle multiple values: @@ -125,3 +132,5 @@ void sendData(String thisData) { lastConnected = client.connected(); } } + + diff --git a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino new file mode 100644 index 000000000..4758041ac --- /dev/null +++ b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino @@ -0,0 +1,143 @@ +/* + Wifi 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 4 Mar 2012 + by Tom Igoe + + This code is in the public domain. + + */ +#include +#include + +char ssid[] = "YourNetwork"; // your network SSID (name) +char pass[] = "password"; // your network password (use for WPA, or use as key for WEP) +int keyIndex = 0; // your network key Index number (needed only for WEP) + + +// initialize the library instance: +WiFiClient client; + +const unsigned long requestInterval = 30*1000; // delay between requests; 30 seconds + +IPAddress server(199,59,149,200); // api.twitter.com + +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 +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); + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + while(true); + } + else { + Serial.println("Connected to wifi"); + printWifiStatus(); + 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 , 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() - 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=arduinoteam HTTP/1.1"); + client.println("HOST:api.twitter.com"); + client.println(); + } + // note the time of this connect attempt: + lastAttemptTime = millis(); +} + + +void printWifiStatus() { + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your WiFi shield's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + + // print the received signal strength: + long rssi = WiFi.RSSI(); + Serial.print("signal strength (RSSI):"); + Serial.print(rssi); + Serial.println(" dBm"); +} + diff --git a/WiFi/examples/WifiWebClient/WifiWebClient.ino b/WiFi/examples/WifiWebClient/WifiWebClient.ino new file mode 100644 index 000000000..d4f1ce5a1 --- /dev/null +++ b/WiFi/examples/WifiWebClient/WifiWebClient.ino @@ -0,0 +1,103 @@ + +/* + Web client + + This sketch connects to a website (http://www.google.com) + using a WiFi shield. + + This example is written for a network using WPA encryption. For + WEP or WPA, change the Wifi.begin() call accordingly. + + This example is written for a network using WPA encryption. For + WEP or WPA, change the Wifi.begin() call accordingly. + + Circuit: + * WiFi shield attached + + created 13 July 2010 + by dlf (Metodo2 srl) + modified 4 Mar 2012 + by Tom Igoe + */ + + +#include +#include + +char ssid[] = "YourNetwork"; // your network SSID (name) +char pass[] = "password"; // 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; +IPAddress server(74,125,115,105); // Google + +// 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): +WiFiClient client; + +void setup() { + Serial.begin(9600); + Serial.println("Attempting to connect to Wifi network..."); + Serial.print("SSID: "); + Serial.println(ssid); + + status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + // don't do anything else: + while(true); + } + else { + Serial.println("Connected to wifi"); + printWifiStatus(); + Serial.println("\nStarting connection to server..."); + // if you get a connection, report back via serial: + if (client.connect(server, 80)) { + Serial.println("connected to server"); + // Make a HTTP request: + client.println("GET /search?q=arduino HTTP/1.0"); + client.println(); + } + } +} + +void loop() { + // if there are incoming bytes available + // from the server, read them and print them: + if (client.available()) { + char c = client.read(); + Serial.print(c); + } + + // if the server's disconnected, stop the client: + if (!client.connected()) { + Serial.println(); + Serial.println("disconnecting from server."); + client.stop(); + + // do nothing forevermore: + while(true); + } +} + + +void printWifiStatus() { + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your WiFi shield's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + + // print the received signal strength: + long rssi = WiFi.RSSI(); + Serial.print("signal strength (RSSI):"); + Serial.print(rssi); + Serial.println(" dBm"); +} + + + + diff --git a/WiFi/examples/WifiWebServer/WifiWebServer.ino b/WiFi/examples/WifiWebServer/WifiWebServer.ino new file mode 100644 index 000000000..11c3c686d --- /dev/null +++ b/WiFi/examples/WifiWebServer/WifiWebServer.ino @@ -0,0 +1,113 @@ +/* + Web Server + + A simple web server that shows the value of the analog input pins. + using a WiFi shield. + + This example is written for a network using WPA encryption. For + WEP or WPA, change the Wifi.begin() call accordingly. + + Circuit: + * WiFi shield attached + * Analog inputs attached to pins A0 through A5 (optional) + + created 13 July 2010 + by dlf (Metodo2 srl) + modified 4 Mar 2012 + by Tom Igoe + */ + + +#include +#include + +char ssid[] = "YourNetwork"; // your network SSID (name) +char pass[] = "password"; // 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; + +WiFiServer server(80); + +void setup() { + // initialize serial: + Serial.begin(9600); + Serial.println("Attempting to connect to Wifi network..."); + Serial.print("SSID: "); + Serial.println(ssid); + + status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + while(true); + } + else { + server.begin(); + Serial.print("Connected to wifi."); + printWifiStatus(); + } +} + + +void loop() { + // listen for incoming clients + WiFiClient client = server.available(); + if (client) { + // an http request ends with a blank line + boolean currentLineIsBlank = true; + while (client.connected()) { + if (client.available()) { + char c = client.read(); + // if you've gotten to the end of the line (received a newline + // character) and the line is blank, the http request has ended, + // so you can send a reply + if (c == '\n' && currentLineIsBlank) { + // send a standard http response header + client.println("HTTP/1.1 200 OK"); + client.println("Content-Type: text/html"); + client.println(); + client.println(""); + // output the value of each analog input pin + for (int analogChannel = 0; analogChannel < 6; analogChannel++) { + client.print("analog input "); + client.print(analogChannel); + client.print(" is "); + client.print(analogRead(analogChannel)); + client.println("
"); + } + client.println(""); + break; + } + if (c == '\n') { + // you're starting a new line + currentLineIsBlank = true; + } + else if (c != '\r') { + // you've gotten a character on the current line + currentLineIsBlank = false; + } + } + } + // give the web browser time to receive the data + delay(1); + // close the connection: + client.stop(); + } +} + + +void printWifiStatus() { + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your WiFi shield's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + + // print the received signal strength: + long rssi = WiFi.RSSI(); + Serial.print("signal strength (RSSI):"); + Serial.print(rssi); + Serial.println(" dBm"); +}