<!-- wp:paragraph -->
<p>In this ESP8266 WiFi Module Project, we will learn about WiFi Controlled LED using ESP8266 and Arduino. I will show you how to control an LED connected to the Digital I/O Pin of the Arduino over WiFi Network using the ESP8266 WiFi Module.</p>
<div id="ez-toc-container" class="counter-hierarchy counter-decimal ez-toc-transparent">
<div class="ez-toc-title-container">
<p class="ez-toc-title">Table of Contents</p>
<i class="ez-toc-glyphicon ez-toc-icon-toggle"></i></div>
<nav>
<ul class="ez-toc-list">
<li>Overview</li>
<li>Concept behind WiFi Controlled LED using ESP8266 and Arduino</li>
<li>Circuit Diagram</li>
<li>Components Required</li>
<li>Circuit Design</li>
<li>Code</li>
<li>HTML Code for Sending Data to ESP8266</li>
<li>Working of WiFi Controlled LED using ESP8266 and Arduino Project</li>
<li>Conclusion</li>
</ul>
</nav></div>
<h3><span id="Overview" class="ez-toc-section">Overview</span></h3>
<p>The ESP8266 WiFi Module is an interesting addition to the makers and hobbyists’ community as it allows us to integrate WiFi and Internet into our DIY Projects.</p>
<div id="div-gpt-ad-1527869606268-4" align="center"> </div>
<div id="div-gpt-ad-1527869606268-8" align="center"> </div>
<p>IoT or Internet of Things is one such area which can be implemented using a variety of sensors connected to a Microcontroller like Arduino and ESP8266 WiFi Module to access the sensor data from anywhere in the World.</p>
<p>Before thinking about big IoT projects, let us begin exploring the ESP8266 WiFi Module and implement a small but useful project called WiFi Controlled LED using ESP8266 and Arduino. </p>
<p><img class="alignnone size-full wp-image-26723" src="https://incotel.com/demo1/wp-content/uploads/2019/02/WiFi-Controlled-LED-using-ESP8266.jpg" alt="" width="580" height="331" /><br /><br /></p>
<p>we have already introduced to you about the ESP8266 WiFi Module in my <a href="https://www.electronicshub.org/esp8266-arduino-interface/" target="_blank" rel="noopener"><strong>GETTING STARTED WITH ESP8266 AND ARDUINO</strong></a> Project. Refer to that project first in order to learn about the basic information on ESP8266 WiFi Module, its Pin Diagram, pin configuration and how to interface ESP8266 with Arduino.</p>
<p><strong>IMPORTANT NOTE:</strong></p>
<ul>
<li>In my second project on ESP8266 WiFi Module, I have shown you <a href="https://www.electronicshub.org/update-flash-esp8266-firmware/" target="_blank" rel="noopener"><strong>How to flash the AT Command Firmware</strong></a> onto the ROM of ESP8266 WiFi Module.</li>
<li>It is very important that you first implement that project i.e. make sure that your ESP8266 WiFi Module has AT firmware installed on it.</li>
<li>If you have programmed your ESP8266 WiFi Module with other program (like Blink, for example), then you must flash the firmware. If your ESP module is already has the AT Commands firmware, then leave it as it is.</li>
</ul>
<h3><span id="Concept_behind_WiFi_Controlled_LED_using_ESP8266_and_Arduino" class="ez-toc-section">Concept behind WiFi Controlled LED using ESP8266 and Arduino</span></h3>
<p>Before getting into the details of the project like the circuit diagram, components, connections and the code, let me take you through the concept behind the WiFi Controlled LED using ESP8266 and Arduino.</p>
<p>The idea behind the project is very simple. Connect an LED to the Arduino Board. This LED must be connected over WiFi i.e. within a local network through a Smart Phone or a Laptop.</p>
<p>For this, use the ESP8266 WiFi Module and interface it to Arduino through Serial Communication. Arduino will command the ESP8266 module to get connected to a WiFi network and receive data from the client (an HTML page).</p>
<p>Based on the information sent by the client (with the help of a Web Browser), the Arduino will either turn ON or OFF the LED. That’s it.</p>
<h3><span id="Circuit_Diagram" class="ez-toc-section">Circuit Diagram</span></h3>
<p>The circuit diagram for the WiFi Controlled LED using ESP8266 and Arduino project is shown I in the image below.</p>
<p>&nbsp;</p>
<h3><span class="ez-toc-section"><img class="alignnone  wp-image-26725" src="https://incotel.com/demo1/wp-content/uploads/2019/02/WiFi-Controlled-LED-using-ESP8266-and-Arduino-Circuit-Diagram-1.jpg" alt="" width="696" height="342" /><br />Components Required  </span>[<a style="font-size: 16px;" href="https://incotel.com" target="_blank" rel="noopener noopener noreferrer">Buy Here</a><span style="font-size: 16px;">]</span></h3>
<ul>
<li>Arduino UNO  </li>
<li>ESP8266 WiFi Module </li>
<li>LED  </li>
<li>330Ω Resistor (1/4 Watt)   </li>
<li>1 KΩ Resistor (1/4 Watt)   </li>
<li>2.2 KΩ Resistor (1/4 Watt)  </li>
<li>Push Button   </li>
<li>Connecting Wires  </li>
<li>Mini Breadboard  </li>
</ul>
<h3><span id="Circuit_Design" class="ez-toc-section">Circuit Design</span></h3>
<p>I have used the software serial feature of the Arduino and made its digital pins 2 and 3 as RX and TX. These pins must be connected to TX and RX Pins of the ESP8266 WiFi Module.</p>
<p><strong>NOTE:</strong> I have used a level convertor consisting of a 1 KΩ Resistor and a 2.2 KΩ Resistor before connecting the TX Pin of Arduino (Pin 3) to the RX Pin of the ESP8266.</p>
<p>An LED is connected to the Digital I/O Pin 11 of the Arduino. (This is the LED that we will be controlling over WiFi).</p>
<p>Coming to the rest of the connections with respect to the ESP8266, it’s VCC and CH_PD pins are connected to 3.3V of the Arduino and GND is connected to, well the GND pin of the Arduino. A Push Button is connected between RESET of ESP8266 and GND.</p>
<p>Both the GPIO Pins of the ESP8266 i.e. GPIO0 and GPIO2 are left open as we won’t be using those pins in this project.</p>
<h3><span id="Code" class="ez-toc-section">Code</span></h3>
<p>The following is the code that is to be uploaded into Arduino. It will configure the WiFi in the ESP8266 Module as well as check for the data from the HTML  page</p>
<table class="alignleft" style="height: 69px; background-color: #ececec; width: 767px;" border="0" width="746">
<tbody>
<tr>
<td style="width: 1012px;">
<pre>#include &lt;SoftwareSerial.h&gt;
SoftwareSerial esp8266(2,3); //Pin 2 &amp; 3 of Arduino as RX and TX. Connect TX and RX of ESP8266 respectively.
#define DEBUG true
#define led_pin 11 //LED is connected to Pin 11 of Arduino


void setup()
  {
    pinMode(led_pin, OUTPUT);
    digitalWrite(led_pin, LOW);
    Serial.begin(9600);
    esp8266.begin(115200); //Baud rate for communicating with ESP8266. Your's might be different.
    esp8266Serial("AT+RST\r\n", 5000, DEBUG); // Reset the ESP8266
    esp8266Serial("AT+CWMODE=1\r\n", 5000, DEBUG); //Set station mode Operation
    esp8266Serial("AT+CWJAP=\"SSID\",\"Password\"\r\n", 5000, DEBUG);//Enter your WiFi network's SSID and Password.
                                   
    while(!esp8266.find("OK")) 
    {
      }
    esp8266Serial("AT+CIFSR\r\n", 5000, DEBUG);//You will get the IP Address of the ESP8266 from this command. 
    esp8266Serial("AT+CIPMUX=1\r\n", 5000, DEBUG);
    esp8266Serial("AT+CIPSERVER=1,80\r\n", 5000, DEBUG);
  }

void loop()
  {
    if (esp8266.available())
      {
        if (esp8266.find("+IPD,"))
          {
            String msg;
            esp8266.find("?");
            msg = esp8266.readStringUntil(' ');
            String command1 = msg.substring(0, 3);
            String command2 = msg.substring(4);
                        
            if (DEBUG) 
              {
                Serial.println(command1);//Must print "led"
                Serial.println(command2);//Must print "ON" or "OFF"
              }
            delay(100);

              if (command2 == "ON") 
                    {
                      digitalWrite(led_pin, HIGH);
                    }
                   else 
                     {
                       digitalWrite(led_pin, LOW);
                     }
          }
      }
  }
   
String esp8266Serial(String command, const int timeout, boolean debug)
  {
    String response = "";
    esp8266.print(command);
    long int time = millis();
    while ( (time + timeout) &gt; millis())
      {
        while (esp8266.available())
          {
            char c = esp8266.read();
            response += c;
          }
      }
    if (debug)
      {
        Serial.print(response);
      }
    return response;
  }
 </pre>
</td>
</tr>
</tbody>
</table>
<!-- /wp:paragraph -->
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h3><span id="HTML_Code_for_Sending_Data_to_ESP8266" class="ez-toc-section">HTML Code for Sending Data to ESP8266</span></h3>
<p>In order to create an interface for the project, I have created a simple HTML based webpage. The HTML code for this webpage is given below.</p>
<table style="height: 97px; background-color: #ececec; width: 710px;" width="437">
<tbody>
<tr>
<td style="width: 700px;">
<pre>&lt;!DOCTYPE html &gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;WiFi controlled LED&lt;/title&gt;
&lt;script src="jquery.js"&gt;&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;h2&gt; &lt;i&gt; WiFi Controlled LED using Arduino and ESP8266 &lt;/i&gt; &lt;/h2&gt;
&lt;h4&gt; &lt;i&gt; Enter the IP address of ESP8266 &lt;/i&gt; &lt;/h4&gt;
&lt;div style="margin: 0; width:400px; height:30px;"&gt;
&lt;FORM NAME="form" ACTION="" METHOD="GET"&gt;
ESP8266 IP Address:
&lt;INPUT TYPE="text" NAME="inputbox" VALUE="" /&gt;
&lt;/FORM&gt;
&lt;/div&gt;
&lt;h3&gt; Click to toggle LED! &lt;/h3&gt;
&lt;input type="button" NAME="butname" value="Turn ON LED" /&gt;
&lt;p&gt;STATUS: LED is OFF!&lt;/p&gt;
&lt;/body&gt;
&lt;script&gt;
$.ajaxSetup({timeout:1000});
btn = document.querySelector('input[name="butname"]');
txt = document.querySelector('p');
btn.addEventListener('click', led1);

function led1()
{
	var val1 = 'OFF';
	if (btn.value === 'Turn OFF LED') 
	{
	btn.value = 'Turn ON LED';
	val1 = 'OFF';
	txt.textContent = 'STATUS: LED is OFF!';
	} 
	else 
	{
    	btn.value = 'Turn OFF LED';
	val1 = 'ON';
    	txt.textContent = 'STATUS: LED is ON!';
	}
	TextVar = form.inputbox.value;
	ArduinoVar = "http://" + TextVar + ":80/";
	$.get( ArduinoVar, {led: val1})	;
	{Connection: close};
}
&lt;/script&gt;
&lt;/html&gt;</pre>
</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p>Download this HTML Code (save it as .html file).</p>
<p>This HTML webpage uses the JavaScript library “jQuery.js”. Download this library from <a href="https://www.electronicshub.org/wp-content/uploads/2018/03/jquery.js" target="_blank" rel="noopener"><strong>here</strong></a>and place it in the same folder as the .html file.</p>
<p>So, your webpage folder consists of two file: one “webpage.html” file and a “jQuery.js” file. Open the HTML file using any web browser. The interface looks like this.</p>
<p><img class="alignnone  wp-image-26728" src="https://incotel.com/demo1/wp-content/uploads/2019/02/x1.jpg" alt="" width="839" height="199" /></p>
<h3><span id="Working_of_WiFi_Controlled_LED_using_ESP8266_and_Arduino_Project" class="ez-toc-section">Working of WiFi Controlled LED using ESP8266 and Arduino Project</span></h3>
<p>Upload the Arduino Code provided above to your Arduino Board after making all the necessary connections. Once the code is uploaded, open the Serial Monitor of Arduino. You can see the progress of the setup done on the ESP8266 WiFi Module.</p>
<p><img class="alignnone size-full wp-image-26729" src="https://incotel.com/demo1/wp-content/uploads/2019/02/x2.jpg" alt="" width="683" height="728" /></p>
<p>&nbsp;</p>
<p>Some of the information in the above image looks garbage but I assure that correct data is transmitted. If you got a clean response, you can find the IP Address of the ESP8266 Module at the highlighted place in the image above.</p>
<p>Since I couldn’t find the IP Address from Serial Monitor, I had to look up for it using another tool called “Advanced IP Scanner”.</p>
<p><img class="alignnone  wp-image-26726" src="https://incotel.com/demo1/wp-content/uploads/2019/02/x3.jpg" alt="" width="794" height="430" /></p>
<p>Now, open the webpage that you saved earlier and enter this IP Address in the IP Address field provided. After entering the IP Address, you can click on the Button on the page to turn ON and OFF the LED.</p>
<p><img class="alignnone  wp-image-26727" src="https://incotel.com/demo1/wp-content/uploads/2019/02/x4.jpg" alt="" width="871" height="200" /></p>
<h3><span id="Conclusion" class="ez-toc-section">Conclusion</span></h3>
<p>A simple project called WiFi Controlled LED using ESP8266 and Arduino is designed here where an LED connected to Arduino is controlled over WiFi (within the same network).</p>
<p>Any device connected in same WiFi Network can control the LED with the help of a simple HTML webpage.</p>
<p>The next step or an advanced version of this project will be controlling the LED over internet i.e. from anywhere in the World (involves PORT Forwarding).<br /><br /><span style="font-size: 10pt; color: #999999;">Project by : electronicshub.org</span></p>
<p>&nbsp;</p>