ESP8266 As a Microcontroller

79,953

741

55

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!

4 People Made This Project!

Recommendations

  • PCB Contest

    PCB Contest
  • First Time Author

    First Time Author
  • Toys Contest

    Toys Contest

55 Discussions

0
None
oladunk321

3 months ago

Nice tutorial but there is one big mistake.

The Arduino can NOT supply enough power to the ESP8266 when it is transmitting.

Use a 5V to 3.3 V regulator to power the ESP8266. Also put 100 microfarad + 100nF across

3.3 V to filter the power supply.

0
None
quanticchaos

1 year ago

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!

0
None
ahmedthamir

1 year ago

think you for explain

esp2866

operation now

0
None
BharathM16

1 year ago

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 ?

0
None
jijoami

1 year ago

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.

0
None
martinhui

2 years ago

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

4 replies
0
None
MathewG15martinhui

Reply 1 year ago

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

0
None
JayconSystemsmartinhui

Reply 2 years ago

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

0
None
martinhuimubashir hanif

Reply 2 years ago

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

0
None
mubashir hanif

2 years ago

warning: espcomm_sync failed

error: espcomm_open failed

error: espcomm_upload_mem failed

3 replies
0
None
MathewG15mubashir hanif

Reply 1 year ago

Well I had the same error and after a lot of googling found this amazing video

https://www.youtube.com/watch?v=Ao5XcORsYxA You can see the circuit and everything and in the end he says some points. You might need to ground the GPIO0 to flash the program...Well I'll write the steps. 1.Plug in the arduino keeping the GPIO0 pin grounded. 2. Flash the code given in this instructable (Thanks for the code). Now you've flashed the code. 3. Unplug the arduino and remove the grounding cable from GPIO0 4.Connect the arduino again.. Thats it...To reflash some other code repeat the steps again :) Hope this helps (Well it has helped me.)

0
None
Vrettelmubashir hanif

Reply 2 years ago

The method JayconSystems suggests, works fine. To upload to the board without these error messages do the following:

1) Upload an empty project(i.e. BareMinimum from the examples) to the arduino uno board (not the ESP).
2) Wire everything correct according to this instructable.

3) Ground the GPIO0 pin of the ESP.

4) Prepare the sketch to be uploaded to the ESP.

5) Select the Generic ESP8266 Module from the board list

6) Press Upload and before the upload starts connect the RST pin of the ESP to Ground. Disconnect immediately and let the process continue. If the errors appear, try again with a better timing. Don't forget, during the upload only the GPIO0 pin must be connected. After the upload you can connect the other GPIO too.

Sorry for the spam, not sure if the timing i described is the only correct one.

0
None
JayconSystemsmubashir hanif

Reply 2 years ago

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

0
None
antochildjesus

2 years ago

can i use android app for this program... you have any app please send... my mail id antochildjesus@gmail.com. Thank you.

1 reply
0
None
MathewG15antochildjesus

Reply 1 year ago

U can make an android app.. I'm a beginner and I could make one with a few lines of code. Just google "sending http request background". You'll find easy way to send http link requests from an app on button click or something :)

0
None
ashwinigunjal

2 years ago

Hello , I have following error please help me to solve this

"error: espcomm_open failed
error: espcomm_upload_mem failed

"

i try all solution given in comments still i didn't get solution.please help me.

0
None
TarekJamal

2 years ago

Hello, first of all very nice instructable.

1- how can I make the form consists of 2 buttons without postback (like a mobile application)?

2- how can I increase the outputs for the esp8266?