Add WiFi to Arduino UNO

19,151

102

18

Posted

Introduction: Add WiFi to Arduino UNO

Connect your mincrocontroller to the internet by adding ESP8266 WiFi Module!

The ESP8266 Wifi module is a complete Wi­Fi 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

Step 1: Build Circuit

Connect the pins more described on attached Pin table

Follow these steps.

  • connect the red wire to VIN(3.3V) to the +3.3V power from the microcontroller.
  • connect the black wire to the ground.
  • connect the green wire to the TX of the Wifi module and microcontroller
  • connect the yellow wite to the RX of the wifi module and microcontroller

About the circuit

ESP8266 is strictly powered only to 3.3 V. More than that will destroy the module.

IMPORTANT do not use voltages more than 3.3V!

Connect the VIN to 3.3v to power up and also the ENABLE pin to enable the module.

TX is connected to RX which means whatever we want to Transmit in ESP8266 will Receive by Arduino UNO. And vice versa for RX to TX. Upon building this circuit, we now ready to start WIFI with Arduino UNO.

Step 2: Talk to Module

Communication with ESP8266 is via Attention Command or AT Commands.

Check the AT Commands table attached to review the codes.

Setting your board

1. Upload the sketch sample attached here to your Arduino UNO.

2. Download Telnet Client for Android

3. Connect to your ESP8266 Wifi

4. Once connected, get the IP address

5. Open Telnet Client on Android phone.

6. create connection by clicking connect , Add IP and port 80.

Once connected send request by typing:

esp8266: <any AT Commands>

Or turn on built in LED using command

LEDON

Or turn off built in LED using command

LEDOFF

Or just say

HELLO

Step 3: About the Code

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 } }

If you want to communicate with Arduino UNO or ask todo something, just add your condition

if(find(message,"")){
//something do do 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 response) */ 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 UNO. * 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 UNO (if there is a response) */ 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; }

Thats it! Now that you've learn how to communicate with ESP8266, 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 on my github repo

Share

    Recommendations

    • Microcontroller Contest

      Microcontroller Contest
    • Science of Cooking

      Science of Cooking
    • Pocket-Sized Contest

      Pocket-Sized Contest
    user

    We have a be nice policy.
    Please be positive and constructive.

    Tips

    2 Questions

    I know this is somewhat of an old post, but aren't the Arduino UNO serial pins at a logic level of 5V? Won't that hurt the ESP module without a level shifter in-between?

    0

    You can use a bidirectional logic level converter in between the rx and tx connections

    The tx and rx of most newer ESP01's are fairly 5v tolerant. That said, I always used a voltage divider, logic level converter, 4050, or adapter module.

    I used 3.3v pin to power the ESP. :)

    0

    @imjeffparedes i fought there is no 3.3v pin in uno ??

    9 Comments

    Very nice software. A problem with the hardware setup, the arduino TX line goes to 5v and that will eventually damage the esp. You should add a simple voltage divider to drop the voltage.

    Can we add two Wi-Fi modules to the arduino UNO? i need one to control the robot through local host , and the other to send the sensor data to cloud through internet. plz

    I've read in other places that the 3.3v pin is only 50ma, but the 8266 needs up to 200ma to function correctly. Is this true?

    Not sure but I tried this setup and it works.

    How get ip address ESP8266 v1?

    Hi, thanks for your instructors can I know if it is possible to connect GoPro hero 3+ via wifi to Arduino Uno, I am trying this but wasn't work with me, any suggestion? thank you :(

    can you do something like this on IOS?

    You can use iTelnet or other telnet app as alternative that can be found on app store.