Wifi Shield can't connect

I just got an Arduino and Wifi Shield. A couple basic programs (LED and temperature sensor) work fine with and without the wifi attached.

The Wifi ScanNetwork example works.

However, the WebClient example fails every time.
It connects to the router and gets an IP, but client.connect always returns false.

The code is basically the provided example with some added checks to see what fails

output:

Attempting to connect to SSID: networkname
Connected to wifi
SSID: networkname
IP Address: 192.168.0.174
signal strength (RSSI):-57 dBm

Starting connection to server…
FAIL

Code:

#include <SPI.h>
#include <WiFi.h>

char ssid = “networkname”; // your network SSID (name)
char pass = “pass”; // 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,239,34); // numeric IP for Google (no DNS)
char server = “www.google.com”; // name address for Google (using DNS)

// Initialize the Ethernet client library
WiFiClient client;

void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);

// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println(“WiFi shield not present”);
quit();
}

// 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);
}
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.1”);
client.println(“Host: www.google.com”);
client.println(“Connection: close”);
client.println();
}
else {
Serial.println(“FAIL”); // <— always fails!!!
quit();
}
}

void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
while (client.available()) {
char c = client.read();
Serial.write(c);
}

// if the server’s disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println(“disconnecting from server.”);
client.stop();
quit();
}
}

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: ");
Serial.println(ip);

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print(“signal strength (RSSI):”);
Serial.print(rssi);
Serial.println(" dBm");
}

void quit(){
while(true);
}

I have the same issue. Interested to hear solution. Can id SSIDs but cannot connect.

What version IDE are you using? http://forum.arduino.cc/index.php?topic=176094.0

I copied and pasted your code into a new sketch and only changed the WiFi SSID and password to mine and it worked. I’m using Arduino version 1.0.5 (Macintosh), but I flashed new WiFi Shield firmware as described here: http://arduino.cc/en/Hacking/WiFiShieldFirmwareUpgrading from Windows using the Flip utility.

You may have a problem with DNS either through your router or your ISP. Try using just the IPAddress. You might try connecting to a local IP, such as 192.168.0.1 and see what happens. The page probably won’t be found, and you’ll get a 404 error back, but it may help show what’s not working.

Here’s some code you can use to delay the sketch from running until you get the Serial Monitor going. (The Serial Monitor will reset the Arduino board, interrupting the automatic start that happens after uploading your sketch, which can cause things to happen before you’re ready.) Press Return in Serial Monitor. Be sure you have the option for line endings to be Carriage Return or Both NL and CR.

Hope this helps.

-Bob

void waitForKey()
{
  // Wait for a carriage return from the serial port before proceeding.
  Serial.print("Waiting for CR...");
  while (Serial.available() == 0) {
    if (Serial.read() == '\r') break;    // Exit loop if we get a CR
  }
  while (Serial.available() != 0) {
    Serial.read();  // Flush any remaining input
  }
  Serial.println("Proceeding.");
}

// Put this everywhere you want to wait for a Return:
  waitForKey();

I've the same problem since I've upgradet the firmware of the wifi shield as described here http://arduino.cc/en/Hacking/WiFiShieldFirmwareUpgrading . Even when I try to connect by IP-Address, the client.connect(ipaddress, 80) returns false. Does anybody hav a solution for that? Is there a way to downgrade the wifi shield?

I am having the same issue with the most basic WiFiClient sample code that I have found: http://arduino.cc/en/Reference/WiFiClient

It does not matter if I use a URL or IP address. client.connect(server,80) always fails and will never get to the line "connected".

Anyone any idea?

This problem happens because the IDE version. Try the 1.0.2. This worked to me. :) Best

Same problem!

Changed to 1.0.2 (as mentioned above) and it worked for me!

LOL. That was after I realized I had the wrong server address.

I have the same problem. And because I use the Arduino DUE I can't change to 1.0.2....maybe someone have an other solution?

I use the Arduino DUE and the IDE 1.5.6-r2, Sample Code from Arduino WiFi Shield Firmware 1.0.0 (updatet a few mins ago)

Can connect to the router but the client.connect(server, 80) doesn't work.

Same here. With 1.0.5 I can obtain IP, but I can't connect to a client or to the example wifi server on the board. With 1.0.2 everything is ok!

napolux: Same here. With 1.0.5 I can obtain IP, but I can't connect to a client or to the example wifi server on the board. With 1.0.2 everything is ok!

Then you probably have not upgraded your wifi shield firmware. This addition to the setup code will tell you. If it reports firmware version 1.0.0, then you need to upgrade to use IDE v1.0.5 or above. If it reports firmware version 1.1.0, you are upgraded.

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

  // check firmware version
  Serial.print(F("Firmware version: "));
  Serial.println(WiFi.firmwareVersion());

I am trying the example of web-client on my WiFi shield but the output is connecting to server and then directly disconnecting. I am using 1.0.5 IDE and updated the firmware to 1.1.0. I think there is problem with WiFiClient client(server,80); which fails every time. i tried the simple server example blinking led l9. that works fine so that's why I am thinking the problem is of the client.

I think there is problem with WiFiClient client(server,80); which fails every time.

Do you mean "client.connect(server,80)" fails? If so, that is an indication the firmware upgrade was not successful.

Did you try the code I posted to check the firmware version?

Yes I checked the version by code you posted it gives 1.1.0

Try this sketch and post what the serial monitor displays.

/*
  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 31 May 2012
 by Tom Igoe
 */


#include <SPI.h>
#include <WiFi.h>

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;
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "www.google.com";    // name address for Google (using DNS)

// 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() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600); 
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  
  // disable SD SPI
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);

  // 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);
  } 
  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.1");
    client.println("Host: www.google.com");
    client.println("Connection: close");
    client.println();
  }
  else {
    Serial.println("connection failed");
    while(true);
  }
}

void loop() {
  // if there are incoming bytes available 
  // from the server, read them and print them:
  while (client.available()) {
    char c = client.read();
    Serial.write(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: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

I noticed that my IP is changing every time i call for the printwifistatus. is it normal?

Attempting to connect to SSID: agrawal Connected to wifi SSID: agrawal IP Address: 192.168.173.105 signal strength (RSSI):-49 dBm

Starting connection to server... connection failed

and sorry thank for reply Tim :)

It is a bit unusual to get a new ip every connection, but not what I would consider a problem.

Maybe your DNS server is not responding fast enough. Look in the code I posted and change these entries. Note I uncommented the IPAddress server and also changed the ip to a current Google ip . Then I commented out the char server.

IPAddress server(74,125,227,244);  // numeric IP for Google (no DNS)
// char server[] = "www.google.com";    // name address for Google (using DNS)

How does that do?

Connected to wifi SSID: agrawal IP Address: 192.168.173.91 signal strength (RSSI):-53 dBm

Starting connection to server... connection failed

before connected to WiFi there are sometimes random line of characters goes on until resetting the board.