ESP8266 As a Microcontroller

74,720

721

56

Posted

Introduction: ESP8266 As a Microcontroller

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!

2 People Made This Project!

  • Thanks for your Intr...-Droxz

    Droxz made it!

  • thanks a lot ! it fi...-AlexM157

    AlexM157 made it!

Recommendations

  • Epilog Challenge 9

    Epilog Challenge 9
  • Sew Warm Contest 2018

    Sew Warm Contest 2018
  • Gluten Free Challenge

    Gluten Free Challenge
user

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

Tips

Questions

54 Comments

Another great tutorial, thanks. I began to make some test and everything worked great until I tried to connect mi esp-01 as a serial module to arduino and now I'm not getting anything at the monitor. I can still upload sketches to the esp and I works but it seems it lost its serial capabilities. I already uploaded a blank sketch to the esp-01 and mi arduino board but nothing, no answer of any kind. Can you throw me a light? thanks!

think you for explain

esp2866

operation now

Hai,

I want to know how do you know the ip address as 192.168.4.1.And why it is same in my esp8266 ?

Hai,

I have done everything as per the instructions. All done. Compiled and done uploading without any warnings or errors. But now the problem is, i cannot find the module wifi "ESP" in the my laptop. please help me with this.

Hi there, I cannot upload code to ESP no matter grounded GPIO0 or not, it respond warnings, are there something wrong?

warning: espcomm_sync failed

error: espcomm_open failed

error: espcomm_upload_mem failed

I've replied above and I'm sure it'll work. Follow the exact procedure :)

Try uploading a BareMinimum example to the Arduino UNO to make sure it is not running other code. Then ground GPIO0. Next use a wire to ground the RST pin for a short time (don't leave it connected to gnd. Just do a quick reset of the chip). Try to download the blinky example to the ESP module. Hope this helps

If you have found any solution plz share. thanks

I tried below workable

add a push button as reset between CHPD and GND

add an one post switch between GND and GPIO0

add a pull up resistor between VCC and CHPD

1. grounded GPIO0 by flipping on the switch

2. push reset button once

3. download file and release GPIO0 afterward

warning: espcomm_sync failed

error: espcomm_open failed

error: espcomm_upload_mem failed