ESP8266 Tutorial: How to Control Anything From the Internet!

By 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 very cheap yet really effective platform for communicating over the internet. It is also 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 we’ll be using the Arduino IDE to develop the web server to control an LED remotely. If you just bought your 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.

Bill of Materials

  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

Connecting the Hardware to Your ESP8266

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 best. Refer the following diagrams for each and set up the hardware accordingly.

1. USB to TTL Converter Using DTR Pin

If you’re using a 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


2. USB to TTL Converter Without DTR Pin

To connect the USB TO TTL converter without the DTR pin we have to use manual flashing. For this, we use two push buttons. Refer the following diagram:


ESP8266 tutorial


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

While uploading the code, press the flash button. Keep the flash button pressed 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.


Using Arduino Uno to Flash the Code to the ESP8266

You can use the Arduino UNO to flash the code to ESP8266 ESP-01. While uploading the code, follow the same procedure to keep the flash button pressed while you once click on reset and release the flash button.


ESP8266 tutorial


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

Uploading the ESP8266 Code

Use any one of the methods shown above and open the Arduino IDE, then 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 code given below to the Arduino IDE and press the 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() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  // Connect to WiFi network
  Serial.print("Connecting to ");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
  Serial.println("WiFi connected");
  // Start the server
  Serial.println("Server started");
  // Print the IP address
  Serial.print("Use this URL to connect: ");
void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
  // Wait until the client sends some data
  Serial.println("new client");
  // Read the first line of the request
  String request = client.readStringUntil('\r');
  // 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.print("Led pin is now: ");
  if(value == HIGH) {
  } else {
  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>");
  Serial.println("Client disconnected");


Open the serial monitor and open the URL shown in your serial monitor through your web browser. Connect GPIO 2 of the ESP8266 to the longer lead of the 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 and OFF.


ESP8266 TutorialRemoving 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

Connecting the ESP8266 to the Internet

Currently, the ESP8266 module can only be accessed through the local wifi network. In order 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: 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: 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 are welcome! 





Recommended Posts
Showing 48 comments
  • Ranveer
    Reply is not a global IP address.It’s a local IP address, which can only be accessible by user in that particular network of router or Wi-Fi.I want to control my LED over internet. How can i configure it? Please Help.

  • S Roy

    I am not a programmer to write the program.Is there any App available on windows or android to control the device.

  • Ron Keno

    Well Done – Very Helpful.

    I couldn’t find your html code for the web page that sends
    Click Here – Turn the LED on Pin 2 ON
    Click Here – Turn the LED on Pin 2 OFF.

    Is the HTML code posted; if not can you please do so?


    • Ron Keno

      As the blind man said, I picked up my hammer and saw!
      Tired eyes missed it, but Find On This Page was spot on.
      Thanks again for your very informative and helpful page.

  • jaky

    I am Using Arduino Uno and esp8266
    I done with upload code.
    But I have trouble:
    i want control pins arduino uno that means i want to flas led from pin 13 or 12

  • eeee

    My serial monitor stays blank after uploading the code.

  • Gabi

    I have this error message, do you have any idea how to solve it?

    warning: espcomm_send_command: didn’t receive command response
    warning: espcomm_send_command(FLASH_DOWNLOAD_BEGIN) failed
    closing bootloader
    flush start
    setting serial port timeouts to 1 ms
    setting serial port timeouts to 1000 ms
    flush complete
    error: espcomm_upload_mem failed

  • Randy

    If you just gets dots …….. and no connection in the Serial Monitor

    GO HERE…..

    by TomTrousers


  • Randy

    Just because you cannot see the connection in the Serial Monitor does not mean it is not working.

    I was never able to see the results in the Serial Monitor as shown in this tutorial. I kept getting an error telling me the serial port was busy and that there was some kind of java error. I don’t what the cause of the error was but it kept me thinking it wasn;t working.

    So, I went to my cell phone and saw that there was in fact a WiFi connection available that was called ESP8266 so obviously my ESP8266 was up and running. I then went to my router and logged in and checked for connected devices and there I saw the device listed in attached devices and it was even showing the correct IP address and MAC address.

    So before you give up, check those two places. It could be working fine but the Serial Monitor simply is not able to display the connection results as the author shows in the tutorial above.

  • françois

    thanks for this tuto. I would like to do the same but I want to use the Ewelink App (on my smartphone) to remote control the led.
    do you have any idea how to do ?

  • Nico

    Archiving built core (caching) in: C:\Users\Nico\AppData\Local\Temp\arduino_cache_59026\core\core_esp8266_esp8266_generic_CpuFrequency_80,FlashFreq_40,FlashMode_dio,UploadSpeed_115200,FlashSize_512K64,ResetMethod_ck,Debug_Disabled,DebugLevel_None_____11e8f9f2ceebc7d65210f7e

    I have this problem sir. Plss help me

  • andrea

    hey, thank you for the tutorial it was very helpful and it all worked for me, but i want to know how the esp8266 choose the local ip address ? and how i can make it a static ip address. I tried to use WiFi.config(192,168,1,6) ( for example i chose the address but it didn’t work.
    Thank youuuu ..

    • Randy

      You need to add code like this near the top

      IPAddress ip(192, 168, 1, 56); // (whatever IP address you want)
      IPAddress gateway(192, 168, 1, 2); // set gateway to match your network
      IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your network

  • jibin

    connecting to wifi….
    never ending loop

    any solution

    • Randy

      See post by Randy….December 1, 2017 below

  • VivSa

    Great Tutorial… everything works.

    ON / OFF -> via Wifi
    ON / OFF -> via Internet (FUN!)

    Wondering what’s best way to check my ESP8266 has stored the Script Code?
    I am not able run the ESP8266 in stand-alone power mode (Without Arduino)

  • Gravity

    Archiving built core (caching) in: C:\Users\RAVISH~1\AppData\Local\Temp\arduino_cache_390540\core\core_esp8266_esp8266_generic_CpuFrequency_80,FlashFreq_40,FlashMode_dio,UploadSpeed_115200,FlashSize_512K64,ResetMethod_ck,Debug_Disabled,DebugLevel_None_____68875fbe385fbf515db95cda6864b619.a
    Sketch uses 230121 bytes (53%) of program storage space. Maximum is 434160 bytes.
    Global variables use 32384 bytes (39%) of dynamic memory, leaving 49536 bytes for local variables. Maximum is 81920 bytes.
    warning: espcomm_sync failed
    error: espcomm_open failed
    error: espcomm_upload_mem failed
    error: espcomm_upload_mem failed

  • JJ

    After uploading codes, unplug de wire on then pin GPIO0 and make a reset by plug during 1 second wire connected to pin Reset of EPS to ground. After that it’s perfect…

  • zen

    I have the following errors, any solution? Thx
    warning: espcomm_sync failed
    error: espcomm_open failed
    error: espcomm_upload_mem failed
    error: espcomm_upload_mem failed

    • JJ

      You must connect GPIO0 to ground and make a reset and after you can upload.

  • William A Lawrence

    My serial monitor stays blank after uploading the code. Any suggestions? Thanks

  • Rodrigo Vilas

    Good tutorial man!!!
    If possible can you give me the code you use in your browser file?

    Best regards.

  • J

    Thanks, Great little code for testing. Missing a on the LED OFF println.

  • sabir khan

    excellent tutorial, and really very helpful for starting working on esp module
    thanks you so much

  • Teo

    one question: should the TX/RX lines limited to 3.3V when connecting to an arduino (like) board?

  • Bac Dai Bang

    I am Using Arduino Uno to Flash the Code to the ESP8266.
    I done with upload code.
    But I have trouble:
    I see in your example above, you control GPIO2 of ESP 8266.
    If I want to control GPIO x of Arduino, How can I do it ?
    Thanks you.

    • LK

      For the RX line on the ESP module just use a 1K/2K resistor voltage divider.

      Arduino TX —> voltage divider —> ESP-01 RX

      Arduino RX <— ESP-01 RX

  • nanapratiwi

    This tutorial help me a lot. Thanks

  • nilesh

    Very Helpful tutorial..thanks a lot..but when we select generic esp8266 module then can we use arduino function like display lcd ?
    i want to display ip address on LCD using arduino.But when i used hello word code of arduino then esp module get reset continuously.
    please help..

  • Jonathan Xu

    This seems like a great tutorial, but I’m having trouble setting up the connections. When connecting the USB to TTL converter without DTR pin to the ESP8266, to I connect to the VCC, 5V or 3V3 pin? When I connect to the 3V3 pin there is no flashing blue light, and the red LED is very dim.

  • couki24

    How to get the status of the LED and print the result in the Web Browser ?

  • Gun Guzeloglu

    This tutorial helped me a lot. Thank you :))

  • chandra mouli

    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte

  • Scott

    I’ve tried to follow this, and connect both through an Arduino and through a ftdi board. Both cases results in an error trying to upload the sketch to the 8266. Here’s the error I’m getting using the ftdi…

    Arduino: 1.6.13 (Windows 10), Board: “Generic ESP8266 Module, 80 MHz, 40MHz, DIO, 115200, 512K (64K SPIFFS), ck, Disabled, None”

    Sketch uses 230,109 bytes (53%) of program storage space. Maximum is 434,160 bytes.
    Global variables use 32,364 bytes (39%) of dynamic memory, leaving 49,556 bytes for local variables. Maximum is 81,920 bytes.
    warning: espcomm_sync failed
    error: espcomm_open failed
    error: espcomm_upload_mem failed
    error: espcomm_upload_mem failed

    This report would have more information with
    “Show verbose output during compilation”
    option enabled in File -> Preferences.


    • MiG

      Hello, I have the same problem. Help please.

    • Jason

      Three things you might want to check:
      1) Make sure you have the right com port and it’s available (i.e. not being used by another terminal)
      2) Make sure GPIO is pulled down to ground while the ESP8266 is powered up so that it is in programming mode.
      3) Make sure you have the proper driver to your ftdi board installed.

      • ViperMav

        Were you able to fix the issue? I have the same problem!

  • Ari

    Serial terminal does not connect to ESP8266. It stays blank. What could be wrong? Thank you in advance.

  • David

    Excellent tutorial!

  • ESP8266 Module Tutorial

    The default baudrate sometime comes rated at 74880, depending on how the producer writed the firmware

  • Jason


    I tried this project and it works perfectly. My only question is how do I get the ability to go back and run AT commands in the Serial Monitor? Thanks.

  • Shardul

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

  • Adam Dimitris

    in step 3 CH_PD must be high!

  • ajinkya wankhede


    gF`\ dX`XPP

    and it goes on.

    • Eduardo Albarado

      could you solve the problem?

    • Paul

      Most likely BAUD rate is wrong !

  • harish ahmad

    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

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

Leave a Comment

− 4 = 1

Start typing and press Enter to search