WiFi Web Client
#include <>
WiFi Web Client
Connect to a remote webserver.
LAST REVISION: 10/05/2022, 08:00 AM
This example shows you how to make a HTTP request using a WiFi shield. It
returns a Google search for the term "Arduino". The results of this search are
viewable as HTML through your Arduino Software (IDE) serial window.
This example is written for a network using WPA encryption. For WEP or WPA,
change the Wifi.begin() call accordingly.
Hardware Required
Arduino WiFi Shield
Shield-compatible Arduino board
Circuit
The WiFi shield uses pins 10, 11, 12, and 13 for the SPI connection to the
HDG104 module. Digital pin 4 is used to control the chip select pin on the SD
card.
You should have access to a 802.11b/g wireless network that connects to the
internet for this example. You will need to change the network settings in the
sketch to correspond to your particular networks SSID.
For networks using WPA/WPA2 Personal encryption, you need the SSID and password.
The shield will not connect to networks using WPA2 Enterprise encryption.
WEP network passwords are hexadecimal strings known as keys. A WEP network can
have 4 different keys; each key is assigned a "Key Index" value. For WEP
encrypted networks, you need the SSID, the key, and key number.
WiFiShield bb
image developed using Fritzing. For more circuit examples, see the Fritzing
project page
In the above image, the Arduino board would be stacked below the WiFi shield.
Code
/*H*******************************************************
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>
//************************* DEFINES ************************************
//************************* PROTOTYPES ************************************
void printWifiStatus();
//************************* VARIABLES ************************************
char ssid[] = "yourNetwork"; // NETWORK SSID (NAME)
char pass[] = "secretPassword";// NETWORK PASSWORD (USE FOR WPA, OR KEY FOR WEP)
int keyIndex = 0; // 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 NUMERIC IP INSTEAD OF NAME FOR 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 ETHERNET CLIENT LIBRARY
// WITH IP ADDRESS AND PORT OF SERVER
// WANT TO CONNECT TO ( PORT 80 IS DEFAULT FOR HTTP):
WiFiClient client;
/*F********************************************************************
*
**********************************************************************/
void
setup()
{
Serial.begin( 9600 ); // INIT SERIAL AND WAIT FOR PORT TO OPEN
while( !Serial )
{
;// WAIT FOR SERIAL PORT TO CONNECT. nEEDED FOR NATIVE USB PORT ONLY
}
if( WiFi.status() == WL_NO_SHIELD) // CHECK FOR PRESENCE OF THE SHIELD
{
Serial.println("WiFi shield not present");
while( true ); // DON'T CONTINUE
}
String fv = WiFi.firmwareVersion();
if( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
while( status != WL_CONNECTED ) // ATTEMPT TO CONNECT TO Wifi NETWORK
{
Serial.print( "Attempting to connect to SSID: " );
Serial.println( ssid );
// CONNECT TO WPA/WPA2 NETWORK. CHANGE LINE USING OPEN OR WEP NETWORK
status = WiFi.begin( ssid, pass );
delay( 10000 ); // WAIT 10 SECONDS FOR CONNECTION
}
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");
client.println( "GET /search?q=arduino HTTP/1.1");// MAKE HTTP REQUEST
client.println( "Host: www.google.com" );
client.println( "Connection: close" );
client.println();
}
}
/*F********************************************************************
*
**********************************************************************/
void
loop()
{
// IF THERE ARE CHARS AVAILABLE FROM SERVER, READ THEM AND PRINT THEM
while( client.available() )
{
char c = client.read();
Serial.write( c );
}
if( !client.connected() ) // IF SERVER'S DISCONNECTED, STOP CLIENT
{
Serial.println();
Serial.println( "disconnecting from server.");
client.stop();
while( true ); // DO NOTHING FOREVERMORE:
}
}
/*F********************************************************************
*
**********************************************************************/
void
printWifiStatus()
{
// PRINT SSID OF NETWORK ATTACHED TO
Serial.print( "SSID: " );
Serial.println( WiFi.SSID() );
IPAddress ip = WiFi.localIP(); // PRINT YOUR WiFi SHIELD'S IP ADDRESS
Serial.print( "IP Address: " );
Serial.println( ip );
long rssi = WiFi.RSSI();
Serial.print( "signal strength (RSSI):");
Serial.print( rssi ); // PRINT RECEIVED SIGNAL STRENGTH
Serial.println( " dBm" );
}