ESP8266 As a Microcontroller

by JayconSystems in Circuits > Microcontrollers

100918 Views, 760 Favorites, 0 Comments

ESP8266 As a Microcontroller

1.jpg

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.

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.

Setup to Blink the LED

2.jpg
3.JPG

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.

Setup to Blink the LED - Continued

4.png

Make sure GPIO0 is grounded and upload the code.

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

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.

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();
}

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.

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.

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.

Results

5.png

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.”

Results - Continued

6.png

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

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

Results - Continued

7.png

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.

Results - Continued

8.png
9.jpg

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.

Results - Continued

10.png
11.png
12.jpg

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

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 Systems website 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!