Arduino UNO + ESP8266 ESP-12E UART WIFI Shield

Hi Everyone!

I would like to write down my experience regarding the communication between Arduino and ESP8266 UART WIFI shield in this article.

I don’t say that it is the best solution, but it’s working, so I hope this description will be helpful for you.

Let’s get started.

I had used my Arduino UNO with ethernet shield, but it wasn’t flexible. I had to place it near to the router or I had to use a long ethernet wire. It wasn’t so practical.

So I decided to search for another solution. I have ordered a wireless ethernet shield from AliExpress for my Arduino. This shield is called ESP8266 ESP-12E UART WIFI Wireless Shield Development Board For Arduino UNO R3 (Quite long name :-) )

When I received this shield I thought that I just connect it to the Arduino, use some kind of predefined library in the Arduino IDE and it will work. Well, I had mistaken.

So I had started to search for solutions on the web, but unfortunately I didn’t find any useful helps. There were only questions without answers.

Finally I found two web sites, what helped me to find out how can I use this shield:

On the first site the author explains how can you upload your own code to the ESP8266. This type of microcontroller is on the WIFI shield.

On the second web site you can read some information about the communication between Arduino and ESP8266 via this shield. (Unfortunately I didn’t know the language what this side is written so I had translated it with Google translator. I don’t say that everything was clear for me, but I think I understood the essence of this article.)

These two sides didn’t give the final solution for me, but helped a lot, so thanks for the authors for these articles.

OK, let’s go back to the problem. How can we use these shield?

I will walk through an example I think this is the best way to understand how it works.

We need have for the following accessories:

  • Arduino UNO
  • ESP8266 ESP-12E UART WIFI Wireless Shield Development Board For Arduino UNO R3
  • FT232RL 3.3V 5.5V FTDI USB to TTL Serial Adapter Module for Arduino Mini Port
  • Arduino IDE
  • And a little C programming language knowledge

Part 1. Upload code to ESP8266

To fulfill this task I have ordered an FT232RL serial adapter module and connect it to my computer via USB cable. When I plugged this into my laptop, the Windows recognized that it is a serial device, listening on COM6 port. (It may be another port in your case.)

After I connected this serial adapter to debug port of shield like this:

But it was not enough to upload the code. I had to switch the DIP buttons to the following status:

DOWN, DOWN, UP, UP. (from 1 to 4)

OK but what does it mean?

You can see four DIP buttons on the shield. I don’t have exact explanation, but I realized that there are two kinds of status what I have to use.

  1. To upload your own code to the ESP8266 you have to set the buttons to: DOWN, DOWN, UP, UP. The fourth DIP button is not so important it just switches up and down a LED on the board what shows that the shield is DFU (Flashing) mode.

2. To connect the ESP8266 to Arduino via TX, RX communication port you have to set these buttons to UP, UP DOWN, DOWN. In this case the shield and Arduino can communicate via TX, RX pins.

Important: If you want to flash your Arduino — without remove the WIFI shield — you need to switch all these buttons to DOWN.

So now you can flash the shield. I show you a simple web server sketch:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
const char* ssid     = "YOUR_SSID";
const char* password = "YOUR_PASS";
ESP8266WebServer server(80); // HTTP server on port 80
void setup() {
Serial.begin(115200);
WiFi.disconnect(); // Disconnect AP
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password); // Connect to WIFI network
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println(".");
}
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", [](){
server.send(200, "text/plain", "Hello World");
});
server.begin(); // Start HTTP server
Serial.println("HTTP server started.");
}
void loop() {
server.handleClient();
}

Before you upload the code, you need to build it. You have to set some settings in your Arduino IDE:

  • Type of the microcontroller: NodeMCU 1.0 (ESP-12E Module) This property is necessary for the build process
  • Port (In my case COM6) This property is necessary for the upload process. I have experienced that I needed to press the reset button on the shield before uploaded the code. Without this action the upload method was failed.

If you successfully uploaded the code you can check the result, using the serial monitor (Don’t forget to set the correct baud rate.)

OK, now the WIFI shield works like a WEB server.

Part 2 Upload code to Arduino

The second thing what you have to do, to write your code for Arduino. First you have to receive the data, what is coming from ESP8266, second you need to do something with this information.

You have to use an external library, called SoftwareSerial.

#include <SoftwareSerial.h>
...

SoftwareSerial mySerial(0, 1); // RX, TX on Arduino

void setup() {
mySerial.begin(115200);

}

As you can see it is not complicated, just you have to set the number of TX, RX pins, and the baud rate. If you set this you can receive data what are coming from the WIFI shield. You need to keep your mind that there are some restrictions in the communication protocol, I will write about these later.

Now let’s see the code what is written for Arduino:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(0, 1); // RX, TX on Arduino
void setup() {
Serial.begin(115200);
mySerial.begin(115200);
}
void loop() {
if (mySerial.available()) {
String msg = mySerial.readString();
Serial.print(“Data received: “);
Serial.println(msg);
}
}

It is easy, isn’t it?

Don’t forget to switch on the first two DIP button in the WIFI shield, otherwise, the communication won’t work.

I have promised, that I write some thoughts about the serial communication.

The SoftwareSerial instance has a buffer to receive or read data. The size of buffer is 64 bytes. So don’t forget this limitation when you plan your own solution.

I have tried to use flush() method to clear the buffer. It worked, but there were a lot of “wrong characters” (I mean, that I have sent char ‘a’ and received ‘b’) in the stream. I don’t have explanation about this problem. Perhaps this problem was caused by another configuration. I have reduced the amount of data and set the Arduino Serial (for printing information to the Serial monitor) and SoftwareSerial baud rates to the same (as you can see in the example above). After these two modifications the data were arrived successfully. (in 99% of cases)

Here is an URL from Arduino official site, where you can read more information about SoftwareSerial

https://www.arduino.cc/en/Reference/SoftwareSerial

So this is my story, I hope there will be at least one person who can use this information.

Happy coding guys, and good luck.