Reference.WiFiClientAvailable History

Hide minor edits - Show changes to markup

November 23, 2011, at 02:10 AM by Scott Fitzgerald -
Added lines 9-10:

available() inherits from the Stream utility class.

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 28-31 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-39 from:

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;

to:

char servername[]="google.com"; // Google

WiFiClient client;

Changed line 53 from:
    if (client.connect(server, 80)) {
to:
    if (client.connect(servername, 80)) {
Changed line 81 from:

@]

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

WiFi : Client class

available()

Description

Returns the number of bytes available for reading (that is, the amount of data that has been written to the client by the server it is connected to).

Syntax

client.available()

Parameters

none

Returns

The number of bytes available.


/*
  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