From 6e60b13013f9d94a7f86d225acdc072d31929036 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Thu, 31 May 2012 12:15:49 -0400 Subject: [PATCH 1/2] Changed wifi_drv.cpp and wl_definitions.h to allow for shield detection --- WiFi/utility/wifi_drv.cpp | 2 +- WiFi/utility/wl_definitions.h | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/WiFi/utility/wifi_drv.cpp b/WiFi/utility/wifi_drv.cpp index 5bd59d38d..45da70bdb 100644 --- a/WiFi/utility/wifi_drv.cpp +++ b/WiFi/utility/wifi_drv.cpp @@ -163,7 +163,7 @@ uint8_t WiFiDrv::getConnectionStatus() SpiDrv::waitForSlaveReady(); // Wait for reply - uint8_t _data = 0; + uint8_t _data = -1; uint8_t _dataLen = 0; SpiDrv::waitResponseCmd(GET_CONN_STATUS_CMD, PARAM_NUMS_1, &_data, &_dataLen); diff --git a/WiFi/utility/wl_definitions.h b/WiFi/utility/wl_definitions.h index e3db8b608..15de781fc 100644 --- a/WiFi/utility/wl_definitions.h +++ b/WiFi/utility/wl_definitions.h @@ -26,7 +26,8 @@ #define WL_MAX_ATTEMPT_CONNECTION 10 typedef enum { - WL_IDLE_STATUS, + WL_NO_SHIELD = 255, + WL_IDLE_STATUS = 0, WL_NO_SSID_AVAIL, WL_SCAN_COMPLETED, WL_CONNECTED, From b2a8a47c586baef8dfc4d38d3e9ddcb6b33e0ac7 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Thu, 31 May 2012 12:16:14 -0400 Subject: [PATCH 2/2] Changed examples to include shield detection --- .../ConnectNoEncryption.ino | 37 +++++++++-------- .../ConnectWithWEP/ConnectWithWEP.ino | 36 +++++++++------- .../ConnectWithWPA/ConnectWithWPA.ino | 41 +++++++++++-------- WiFi/examples/ScanNetworks/ScanNetworks.ino | 14 +++++-- .../WifiChatServer/WifiChatServer.ino | 21 ++++++---- .../WifiCosmClient/WifiCosmClient.ino | 15 ++++--- .../WifiCosmClientString.ino | 17 +++++--- .../WifiTwitterClient/WifiTwitterClient.ino | 20 +++++---- WiFi/examples/WifiWebClient/WifiWebClient.ino | 21 ++++++---- .../WifiWebClientRepeating.ino | 16 +++++--- WiFi/examples/WifiWebServer/WifiWebServer.ino | 11 ++++- 11 files changed, 157 insertions(+), 92 deletions(-) diff --git a/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino b/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino index cf1eb432d..2609320c1 100644 --- a/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino +++ b/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino @@ -9,8 +9,8 @@ created 13 July 2010 by dlf (Metodo2 srl) - modified 29 Feb 2012 - by Scott Fitzgerald + modified 31 May 2012 + by Tom Igoe */ #include @@ -20,23 +20,28 @@ int status = WL_IDLE_STATUS; // the Wifi radio's status void setup() { // initialize serial: Serial.begin(9600); - - // attempt to connect to an open network: - Serial.print("Attempting to connect to open network: "); - Serial.println(ssid); - status = WiFi.begin(ssid); - - // if you're not connected, stop here: - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); + + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: while(true); } - // if you are connected : - else { - Serial.print("You're connected to the network"); - printCurrentNet(); - printWifiData(); + + // attempt to connect to Wifi network: + while ( status != WL_CONNECTED) { + Serial.print("Attempting to connect to open SSID: "); + Serial.println(ssid); + status = WiFi.begin(ssid); + + // wait 10 seconds for connection: + delay(10000); } + + // you're connected now, so print out the data: + Serial.print("You're connected to the network"); + printCurrentNet(); + printWifiData(); } void loop() { diff --git a/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino b/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino index 1159e37f4..e6f531ca8 100644 --- a/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino +++ b/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino @@ -19,7 +19,7 @@ created 13 July 2010 by dlf (Metodo2 srl) - modified 4 Mar 2012 + modified 31 May 2012 by Tom Igoe */ #include @@ -33,22 +33,27 @@ void setup() { // initialize serial: Serial.begin(9600); - // attempt to connect to an open network: - Serial.print("Attempting to connect to WEP network: "); - Serial.println(ssid); - status = WiFi.begin(ssid, keyIndex, key); - - // if you're not connected, stop here: - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: while(true); } - // if you are connected : - else { - Serial.print("You're connected to the network"); - printCurrentNet(); - printWifiData(); + + // attempt to connect to Wifi network: + while ( status != WL_CONNECTED) { + Serial.print("Attempting to connect to WEP network, SSID: "); + Serial.println(ssid); + status = WiFi.begin(ssid, keyIndex, key); + + // wait 10 seconds for connection: + delay(10000); } + + // once you are connected : + Serial.print("You're connected to the network"); + printCurrentNet(); + printWifiData(); } void loop() { @@ -60,7 +65,7 @@ void loop() { void printWifiData() { // print your WiFi shield's IP address: IPAddress ip = WiFi.localIP(); - Serial.print("IP Address: "); + Serial.print("IP Address: "); Serial.println(ip); Serial.println(ip); @@ -115,3 +120,4 @@ void printCurrentNet() { } + diff --git a/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino b/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino index 7b5752cc7..953841500 100644 --- a/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino +++ b/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino @@ -9,35 +9,42 @@ created 13 July 2010 by dlf (Metodo2 srl) - modified 29 Feb 2012 - by Scott Fitzgerald + modified 31 May 2012 + by Tom Igoe */ #include -char ssid[] = "networkName"; // your network SSID (name) -char pass[] = "yourPassword"; // your network password +char ssid[] = "yourNetwork"; // your network SSID (name) +char pass[] = "secretPassword"; // your network password int status = WL_IDLE_STATUS; // the Wifi radio's status void setup() { // initialize serial: Serial.begin(9600); - - // attempt to connect to an open network: - Serial.print("Attempting to connect to WPA network: "); - Serial.println(ssid); - status = WiFi.begin(ssid, pass); - // if you're not connected, stop here: - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: while(true); } - // if you are connected : - else { - Serial.print("You're connected to the network"); - printCurrentNet(); - printWifiData(); + + // attempt to connect to Wifi network: + while ( status != WL_CONNECTED) { + Serial.print("Attempting to connect to WPA SSID: "); + Serial.println(ssid); + // Connect to WPA/WPA2 network: + status = WiFi.begin(ssid, pass); + + // wait 10 seconds for connection: + delay(10000); } + + // you're connected now, so print out the data: + Serial.print("You're connected to the network"); + printCurrentNet(); + printWifiData(); + } void loop() { diff --git a/WiFi/examples/ScanNetworks/ScanNetworks.ino b/WiFi/examples/ScanNetworks/ScanNetworks.ino index 7ff9410e3..efe171adb 100644 --- a/WiFi/examples/ScanNetworks/ScanNetworks.ino +++ b/WiFi/examples/ScanNetworks/ScanNetworks.ino @@ -10,7 +10,7 @@ created 13 July 2010 by dlf (Metodo2 srl) - modified 22 April 2012 + modified 31 May 2012 by Tom Igoe */ @@ -21,9 +21,15 @@ void setup() { // initialize serial and wait for the port to open: Serial.begin(9600); - - // attempt to connect using WEP encryption: - Serial.println("Initializing Wifi..."); + + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: + while(true); + } + + // Print WiFi MAC address: printMacAddress(); // scan for existing networks: diff --git a/WiFi/examples/WifiChatServer/WifiChatServer.ino b/WiFi/examples/WifiChatServer/WifiChatServer.ino index 7eb0ed545..f231073ba 100644 --- a/WiFi/examples/WifiChatServer/WifiChatServer.ino +++ b/WiFi/examples/WifiChatServer/WifiChatServer.ino @@ -14,7 +14,7 @@ created 18 Dec 2009 by David A. Mellis - modified 23 Apr 2012 + modified 31 May 2012 by Tom Igoe */ @@ -22,8 +22,8 @@ #include #include -char ssid[] = "YourNetwork"; // your network SSID (name) -char pass[] = "password"; // your network password (use for WPA, or use as key for WEP) +char ssid[] = "yourNetwork"; // your network SSID (name) +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) @@ -36,16 +36,21 @@ boolean alreadyConnected = false; // whether or not the client was connected pre void setup() { // start serial port: Serial.begin(9600); - + + // 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 ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - while(true); - } + // wait 10 seconds for connection: delay(10000); } diff --git a/WiFi/examples/WifiCosmClient/WifiCosmClient.ino b/WiFi/examples/WifiCosmClient/WifiCosmClient.ino index 210c5134a..668c9e0ac 100644 --- a/WiFi/examples/WifiCosmClient/WifiCosmClient.ino +++ b/WiFi/examples/WifiCosmClient/WifiCosmClient.ino @@ -16,7 +16,7 @@ * Wifi shield attached to pins 10, 11, 12, 13 created 13 Mar 2012 - modified 14 May 2012 + modified 31 May 2012 by Tom Igoe This code is in the public domain. @@ -49,15 +49,20 @@ void setup() { // start serial port: Serial.begin(9600); + // 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 ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - while(true); - } + // wait 10 seconds for connection: delay(10000); } diff --git a/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino b/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino index f862551aa..3634ed762 100644 --- a/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino +++ b/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino @@ -19,7 +19,7 @@ * Wifi shield attached to pins 10, 11, 12, 13 created 16 Mar 2012 - modified 14 May 2012 + modified 31 May 2012 by Tom Igoe This code is in the public domain. @@ -53,16 +53,21 @@ const unsigned long postingInterval = 10*1000; //delay between updates to cosm. void setup() { // start serial port: Serial.begin(9600); - + + // 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 ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - while(true); - } + // wait 10 seconds for connection: delay(10000); } diff --git a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino index a8b3ef002..3bbe63e8e 100644 --- a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino +++ b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino @@ -14,6 +14,7 @@ * WiFi shield attached to pins 10, 11, 12, 13 created 23 apr 2012 + modified 31 May 2012 by Tom Igoe This code is in the public domain. @@ -22,7 +23,7 @@ #include #include -char ssid[] = "YourNetwork"; // your network SSID (name) +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) @@ -51,16 +52,21 @@ void setup() { tweet.reserve(150); // start serial port: Serial.begin(9600); - + + // 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); - status = WiFi.begin(ssid, pass); - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - while(true); - } + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: + status = WiFi.begin(ssid, pass); + // wait 10 seconds for connection: delay(10000); } diff --git a/WiFi/examples/WifiWebClient/WifiWebClient.ino b/WiFi/examples/WifiWebClient/WifiWebClient.ino index 661b447ab..54ec68b71 100644 --- a/WiFi/examples/WifiWebClient/WifiWebClient.ino +++ b/WiFi/examples/WifiWebClient/WifiWebClient.ino @@ -16,7 +16,7 @@ created 13 July 2010 by dlf (Metodo2 srl) - modified 23 Apr 2012 + modified 31 May 2012 by Tom Igoe */ @@ -24,8 +24,8 @@ #include #include -char ssid[] = "YourNetwork"; // your network SSID (name) -char pass[] = "password"; // your network password (use for WPA, or use as key for WEP) +char ssid[] = "yourNetwork"; // your network SSID (name) +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 status = WL_IDLE_STATUS; @@ -41,16 +41,21 @@ WiFiClient client; void setup() { Serial.begin(9600); - + + // 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 ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - while(true); - } + // wait 10 seconds for connection: delay(10000); } diff --git a/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino b/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino index f10d2191d..1c623d12e 100644 --- a/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino +++ b/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino @@ -7,7 +7,8 @@ Circuit: * Wifi shield attached to pins 10, 11, 12, 13 - created 23 Apr 2012 + created 23 April 2012 + modifide 31 May 2012 by Tom Igoe http://arduino.cc/en/Tutorial/WifiWebClientRepeating @@ -38,15 +39,20 @@ void setup() { // start serial port: Serial.begin(9600); + // 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 ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - while(true); - } + // wait 10 seconds for connection: delay(10000); } diff --git a/WiFi/examples/WifiWebServer/WifiWebServer.ino b/WiFi/examples/WifiWebServer/WifiWebServer.ino index 34a231b5c..72bd8e0b9 100644 --- a/WiFi/examples/WifiWebServer/WifiWebServer.ino +++ b/WiFi/examples/WifiWebServer/WifiWebServer.ino @@ -13,7 +13,7 @@ created 13 July 2010 by dlf (Metodo2 srl) - modified 23 Apr 2012 + modified 31 May 2012 by Tom Igoe */ #include @@ -32,11 +32,20 @@ void setup() { // start serial port: Serial.begin(9600); + // 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); + // wait 10 seconds for connection: delay(10000); }