Initializes the WiFi library's network settings and provides the current status.
WiFi.begin(); WiFi.begin( ssid ); WiFi.begin( ssid, pass ); WiFi.begin( ssid, keyIndex, key );
ssid: | the SSID (Service Set Identifier) is the name of the WiFi network you want to connect to. |
keyIndex: | WEP encrypted networks can hold up to 4 different keys. This identifies which key you are going to use. |
key: | a hexadecimal string used as a security code for WEP encrypted networks. |
pass: | WPA encrypted networks use a password in the form of a string for security. |
/*F******************************************************************** * **********************************************************************/ #include <WiFi.h> char ssid[] = "yourNetwork"; // SSID OF YOUR NETWORK char pass[] = "secretPassword"; // PASSWORD OF YOUR WPA NETWORK /*F******************************************************************** * **********************************************************************/ void setup() { WiFi.begin(ssid, pass); } /*F******************************************************************** * **********************************************************************/ void loop() {}
WiFi.config() allows you to configure a static IP address as well as change the DNS, gateway, and subnet addresses on the WiFi shield.
Unlike WiFi.begin() which automatically configures the WiFi shield to use DHCP, WiFi.config() allows you to manually set the network address of the shield.
Calling WiFi.config() before WiFi.begin() forces begin() to configure the WiFi shield with the network addresses specified in config().
You can call WiFi.config() after WiFi.begin(), but the shield will initialize with begin() in the default DHCP mode. Once the config() method is called, it will change the network address as requested.
WiFi.config(ip); WiFi.config(ip, dns); WiFi.config(ip, dns, gateway); WiFi.config(ip, dns, gateway, subnet);
ip: | the IP address of the device (array of 4 bytes) |
dns: | the address for a DNS server. |
gateway: | the IP address of the network gateway (array of 4 bytes). optional: defaults to the device IP address with the last octet set to 1 |
subnet: | the subnet mask of the network (array of 4 bytes). optional: defaults to 255.255.255.0 |
/*F******************************************************************** * This example shows how to set the static IP address, 192.168.0.177, of the LAN network to the WiFi shield: **********************************************************************/ #include <SPI.h> #include <WiFi.h> // IP address for shield: IPAddress ip(192, 168, 0, 177); char ssid[] = "yourNetwork"; // your network SSID (name) char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP) int status = WL_IDLE_STATUS; /*F******************************************************************** * **********************************************************************/ 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"); while( true ); // don't continue } WiFi.config( ip ); // attempt to connect to Wifi network: while( status != WL_CONNECTED ) { Serial.print( "Attempting to connect to SSID: "); Serial.println( ssid); status = WiFi.begin( ssid, pass ); // Connect to WPA/WPA2 network. Change this line if using open or WEP network: delay( 10000 ); // wait 10 seconds for connection: } Serial.print("IP Address: "); // print your WiFi shield's IP address: Serial.println( WiFi.localIP() ); } /*F******************************************************************** * **********************************************************************/ void loop() {}
WiFi.setDNS( dns_server1 ) WiFi.setDNS( dns_server1, dns_server2)
dns_server1: | the IP address of the primary DNS server |
dns_server2: | the IP address of the secondary DNS server |
/*F******************************************************************** * This example shows how to set the Google DNS (8.8.8.8). You can set it as an object IPAddress. **********************************************************************/ #include <SPI.h> #include <WiFi.h> //************************* DEFINES ************************************ //************************* PROTOTYPES ************************************ //************************* VARIABLES ************************************ // the IP address for the shield: IPAddress dns( 8, 8, 8, 8); //Google dns char ssid[] = "yourNetwork"; // your network SSID (name) char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP) int status = WL_IDLE_STATUS; /*F******************************************************************** * **********************************************************************/ 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"); while( true ); // don't continue } // 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 ); delay( 10000 ); // wait 10 seconds for connection: } WiFi.setDNS( dns ); // print your WiFi shield's IP address: Serial.print( "Dns configured."); } /*F******************************************************************** * **********************************************************************/ void loop() { }
WiFi.SSID(); WiFi.SSID( wifiAccessPoint )
wifiAccessPoint: | specifies from which network to get the information |
A string containing the SSID the WiFi shield is currently connected to.
/*F******************************************************************** * **********************************************************************/ #include <SPI.h> #include <WiFi.h> //************************* DEFINES ************************************ //************************* PROTOTYPES ************************************ //************************* VARIABLES ************************************ char ssid[] = "yourNetwork"; //SSID of your network int status = WL_IDLE_STATUS; // the Wifi radio's status /*F******************************************************************** * **********************************************************************/ void setup() { // initialize serial: Serial.begin( BAUD ); Serial.println("Scanning available networks..."); scanNetworks(); // scan for existing networks: Serial.println( "Attempting to connect to open network..."); status = WiFi.begin(ssid); // attempt to connect using WEP encryption: Serial.print( "SSID: " ); Serial.println( ssid ); } /*F******************************************************************** * **********************************************************************/ void loop() {} /*F******************************************************************** * **********************************************************************/ void scanNetworks() { Serial.println( "** Scan Networks **"); byte numSsid = WiFi.scanNetworks(); // scan for nearby networks Serial.print( "SSID List:" ); // print list of networks seen Serial.println( numSsid ); for( int thisNet = 0; thisNet<numSsid; thisNet++) { // PRINT NETWORK NUMBER AND NAME FOR EACH NETWORK FOUND Serial.print( thisNet ); Serial.print( ") Network: " ); Serial.println( WiFi.SSID( thisNet ) ); } }
Gets the MAC address of the routher you are connected to
WiFi.BSSID( bssid );
bssid: | 6 byte array |
A byte array containing the MAC address of the router the WiFi shield is currently connected to.
/*H******************************************************************** * **********************************************************************/ #include <WiFi.h> //************************* DEFINES ************************************ //************************* PROTOTYPES ************************************ //************************* VARIABLES ************************************ char ssid[] = "yourNetwork"; // SSID OF YOUR NETWORK char pass[] = "secretPassword"; // PASSWORD OF YOUR WPA NETWORK /*F******************************************************************** * **********************************************************************/ void setup() { WiFi.begin(ssid, pass); if( status != WL_CONNECTED) { Serial.println( "Couldn't get a wifi connection"); while( true ); } // IF YOU ARE CONNECTED, PRINT OUT INFO ABOUT THE CONNECTION else { // PRINT MAC ADDRESS OF ROUTER YOU'RE ATTACHED TO byte bssid[6]; WiFi.BSSID( bssid ); Serial.print( "BSSID: "); Serial.print( bssid[5], HEX); Serial.print( ":" ); Serial.print( bssid[4], HEX); Serial.print( ":" ); Serial.print( bssid[3], HEX); Serial.print( ":" ); Serial.print( bssid[2], HEX); Serial.print( ":" ); Serial.print( bssid[1], HEX); Serial.print( ":" ); Serial.println( bssid[0], HEX); } } /*F******************************************************************** * **********************************************************************/ void loop() {}
Gets the signal strength of the connection to the router
WiFi.RSSI(); WiFi.RSSI( wifiAccessPoint );
wifiAccessPoint: | specifies from which network to get the information |
long : the current RSSI /Received Signal Strength in dBm
/*F******************************************************************** * **********************************************************************/ #include <SPI.h> #include <WiFi.h> char ssid[] = "yourNetwork"; //SSID of your network char pass[] = "secretPassword"; //password of your WPA Network /*F******************************************************************** * **********************************************************************/ void setup() { WiFi.begin( ssid, pass ); if( WiFi.status() != WL_CONNECTED) { Serial.println( "Couldn't get a wifi connection"); while( true ); } // IF YOU ARE CONNECTED, PRINT OUT INFO ABOUT CONNECTION else { long rssi = WiFi.RSSI(); Serial.print("RSSI:"); Serial.println( rssi ); // PRINT RECEIVED SIGNAL STRENGTH } } /*F******************************************************************** * **********************************************************************/ void loop() {}
Gets the encryption type of the current network
WiFi.encryptionType(); WiFi.encryptionType( wifiAccessPoint );
wifiAccessPoint: | specifies which network to get information from |
byte : value represents type of encryption
TKIP (WPA) = 2
WEP = 5
CCMP (WPA) = 4
NONE = 7
AUTO = 8
/*F******************************************************************** * **********************************************************************/ #include <SPI.h> #include <WiFi.h> char ssid[] = "yourNetwork"; //SSID of your network char pass[] = "secretPassword"; //password of your WPA Network /*F******************************************************************** * **********************************************************************/ void setup() { WiFi.begin( ssid, pass ); if( status != WL_CONNECTED) { Serial.println( "Couldn't get a wifi connection"); while( true ); } else // IF CONNECTED, PRINT OUT INFO ABOUT CONNECTION { byte encryption = WiFi.encryptionType(); Serial.print( "Encryption Type:"); Serial.println( encryption, HEX ); // PRINT ENCRYPTION TYPE } } /*F******************************************************************** * **********************************************************************/ void loop() {}
Scans for available WiFi networks and returns the discovered number
WiFi.scanNetworks();
none
byte : number of discovered networks
/*F******************************************************************** * **********************************************************************/ #include <SPI.h> #include <WiFi.h> char ssid[] = "yourNetwork"; // the name of your network /*F******************************************************************** * **********************************************************************/ void setup() { Serial.begin( 9600 ); int status = WiFi.begin( ssid ); if( status != WL_CONNECTED) { Serial.println( "Couldn't get a WiFi connection"); while( true ); } else { // IF CONNECTED, SCAN FOR WiFi NETWORKS AND PRINT NUMBER DISCOVERED Serial.println( "** Scan Networks **" ); byte numSsid = WiFi.scanNetworks(); Serial.print( "Number of available WiFi networks discovered:"); Serial.println( numSsid ); } } /*F******************************************************************** * **********************************************************************/ void loop() {}
WL_CONNECTED: | assigned when connected to a WiFi network; |
WL_NO_SHIELD: | assigned when no WiFi shield is present; |
WL_IDLE_STATUS: | it is a temporary status assigned when WiFi.begin() is called and remains active until the number of attempts expires (resulting in WL_CONNECT_FAILED) or a connection is established (resulting in WL_CONNECTED); |
WL_NO_SSID_AVAIL: | assigned when no SSID are available; |
WL_SCAN_COMPLETED: | assigned when the scan networks is completed; |
WL_CONNECT_FAILED: | assigned when the connection fails for all the attempts; |
WL_CONNECTION_LOST: | assigned when the connection is lost; |
WL_DISCONNECTED: | assigned when disconnected from a network; |
/*F******************************************************************** * **********************************************************************/ #include <SPI.h> #include <WiFi.h> char ssid[] = "yourNetwork"; // your network SSID (name) char key[] = "D0D0DEADF00DABBADEAFBEADED"; // your network key int keyIndex = 0; // your network key Index number int status = WL_IDLE_STATUS; // the Wifi radio's status /*F******************************************************************** * **********************************************************************/ 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 if( WiFi.status() == WL_NO_SHIELD) // CHECK FOR PRESENCE OF SHIELD { // DON'T CONTINUE Serial.println( "WiFi shield not present"); while( true ); } while( status != WL_CONNECTED) { Serial.print("Attempting to connect to WEP network, SSID: "); Serial.println( ssid ); // ATTEMPT TO CONNECT TO Wifi NETWORK status = WiFi.begin( ssid, keyIndex, key); delay( 10000 ); // WAIT 10 SECONDS FOR CONNECTION } Serial.print("You're connected to the network"); // ONCE YOU ARE CONNECTED } /*F******************************************************************** * **********************************************************************/ void loop() { delay( 10000 ); // CHECK NETWORK STATUS CONNECTION ONCE EVERY 10 SECONDS Serial.println( WiFi.status() ); }
mac | : | a 6 byte array to hold the MAC address |
/*F******************************************************************** * **********************************************************************/ #include <SPI.h> #include <WiFi.h> char ssid[] = "yourNetwork"; // the name of your network int status = WL_IDLE_STATUS; // the Wifi radio's status byte mac[6]; // the MAC address of your Wifi shield /*F******************************************************************** * **********************************************************************/ void setup() { Serial.begin( BAUD ); status = WiFi.begin( ssid ); if( status != WL_CONNECTED) { Serial.println( "Couldn't get a wifi connection"); while( true ); } else // IF CONNECTED, PRINT YOUR MAC ADDRESS { WiFi.macAddress( mac ); Serial.print( "MAC: "); Serial.print( mac[5], HEX); Serial.print( ":" ); Serial.print( mac[4], HEX); Serial.print( ":" ); Serial.print( mac[3], HEX); Serial.print( ":" ); Serial.print( mac[2], HEX); Serial.print( ":" ); Serial.print( mac[1], HEX); Serial.print( ":" ); Serial.println( mac[0], HEX); } } /*F******************************************************************** * **********************************************************************/ void loop() {}
/*F******************************************************************** * **********************************************************************/ #include <WiFi.h> char ssid[] = "yourNetwork"; //SSID of your network int status = WL_IDLE_STATUS; // the Wifi radio's status IPAddress ip; // the IP address of your shield /*F******************************************************************** * **********************************************************************/ void setup() { Serial.begin( BAUD ); // INITIALIZE SERIAL WiFi.begin( ssid ); if( status != WL_CONNECTED ) { Serial.println( "Couldn't get a wifi connection"); while( true ); } else // IF CONNECTED, PRINT OUT INFO ABOUT CONNECTION { ip = WiFi.localIP(); Serial.println( ip ); // PRINT LOCAL IP ADDRESS } } /*F******************************************************************** * **********************************************************************/ void loop() {}
/*H******************************************************************** * **********************************************************************/ #include <WiFi.h> int status = WL_IDLE_STATUS; // the Wifi radio's status //SSID of your network char ssid[] = "yourNetwork"; //password of your WPA Network char pass[] = "secretPassword"; IPAddress ip; IPAddress subnet; IPAddress gateway; /*F******************************************************************** * **********************************************************************/ void setup() { WiFi.begin( ssid, pass ); if( status != WL_CONNECTED) { Serial.println( "Couldn't get a wifi connection"); while( true ); } else // IF CONNECTED, PRINT OUT INFO ABOUT CONNECTION { subnet = WiFi.subnetMask(); Serial.print( "NETMASK: "); Serial.println( subnet ); // PRINT YOUR SUBNET MASK } } /*F******************************************************************** * **********************************************************************/ void loop() { }
/*H******************************************************************** * **********************************************************************/ #include <SPI.h> #include <WiFi.h> int status = WL_IDLE_STATUS; // the Wifi radio's status //SSID of your network char ssid[] = "yourNetwork"; //password of your WPA Network char pass[] = "secretPassword"; IPAddress gateway; /*F******************************************************************** * **********************************************************************/ void setup() { Serial.begin(9600); WiFi.begin(ssid, pass); if( status != WL_CONNECTED) { Serial.println("Couldn't get a wifi connection"); while(true); } // if you are connected, print out info about the connection: else { // print your gateway address: gateway = WiFi.gatewayIP(); Serial.print("GATEWAY: "); Serial.println(gateway); } } /*F******************************************************************** * **********************************************************************/ void loop() {}
Server is the base class for all WiFi server based calls. It is not called directly, but invoked whenever you use a function that relies on it.
port: | the port to listen on (int) |
/*H******************************************************************** * **********************************************************************/ #include <SPI.h> #include <WiFi.h> char ssid[] = "myNetwork"; // your network SSID (name) char pass[] = "myPassword"; // your network password int status = WL_IDLE_STATUS; WiFiServer server(80); /*F******************************************************************** * **********************************************************************/ void setup() { // initialize serial: Serial.begin( BAUD ); 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"); while( true ); } else { server.begin(); Serial.print("Connected to wifi. My address:"); IPAddress myAddress = WiFi.localIP(); Serial.println(myAddress); } } /*F******************************************************************** * **********************************************************************/ void loop() { }
/*F******************************************************************** * **********************************************************************/ #include <SPI.h> #include <WiFi.h> char ssid[] = "lamaison"; // your network SSID (name) char pass[] = "tenantaccess247"; // your network password int status = WL_IDLE_STATUS; WiFiServer server(80); /*F******************************************************************** * **********************************************************************/ void setup() { // initialize serial: Serial.begin( BAUD ); 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"); while( true ); } else { server.begin(); Serial.print( "Connected to wifi. My address:"); IPAddress myAddress = WiFi.localIP(); Serial.println( myAddress ); } } /*F******************************************************************** * **********************************************************************/ void loop() { }
/*F******************************************************************** * **********************************************************************/ #include <SPI.h> #include <WiFi.h> char ssid[] = "Network"; // your network SSID (name) char pass[] = "myPassword"; // your network password int status = WL_IDLE_STATUS; WiFiServer server(80); /*F******************************************************************** * **********************************************************************/ void setup() { // initialize serial: Serial.begin( BAUD ); 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"); while( true ); } else { server.begin(); Serial.print( "Connected to wifi. My address:"); IPAddress myAddress = WiFi.localIP(); Serial.println( myAddress ); } } /*F******************************************************************** * **********************************************************************/ void loop() { // listen for incoming clients WiFiClient client = server.available(); if( client ) { if( client.connected()) Serial.println( "Connected to client"); client.stop(); // CLOSE CONNECTION } }
/*F******************************************************************** * **********************************************************************/ #include <SPI.h> #include <WiFi.h> char ssid[] = "yourNetwork"; char pass[] = "yourPassword"; int status = WL_IDLE_STATUS; WiFiServer server( 80 ); /*F******************************************************************** * **********************************************************************/ void setup() { // initialize serial: Serial.begin( BAUD ); 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"); while( true ); } else server.begin(); } /*F******************************************************************** * **********************************************************************/ void loop() { WiFiClient client = server.available(); // listen for incoming clients if( client == true) { // read from incoming client and write back to clients connected to server server.write( client.read() ); } }
Functions
/*F******************************************************************** * **********************************************************************/ #include <SPI.h> #include <WiFi.h> char ssid[] = "myNetwork"; // your network SSID (name) char pass[] = "myPassword"; // your network password int status = WL_IDLE_STATUS; IPAddress server(74,125,115,105); // Google // Initialize the client library WiFiClient client; /*F******************************************************************** * **********************************************************************/ void setup() { Serial.begin( BAUD ); 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"); while( true ); // DON'T DO ANYTHING ELSE } else { Serial.println( "Connected to wifi" ); Serial.println( "\nStarting connection..." ); if( client.connect(server, 80)) { // IF GOT CONNECTION, REPORT BACK VIA SERIAL Serial.println( "connected" ); client.println( "GET /search?q=arduino HTTP/1.0" ); client.println(); // MAKE A HTTP REQUEST } } } /*F******************************************************************** * **********************************************************************/ void loop() {}
/*F******************************************************************** * **********************************************************************/ #include <SPI.h> #include <WiFi.h> char ssid[] = "myNetwork"; // your network SSID (name) char pass[] = "myPassword"; // your network password int status = WL_IDLE_STATUS; IPAddress server(74,125,115,105); // Google // Initialize the client library WiFiClient client; /*F******************************************************************** * **********************************************************************/ void setup() { Serial.begin( BAUD ); 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"); while( true ); // DON'T DO ANYTHING ELSE } else { Serial.println( "Connected to wifi"); Serial.println( "\nStarting connection..."); if( client.connect( server, 80)) { // GOT CONNECTION, REPORT BACK VIA SERIAL Serial.println( "connected"); client.println( "GET /search?q=arduino HTTP/1.0"); client.println(); // MAKE A HTTP REQUEST } } } /*F******************************************************************** * **********************************************************************/ void loop() { if( client.available() ) { char c = client.read(); Serial.print( c ); } if( !client.connected()) { Serial.println(); Serial.println( "disconnecting."); client.stop(); for( ; ; ) ; } }
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) |
/*F******************************************************************** * **********************************************************************/ #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; /*F******************************************************************** * **********************************************************************/ void setup() { Serial.begin( BAUD ); 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"); while( true ); // don't do anything else: } else { Serial.println( "Connected to wifi"); Serial.println( "\nStarting connection..."); if( client.connect(servername, 80)) { // IF GOT CONNECTION, REPORT BACK VIA SERIAL Serial.println( "connected"); client.println( "GET /search?q=arduino HTTP/1.0"); client.println(); // MAKE A HTTP REQUEST } } } /*F******************************************************************** * **********************************************************************/ void loop() { }
/*F******************************************************************** * **********************************************************************/ #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"; // Google WiFiClient client; /*F******************************************************************** * **********************************************************************/ void setup() { Serial.begin( BAUD ); 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"); while( true ); // DON'T DO ANYTHING ELSE } else { Serial.println( "Connected to wifi"); Serial.println( "\nStarting connection..."); if( client.connect( servername, 80)) { // GOT A CONNECTION, REPORT BACK VIA SERIAL Serial.println( "connected"); client.println( "GET /search?q=arduino HTTP/1.0"); client.println(); // MAKE A HTTP REQUEST } } } /*F******************************************************************** * **********************************************************************/ void loop() { // if there are incoming bytes available // from server, read them and print them: if( client.available()) { char c = client.read(); Serial.print( c ); } if( !client.connected()) { // SERVER'S DISCONNECTED, STOP CLIENT Serial.println(); Serial.println( "disconnecting."); client.stop(); for(;;) // DO NOTHING FOREVERMORE ; } }