1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-08-05 13:16:13 +03:00

WiFi Shield examples: added the firmware version check

This commit is contained in:
Fede85
2014-02-06 18:40:48 +01:00
parent b415903b69
commit 5dfafe7847
13 changed files with 406 additions and 362 deletions

View File

@@ -1,38 +1,43 @@
/* /*
This example connects to an unencrypted Wifi network. This example connects to an unencrypted Wifi network.
Then it prints the MAC address of the Wifi shield, Then it prints the MAC address of the Wifi shield,
the IP address obtained, and other network details. the IP address obtained, and other network details.
Circuit: Circuit:
* WiFi shield attached * WiFi shield attached
created 13 July 2010 created 13 July 2010
by dlf (Metodo2 srl) by dlf (Metodo2 srl)
modified 31 May 2012 modified 31 May 2012
by Tom Igoe by Tom Igoe
*/ */
#include <WiFi.h> #include <SPI.h>
#include <WiFi.h>
char ssid[] = "yourNetwork"; // the name of your network char ssid[] = "yourNetwork"; // the name of your network
int status = WL_IDLE_STATUS; // the Wifi radio's status int status = WL_IDLE_STATUS; // the Wifi radio's status
void setup() { void setup() {
//Initialize serial and wait for port to open: //Initialize serial and wait for port to open:
Serial.begin(9600); Serial.begin(9600);
while (!Serial) { while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only ; // wait for serial port to connect. Needed for Leonardo only
} }
// check for the presence of the shield: // check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) { if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present"); Serial.println("WiFi shield not present");
// don't continue: // don't continue:
while(true); while (true);
} }
// attempt to connect to Wifi network: String fv = WiFi.firmwareVersion();
while ( status != WL_CONNECTED) { if ( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to open SSID: "); Serial.print("Attempting to connect to open SSID: ");
Serial.println(ssid); Serial.println(ssid);
status = WiFi.begin(ssid); status = WiFi.begin(ssid);
@@ -40,7 +45,7 @@ void setup() {
// wait 10 seconds for connection: // wait 10 seconds for connection:
delay(10000); delay(10000);
} }
// you're connected now, so print out the data: // you're connected now, so print out the data:
Serial.print("You're connected to the network"); Serial.print("You're connected to the network");
printCurrentNet(); printCurrentNet();
@@ -56,26 +61,26 @@ void loop() {
void printWifiData() { void printWifiData() {
// print your WiFi shield's IP address: // print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP(); IPAddress ip = WiFi.localIP();
Serial.print("IP Address: "); Serial.print("IP Address: ");
Serial.println(ip); Serial.println(ip);
Serial.println(ip); Serial.println(ip);
// print your MAC address: // print your MAC address:
byte mac[6]; byte mac[6];
WiFi.macAddress(mac); WiFi.macAddress(mac);
Serial.print("MAC address: "); Serial.print("MAC address: ");
Serial.print(mac[5],HEX); Serial.print(mac[5], HEX);
Serial.print(":"); Serial.print(":");
Serial.print(mac[4],HEX); Serial.print(mac[4], HEX);
Serial.print(":"); Serial.print(":");
Serial.print(mac[3],HEX); Serial.print(mac[3], HEX);
Serial.print(":"); Serial.print(":");
Serial.print(mac[2],HEX); Serial.print(mac[2], HEX);
Serial.print(":"); Serial.print(":");
Serial.print(mac[1],HEX); Serial.print(mac[1], HEX);
Serial.print(":"); Serial.print(":");
Serial.println(mac[0],HEX); Serial.println(mac[0], HEX);
// print your subnet mask: // print your subnet mask:
IPAddress subnet = WiFi.subnetMask(); IPAddress subnet = WiFi.subnetMask();
Serial.print("NetMask: "); Serial.print("NetMask: ");
@@ -94,19 +99,19 @@ void printCurrentNet() {
// print the MAC address of the router you're attached to: // print the MAC address of the router you're attached to:
byte bssid[6]; byte bssid[6];
WiFi.BSSID(bssid); WiFi.BSSID(bssid);
Serial.print("BSSID: "); Serial.print("BSSID: ");
Serial.print(bssid[5],HEX); Serial.print(bssid[5], HEX);
Serial.print(":"); Serial.print(":");
Serial.print(bssid[4],HEX); Serial.print(bssid[4], HEX);
Serial.print(":"); Serial.print(":");
Serial.print(bssid[3],HEX); Serial.print(bssid[3], HEX);
Serial.print(":"); Serial.print(":");
Serial.print(bssid[2],HEX); Serial.print(bssid[2], HEX);
Serial.print(":"); Serial.print(":");
Serial.print(bssid[1],HEX); Serial.print(bssid[1], HEX);
Serial.print(":"); Serial.print(":");
Serial.println(bssid[0],HEX); Serial.println(bssid[0], HEX);
// print the received signal strength: // print the received signal strength:
long rssi = WiFi.RSSI(); long rssi = WiFi.RSSI();
@@ -116,6 +121,6 @@ void printCurrentNet() {
// print the encryption type: // print the encryption type:
byte encryption = WiFi.encryptionType(); byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:"); Serial.print("Encryption Type:");
Serial.println(encryption,HEX); Serial.println(encryption, HEX);
} }

View File

@@ -1,50 +1,55 @@
/* /*
This example connects to a WEP-encrypted Wifi network. This example connects to a WEP-encrypted Wifi network.
Then it prints the MAC address of the Wifi shield, Then it prints the MAC address of the Wifi shield,
the IP address obtained, and other network details. the IP address obtained, and other network details.
If you use 40-bit WEP, you need a key that is 10 characters long, If you use 40-bit WEP, you need a key that is 10 characters long,
and the characters must be hexadecimal (0-9 or A-F). and the characters must be hexadecimal (0-9 or A-F).
e.g. for 40-bit, ABBADEAF01 will work, but ABBADEAF won't work e.g. for 40-bit, ABBADEAF01 will work, but ABBADEAF won't work
(too short) and ABBAISDEAF won't work (I and S are not (too short) and ABBAISDEAF won't work (I and S are not
hexadecimal characters). hexadecimal characters).
For 128-bit, you need a string that is 26 characters long. For 128-bit, you need a string that is 26 characters long.
D0D0DEADF00DABBADEAFBEADED will work because it's 26 characters, D0D0DEADF00DABBADEAFBEADED will work because it's 26 characters,
all in the 0-9, A-F range. all in the 0-9, A-F range.
Circuit: Circuit:
* WiFi shield attached * WiFi shield attached
created 13 July 2010 created 13 July 2010
by dlf (Metodo2 srl) by dlf (Metodo2 srl)
modified 31 May 2012 modified 31 May 2012
by Tom Igoe by Tom Igoe
*/ */
#include <SPI.h>
#include <WiFi.h> #include <WiFi.h>
char ssid[] = "yourNetwork"; // your network SSID (name) char ssid[] = "yourNetwork"; // your network SSID (name)
char key[] = "D0D0DEADF00DABBADEAFBEADED"; // your network key char key[] = "D0D0DEADF00DABBADEAFBEADED"; // your network key
int keyIndex = 0; // your network key Index number int keyIndex = 0; // your network key Index number
int status = WL_IDLE_STATUS; // the Wifi radio's status int status = WL_IDLE_STATUS; // the Wifi radio's status
void setup() { void setup() {
//Initialize serial and wait for port to open: //Initialize serial and wait for port to open:
Serial.begin(9600); Serial.begin(9600);
while (!Serial) { while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only ; // wait for serial port to connect. Needed for Leonardo only
} }
// check for the presence of the shield: // check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) { if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present"); Serial.println("WiFi shield not present");
// don't continue: // don't continue:
while(true); while (true);
} }
String fv = WiFi.firmwareVersion();
if ( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network: // attempt to connect to Wifi network:
while ( status != WL_CONNECTED) { while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WEP network, SSID: "); Serial.print("Attempting to connect to WEP network, SSID: ");
Serial.println(ssid); Serial.println(ssid);
status = WiFi.begin(ssid, keyIndex, key); status = WiFi.begin(ssid, keyIndex, key);
@@ -73,20 +78,20 @@ void printWifiData() {
Serial.println(ip); Serial.println(ip);
// print your MAC address: // print your MAC address:
byte mac[6]; byte mac[6];
WiFi.macAddress(mac); WiFi.macAddress(mac);
Serial.print("MAC address: "); Serial.print("MAC address: ");
Serial.print(mac[5],HEX); Serial.print(mac[5], HEX);
Serial.print(":"); Serial.print(":");
Serial.print(mac[4],HEX); Serial.print(mac[4], HEX);
Serial.print(":"); Serial.print(":");
Serial.print(mac[3],HEX); Serial.print(mac[3], HEX);
Serial.print(":"); Serial.print(":");
Serial.print(mac[2],HEX); Serial.print(mac[2], HEX);
Serial.print(":"); Serial.print(":");
Serial.print(mac[1],HEX); Serial.print(mac[1], HEX);
Serial.print(":"); Serial.print(":");
Serial.println(mac[0],HEX); Serial.println(mac[0], HEX);
} }
void printCurrentNet() { void printCurrentNet() {
@@ -96,19 +101,19 @@ void printCurrentNet() {
// print the MAC address of the router you're attached to: // print the MAC address of the router you're attached to:
byte bssid[6]; byte bssid[6];
WiFi.BSSID(bssid); WiFi.BSSID(bssid);
Serial.print("BSSID: "); Serial.print("BSSID: ");
Serial.print(bssid[5],HEX); Serial.print(bssid[5], HEX);
Serial.print(":"); Serial.print(":");
Serial.print(bssid[4],HEX); Serial.print(bssid[4], HEX);
Serial.print(":"); Serial.print(":");
Serial.print(bssid[3],HEX); Serial.print(bssid[3], HEX);
Serial.print(":"); Serial.print(":");
Serial.print(bssid[2],HEX); Serial.print(bssid[2], HEX);
Serial.print(":"); Serial.print(":");
Serial.print(bssid[1],HEX); Serial.print(bssid[1], HEX);
Serial.print(":"); Serial.print(":");
Serial.println(bssid[0],HEX); Serial.println(bssid[0], HEX);
// print the received signal strength: // print the received signal strength:
long rssi = WiFi.RSSI(); long rssi = WiFi.RSSI();
@@ -118,7 +123,7 @@ void printCurrentNet() {
// print the encryption type: // print the encryption type:
byte encryption = WiFi.encryptionType(); byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:"); Serial.print("Encryption Type:");
Serial.println(encryption,HEX); Serial.println(encryption, HEX);
Serial.println(); Serial.println();
} }

View File

@@ -1,48 +1,53 @@
/* /*
This example connects to an unencrypted Wifi network. This example connects to an unencrypted Wifi network.
Then it prints the MAC address of the Wifi shield, Then it prints the MAC address of the Wifi shield,
the IP address obtained, and other network details. the IP address obtained, and other network details.
Circuit: Circuit:
* WiFi shield attached * WiFi shield attached
created 13 July 2010 created 13 July 2010
by dlf (Metodo2 srl) by dlf (Metodo2 srl)
modified 31 May 2012 modified 31 May 2012
by Tom Igoe by Tom Igoe
*/ */
#include <WiFi.h> #include <SPI.h>
#include <WiFi.h>
char ssid[] = "yourNetwork"; // your network SSID (name) char ssid[] = "yourNetwork"; // your network SSID (name)
char pass[] = "secretPassword"; // your network password char pass[] = "secretPassword"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status int status = WL_IDLE_STATUS; // the Wifi radio's status
void setup() { void setup() {
//Initialize serial and wait for port to open: //Initialize serial and wait for port to open:
Serial.begin(9600); Serial.begin(9600);
while (!Serial) { while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only ; // wait for serial port to connect. Needed for Leonardo only
} }
// check for the presence of the shield: // check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) { if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present"); Serial.println("WiFi shield not present");
// don't continue: // don't continue:
while(true); while (true);
} }
// attempt to connect to Wifi network: String fv = WiFi.firmwareVersion();
while ( status != WL_CONNECTED) { if ( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: "); Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid); Serial.println(ssid);
// Connect to WPA/WPA2 network: // Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass); status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection: // wait 10 seconds for connection:
delay(10000); delay(10000);
} }
// you're connected now, so print out the data: // you're connected now, so print out the data:
Serial.print("You're connected to the network"); Serial.print("You're connected to the network");
printCurrentNet(); printCurrentNet();
@@ -59,26 +64,26 @@ void loop() {
void printWifiData() { void printWifiData() {
// print your WiFi shield's IP address: // print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP(); IPAddress ip = WiFi.localIP();
Serial.print("IP Address: "); Serial.print("IP Address: ");
Serial.println(ip); Serial.println(ip);
Serial.println(ip); Serial.println(ip);
// print your MAC address: // print your MAC address:
byte mac[6]; byte mac[6];
WiFi.macAddress(mac); WiFi.macAddress(mac);
Serial.print("MAC address: "); Serial.print("MAC address: ");
Serial.print(mac[5],HEX); Serial.print(mac[5], HEX);
Serial.print(":"); Serial.print(":");
Serial.print(mac[4],HEX); Serial.print(mac[4], HEX);
Serial.print(":"); Serial.print(":");
Serial.print(mac[3],HEX); Serial.print(mac[3], HEX);
Serial.print(":"); Serial.print(":");
Serial.print(mac[2],HEX); Serial.print(mac[2], HEX);
Serial.print(":"); Serial.print(":");
Serial.print(mac[1],HEX); Serial.print(mac[1], HEX);
Serial.print(":"); Serial.print(":");
Serial.println(mac[0],HEX); Serial.println(mac[0], HEX);
} }
void printCurrentNet() { void printCurrentNet() {
@@ -88,19 +93,19 @@ void printCurrentNet() {
// print the MAC address of the router you're attached to: // print the MAC address of the router you're attached to:
byte bssid[6]; byte bssid[6];
WiFi.BSSID(bssid); WiFi.BSSID(bssid);
Serial.print("BSSID: "); Serial.print("BSSID: ");
Serial.print(bssid[5],HEX); Serial.print(bssid[5], HEX);
Serial.print(":"); Serial.print(":");
Serial.print(bssid[4],HEX); Serial.print(bssid[4], HEX);
Serial.print(":"); Serial.print(":");
Serial.print(bssid[3],HEX); Serial.print(bssid[3], HEX);
Serial.print(":"); Serial.print(":");
Serial.print(bssid[2],HEX); Serial.print(bssid[2], HEX);
Serial.print(":"); Serial.print(":");
Serial.print(bssid[1],HEX); Serial.print(bssid[1], HEX);
Serial.print(":"); Serial.print(":");
Serial.println(bssid[0],HEX); Serial.println(bssid[0], HEX);
// print the received signal strength: // print the received signal strength:
long rssi = WiFi.RSSI(); long rssi = WiFi.RSSI();
@@ -110,7 +115,7 @@ void printCurrentNet() {
// print the encryption type: // print the encryption type:
byte encryption = WiFi.encryptionType(); byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:"); Serial.print("Encryption Type:");
Serial.println(encryption,HEX); Serial.println(encryption, HEX);
Serial.println(); Serial.println();
} }

View File

@@ -1,13 +1,13 @@
/* /*
This example prints the Wifi shield's MAC address, and This example prints the Wifi shield's MAC address, and
scans for available Wifi networks using the Wifi shield. scans for available Wifi networks using the Wifi shield.
Every ten seconds, it scans again. It doesn't actually Every ten seconds, it scans again. It doesn't actually
connect to any network, so no encryption scheme is specified. connect to any network, so no encryption scheme is specified.
Circuit: Circuit:
* WiFi shield attached * WiFi shield attached
created 13 July 2010 created 13 July 2010
by dlf (Metodo2 srl) by dlf (Metodo2 srl)
modified 21 Junn 2012 modified 21 Junn 2012
@@ -20,17 +20,21 @@
void setup() { void setup() {
//Initialize serial and wait for port to open: //Initialize serial and wait for port to open:
Serial.begin(9600); Serial.begin(9600);
while (!Serial) { while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only ; // wait for serial port to connect. Needed for Leonardo only
} }
// check for the presence of the shield: // check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) { if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present"); Serial.println("WiFi shield not present");
// don't continue: // don't continue:
while(true); while (true);
} }
String fv = WiFi.firmwareVersion();
if ( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// Print WiFi MAC address: // Print WiFi MAC address:
printMacAddress(); printMacAddress();
@@ -49,22 +53,22 @@ void loop() {
void printMacAddress() { void printMacAddress() {
// the MAC address of your Wifi shield // the MAC address of your Wifi shield
byte mac[6]; byte mac[6];
// print your MAC address: // print your MAC address:
WiFi.macAddress(mac); WiFi.macAddress(mac);
Serial.print("MAC: "); Serial.print("MAC: ");
Serial.print(mac[5],HEX); Serial.print(mac[5], HEX);
Serial.print(":"); Serial.print(":");
Serial.print(mac[4],HEX); Serial.print(mac[4], HEX);
Serial.print(":"); Serial.print(":");
Serial.print(mac[3],HEX); Serial.print(mac[3], HEX);
Serial.print(":"); Serial.print(":");
Serial.print(mac[2],HEX); Serial.print(mac[2], HEX);
Serial.print(":"); Serial.print(":");
Serial.print(mac[1],HEX); Serial.print(mac[1], HEX);
Serial.print(":"); Serial.print(":");
Serial.println(mac[0],HEX); Serial.println(mac[0], HEX);
} }
void listNetworks() { void listNetworks() {
@@ -72,17 +76,17 @@ void listNetworks() {
Serial.println("** Scan Networks **"); Serial.println("** Scan Networks **");
int numSsid = WiFi.scanNetworks(); int numSsid = WiFi.scanNetworks();
if (numSsid == -1) if (numSsid == -1)
{ {
Serial.println("Couldn't get a wifi connection"); Serial.println("Couldn't get a wifi connection");
while(true); while (true);
} }
// print the list of networks seen: // print the list of networks seen:
Serial.print("number of available networks:"); Serial.print("number of available networks:");
Serial.println(numSsid); Serial.println(numSsid);
// print the network number and name for each network found: // print the network number and name for each network found:
for (int thisNet = 0; thisNet<numSsid; thisNet++) { for (int thisNet = 0; thisNet < numSsid; thisNet++) {
Serial.print(thisNet); Serial.print(thisNet);
Serial.print(") "); Serial.print(") ");
Serial.print(WiFi.SSID(thisNet)); Serial.print(WiFi.SSID(thisNet));
@@ -97,22 +101,22 @@ void listNetworks() {
void printEncryptionType(int thisType) { void printEncryptionType(int thisType) {
// read the encryption type and print out the name: // read the encryption type and print out the name:
switch (thisType) { switch (thisType) {
case ENC_TYPE_WEP: case ENC_TYPE_WEP:
Serial.println("WEP"); Serial.println("WEP");
break; break;
case ENC_TYPE_TKIP: case ENC_TYPE_TKIP:
Serial.println("WPA"); Serial.println("WPA");
break; break;
case ENC_TYPE_CCMP: case ENC_TYPE_CCMP:
Serial.println("WPA2"); Serial.println("WPA2");
break; break;
case ENC_TYPE_NONE: case ENC_TYPE_NONE:
Serial.println("None"); Serial.println("None");
break; break;
case ENC_TYPE_AUTO: case ENC_TYPE_AUTO:
Serial.println("Auto"); Serial.println("Auto");
break; break;
} }
} }

View File

@@ -1,29 +1,29 @@
/* /*
WiFi Web Server LED Blink WiFi Web Server LED Blink
A simple web server that lets you blink an LED via the web. A simple web server that lets you blink an LED via the web.
This sketch will print the IP address of your WiFi Shield (once connected) This sketch will print the IP address of your WiFi Shield (once connected)
to the Serial monitor. From there, you can open that address in a web browser to the Serial monitor. From there, you can open that address in a web browser
to turn on and off the LED on pin 9. to turn on and off the LED on pin 9.
If the IP address of your shield is yourAddress: If the IP address of your shield is yourAddress:
http://yourAddress/H turns the LED on http://yourAddress/H turns the LED on
http://yourAddress/L turns it off http://yourAddress/L turns it off
This example is written for a network using WPA encryption. For This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly. WEP or WPA, change the Wifi.begin() call accordingly.
Circuit: Circuit:
* WiFi shield attached * WiFi shield attached
* LED attached to pin 9 * LED attached to pin 9
created 25 Nov 2012 created 25 Nov 2012
by Tom Igoe by Tom Igoe
*/ */
#include <SPI.h> #include <SPI.h>
#include <WiFi.h> #include <WiFi.h>
char ssid[] = "yourNetwork"; // your network SSID (name) char ssid[] = "yourNetwork"; // your network SSID (name)
char pass[] = "secretPassword"; // your network password char pass[] = "secretPassword"; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP) int keyIndex = 0; // your network key Index number (needed only for WEP)
@@ -36,20 +36,24 @@ void setup() {
// check for the presence of the shield: // check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) { if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present"); Serial.println("WiFi shield not present");
while(true); // don't continue while (true); // don't continue
} }
String fv = WiFi.firmwareVersion();
if ( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network: // attempt to connect to Wifi network:
while ( status != WL_CONNECTED) { while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: "); Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID); Serial.println(ssid); // print the network name (SSID);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network: // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass); status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection: // wait 10 seconds for connection:
delay(10000); delay(10000);
} }
server.begin(); // start the web server on port 80 server.begin(); // start the web server on port 80
printWifiStatus(); // you're connected now, so print out the status printWifiStatus(); // you're connected now, so print out the status
} }
@@ -69,9 +73,9 @@ void loop() {
// if the current line is blank, you got two newline characters in a row. // if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response: // that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) { if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line: // and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK"); client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html"); client.println("Content-type:text/html");
client.println(); client.println();
@@ -83,12 +87,12 @@ void loop() {
// The HTTP response ends with another blank line: // The HTTP response ends with another blank line:
client.println(); client.println();
// break out of the while loop: // break out of the while loop:
break; break;
} }
else { // if you got a newline, then clear currentLine: else { // if you got a newline, then clear currentLine:
currentLine = ""; currentLine = "";
} }
} }
else if (c != '\r') { // if you got anything else but a carriage return character, else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine currentLine += c; // add it to the end of the currentLine
} }

View File

@@ -1,28 +1,28 @@
/* /*
Chat Server Chat Server
A simple server that distributes any incoming messages to all A simple server that distributes any incoming messages to all
connected clients. To use telnet to your device's IP address and type. connected clients. To use telnet to your device's IP address and type.
You can see the client's input in the serial monitor as well. You can see the client's input in the serial monitor as well.
This example is written for a network using WPA encryption. For This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly. WEP or WPA, change the Wifi.begin() call accordingly.
Circuit: Circuit:
* WiFi shield attached * WiFi shield attached
created 18 Dec 2009 created 18 Dec 2009
by David A. Mellis by David A. Mellis
modified 31 May 2012 modified 31 May 2012
by Tom Igoe by Tom Igoe
*/ */
#include <SPI.h> #include <SPI.h>
#include <WiFi.h> #include <WiFi.h>
char ssid[] = "yourNetwork"; // your network SSID (name) char ssid[] = "yourNetwork"; // your network SSID (name)
char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP) char pass[] = "secretPassword"; // 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 keyIndex = 0; // your network key Index number (needed only for WEP)
@@ -35,33 +35,38 @@ boolean alreadyConnected = false; // whether or not the client was connected pre
void setup() { void setup() {
//Initialize serial and wait for port to open: //Initialize serial and wait for port to open:
Serial.begin(9600); Serial.begin(9600);
while (!Serial) { while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only ; // wait for serial port to connect. Needed for Leonardo only
} }
// check for the presence of the shield: // check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) { if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present"); Serial.println("WiFi shield not present");
// don't continue: // don't continue:
while(true); while (true);
} }
String fv = WiFi.firmwareVersion();
if ( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network: // attempt to connect to Wifi network:
while ( status != WL_CONNECTED) { while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: "); Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid); Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network: // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass); status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection: // wait 10 seconds for connection:
delay(10000); delay(10000);
} }
// start the server: // start the server:
server.begin(); server.begin();
// you're connected now, so print out the status: // you're connected now, so print out the status:
printWifiStatus(); printWifiStatus();
} }
void loop() { void loop() {
@@ -73,11 +78,11 @@ void loop() {
if (client) { if (client) {
if (!alreadyConnected) { if (!alreadyConnected) {
// clead out the input buffer: // clead out the input buffer:
client.flush(); client.flush();
Serial.println("We have a new client"); Serial.println("We have a new client");
client.println("Hello, client!"); client.println("Hello, client!");
alreadyConnected = true; alreadyConnected = true;
} }
if (client.available() > 0) { if (client.available() > 0) {
// read the bytes incoming from the client: // read the bytes incoming from the client:

View File

@@ -1,22 +1,22 @@
/* /*
Udp NTP Client Udp NTP Client
Get the time from a Network Time Protocol (NTP) time server Get the time from a Network Time Protocol (NTP) time server
Demonstrates use of UDP sendPacket and ReceivePacket Demonstrates use of UDP sendPacket and ReceivePacket
For more on NTP time servers and the messages needed to communicate with them, For more on NTP time servers and the messages needed to communicate with them,
see http://en.wikipedia.org/wiki/Network_Time_Protocol see http://en.wikipedia.org/wiki/Network_Time_Protocol
created 4 Sep 2010 created 4 Sep 2010
by Michael Margolis by Michael Margolis
modified 9 Apr 2012 modified 9 Apr 2012
by Tom Igoe by Tom Igoe
This code is in the public domain. This code is in the public domain.
*/ */
#include <SPI.h> #include <SPI.h>
#include <WiFi.h> #include <WiFi.h>
#include <WiFiUdp.h> #include <WiFiUdp.h>
@@ -31,12 +31,12 @@ IPAddress timeServer(129, 6, 15, 28); // time.nist.gov NTP server
const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
// A UDP instance to let us send and receive packets over UDP // A UDP instance to let us send and receive packets over UDP
WiFiUDP Udp; WiFiUDP Udp;
void setup() void setup()
{ {
// Open serial communications and wait for port to open: // Open serial communications and wait for port to open:
Serial.begin(9600); Serial.begin(9600);
@@ -46,17 +46,20 @@ void setup()
// check for the presence of the shield: // check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) { if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present"); Serial.println("WiFi shield not present");
// don't continue: // don't continue:
while(true); while (true);
} }
String fv = WiFi.firmwareVersion();
if ( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network: // attempt to connect to Wifi network:
while ( status != WL_CONNECTED) { while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: "); Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid); Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network: // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass); status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection: // wait 10 seconds for connection:
@@ -73,61 +76,61 @@ void setup()
void loop() void loop()
{ {
sendNTPpacket(timeServer); // send an NTP packet to a time server sendNTPpacket(timeServer); // send an NTP packet to a time server
// wait to see if a reply is available // wait to see if a reply is available
delay(1000); delay(1000);
Serial.println( Udp.parsePacket() ); Serial.println( Udp.parsePacket() );
if ( Udp.parsePacket() ) { if ( Udp.parsePacket() ) {
Serial.println("packet received"); Serial.println("packet received");
// We've received a packet, read the data from it // We've received a packet, read the data from it
Udp.read(packetBuffer,NTP_PACKET_SIZE); // read the packet into the buffer Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
//the timestamp starts at byte 40 of the received packet and is four bytes, //the timestamp starts at byte 40 of the received packet and is four bytes,
// or two words, long. First, esxtract the two words: // or two words, long. First, esxtract the two words:
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]); unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]); unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
// combine the four bytes (two words) into a long integer // combine the four bytes (two words) into a long integer
// this is NTP time (seconds since Jan 1 1900): // this is NTP time (seconds since Jan 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord; unsigned long secsSince1900 = highWord << 16 | lowWord;
Serial.print("Seconds since Jan 1 1900 = " ); Serial.print("Seconds since Jan 1 1900 = " );
Serial.println(secsSince1900); Serial.println(secsSince1900);
// now convert NTP time into everyday time: // now convert NTP time into everyday time:
Serial.print("Unix time = "); Serial.print("Unix time = ");
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800: // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
const unsigned long seventyYears = 2208988800UL; const unsigned long seventyYears = 2208988800UL;
// subtract seventy years: // subtract seventy years:
unsigned long epoch = secsSince1900 - seventyYears; unsigned long epoch = secsSince1900 - seventyYears;
// print Unix time: // print Unix time:
Serial.println(epoch); Serial.println(epoch);
// print the hour, minute and second: // print the hour, minute and second:
Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT) Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day) Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
Serial.print(':'); Serial.print(':');
if ( ((epoch % 3600) / 60) < 10 ) { if ( ((epoch % 3600) / 60) < 10 ) {
// In the first 10 minutes of each hour, we'll want a leading '0' // In the first 10 minutes of each hour, we'll want a leading '0'
Serial.print('0'); Serial.print('0');
} }
Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute) Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
Serial.print(':'); Serial.print(':');
if ( (epoch % 60) < 10 ) { if ( (epoch % 60) < 10 ) {
// In the first 10 seconds of each minute, we'll want a leading '0' // In the first 10 seconds of each minute, we'll want a leading '0'
Serial.print('0'); Serial.print('0');
} }
Serial.println(epoch %60); // print the second Serial.println(epoch % 60); // print the second
} }
// wait ten seconds before asking for the time again // wait ten seconds before asking for the time again
delay(10000); delay(10000);
} }
// send an NTP request to the time server at the given address // send an NTP request to the time server at the given address
unsigned long sendNTPpacket(IPAddress& address) unsigned long sendNTPpacket(IPAddress& address)
{ {
//Serial.println("1"); //Serial.println("1");
// set all bytes in the buffer to 0 // set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE); memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request // Initialize values needed to form NTP request
// (see URL above for details on the packets) // (see URL above for details on the packets)
//Serial.println("2"); //Serial.println("2");
@@ -136,20 +139,20 @@ unsigned long sendNTPpacket(IPAddress& address)
packetBuffer[2] = 6; // Polling Interval packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion // 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49; packetBuffer[12] = 49;
packetBuffer[13] = 0x4E; packetBuffer[13] = 0x4E;
packetBuffer[14] = 49; packetBuffer[14] = 49;
packetBuffer[15] = 52; packetBuffer[15] = 52;
//Serial.println("3"); //Serial.println("3");
// all NTP fields have been given values, now // all NTP fields have been given values, now
// you can send a packet requesting a timestamp: // you can send a packet requesting a timestamp:
Udp.beginPacket(address, 123); //NTP requests are to port 123 Udp.beginPacket(address, 123); //NTP requests are to port 123
//Serial.println("4"); //Serial.println("4");
Udp.write(packetBuffer,NTP_PACKET_SIZE); Udp.write(packetBuffer, NTP_PACKET_SIZE);
//Serial.println("5"); //Serial.println("5");
Udp.endPacket(); Udp.endPacket();
//Serial.println("6"); //Serial.println("6");
} }

View File

@@ -1,13 +1,13 @@
/* /*
WiFi UDP Send and Receive String WiFi UDP Send and Receive String
This sketch wait an UDP packet on localPort using a WiFi shield. This sketch wait an UDP packet on localPort using a WiFi shield.
When a packet is received an Acknowledge packet is sent to the client on port remotePort When a packet is received an Acknowledge packet is sent to the client on port remotePort
Circuit: Circuit:
* WiFi shield attached * WiFi shield attached
created 30 December 2012 created 30 December 2012
by dlf (Metodo2 srl) by dlf (Metodo2 srl)
@@ -19,7 +19,7 @@
#include <WiFiUdp.h> #include <WiFiUdp.h>
int status = WL_IDLE_STATUS; int status = WL_IDLE_STATUS;
char ssid[] = "yourNetwork"; // your network SSID (name) char ssid[] = "yourNetwork"; // your network SSID (name)
char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP) char pass[] = "secretPassword"; // 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 keyIndex = 0; // your network key Index number (needed only for WEP)
@@ -32,42 +32,46 @@ WiFiUDP Udp;
void setup() { void setup() {
//Initialize serial and wait for port to open: //Initialize serial and wait for port to open:
Serial.begin(9600); Serial.begin(9600);
while (!Serial) { while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only ; // wait for serial port to connect. Needed for Leonardo only
} }
// check for the presence of the shield: // check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) { if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present"); Serial.println("WiFi shield not present");
// don't continue: // don't continue:
while(true); while (true);
} }
String fv = WiFi.firmwareVersion();
if ( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network: // attempt to connect to Wifi network:
while ( status != WL_CONNECTED) { while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: "); Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid); Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network: // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid); status = WiFi.begin(ssid);
// wait 10 seconds for connection: // wait 10 seconds for connection:
delay(10000); delay(10000);
} }
Serial.println("Connected to wifi"); Serial.println("Connected to wifi");
printWifiStatus(); printWifiStatus();
Serial.println("\nStarting connection to server..."); Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial: // if you get a connection, report back via serial:
Udp.begin(localPort); Udp.begin(localPort);
} }
void loop() { void loop() {
// if there's data available, read a packet // if there's data available, read a packet
int packetSize = Udp.parsePacket(); int packetSize = Udp.parsePacket();
if(packetSize) if (packetSize)
{ {
Serial.print("Received packet of size "); Serial.print("Received packet of size ");
Serial.println(packetSize); Serial.println(packetSize);
Serial.print("From "); Serial.print("From ");
@@ -77,16 +81,16 @@ void loop() {
Serial.println(Udp.remotePort()); Serial.println(Udp.remotePort());
// read the packet into packetBufffer // read the packet into packetBufffer
int len = Udp.read(packetBuffer,255); int len = Udp.read(packetBuffer, 255);
if (len >0) packetBuffer[len]=0; if (len > 0) packetBuffer[len] = 0;
Serial.println("Contents:"); Serial.println("Contents:");
Serial.println(packetBuffer); Serial.println(packetBuffer);
// send a reply, to the IP address and port that sent us the packet we received // send a reply, to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(ReplyBuffer); Udp.write(ReplyBuffer);
Udp.endPacket(); Udp.endPacket();
} }
} }

View File

@@ -1,19 +1,19 @@
/* /*
Web client Web client
This sketch connects to a website (http://www.google.com) This sketch connects to a website (http://www.google.com)
using a WiFi shield. using a WiFi shield.
This example is written for a network using WPA encryption. For This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly. WEP or WPA, change the Wifi.begin() call accordingly.
This example is written for a network using WPA encryption. For This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly. WEP or WPA, change the Wifi.begin() call accordingly.
Circuit: Circuit:
* WiFi shield attached * WiFi shield attached
created 13 July 2010 created 13 July 2010
by dlf (Metodo2 srl) by dlf (Metodo2 srl)
modified 31 May 2012 modified 31 May 2012
@@ -24,7 +24,7 @@
#include <SPI.h> #include <SPI.h>
#include <WiFi.h> #include <WiFi.h>
char ssid[] = "yourNetwork"; // your network SSID (name) char ssid[] = "yourNetwork"; // your network SSID (name)
char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP) char pass[] = "secretPassword"; // 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 keyIndex = 0; // your network key Index number (needed only for WEP)
@@ -35,37 +35,41 @@ int status = WL_IDLE_STATUS;
char server[] = "www.google.com"; // name address for Google (using DNS) char server[] = "www.google.com"; // name address for Google (using DNS)
// Initialize the Ethernet client library // Initialize the Ethernet client library
// with the IP address and port of the server // with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP): // that you want to connect to (port 80 is default for HTTP):
WiFiClient client; WiFiClient client;
void setup() { void setup() {
//Initialize serial and wait for port to open: //Initialize serial and wait for port to open:
Serial.begin(9600); Serial.begin(9600);
while (!Serial) { while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only ; // wait for serial port to connect. Needed for Leonardo only
} }
// check for the presence of the shield: // check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) { if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present"); Serial.println("WiFi shield not present");
// don't continue: // don't continue:
while(true); while (true);
} }
String fv = WiFi.firmwareVersion();
if ( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network: // attempt to connect to Wifi network:
while (status != WL_CONNECTED) { while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: "); Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid); Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network: // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass); status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection: // wait 10 seconds for connection:
delay(10000); delay(10000);
} }
Serial.println("Connected to wifi"); Serial.println("Connected to wifi");
printWifiStatus(); printWifiStatus();
Serial.println("\nStarting connection to server..."); Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial: // if you get a connection, report back via serial:
if (client.connect(server, 80)) { if (client.connect(server, 80)) {
@@ -79,7 +83,7 @@ void setup() {
} }
void loop() { void loop() {
// if there are incoming bytes available // if there are incoming bytes available
// from the server, read them and print them: // from the server, read them and print them:
while (client.available()) { while (client.available()) {
char c = client.read(); char c = client.read();
@@ -93,7 +97,7 @@ void loop() {
client.stop(); client.stop();
// do nothing forevermore: // do nothing forevermore:
while(true); while (true);
} }
} }

View File

@@ -1,24 +1,26 @@
/* /*
Repeating Wifi Web client Repeating Wifi Web Client
This sketch connects to a a web server and makes a request This sketch connects to a a web server and makes a request
using an Arduino Wifi shield. using an Arduino Wifi shield.
Circuit: Circuit:
* Wifi shield attached to pins 10, 11, 12, 13 * WiFi shield attached to pins SPI pins and pin 7
created 23 April 2012 created 23 April 2012
modifide 31 May 2012 modified 31 May 2012
by Tom Igoe by Tom Igoe
modified 13 Jan 2014
by Federico Vanzati
http://arduino.cc/en/Tutorial/WifiWebClientRepeating http://arduino.cc/en/Tutorial/WifiWebClientRepeating
This code is in the public domain. This code is in the public domain.
*/ */
#include <SPI.h> #include <SPI.h>
#include <WiFi.h> #include <WiFi.h>
char ssid[] = "yourNetwork"; // your network SSID (name) char ssid[] = "yourNetwork"; // your network SSID (name)
char pass[] = "secretPassword"; // your network password char pass[] = "secretPassword"; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP) int keyIndex = 0; // your network key Index number (needed only for WEP)
@@ -31,34 +33,37 @@ WiFiClient client;
char server[] = "www.arduino.cc"; char server[] = "www.arduino.cc";
//IPAddress server(64,131,82,241); //IPAddress server(64,131,82,241);
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds unsigned 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 = 10L * 1000L; // delay between updates, in milliseconds
const unsigned long postingInterval = 10*1000; // delay between updates, in milliseconds
void setup() { void setup() {
//Initialize serial and wait for port to open: //Initialize serial and wait for port to open:
Serial.begin(9600); Serial.begin(9600);
while (!Serial) { while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only ; // wait for serial port to connect. Needed for Leonardo only
} }
// check for the presence of the shield: // check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) { if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present"); Serial.println("WiFi shield not present");
// don't continue: // don't continue:
while(true); while (true);
} }
String fv = WiFi.firmwareVersion();
if ( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network: // attempt to connect to Wifi network:
while ( status != WL_CONNECTED) { while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: "); Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid); Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network: // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass); status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection: // wait 10 seconds for connection:
delay(10000); delay(10000);
} }
// you're connected now, so print out the status: // you're connected now, so print out the status:
printWifiStatus(); printWifiStatus();
} }
@@ -72,44 +77,36 @@ void loop() {
Serial.write(c); Serial.write(c);
} }
// if there's no net connection, but there was one last time // if ten seconds have passed since your last connection,
// through the loop, then stop the client: // then connect again and send data:
if (!client.connected() && lastConnected) { if (millis() - lastConnectionTime > postingInterval) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
httpRequest(); httpRequest();
} }
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
} }
// this method makes a HTTP connection to the server: // this method makes a HTTP connection to the server:
void httpRequest() { void httpRequest() {
// close any connection before send a new request.
// This will free the socket on the WiFi shield
client.stop();
// if there's a successful connection: // if there's a successful connection:
if (client.connect(server, 80)) { if (client.connect(server, 80)) {
Serial.println("connecting..."); Serial.println("connecting...");
// send the HTTP PUT request: // send the HTTP PUT request:
client.println("GET /latest.txt HTTP/1.1"); client.println("GET /latest.txt HTTP/1.1");
client.println("Host: www.arduino.cc"); client.println("Host: www.arduino.cc");
client.println("User-Agent: arduino-ethernet"); client.println("User-Agent: ArduinoWiFi/1.1");
client.println("Connection: close"); client.println("Connection: close");
client.println(); client.println();
// note the time that the connection was made: // note the time that the connection was made:
lastConnectionTime = millis(); lastConnectionTime = millis();
} }
else { else {
// if you couldn't make a connection: // if you couldn't make a connection:
Serial.println("connection failed"); Serial.println("connection failed");
Serial.println("disconnecting.");
client.stop();
} }
} }
@@ -132,7 +129,3 @@ void printWifiStatus() {
} }

View File

@@ -1,16 +1,16 @@
/* /*
WiFi Web Server WiFi Web Server
A simple web server that shows the value of the analog input pins. A simple web server that shows the value of the analog input pins.
using a WiFi shield. using a WiFi shield.
This example is written for a network using WPA encryption. For This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly. WEP or WPA, change the Wifi.begin() call accordingly.
Circuit: Circuit:
* WiFi shield attached * WiFi shield attached
* Analog inputs attached to pins A0 through A5 (optional) * Analog inputs attached to pins A0 through A5 (optional)
created 13 July 2010 created 13 July 2010
by dlf (Metodo2 srl) by dlf (Metodo2 srl)
modified 31 May 2012 modified 31 May 2012
@@ -22,7 +22,7 @@
#include <WiFi.h> #include <WiFi.h>
char ssid[] = "yourNetwork"; // your network SSID (name) char ssid[] = "yourNetwork"; // your network SSID (name)
char pass[] = "secretPassword"; // your network password char pass[] = "secretPassword"; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP) int keyIndex = 0; // your network key Index number (needed only for WEP)
@@ -32,28 +32,32 @@ WiFiServer server(80);
void setup() { void setup() {
//Initialize serial and wait for port to open: //Initialize serial and wait for port to open:
Serial.begin(9600); Serial.begin(9600);
while (!Serial) { while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only ; // wait for serial port to connect. Needed for Leonardo only
} }
// check for the presence of the shield: // check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) { if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present"); Serial.println("WiFi shield not present");
// don't continue: // don't continue:
while(true); while (true);
} }
String fv = WiFi.firmwareVersion();
if ( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network: // attempt to connect to Wifi network:
while ( status != WL_CONNECTED) { while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: "); Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid); Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network: // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass); status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection: // wait 10 seconds for connection:
delay(10000); delay(10000);
} }
server.begin(); server.begin();
// you're connected now, so print out the status: // you're connected now, so print out the status:
printWifiStatus(); printWifiStatus();
@@ -90,15 +94,15 @@ void loop() {
client.print(analogChannel); client.print(analogChannel);
client.print(" is "); client.print(" is ");
client.print(sensorReading); client.print(sensorReading);
client.println("<br />"); client.println("<br />");
} }
client.println("</html>"); client.println("</html>");
break; break;
} }
if (c == '\n') { if (c == '\n') {
// you're starting a new line // you're starting a new line
currentLineIsBlank = true; currentLineIsBlank = true;
} }
else if (c != '\r') { else if (c != '\r') {
// you've gotten a character on the current line // you've gotten a character on the current line
currentLineIsBlank = false; currentLineIsBlank = false;
@@ -107,7 +111,7 @@ void loop() {
} }
// give the web browser time to receive the data // give the web browser time to receive the data
delay(1); delay(1);
// close the connection: // close the connection:
client.stop(); client.stop();
Serial.println("client disonnected"); Serial.println("client disonnected");

View File

@@ -3,26 +3,26 @@
This sketch connects an analog sensor to Xively (http://www.xively.com) This sketch connects an analog sensor to Xively (http://www.xively.com)
using an Arduino Wifi shield. using an Arduino Wifi shield.
This example is written for a network using WPA encryption. For This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly. WEP or WPA, change the Wifi.begin() call accordingly.
This example has been updated to use version 2.0 of the Xively API. This example has been updated to use version 2.0 of the Xively API.
To make it work, create a feed with a datastream, and give it the ID 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. sensor1. Or change the code below to match your feed.
Circuit: Circuit:
* Analog sensor attached to analog in 0 * Analog sensor attached to analog in 0
* Wifi shield attached to pins 10, 11, 12, 13 * Wifi shield attached to pins 10, 11, 12, 13
created 13 Mar 2012 created 13 Mar 2012
modified 31 May 2012 modified 31 May 2012
by Tom Igoe by Tom Igoe
modified 8 Nov 2013 modified 8 Nov 2013
by Scott Fitzgerald by Scott Fitzgerald
This code is in the public domain. This code is in the public domain.
*/ */
#include <SPI.h> #include <SPI.h>
#include <WiFi.h> #include <WiFi.h>
@@ -31,7 +31,7 @@
#define FEEDID 00000 // replace your feed ID #define FEEDID 00000 // replace your feed ID
#define USERAGENT "My Arduino Project" // user agent is the project name #define USERAGENT "My Arduino Project" // user agent is the project name
char ssid[] = "yourNetwork"; // your network SSID (name) char ssid[] = "yourNetwork"; // your network SSID (name)
char pass[] = "secretPassword"; // your network password char pass[] = "secretPassword"; // your network password
int status = WL_IDLE_STATUS; int status = WL_IDLE_STATUS;
@@ -49,28 +49,32 @@ const unsigned long postingInterval = 10*1000; //delay between updates to xively
void setup() { void setup() {
//Initialize serial and wait for port to open: //Initialize serial and wait for port to open:
Serial.begin(9600); Serial.begin(9600);
while (!Serial) { while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only ; // wait for serial port to connect. Needed for Leonardo only
} }
// check for the presence of the shield: // check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) { if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present"); Serial.println("WiFi shield not present");
// don't continue: // don't continue:
while(true); while (true);
} }
String fv = WiFi.firmwareVersion();
if ( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network: // attempt to connect to Wifi network:
while ( status != WL_CONNECTED) { while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: "); Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid); Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network: // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass); status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection: // wait 10 seconds for connection:
delay(10000); delay(10000);
} }
// you're connected now, so print out the status: // you're connected now, so print out the status:
printWifiStatus(); printWifiStatus();
} }
@@ -78,7 +82,7 @@ void setup() {
void loop() { void loop() {
// read the analog sensor: // read the analog sensor:
int sensorReading = analogRead(A0); int sensorReading = analogRead(A0);
// if there's incoming data from the net connection. // if there's incoming data from the net connection.
// send it out the serial port. This is for debugging // send it out the serial port. This is for debugging
@@ -98,7 +102,7 @@ void loop() {
// if you're not connected, and ten seconds have passed since // if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data: // your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) { if (!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
sendData(sensorReading); sendData(sensorReading);
} }
// store the state of the connection for next time through // store the state of the connection for next time through
@@ -135,8 +139,8 @@ void sendData(int thisData) {
// here's the actual content of the PUT request: // here's the actual content of the PUT request:
client.print("sensor1,"); client.print("sensor1,");
client.println(thisData); client.println(thisData);
} }
else { else {
// if you couldn't make a connection: // if you couldn't make a connection:
Serial.println("connection failed"); Serial.println("connection failed");
@@ -144,7 +148,7 @@ void sendData(int thisData) {
Serial.println("disconnecting."); Serial.println("disconnecting.");
client.stop(); client.stop();
} }
// note the time that the connection was made or attempted: // note the time that the connection was made or attempted:
lastConnectionTime = millis(); lastConnectionTime = millis();
} }
@@ -157,12 +161,12 @@ void sendData(int thisData) {
int getLength(int someValue) { int getLength(int someValue) {
// there's at least one byte: // there's at least one byte:
int digits = 1; int digits = 1;
// continually divide the value by ten, // continually divide the value by ten,
// adding one to the digit count for each // adding one to the digit count for each
// time you divide, until you're at 0: // time you divide, until you're at 0:
int dividend = someValue /10; int dividend = someValue / 10;
while (dividend > 0) { while (dividend > 0) {
dividend = dividend /10; dividend = dividend / 10;
digits++; digits++;
} }
// return the number of digits: // return the number of digits:

View File

@@ -3,29 +3,29 @@
This sketch connects an analog sensor to Xively (http://www.xively.com) This sketch connects an analog sensor to Xively (http://www.xively.com)
using a Arduino Wifi shield. using a Arduino Wifi shield.
This example is written for a network using WPA encryption. For This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly. WEP or WPA, change the Wifi.begin() call accordingly.
This example has been updated to use version 2.0 of the xively.com API. This example has been updated to use version 2.0 of the xively.com API.
To make it work, create a feed with a datastream, and give it the ID 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. sensor1. Or change the code below to match your feed.
This example uses the String library, which is part of the Arduino core from This example uses the String library, which is part of the Arduino core from
version 0019. version 0019.
Circuit: Circuit:
* Analog sensor attached to analog in 0 * Analog sensor attached to analog in 0
* Wifi shield attached to pins 10, 11, 12, 13 * Wifi shield attached to pins 10, 11, 12, 13
created 16 Mar 2012 created 16 Mar 2012
modified 31 May 2012 modified 31 May 2012
by Tom Igoe by Tom Igoe
modified 8 Sept 2012 modified 8 Sept 2012
by Scott Fitzgerald by Scott Fitzgerald
This code is in the public domain. This code is in the public domain.
*/ */
#include <SPI.h> #include <SPI.h>
@@ -35,7 +35,7 @@
#define FEEDID 00000 // replace your feed ID #define FEEDID 00000 // replace your feed ID
#define USERAGENT "My Arduino Project" // user agent is the project name #define USERAGENT "My Arduino Project" // user agent is the project name
char ssid[] = "yourNetwork"; // your network SSID (name) char ssid[] = "yourNetwork"; // your network SSID (name)
char pass[] = "secretPassword"; // your network password char pass[] = "secretPassword"; // your network password
int status = WL_IDLE_STATUS; int status = WL_IDLE_STATUS;
@@ -54,35 +54,39 @@ const unsigned long postingInterval = 10*1000; //delay between updates to xivel
void setup() { void setup() {
//Initialize serial and wait for port to open: //Initialize serial and wait for port to open:
Serial.begin(9600); Serial.begin(9600);
while (!Serial) { while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only ; // wait for serial port to connect. Needed for Leonardo only
} }
// check for the presence of the shield: // check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) { if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present"); Serial.println("WiFi shield not present");
// don't continue: // don't continue:
while(true); while (true);
} }
String fv = WiFi.firmwareVersion();
if ( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network: // attempt to connect to Wifi network:
while ( status != WL_CONNECTED) { while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: "); Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid); Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network: // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass); status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection: // wait 10 seconds for connection:
delay(10000); delay(10000);
} }
// you're connected now, so print out the status: // you're connected now, so print out the status:
printWifiStatus(); printWifiStatus();
} }
void loop() { void loop() {
// read the analog sensor: // read the analog sensor:
int sensorReading = analogRead(A0); int sensorReading = analogRead(A0);
// convert the data to a String to send it: // convert the data to a String to send it:
String dataString = "sensor1,"; String dataString = "sensor1,";
@@ -111,8 +115,8 @@ void loop() {
} }
// if you're not connected, and ten seconds have passed since // if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data: // your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) { if (!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
sendData(dataString); sendData(dataString);
} }
// store the state of the connection for next time through // store the state of the connection for next time through
@@ -144,7 +148,7 @@ void sendData(String thisData) {
// here's the actual content of the PUT request: // here's the actual content of the PUT request:
client.println(thisData); client.println(thisData);
} }
else { else {
// if you couldn't make a connection: // if you couldn't make a connection:
Serial.println("connection failed"); Serial.println("connection failed");