Everything seems fine but arduino won't reach internet

Hi!

I have my Wifi Shield, which seems to be working properly (it connects to wifis and i've used it before as a webserver), but I can't reach a website. I want Arduino to do a GET against a public server I own to store temperatures but it just won't connect. I've tried other servers, I even tried the simplest thing (using the untouched example of WifiWebClient that comes with the software) and it never connects.

It says Starting connection to server... and after a few seconds: disconnecting from server. Which makes me think that client.connect(server, 80) is failing. I've tried "server" as a char[] with the URL, and also as an "IPAddress server(74,125,132,94)" just in case it's a DNS thing...but same results.

Any idea? Thanks!

Any idea?

The usual one, for posts in the Programming section. Where is your code?

As I said, i have the same problem with the default "WiFiWebClient" that comes with the Arduino suite, so i don't think there's a code problem there. I can paste it anyway for reference (I only edited my wifi login info):

#include 
#include 

char ssid[] = "myssid"; //  your network SSID (name) 
char pass[] = "mypass";    // 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,132,94);  // 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
  }
  
  // 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();
  }
}

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");
}

By the way, I just tried another default sketch example (WifiWebServer) just to try and I can't connect to it! When I try, chrome says Error 101 (net::ERR_CONNECTION_RESET)

What does the "IP Address:" show when you start the Arduino?

It shows a seemingly correct IP (192.168.1.16) giving my router settings, and checking my router admin page it shows arduino connected as a wifi device.

I've just now tried using my android as an AP just to rule out router problems... but same result :(

Does your router have a html setup page? Should be http://192.168.1.1 Can you connect to that ip with the Wifi shield?

Same result, client.connect(server, 80) returns 0.

Is there a way to get a more verbose error message?

You said the web server example works? I've helped others with WiFi shield code, and it seems to do ok as a client. http://playground.arduino.cc//Code/WiFiEmailClient What version IDE are you using?

edit: There does not seem to be any more detailed error checking, except if the shield runs out of sockets, it should display a "No Socket available" serial message. Otherwise, for all other errors, it just returns 0. This is the connect function from WiFiClient.cpp.

int WiFiClient::connect(IPAddress ip, uint16_t port) {
    _sock = getFirstSocket();
    if (_sock != NO_SOCKET_AVAIL)
    {
      ServerDrv::startClient(uint32_t(ip), port, _sock);
      WiFiClass::_state[_sock] = _sock;

      unsigned long start = millis();

      // wait 4 second for the connection to close
      while (!connected() && millis() - start < 10000)
          delay(1);

      if (!connected())
         {
          return 0;
      }
    }else{
      Serial.println("No Socket available");
      return 0;
    }
    return 1;
}

nmaxcom: By the way, I just tried another default sketch example (WifiWebServer) just to try and I can't connect to it! When I try, chrome says Error 101 (net::ERR_CONNECTION_RESET)

I don't think I'm good enough in C/C++ to write a verbose version of WiFiClient.cpp ... What to do?

Sounds like a firmware or library problem. Someone else may recognize the symptoms and know what you should do. What version IDE are you using? That seems to be an issue in some cases, just by a quick forum search.

Uhmm... I've never changed the firmware (yet). But it's true that if i recall correctly when the WiFiWebServer worked it was before my last PC reinstall so maybe I now have a different IDE version (which now i'm using 1.0.5).

Well, it seems that you got it right! I downloaded and tried Arduino 1.0.2, using the WifiWebClient sketch example and it works perfectly fine now. So I guess I'll stick with this version from now on...