Home Page | Language Reference (extended) | SD Reference | Libraries | Comparison | Changes


WiFi : Client class

connect()

Description

Connect to the IP address and port specified in the constructor. The return value indicates success or failure. connect() also supports DNS lookups when using a domain name (ex:google.com).

Syntax

client.connect(ip, port)
client.connect(URL, port)

Parameters

ip: the IP address that the client will connect to (array of 4 bytes)

URL: the domain name the client will connect to (string, ex.:"arduino.cc")

port: the port that the client will connect to (int)

Returns

Returns true if the connection succeeds, false if not.

Example

#include <SPI.h>
#include <WiFi.h> char ssid[ ] = "myNetwork" ;          //  your network SSID (name)
char pass[ ] = "myPassword" ;   // your network password int status = WL_IDLE_STATUS;
char servername[ ] = "google.com" ;  // remote server we will connect to WiFiClient 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 (servername, 80 ) ) {
      Serial.println ( "connected" ) ;
      // Make a HTTP request:
      client.println ( "GET /search?q=arduino HTTP/1.0" ) ;
      client.println ( ) ;
    }
  }
} void loop ( ) { }

Reference Home

Corrections, suggestions, and new documentation should be posted to the Forum.

The text of the Arduino reference is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.