Reference.WiFiClientConnected History

Hide minor edits - Show changes to markup

November 23, 2011, at 01:43 AM by Scott Fitzgerald -
Changed lines 23-39 from:

[@ /*

  Web client

 This sketch connects to a website (http://www.google.com)
 using a WiFi shield.

 Circuit:
 * WiFi shield attached

 created 13 July 2010
 by Domenico La Fauci
 modified 21 May 2011
 by Tom Igoe
 */

to:
Changed lines 26-29 from:
  1. include <IPAddress.h>

char ssid[] = "yourNetwork"; char pass[] = "secretPassword";

to:

char ssid[] = "myNetwork"; // your network SSID (name) char pass[] = "myPassword"; // your network password

Changed lines 33-37 from:

// 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): Client client;

to:

// Initialize the client library WiFiClient client;

Changed lines 62-64 from:
  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available()) {
to:
   if (client.available()) {
Deleted line 66:
  // if the server's disconnected, stop the client:
Deleted lines 70-71:
    // do nothing forevermore:
Changed line 75 from:

@]

to:
November 22, 2011, at 03:56 AM by Scott Fitzgerald -
Added lines 1-98:

WiFi : Client class

connected()

Description

Whether or not the client is connected. Note that a client is considered connected if the connection has been closed but there is still unread data.

Syntax

client.connected()

Parameters

none

Returns

Returns true if the client is connected, false if not.

Example

/*
  Web client

 This sketch connects to a website (http://www.google.com)
 using a WiFi shield.

 Circuit:
 * WiFi shield attached

 created 13 July 2010
 by Domenico La Fauci
 modified 21 May 2011
 by Tom Igoe
 */


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

char ssid[] = "yourNetwork";
char pass[] = "secretPassword";
int status = WL_IDLE_STATUS;
IPAddress server(74,125,115,105);  // Google

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
Client client;

void setup() {
  Serial.begin(9600);
  Serial.println("Attempting to connect to WPA network...");
  Serial.print("SSID: ");
  Serial.println(ssid);

  status = WiFi.begin(ssid, pass);
  if ( status != WL_CONNECTED) { 
    Serial.println("Couldn't get a wifi connection");
    // don't do anything else:
    while(true);
  } 
  else {
    Serial.println("Connected to wifi");
    Serial.println("\nStarting connection...");
    // if you get a connection, report back via serial:
    if (client.connect(server, 80)) {
      Serial.println("connected");
      // Make a HTTP request:
      client.println("GET /search?q=arduino HTTP/1.0");
      client.println();
    }
  }
}

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

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    for(;;)
      ;
  }
}

Share