Introduction: ESP8266 As a Microcontroller

About: We bring hardware products to life, specializing in engineering, prototyping, and mass manufacturing. We've worked with over 20 Kickstarter projects.

As it was already mentioned in the Getting Started with ESP8266 ESP-01 tutorial, the Wi-Fi module is fully programmable, allowing us to use it as a microcontroller and manipulate inputs and outputs.

In this tutorial we are going to show how to program the ESP module via Arduino UNO to blink an LED and control it from a wireless device.

Step 1: Arduino Setup

First we need to set up the Arduino environment to make it compatible with the ESP module. It is required to have Arduino version 1.6.4 or higher in order to be able to install the ESP’s platform packages.

Once we open Arduino IDE we go to preferences and in the “Additional Boards Manager URLs:” section we type the following line:

http://arduino.esp8266.com/package_esp8266com_inde...

After clicking on “OK,” we go to Tools -> Board -> Boards manager and install the ESP8266 platform.

The Arduino can now be used to program the ESP8266 Wi-Fi module.

Step 2: Setup to Blink the LED

The ESP8266 ESP-01 has two GPIOs: GPIO0 and GPIO2.

Every time we want to program the ESP module, it is required to ground GPIO0 before downloading a code.

An easy way to show how this works is using the “Blink” example.

Open the example on Arduino IDE and choose “Generic ESP8266 Module” as the board.

Make sure you change pin 13 to pin 2 in the code.

Remember that we are using the ESP’s GPIOs, not the Arduino’s.

Connect the ESP as shown in the images above.

Step 3: Setup to Blink the LED - Continued

Make sure GPIO0 is grounded and upload the code.

After a couple of seconds you will get a message similar to the one above.

Step 4: ESP8266 As Access Point

Now we are going to control the LED wirelessly using only the ESP module.

In the previous two tutorials, Getting Started with ESP8266 EP-01 and Using ESP-01 and Arduino UNO, we have used the ESP-01 module in STA mode.

For this tutorial, however, we are going to use it in AP mode to see how it differs.

Step 5: Code

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
 
const char* ssid = "ESP";
const char* password = "123456789";
 
String form = "<form action='led'><input type='radio' name='state' value='1' checked>On<input type='radio' name='state' value='0'>Off<input type='submit' value='Submit'></form>";
 
ESP8266WebServer server(80);
 
const int led = 2;
 
void handle_led() {
  // get the value of request argument "state" and convert it to an int
  int state = server.arg("state").toInt();
 
  digitalWrite(led, state);
  server.send(200, "text/plain", String("LED is now le ") + ((state)?"on":"off"));
}
 
void setup(void) {
  Serial.begin(115200);
  pinMode(led, OUTPUT);
  WiFi.softAP(ssid, password);
 
  server.on("/", [](){
   server.send(200, "text/html", form);
  });
 
   server.on("/led", handle_led);
 
  // Start the server
  server.begin();
}
 
void loop(void) {
  // check for incoming client connections frequently in the main loop:
  server.handleClient();
}

Step 6: Code Explanation

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
 
const char* ssid = "ESP";
const char* password = "123456789";
 
String form = "<form action='led'><input type='radio' name='state' value='1' checked>On<input type='radio' name='state' value='0'>Off<input type='submit' value='Submit'></form>";
 
ESP8266WebServer server(80);
 
const int led = 2;
 
void handle_led() {
  // get the value of request argument "state" and convert it to an int
  int state = server.arg("state").toInt();
 
  digitalWrite(led, state);
  server.send(200, "text/plain", String("LED is now  ") + ((state)?"on":"off"));
}

In this first part of the code, we include the libraries required to initialize the ESP module in AP mode and create a web server. Then we provide the name and password of our access point (ESP module). This is the name that will appear in the available wireless networks.

If you want, you can leave the “password” section in blank so that you don’t have to type a password when you connect to the module. Then we create a string that contains html text to generate a submit button and two radio buttons that will allow us to choose the state of the LED (on or off).

Next we make the server listen to port 80 and assign the name “led” to pin 2 of the ESP module.

Finally we create a function that does three things. First, it gets the value of the request argument “state” and converts it into an integer. Second, it assigns the value to the state of the LED to turn it on (1) or off (0). Lastly, it sends a message to the web browser’s window stating whether the LED is currently on or off.

Step 7: Code Explanation - Continued

void setup(void) {
Serial.begin(115200); pinMode(led, OUTPUT); WiFi.softAP(ssid, password); server.on("/", [](){ server.send(200, "text/html", form); }); server.on("/led", handle_led); // Start the server server.begin(); }

In the setup section of the code, we start serial communication and define the baud rate.

Then we make the LED an output, initialize the ESP-01 as an access point and send the html code from the server to the client to display the buttons in the web browser.

Finally we call the function “handle_led” to manage the LED’s state and then we start the server.

Step 8: Code Explanation - Continued

void loop(void) {
// check for incoming client connections frequently in the main loop: server.handleClient(); }

In the loop, we constantly check if a client initiates connection with our server.

Step 9: Results

When we open the Wireless Network Connections from our computer, we get a list of all the available networks.

We can see how our ESP-01 module appears in the list with the name “ESP.”

Step 10: Results - Continued

When we connect to ESP we get the following window as shown above.

Here we type the password of the network and click “OK.”

Step 11: Results - Continued

Once we are connected to our ESP module we can open a web browser’s window and type the following IP address, as shown in the image above.

Notice how the IP Address is different than when the module was operating in STA mode.

Each mode of operation has a different IP Address.

When we hit enter, we get the three buttons that allow us to change the LED’s state.

Step 12: Results - Continued

When we click on the submit button, we get the following response, shown in the first image above with the effect on the LED shown in the second image.

Step 13: Results - Continued

Similarly, if we choose off we get the following response, as shown above in the images.

Step 14: Conclusion

In this tutorial, we use the Arduino board to supply the module with power and as an intermediary to download the code to the ESP-01. However, after uploading the code to the module, we can get rid of the Arduino board by disconnecting the Rx and Tx lines of communication and powering the module with an external power supply.

An alternate way of programming the ESP module is using an FTDI connector.

If you have any questions about this tutorial, do not hesitate to post a comment, shoot us an email, or post it in our forum.

Check out the Jaycon Systemswebsite to pick up the materials you need from our online store.

While you're there, read through the other great tutorials we have available.

If you haven't done so already, visit our Instructables profile for more step-by-step guides to creating interesting projects. Let us know when you create something from a tutorial we created; we love hearing your stories!

Thanks for reading!