ESP8266 Wi-Fi Module Revisited

A while back I posted some information about the ESP8266 Wi-Fi Module. You can view that post here. I mentioned that for testing it, an Arduino with multiple hardware serial ports would be ideal. Well, I just got an Arduino Leonardo which matches that criteria.

Here is how I hooked it up:

 

ESP8266 Fritzing Diagramyou can get the Fritzing file here. (The Fritzing part for the ESP8266 can be found here.)  Notice that the TX and RX serial connections ‘crossover’. Not crossing over is a common mistake when first hooking up a serial communications device.

The Arduino sketch I used was chosen via

Examples->04.Communication->MultiSerialMega

 

void setup()  {
  Serial.begin(9600);
  Serial1.begin(115200);
}
void loop()  {
   if (Serial1.available()) {
      int inByte = Serial1.read();
      Serial.write(inByte);
   }
 
  if (Serial.available()) {
     int inByte = Serial.read();
     Serial1.write(inByte);
  }
}

You only need to change the serial BAUD rate for Serial1 (the ESP8266) from the default of 9600 to 115200. Once you upload the sketch to the Arduino, run the serial monitor and the commands you type will be sent to the ESP8266. Any response from the ESP8266 will be ‘echoed’ in the serial monitor.

Testing the ESP8266 on an Arduino with two hardware serial ports is much easier than my original way. Now I can add sensors to this circuit and send the information they gather through the ESP8266 to the WEB.

 

UPDATE: 12/29/2014

 

I ran into a common problem with the ESP8266. Sometimes the ESP8266 will return ‘busy’ when you enter a command. The ESP8266 is essentially locked up (i.e. frozen) at this point. To recover you can power off the device and power back on. However that is not a programatic solution. Fortunately there is a way to ‘RESET’ the ESP8266 from code if this situation occurs.

 

Here is how to wire the ESP8266:

 

ESP8266 Reset Circuit

 

 

 

 

 

 

You can download the Fritzing diagram source here.

 

Here is a sketch showing the Reset function.

 

/*
Arduino Leonardo <--> ESP8266
Eamil:  earl@microcontrollerelectonics.com
*/

#define SSID "your_SSID"
#define PASS "your_password"
#define DST_IP "192.168.0.100"
#define CONNECT_ATTEMPTS 5
#define RST 8
#define CHP 9
#define LED 13

void setup() {

  pinMode(RST, OUTPUT);
  pinMode(LED, OUTPUT);
  pinMode(CHP, OUTPUT);

  reset();

  Serial.begin(9600);
  Serial1.begin(115200);

  delay(5000);

  Serial1.println("AT+RST");
  delay(1000);

  if (Serial1.find("ready")) Serial.println("Module is ready");
  else {
    Serial.println("ESP8266 Module did not respond.");
    Serial.println("Enter Commands Manually.");
    while (1) chk_serial_io();
  }
  delay(1000);
  boolean connected = false;
  for (int i = 0; i < CONNECT_ATTEMPTS; i++) {

    if (connectWiFi()) {
      connected = true;
      break;
    }
  }
  if (!connected) {
    Serial.println("Enter Commands Manually.");
    while (1) chk_serial_io();
  }
  delay(5000);
  Serial1.println("AT+CIFSR");
  Serial.println("ip address:");
  while (Serial1.available()) Serial.write(Serial1.read());
  Serial1.println("AT+CIPMUX=0");
}

void reset() {
  digitalWrite(LED,HIGH);
  digitalWrite(CHP,HIGH);
  digitalWrite(RST,LOW);
  delay(100);
  digitalWrite(RST,HIGH);
  delay(1000);
  digitalWrite(LED,LOW);
}

void loop() {
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += DST_IP;
  cmd += "\",80";
  Serial1.println(cmd);
  Serial.println(cmd);
  if (Serial1.find("Error")) return;
  cmd = "GET / HTTP/1.0\r\n\r\n";
  Serial1.print("AT+CIPSEND=");
  Serial1.println(cmd.length());
  if (Serial1.find(">")) {
    Serial.print(">");

  } else {
    Serial1.println("AT+CIPCLOSE");
    Serial.println("connect timeout");
    delay(1000);
    return;
  }
  Serial1.print(cmd);
  delay(2000);
  while (Serial1.available()) {
    char c = Serial1.read();
    Serial.write(c);
    if (c == '\r') Serial.print('\n');
  }
  Serial.println("====");
  delay(1000);
}

void chk_serial_io() {
  if (Serial1.available()) {
//    if (Serial1.find("busy")) {
//      Serial.println("Resetting...");
//      reset();
//    }
    int inByte = Serial1.read();
    Serial.write(inByte);
  }
  if (Serial.available()) {
    int inByte = Serial.read();
    Serial1.write(inByte);
  }
}

boolean connectWiFi() {
  Serial1.println("AT+CWMODE=1");
  String cmd = "AT+CWJAP=\"";
  cmd += SSID;
  cmd += "\",\"";
  cmd += PASS;
  cmd += "\"";
  Serial.println(cmd);
  Serial1.println(cmd);
  delay(2000);
  if (Serial1.find("OK")) {
    Serial.println("OK, WiFi Connected.");
    return true;
  } else {
    Serial.println("Can not connect to WiFi.");
    return false;
  }
}

 

 

20 comments

Skip to comment form

    • Rumpel on August 1, 2015 at 2:27 pm
    • Reply

    Hi, thanks for this post! I’m curious though if you ran into any problems regarding the voltage?
    i.e. the 3V3 current supply of the Leonardo only supports 50mA, while the wifi module may pull up to ~200mA?
    Then also I’ve heard that the ESP8266 RX PIN doesn’t like 5V either?

    1. Hello!
      I have several of these modules (bought months apart) and have not had any problems powering them from the Arduino. Although I have not had them on for extended periods of time. I did not try any of them at 5V. You might ask on the ESP8266 Web site forum http://www.esp8266.com
      earl

    • Guest on October 13, 2015 at 9:10 pm
    • Reply

    I’m trying to work with esp8266. Strange problem, libraries don’t working. I tried one, than go to your example (you at last use RESET and CP_PD pins).

    Your example fully not worked, until I not change
    if (Serial1.find(“ready”))… to if (Serial1.find(“OK”)) ..
    (Message of ESP to arduino: OK \n ets Jan 8 2013, rst … boot … load … len … room… tail .. chksum … loa”

    Next it not connect to Wi-Fi: first message “Can not connect to WiFi”, second “OK, WiFi Connected.”, and not get TCP connection and router say that nothing connected.
    Response of AT+CWJAP=”……
    first: “ets Jan 8 2013, rst … boot … load … len … room… tail .. chksum … loa”
    second: “OK”

    Maybe I need library for another version of ESP8266 or update firmware, or problem in something else.

      • earl on October 14, 2015 at 1:36 pm
        Author
      • Reply

      There are various versions of the firmware on the ESP8266 modules so that can account for the difference you are seeing. I connect to the ESP8266 with a terminal program and give it commands to test it out first to see what it responds then create a script to automate what I want it do to.

    • Hemant Sharma on October 17, 2015 at 12:34 pm
    • Reply

    Can you please let me know that how I configure my Arduino Uno 3 with this module (esp8266).

    Thanks

      • earl on October 19, 2015 at 1:41 pm
        Author
      • Reply

      Hi! I think you will find your answer reading this post: http://forum.arduino.cc/index.php?topic=283043.0 as it documents very well the answer to your question.

    • Jose on January 23, 2016 at 10:09 am
    • Reply

    Thank you Very much!
    Changing the ports somehow got my arduino to work ! I had a lot of problmes with ESP 8266, because it seems that Leonardo es kinda troublemaker when concerning to communications..But you solved it! Thanks!!

  1. Thank you so much for explaining, i am am very beginner with ESP 8266 can you please explain Serial1 what dose that for?

    1. Hi, One serial port (serial) is used to communicate with the user (over usb) and the other serial port (serial1) is used to communicate with the ESP8266. So to be able to communicate with two things, a human 🙂 and the ESP8266,you need an arduino with two serial ports. Some have even more ports labeled (serial,serial1, serial2, etc.) Earl

    • Rasmus Bidstrup on August 21, 2017 at 12:17 pm
    • Reply

    Hi, finally some code I got working 🙂

    I would very much like to turn on a LED via a HTTPRequest call or API, would that be possible to with your code, and adding some more to it?

      • earl on August 22, 2017 at 5:03 am
        Author
      • Reply

      Hello! Yes it is possible. You might be interested in my post HERE which turns on a LED from the ESP8266 and the two posts prior to that.

    • Juliana on November 6, 2017 at 8:43 am
    • Reply

    Hello i hope you answer this soon enough, i have been trying for hours to get it to work but it hasn’t. I ams. Using the esp for the first time, i am not sure if it has the firmware and if it doesn’t, how can i install it into the module. Nothing ive tried with this module seems to work, i only have this and a arduino Leonardo, i want it to play music on a android app depending on a number or a string i send

      • earl on November 6, 2017 at 12:07 pm
        Author
      • Reply

      Hi, you did not say what kind of ESP8266 module you are using or how you hooked it up. More specific information is needed about your configuration (wiring) is needed before we can help. Earl

    • adithya on November 13, 2017 at 5:26 pm
    • Reply

    I tried this but I am getting board not responding repeatedly

      • earl on November 14, 2017 at 4:41 am
        Author
      • Reply

      Hi, What specific hardware are you using and how is it hooked up? Earl

    • Juan on December 12, 2017 at 4:34 pm
    • Reply

    Thank you for your support, I’m started in the arduino land, I have a leonardo and ESP-01, I’m working trying communicate the leonardo with the ESP-01 but I can’t, I have some things in mind, one of them is try controlling devices whit my cel phone, after read a lot of blogs, I’m very confused, I have separated 3v3 external power module, and some people told me that the GND need be the same, I don’t know how need connect the ESP-01 and the leonardo using the same GND, I don’t know if I need use librarys and which ones.

    for today I practice your solution and tell you tomorrow how it works.

    • Lestat on December 21, 2017 at 6:22 pm
    • Reply

    Hello! from Chile jeje. I’m new in arduino (begin with a Leonardo), i try with this tutorial, but print “ESP8266 Module did not respond.”, what could I do? I tried with the two connection schemes, but the ESP8266 is still in red light 🙁 help me plis u.u

      • earl on December 22, 2017 at 5:46 am
        Author
      • Reply

      Hi!

      A lot depends on what firmware you have in your ESP8266. Some firmware versions do not respond “Ready” after the “AT+RST” command. Please try the MultiSerialMega sketch that I refer to in the post to make sure you can communicate with the ESP8266 via manual commands. Once you establish that you can communicate with it, then you can try the other sketch to automate commands. (Then if your ESP8266 responds differently to “AT+RST” you can change the sketch respectively.)

      Earl

    • Josh on June 25, 2018 at 4:37 am
    • Reply

    HOW CAN I HOOK IT UP PROPERLY, I tried every detail in your step by step process but it doesn’t seem to work with what it needs to do, EVEN BLINKING ITS LED

    1. Hi Josh, I’d need a lot more information from you setup in order to help. What EP8266 module do you have? What firmware version? What are you connecting to it? Which arduino? Can you attach a link to a photo of your connections? The more info the better someone can help.

      Earl

Leave a Reply

Your email address will not be published.