Wifi library for the Arduino WiFi 101 Shield. Contribute to arduino-libraries/WiFi101 development by creating an account on GitHub.
Getting Started with the Arduino WiFi Shield
The Arduino WiFi shield allows an Arduino board to connect to the internet using the WiFi library and to read and write an SD card using the SD library.
The WiFi Library is included with the most recent version of the Arduino IDE. The firmware for the WiFi shield has changed in Arduino IDE 1.0.4. It is strongly recommended to install this update per these instructions
The WiFI library is similar to the Ethernet library and many of the function calls are the same.
On this page... (hide)
Connecting the Shield
To use the shield, mount it on top of an Arduino board (e.g. the Uno). To upload sketches to the board, connect it to your computer with a USB cable as you normally would. Once the sketch has been uploaded, you can disconnect the board from your computer and power it with an external power supply.
Digital pin 7 is used as a handshake pin between the WiFi shield and the Arduino, and should not be used.
Using the Shield With Older Boards
If you are using the WiFi shield with an Arduino earlier than the Uno rev3, you need to make the connection below for the board to work. The WiFi board uses the IOREF pin on newer Arduino pin layouts (Uno rev3, Mega2560 rev3, and later) to sense the reference voltage for the I/O pins of the board to which it is attached. If you are using the shield with an older board, you need to connect the shield's IOREF pin to 3.3V. You can do this either with a jumper wire connecting IOREF to 3.3V as shown in the photo below, or by soldering the IOREF jumper on the bottom of the shield, shown below. WARNING: If you use the solder jumper, do not connect the shield to a rev3 or later board. To be safe, remove the IOREF pin on the shield. Otherwise, you will be shorting 3.3V to 5V through the IOREF pin.
Jumping 3.3V to IOREF (recommended)
Soldering 3.3V to IOREF
Ports on the WiFi Shield
There is an onboard micro-USB connector. This is not for programming an attached Arduino, it is for updating the Atmega 32UC3 using the Atmel DFU protocol. The programming jumper adjacent to the power bus and analog inputs should be left unconnected for typical use. It is only used for DFU programming mode.
A FTDI connection enables serial communication with the 32UC3 for debugging purposes. A list of available commands can be found here.
Network Settings
The shield will connect to open networks, as well as those using WEP and WPA2 Personal encryption. The shield will not connect to networks using WPA2 Enterprise encryption.
The SSID (network name) must be broadcast for the shield to connect.
Depending on your wireless router configuration, you need different information.
- For an open (unencrypted) network, you need the SSID.
- For networks using WPA/WPA2 Personal encryption, you need the SSID and password.
- 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.
Scan for available networks
The sketch below is a good one to run the first time you use the board in a new area. This sketch will not connect to a network, but it will show you what networks the shield can view. Your WiFi shield will probably not see as many networks as a computer with a larger WiFi antenna. Once you have downloaded the sketch to your Arduino, open the serial port to see available networks.
#include <SPI.h>
#include <WiFi.h>
voidsetup(){
// initialize serial and wait for the port to open:
Serial.begin(9600);
while(!Serial);
// attempt to connect using WEP encryption:
Serial.println('Initializing Wifi...');
printMacAddress();
// scan for existing networks:
Serial.println('Scanning available networks...');
listNetworks();
}
voidloop(){
delay(10000);
// scan for existing networks:
Serial.println('Scanning available networks...');
listNetworks();
}
void printMacAddress(){
// the MAC address of your Wifi shield
byte mac[6];
// 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);
}
void listNetworks(){
// scan for nearby networks:
Serial.println('** Scan Networks **');
byte numSsid = WiFi.scanNetworks();
// print the list of networks seen:
Serial.print('number of available networks:');
Serial.println(numSsid);
// print the network number and name for each network found:
for(int thisNet =0; thisNet<numSsid; thisNet++){
Serial.print(thisNet);
Serial.print(') ');
Serial.print(WiFi.SSID(thisNet));
Serial.print('tSignal: ');
Serial.print(WiFi.RSSI(thisNet));
Serial.print(' dBm');
Serial.print('tEncryption: ');
Serial.println(WiFi.encryptionType(thisNet));
}
}
#include <WiFi.h>
voidsetup(){
// initialize serial and wait for the port to open:
Serial.begin(9600);
while(!Serial);
// attempt to connect using WEP encryption:
Serial.println('Initializing Wifi...');
printMacAddress();
// scan for existing networks:
Serial.println('Scanning available networks...');
listNetworks();
}
voidloop(){
delay(10000);
// scan for existing networks:
Serial.println('Scanning available networks...');
listNetworks();
}
void printMacAddress(){
// the MAC address of your Wifi shield
byte mac[6];
// 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);
}
void listNetworks(){
// scan for nearby networks:
Serial.println('** Scan Networks **');
byte numSsid = WiFi.scanNetworks();
// print the list of networks seen:
Serial.print('number of available networks:');
Serial.println(numSsid);
// print the network number and name for each network found:
for(int thisNet =0; thisNet<numSsid; thisNet++){
Serial.print(thisNet);
Serial.print(') ');
Serial.print(WiFi.SSID(thisNet));
Serial.print('tSignal: ');
Serial.print(WiFi.RSSI(thisNet));
Serial.print(' dBm');
Serial.print('tEncryption: ');
Serial.println(WiFi.encryptionType(thisNet));
}
}
Open network example
The sketch below shows you how to initiate a connection with an open network named 'yourNetwork'.
#include <WiFi.h>
char ssid[]='yourNetwork';// the name of your network
int status = WL_IDLE_STATUS;// the Wifi radio's status
voidsetup(){
// initialize serial:
Serial.begin(9600);
// attempt to connect to an open network:
Serial.println('Attempting to connect to open network...');
status = WiFi.begin(ssid);
// if you're not connected, stop here:
if( status != WL_CONNECTED){
Serial.println('Couldn't get a wifi connection');
while(true);
}
// if you are connected :
else{
Serial.print('Connected to the network');
}
}
voidloop(){
// do nothing
}
char ssid[]='yourNetwork';// the name of your network
int status = WL_IDLE_STATUS;// the Wifi radio's status
voidsetup(){
// initialize serial:
Serial.begin(9600);
// attempt to connect to an open network:
Serial.println('Attempting to connect to open network...');
status = WiFi.begin(ssid);
// if you're not connected, stop here:
if( status != WL_CONNECTED){
Serial.println('Couldn't get a wifi connection');
while(true);
}
// if you are connected :
else{
Serial.print('Connected to the network');
}
}
voidloop(){
// do nothing
}
WPA network example
The example below shows how to connect to a WPA/WPA2 Personal encrypted network named 'yourNetwork' with a password '12345678'.
#include <WiFi.h>
char ssid[]='yourNetwork';// your network SSID (name)
char pass[]='12345678';// your network password
int status = WL_IDLE_STATUS;// the Wifi radio's status
voidsetup(){
// initialize serial:
Serial.begin(9600);
// attempt to connect using WPA2 encryption:
Serial.println('Attempting to connect to WPA network...');
status = WiFi.begin(ssid, pass);
// if you're not connected, stop here:
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{
Serial.println('Connected to network');
}
}
voidloop(){
// do nothing
}
char ssid[]='yourNetwork';// your network SSID (name)
char pass[]='12345678';// your network password
int status = WL_IDLE_STATUS;// the Wifi radio's status
voidsetup(){
// initialize serial:
Serial.begin(9600);
// attempt to connect using WPA2 encryption:
Serial.println('Attempting to connect to WPA network...');
status = WiFi.begin(ssid, pass);
// if you're not connected, stop here:
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{
Serial.println('Connected to network');
}
}
voidloop(){
// do nothing
}
WEP Network example
Your router will most likely have a settings dialog similar to the one below for generating the WEP key based on an ASCII passphrase:
If you do not have access to your router's administrative tools, consult your network administrator.
Each key is 10 or 26 hexadecimal digits long (40 or 128 bits) and paired with a key number. For example, a 40-bit key, ABBADEAF01 will work, but ABBADEAF won't work (too short) and ABBAISDEAF won't work (I and S are not hexadecimal characters).vFor 128-bit, you need a string that is 26 characters long. D0D0DEADF00DABBADEAFBEADED will work because it's 26 characters, all in the 0-9, A-F range.
NB : WEP provides a basic encryption mechanism, but it can be cracked. If you require strong encryption for your network, it is recommended you use WPA
The example below shows how to connect to a WEP encrypted network named 'yourNetwork' with a hex key of 'ABBADEAF01', and a key index of 0.
#include <WiFi.h>
char ssid[]='yourNetwork';// your network SSID (name)
char key[]='ABBADEAF01';// your network key
int keyIndex =0;//your network key Index number
int status = WL_IDLE_STATUS;// the Wifi radio's status
voidsetup(){
// initialize serial:
Serial.begin(9600);
// attempt to connect using WEP encryption:
Serial.println('Attempting to connect to WEP network...');
status = WiFi.begin(ssid, keyIndex, key);
// if you're not connected, stop here:
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{
Serial.println('Connected to network');
}
}
voidloop(){
// do nothing
}
char ssid[]='yourNetwork';// your network SSID (name)
char key[]='ABBADEAF01';// your network key
int keyIndex =0;//your network key Index number
int status = WL_IDLE_STATUS;// the Wifi radio's status
voidsetup(){
// initialize serial:
Serial.begin(9600);
// attempt to connect using WEP encryption:
Serial.println('Attempting to connect to WEP network...');
status = WiFi.begin(ssid, keyIndex, key);
// if you're not connected, stop here:
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{
Serial.println('Connected to network');
}
}
voidloop(){
// do nothing
}
SD Card and SPI
The WiFI Shield includes a micro-SD card slot, which can be interfaced with using the SD library. The SS for the SD card is pin 4.
Arduino communicates with the shield using the SPI bus. This is on digital pins 11, 12, and 13 on the Uno and pins 50, 51, and 52 on the Mega. On both boards, pin 10 is used as SS. On the Mega, the hardware SS pin, 53, is not used, but it must be kept as an output or the SPI interface won't work.
Updating firmware on the shield
Please follow this guide to update the firmware on your shield.
Next steps
Refer to the WiFi library page for more information on the functionality of the shield, as well as further examples.
The text of the Arduino getting started guide is licensed under aCreative Commons Attribution-ShareAlike 3.0 License. Code samples in the guide are released into the public domain.
The text of the Arduino getting started guide is licensed under aCreative Commons Attribution-ShareAlike 3.0 License. Code samples in the guide are released into the public domain.
Connect your microcontroller to the internet by adding ESP8266 WiFi module!
- 290,833 views
- 112 comments
- 181 respects
Components and supplies
| × | 1 | |
| × | 1 | |
| × | 1 | |
| × | 1 | |
| × | 2 |
Apps and online services
About this project
Updated as of February 1, 2019
Connect your Arduino to the internet by adding ESP8266 WiFi Module!
The ESP8266 Wifi module is a complete WiFi network where you can easily connect as a serving Wi- Fi adapter, wireless internet access interface to any microcontroller based design on its simple connectivity through Serial Communication or UART interface.
Adding this module to your Arduino UNO will open you to more and exciting projects.
What is the process?
There are a lot of ways to use ESP866 for communication. Some may use it to send / receive data online or regularly upload data. For this tutorial I will just show you how can we communicate to Arduino wirelessly using your phone (Android or iPhone) . This will be done offline so no need to have internet connection.
Our ESP8266 will serve as Access Point (AP Mode), meaning it will provide access to Wi-Fi network to other devices (stations) and connects them further to a wired network
The process it pretty simple. Use your phone to send any command to Arduino. With the help of ESP8266, everything will work wirelessly.
Upload Sketch
First of all, upload my sample Arduino sketch.
If you want to know whats on my sketch go to About the code section
You can download the attached sketch or get the updated version on my github repo.
If you don't know how to upload a sketch then Read here
Do I need to flash my ESP?
No. Just use the stock firmware. But in case you need then read here
Built Circuit
Connect the pins more described on this Pin table
Follow these steps.
- connect both ESP's VCC/3.3V/Power Pin and Enable Pin (red wires) to 10K resistor then to Uno's +3.3V power pin.
- connect ESP's Ground/GND Pin (Black Wire) to Uno's Ground/GND Pin.
- connect ESP's TX (green wire) to Uno's Pin 3
- cconnect ESP's RX (blue wire) to 1K resistor then to Uno's Pin 2.
- cconnect ESP's RX (blue wire) to 1K resistor then to Uno's GND Pin.
Set up connection
Once everything is set up, you would notice that your ESP8266 Wifi will be available within the range of your phone.
1. Download TCP Client for Android
You can download any TCP Client available in Play Store but I used TCP Client by Sollae Systems
2. From your phone, Connect to your ESP8266 Wifi
If your ESP8266 wifi is not displayed from available wifi networks, make sure your Arduino is running and everything is connected correctly. If not troubleshoot your ESP by following its documentation.
Usually the name of the wifi / ssid will start in ESP following its version name, mine is ESP11.
3. Once connected, get the Static IP address.
Important:You can check theESP's IP by going to Wifi Settings of your phone, and click the network info.
The default IP Address in AP mode is 192.168.4.1 .
You can change the static IP by following this Wifi.config() reference.
4. Open TCP Client you downloaded earlier.
Create connection by clicking connect , Add ESP's IP and port 80 like this:
80 is the port that I used for our ESP Server, but you can change it by replacing 80 to any port number you want from our code on line 23
6. Wait for the TCP Console to say 'Connected'.
Talk to you Arduino Uno via Smart Phone
Once connected send request by typing the following code to TCP Client:
esp8266: <any AT Commands>
Communication with ESP8266 is via Attention Command or AT Commands.
Check the AT Commands table attached to review the codes.
Or turn on built-in LED using command
LEDON
Or turn off built-in LED using command
LEDOFF
Or just say
HELLO
You can change what response from what you send depending on the logic you put into the code.
Important: esp8266, LEDON, LEDOFF, and HELLO is my custom command identifier. If you put anything other than these, it will return ErrRead.
ErrRead means no command identifier matched from the message you sent.
The ErrRead message is coded on Line 64.
About the circuit
![Wireless ad hoc network setup Wireless ad hoc network setup](/uploads/1/2/5/8/125861422/440286041.png)
ESP's Power pin is labeled VIN on my ESP11 however for some version it could be 3.3V or Power or VCC.
You will also need to power ESP's CH_EN Pin or Enable Pin in order for it to work.
IMPORTANT do not use voltageto ESP more than 3.3VC
ESP8266 is strictly uses 3.3 V. More that that will destroy the module.
Since Arduino is 5V, I had to put a voltage divider. Those are the resistors.
ESP's TX is connected to Uno's RX which means whatever we want to transmit(TX) in ESP will Receive(RX) by Uno, and vice versa.
Upon building this circuit, we now ready to start WIFI with Arduino UNO.
Note: If you attach serial debugger via USB cable or you open the COM port, the communication between ESP and Arduino will be disturbed and cannot work.
So before flashing Uno, remove the Rx/Tx of ESP first.
About the Code
![Arduino Wifi Shield Ad Hoc Network Arduino Wifi Shield Ad Hoc Network](/uploads/1/2/5/8/125861422/291825626.jpg)
Important: There are different types of ESP8266. Please change the baud rate on the attached code on Line 16 based on what your ESP8266 uses.
All our request will be read and parse on the loop() function
You can see from above that I used my function find(<received message>,<message you want to find> ) to interpret the message and to tell Arduino which code to call.
If you want to communicate with Arduino UNO or ask todo something, just add your condition. for example :
I added some function to communicate with ESP8266.
Thats it! Now that you've learnt how to communicate to Arduino Uno through Wifi, you may now explore more Internet of Things projects. Maximize the power of this WiFI module and be a Maker!. Feel free to add revisions to my github repo
Sample Code
You can find the code on my github repo.
Feel free to send pull requests :)
Expert Mode
If you are familiar with building mobile apps, web apps, web services or web development in general, you can make client applications that can send TCP request to ESP.
Sample apps you can make are Remote Control, Web Control Panel, Chat Bot, Push the button app etc etc.
You can also make 2 of these project so they can talk to each other.
Or make another device that sends TCP request to command the Arduino Uno.
Have fun! :)
Code
ESP8266 Sample Communication with Arduino UNO
Schematics
connect both ESP's VCC/3.3V/Power Pin and Enable Pin (red wires) to 10K resistor then to Uno's +3.3V power pin.
connect ESP's Ground/GND Pin (Black Wire) to Uno's Ground/GND Pin.
connect ESP's TX (green wire) to Uno's Pin 3
cconnect ESP's RX (blue wire) to 1K resistor then to Uno's Pin 2.
cconnect ESP's RX (blue wire) to 1K resistor then to Uno's GND Pin.
connect ESP's Ground/GND Pin (Black Wire) to Uno's Ground/GND Pin.
connect ESP's TX (green wire) to Uno's Pin 3
cconnect ESP's RX (blue wire) to 1K resistor then to Uno's Pin 2.
cconnect ESP's RX (blue wire) to 1K resistor then to Uno's GND Pin.
Author
Jefferson Paredes
- 7 projects
- 63 followers
Published on
July 6, 2017Write a comment
Members who respect this project
and 173 others
See similar projectsyou might like
Table of contents
Write a comment