ESP8266 Tutorial: Learn to control anything from the Internet!

From our STAR Maker: Robin Thomas

ESP8266 Tutorial
In this ESP8266 tutorial we are using an ESP-01 module to control an LED over the internet. The ESP8266 is a really cheap yet really effective platform for communicating over the internet. It is also very easy to use with an Arduino. After going through this ESP8266 tutorial, you will have the know-how to control any electrical device through the internet from anywhere in the World!

Here, we will be using an USB to TTL converter to program the Esp8266 ESP-01 module. And will be using the Arduino IDE to develop the web server to control an led remotely. If you have just bought the module, you can also refer my previous ESP8266 tutorial to see how to get started with the ESP-01 Wi-Fi module. It also shows how you can configure it, and verify that there is communication established between the ESP8266 and another device without using a USB to TTL converter: tutorial.

What are the stuff required to do this project?

  1. Arduino board or USB to TTL converter
  2. LED
  3. ESP8266
  4. Breadboard
  5. Female to male and male to male jumpers
  6. Lm117 3.3 voltage regulator

How does it work?

The ESP8266 can be controlled from your local wifi network or from the internet (after port forwarding). The ESP-01 module has GPIO pins that can be programmed to turn an LED or a relay ON/OFF through the internet. The module can be programmed using an Arduino/USB to TTL converter through the serial pins (RX,TX).

ESP8266 tutorial

STEP 1: Hardware setup for this ESP8266 tutorial

We can either use a USB to TTL converter or use an arduino to program the Esp8266. Here there are three methods you can follow to upload the code to ESP8266, select the one which suits you the most. Refer the following diagrams for each and set up the hardware accordingly.

1. USB to TTL converter using DTR pin
If your using an USB to TTL converter which has a DTR pin, then uploading will go smoothly. Please be informed that Serial monitor will not work anymore while doing this.ESP8266 tutorial

USB TTL ——> ESP8266 ESP-01
GND————>GND
TX————–>RX
RX————–>TX
RTS————->RST
DTR————->GPIO0

2. USB to TTL converter without DTR pin
For connecting the USB TO TTL converter without the DTR pin we have to use manual flashing. For this, we must use two push buttons. Refer the following diagram:ESP8266 tutorial

USB TTL ———-> ESP8266 ESP-01
GND—————->GND
TX——————->RX
RX——————->TX
Reset Button——–>RST
Flash Button——–>GPIO0

While uploading the code press the flash button, keep the flash button pushed while you click once on reset. You may now release the flash button. The ESP8266 is now in flash mode. You will be able to upload the sketch now.

3. Using Arduino Uno to Flash the code to ESP8266
You can use arduino uno for flashing code to ESP8266 ESP-01. While uploading the code follow the same procedure to keep the flash button pushed while you once click on reset and release the flash button.

ESP8266 tutorial

ARDUINO —————> ESP8266 ESP-01
GND———————->GND
TX————————>TX
RX————————>RX
Reset Button————>RST
Flash Button————>GPIO0

STEP 2: Uploading the code for ESP8266

Use any one of the above suitable methods and now open the Arduino IDE and select your ESP8266 board from Tools > Board > Generic ESP8266 Module.

Note: If you haven’t installed the ESP8266 board setup for Arduino, do that by following STEP 2 of this tutorial. And then continue with this ESP8266 tutorial.

ESP8266 Tutorial

Now copy the below given code to Arduino IDE and press upload button. Change ssid into your wifi access point, and change the password to your wifi password and compile. 

#include <ESP8266WiFi.h>
 
const char* ssid = "YOUR_SSID";//type your ssid
const char* password = "YOUR_PASSWORD";//type your password
 
int ledPin = 2; // GPIO2 of ESP8266
WiFiServer server(80);//Service Port
 
void setup() {
  Serial.begin(115200);
  delay(10);
 
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
   
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
   
  WiFi.begin(ssid, password);
   
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
   
  // Start the server
  server.begin();
  Serial.println("Server started");
 
  // Print the IP address
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
}
 
void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
   
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
   
  // Read the first line of the request
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();
   
  // Match the request
 
  int value = LOW;
  if (request.indexOf("/LED=ON") != -1) {
    digitalWrite(ledPin, HIGH);
    value = HIGH;
  } 
  if (request.indexOf("/LED=OFF") != -1){
    digitalWrite(ledPin, LOW);
    value = LOW;
  }
 
  //Set ledPin according to the request
  //digitalWrite(ledPin, value);
   
  // Return the response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  do not forget this one
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
   
  client.print("Led pin is now: ");
   
  if(value == HIGH) {
    client.print("On");  
  } else {
    client.print("Off");
  }
  client.println("<br><br>");
  client.println("Click <a href=\"/LED=ON\">here</a> turn the LED on pin 2 ON<br>");
  client.println("Click <a href=\"/LED=OFF\">here turn the LED on pin 2 OFF<br>");
  client.println("</html>");
 
  delay(1);
  Serial.println("Client disconnected");
  Serial.println("");
}

Open the serial monitor and open the URL shown in your serial monitor through your web browser. Connect GPIO 2 of ESP8266 to the longer lead of led (+ve terminal). Now you can control the LED remotely through the internet.

ESP8266 Tutorial

Click on the respective hyperlinks in your browser to toggle the LED ON/OFF.

ESP8266 Tutorial

STEP 3: Remove the programmer

Remove all the wires which were required for uploading .Lm117 is used to provide regulated 3.3V output. This will let you make the ESP8266 or ESP-01 module stand alone.ESP8266 Tutorial

STEP 4: Connecting the ESP8266 to the Internet

Currently, the ESP8266 module can only be accessed through the local wifi network. To be able to control your devices from the internet, you have to do port forwarding on your router.

To do this, find the ip address of your system either by using the “ifconfig” command on your terminal or go to: http://www.whatsmyip.org/. Copy down your ip address. Now open your router setting and goto the “Forwarding” settings. Enter the details for the “Service Port” and “IP Address”. The service port is the port number from your Arduino code (Service port: 80):

WiFiServer server(80);//Service Port

And the IP address is the one you noted down before. Leave the other settings as default. Now go to your browser and enter the address: xxx.xxx.xx.xx:80. This should open up the page for controlling the LED. For a detailed tutorial on port forwarding, check this: tutorial.
Check out the demo video for this ESP8266 tutorial:

Thanks for reading this ESP8266 tutorial. Comments and Questions welcome! 

Robin Thomas - STAR Maker at DIY Hacking

Robin Thomas – STAR Maker at DIY Hacking

 

-Submitted by Robin Thomas

I am an electrical engineering student, and a Maker by passion who loves open source technologies.

Recommended Posts
Showing 6 comments
  • Shardul
    Reply

    Sometimes it takes more than 5 seconds to load on the browser. Is it because of HTML loading?

  • Adam Dimitris
    Reply

    in step 3 CH_PD must be high!

  • ajinkya wankhede
    Reply

    I DID EVERYTHING .. BUT WHEN I CONNECT TO ESP USING SERIAL MONITOR. I GET THE FOLLOWING SYMBOLS INSTEAD OF DESIRED MESSAGE.

    e²`\`X`X`VÔXvtX!`X`XPPŠeZi*9àX`X`XPPJa¶`\`XbX`GÑcyÔ\¶a‹TD\D
    gF`\ dX`XPP
    e²`\`X`X`VÔXvvX!`X`XPPŠei*9àX`X`XPPJa¶`\bX`X`GÑcyÔ\¶a‹}TD\D

    and it goes on.

    • Eduardo Albarado
      Reply

      could you solve the problem?

  • harish ahmad
    Reply

    i build this project, it is working after uploading the codes i use arduino for burning code but when i unplugged the arduino, led and 3.3v remain connected 8266 not working,
    i used mobile hotspot(bcoz i dont have the router) to connect the 8266 and use third mobile for control.
    what can i do please help me.

    • jmw
      Reply

      change your serial monitor baud rate to 115200 and try again.

Leave a Comment

4 + 6 =

Start typing and press Enter to search