Add WiFi to Arduino UNO with ESP8266 WiFi Module

Add WiFi to Arduino UNO with ESP8266 WiFi Module

The ESP8266 WiFi module is a full Wi-Fi network that can be readily connected to any microcontroller-based design as a serving Wi-Fi adapter and wireless internet access interface through its simple serial communication, or UART interface.

By adding this module to your Arduino UNO, you will be able to create more fascinating projects.

Parts List

You will need the following items to proceed with this project:

You can use the preceding links to find all the parts for your projects at the best price!

What is the process?

The ESP866 may be used for communication in a variety of ways. Some people could use it to regularly upload data, communicate, or receive data online. In this tutorial, I'll merely show you how to interact wirelessly with Arduino using your phone. There is no need for an internet connection since this will be done offline.

Our ESP8266 will serve as an access point (AP Mode), meaning that it will provide other devices (stations) access to the Wi-Fi network and further link them to a wired network.

The process is pretty simple. Send any command to the Arduino using your phone. Everything will work wirelessly thanks to the ESP8266's help.

Communication Process

Upload Sketch

First of all, upload my sample Arduino sketch.

If you want to know what's in my sketch, go to the 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 it, read here.

Built Circuit

Connect the pins as described in this Pin table.

ESP8266ResistorArduino UNO
GNDGND
VCC/3.3V/Power10K3.3V
CH_EN/Enable10K3.3V
TXPIN 3
RX1KPIN 2
RX1KGND

Follow these steps:

  • Connect both ESP's VCC/3.3V/Power Pin and Enable Pin (red wires) to a 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.
  • Connect the ESP's RX (blue wire) to the 1K resistor, then to Uno's Pin 2.
  • Connect the ESP's RX (blue wire) to the 1K resistor, then to the Uno's GND Pin.
esp uno diagram
ESP8266 Pinout

Set up connection

You'll notice that your ESP8266 WiFi will be available within a range of your phone after everything is set up.

1. Download TCP Client for Android:

You can download any TCP Client available in the Play Store, but I used TCP Client by Sollae Systems.

2. Connect to your ESP8266 WiFi from your phone:

Make sure your Arduino is running and everything is connected properly if your ESP8266's wifi is not displayed among the accessible wifi networks. If not, try troubleshooting your ESP by following its manual.

Usually, the name of the wifi / ssid will start in ESP, following its version name; mine is ESP11.

3. Get the Static IP address after connecting:

Important: You can check the ESP's IP by going to the WiFi settings of your phone and clicking 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 the TCP Client you downloaded earlier:

Create a connection by clicking connect. Add the ESP's IP and port 80 like this:

Open TCP Client you downloaded earlier

I chose port 80 for our ESP Server, but you can change it by replacing 80 in our code on line 23 with whatever port number you choose.

6. Wait for the TCP Console to say “Connected”:

Talk to you Arduino Uno via Smart Phone

Once connected, send a request by typing the following code to the TCP Client:

esp8266: <any AT Commands>

Communication with the ESP8266 is via Attention Commands or AT Commands.

Attention Commands
telnet client

To review the codes, see the AT Commands table attached.

Or turn on the built-in LED using the command LEDON .

Or turn off the built-in LED using the command LEDOFF .

Or just say HELLO .

Depending on the logic you put into the code, you may change the response from what you send.

Important: esp8266, LEDON, LEDOFF, and HELLO are my custom command identifiers. If you put anything other than these, it will return ErrRead.

ErrRead means no command identifier matched the message you sent.

Note: The ErrRead message is coded on Line 64.

About the circuit

ESP's Power pin is labeled VIN on my ESP11; however, for some versions, it could be 3.3V, Power, or VCC.

You will also need to power ESP's CH_EN Pin or Enable Pin for it to work.

Important: do not use voltage to ESP more than 3.3VC

The ESP8266 strictly uses 3.3 V. More than that will destroy the module.

Since Arduino is 5V, I had to put in 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 are now ready to start WiFi with the Arduino UNO.

Note: The communication between the ESP and Arduino will be disturbed and will not work if you attach the serial debugger via a USB cable or open the COM port.

So before flashing Uno, remove the Rx/Tx of the ESP first.

About the Code

The ESP8266 comes in different types. Based on the baud rate used by your ESP8266, please change Line 16 of the attached code.

All our requests will be read and parsed by the loop() function.

if(wifiSerial.available()>0){ 
   String message = readWifiSerialMessage(); 
   if(find(message,"esp8266:")){ 
      String result = sendToWifi(message.substring(8,message.length()),responseTime,DEBUG); 
     if(find(result,"OK")) 
       sendData("\n"+result); 
     else 
       sendData("\nErrRead");               //At command ERROR CODE for Failed Executing statement 
   }else 
   if(find(message,"HELLO")){  //receives HELLO from wifi 
       sendData("\\nHI!");    //arduino says HI 
   }else if(find(message,"LEDON")){ 
     digitalWrite(13,HIGH); 
   }else if(find(message,"LEDOFF")){ 
     digitalWrite(13,LOW); 
   } 
   else{ 
     sendData("\nErrRead");                 //Command ERROR CODE for UNABLE TO READ 
   } 
 }

As you can see from the example above, I used my function find(<received message>,<message you want to find>) to interpret the message and tell Arduino which code to run.

Just add your condition if you want to ask Arduino UNO to do a task or communicate with it. For example:

if(find(message,"MY CODE")){
     // I found 'MY CODE' from received message
     // lets do something here
}
if(find(message,"A")){
     // I found 'A' from received message
     // lets do something here
}

I added some functions to communicate with ESP8266.

/*
* Name: sendData
* Description: Function used to send string to tcp client using cipsend
* Params: 
* Returns: void
*/
void sendData(String str){
 String len="";
 len+=str.length();
 sendToWifi("AT+CIPSEND=0,"+len,responseTime,DEBUG);
 delay(100);
 sendToWifi(str,responseTime,DEBUG);
 delay(100);
 sendToWifi("AT+CIPCLOSE=5",responseTime,DEBUG);
}
/*
* Name: find
* Description: Function used to match two string
* Params: 
* Returns: true if match else false
*/
boolean find(String string, String value){
 if(string.indexOf(value)>=0)
   return true;
 return false;
}
/*
* Name: readSerialMessage
* Description: Function used to read data from Arduino Serial.
* Params: 
* Returns: The response from the Arduino (if there is a reponse)
*/
String  readSerialMessage(){
 char value[100]; 
 int index_count =0;
 while(Serial.available()>0){
   value[index_count]=Serial.read();
   index_count++;
   value[index_count] = '\0'; // Null terminate the string
 }
 String str(value);
 str.trim();
 return str;
}
/*
* Name: readWifiSerialMessage
* Description: Function used to read data from ESP8266 Serial.
* Params: 
* Returns: The response from the esp8266 (if there is a reponse)
*/
String  readWifiSerialMessage(){
 char value[100]; 
 int index_count =0;
 while(wifiSerial.available()>0){
   value[index_count]=wifiSerial.read();
   index_count++;
   value[index_count] = '\0'; // Null terminate the string
 }
 String str(value);
 str.trim();
 return str;
}
/*
* Name: sendToWifi
* Description: Function used to send data to ESP8266.
* Params: command - the data/command to send; timeout - the time to wait for a response; debug - print to Serial window?(true = yes, false = no)
* Returns: The response from the esp8266 (if there is a reponse)
*/
String sendToWifi(String command, const int timeout, boolean debug){
 String response = "";
 wifiSerial.println(command); // send the read character to the esp8266
 long int time = millis();
 while( (time+timeout) > millis())
 {
   while(wifiSerial.available())
   {
   // The esp has data so display its output to the serial window 
   char c = wifiSerial.read(); // read the next character.
   response+=c;
   }  
 }
 if(debug)
 {
   Serial.println(response);
 }
 return response;
}
/*
* Name: sendToUno
* Description: Function used to send data to Arduino.
* Params: command - the data/command to send; timeout - the time to wait for a response; debug - print to Serial window?(true = yes, false = no)
* Returns: The response from the esp8266 (if there is a reponse)
*/
String sendToUno(String command, const int timeout, boolean debug){
 String response = "";
 Serial.println(command); // send the read character to the esp8266
 long int time = millis();
 while( (time+timeout) > millis())
 {
   while(Serial.available())
   {
     // The esp has data so display its output to the serial window 
     char c = Serial.read(); // read the next character.
     response+=c;
   }  
 }
 if(debug)
 {
   Serial.println(response);
 }
 return response;
The story

That's all! You may now explore other Internet of Things projects that you've learned how to communicate with the Arduino Uno over WiFi. Make the most of this WiFi module's power and become a Maker! Please add revisions to my GitHub repo.

Sample Code

You can find the code in my GitHub repo. Feel free to send pull requests.

Expert Mode

You can create client applications that can submit TCP requests to ESP if you are familiar with creating mobile apps, web apps, web services, or web development in general.

Remote Control, Web Control Panel, Chat Bot, Push the button App, and so on are examples of apps you can develop.

For them to talk to one another, you may also make two of these projects. To command the Arduino Uno, make another device that sends TCP requests.

Oh hi there It’s nice to meet you.

Sign up to receive awesome content in your inbox, every month.

We don’t spam! Read our privacy policy for more info.

Leave a Reply

Your email address will not be published. Required fields are marked *

ESP8266 Home Automation Projects

Leverage the power of this tiny WiFi chip to build exciting smart home projects