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.
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 CircuitConnect 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.
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 the ESP'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 PhoneOnce 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
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 voltage to 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
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
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
}
}
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 :
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 function 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
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 CodeYou can find the code on my github repo.
Feel free to send pull requests :)
Expert ModeIf 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! :)
Comments