Skip to main content

LED Control by ESP8266 as Web Server – IoT

 

LED Control by ESP8266 as Web Server – IoT

Here we are programing ESP8266 as a web server, the chip enables WiFi connectivity and can be turned into a small functioning web server. We will connect ESP8266 to our local wifi network and we can control the LED through this local network. If you need to control it through internet you can use DDNS and port forwarding.

Components Required

  • ESP8266
  • Resistor 470Ω
  • LED
  • USB cable
  • Connecting wires

Hardware

Circuit Diagram

LED Control From Web - Circuit Diagram
LED Control From Web – Circuit Diagram

Connections:

  1. Connect LED positive terminal to D0 pin of ESP8266
  2. Connect LED negative terminal to one end of Resistor 470Ω
  3. Connect another end of Resistor 470Ω to GND terminal of ESP8266.

Software

#include <ESP8266WiFi.h>

const char* ssid = "WiFi Name (SSID)";
const char* password = "WiFi Password";

int LED = 16; // led connected to D0
WiFiServer server(80);

void setup()
{
  Serial.begin(115200);
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);

  Serial.print("Connecting to the Newtork");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected");  
  server.begin();  // Starts the Server
  Serial.println("Server started");

  Serial.print("IP Address of network: "); // Prints IP address on Serial Monitor
  Serial.println(WiFi.localIP());
  Serial.print("Copy and paste the following URL: https://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
}

void loop()
{
  WiFiClient client = server.available();
  if (!client)
  {
    return;
  }
  Serial.println("Waiting for new client");
  while(!client.available())
  {
    delay(1);
  }

  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();

  int value = LOW;
  if(request.indexOf("/LED=ON") != -1)
  {
    digitalWrite(LED, HIGH); // Turn ON LED
    value = HIGH;
  }
  if(request.indexOf("/LED=OFF") != -1)
  {
    digitalWrite(LED, LOW); // Turn OFF LED
    value = LOW;
  }

/*------------------HTML Page Creation---------------------*/

  client.println("HTTP/1.1 200 OK"); // standalone web server with an ESP8266
  client.println("Content-Type: text/html");
  client.println("");
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");

  client.print("LED: ");
 
  if(value == HIGH)
  {
    client.print("ON");
  }
  else
  {
    client.print("OFF");
  }
  client.println("<br><br>");
  client.println("<a href=\"/LED=ON\"\"><button>ON</button></a>");
  client.println("<a href=\"/LED=OFF\"\"><button>OFF</button></a><br />");
  client.println("</html>");

  delay(1);
  Serial.println("Client disonnected");
  Serial.println("");
}

Code Explanation

We are including ESP8266 WiFi library which provides ESP8266 specific WiFi routines and we are calling to connect to network.

#include <ESP8266WiFi.h>

Get and enter the “ssid” and “password” i.e.,  your WiFi name and password.


const char* ssid = "WiFi Name (SSID)";
const char* password = "WiFi Password";

Set D0 as output pin. Start the server, initialize the serial communication with baud-rate 115200.

int LED = 16; 
WiFiServer server(80);

void setup()
{
  Serial.begin(115200);
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);

Actual connection to WiFi is initialized by calling.

Serial.print("Connecting to Internet ");
WiFi.begin(ssid, password);

Connection process can take couple of seconds. While() loop checks the module connection with WiFi. If it connected to WiFi then it will display “WiFi Connected message on serial monitor and web server starts”, otherwise it display “dots (……)” on Serial Monitor.

while (WiFi.status() != WL_CONNECTED) 
{
  delay(500);
  Serial.print(".");
}
Serial.println("WiFi connected");
server.begin();
Serial.println("Server started");

It is printing the IP address of our ESP8266 module. This IP address is given by the DHCP server running on the WiFi router. For accessing it, we need to connect our system to the same WiFi network (our system will get another IP address from the router DHCP server) and we need to enter the IP address of the ESP8266 in the browser of our system.

  Serial.print("IP Address of network: ");
  Serial.println(WiFi.localIP());
  Serial.print("Copy and paste the following URL: https://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
}

Waiting and checking for a client to connect.

Note : Client means browser. When we are loading ESP8266 IP address in a browser it is actually sending a request to the web server listening on the TCP port 80.

void loop() 
{
  WiFiClient client = server.available(); 
  if (!client) 
  {
    return;
  }
  Serial.println("Waiting for new client"); 
  while(!client.available())
  {
    delay(1);
  }
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();

Next we will be controlling the LED and providing response (HTML page) based on the request given by the browser.

int value = LOW;
if (request.indexOf("/LED=ON") != -1) 
{
  digitalWrite(LED, HIGH);
  value = HIGH;
}
if (request.indexOf("/LED=OFF") != -1) 
{
  digitalWrite(LED, LOW);
  value = LOW;
}

Write html code to create a web page with LED ON and OFF status.

client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");

client.print("LED: ");

if(value == HIGH)
{
  client.print("ON");
}
else
{
  client.print("OFF");
}
client.println("<br><br>");
client.println("<a href=\"/LED=ON\"\"><button>ON</button></a>");
client.println("<a href=\"/LED=OFF\"\"><button>OFF</button></a><br />");
client.println("</html>");

delay(1);
Serial.println("Client disonnected");
Serial.println("");
}

Practical Implementation of  Circuit

LED Control From Web Server - Practical Implementation
LED Control From Web Server – Practical Implementation

Working Procedure

This project is designed to control an LED using ESP8266, which is programmed as a web server. We need the IP address of the device to access html content which we are programming.

  • Edit the above code in Arduino IDE, replace WiFi Name and Password with yours
  • Save
  • Compile
  • Upload
  • Open Serial Monitor at a baud rate of 115200.
  • Press the reset button which is provided on ESP8266, then  this module displays the IP address of the ESP8266.
LED Control From Web - Serial Port
LED Control From Web – Serial Port
  • Copy that IP address which is displaying on Serial Monitor and paste it on browser of our system (which is connected to the same WiFi network).
  • Click on ON and OFF button to send ON or OFF command to ESP8266.

Then LED which is connected at D0 pin of ESP8266 will be controlled based on the given command.

How to control through Internet ?

We are not going to explain this in detail in this tutorial. Most of the wifi routers available in the market supports DDNS (Dynamic DNS) and port forwarding. This can be used to access the web page served by ESP8266. In real IoT applications we need to connect ESP8266 to a internet server. We will explain those in next tutorials.

Video

Comments

Popular posts from this blog

Interfacing L298N Motor Driver with Arduino Uno

1 May Interfacing L298N Motor Driver with Arduino Uno In this tutorial we will learn how to interface  L298N  motror driver with  Arduino Uno . You might be thinking why we need L298N for controlling a motor. The answer is very simple,  Arduino  board or a  microcontroller  IO pins don’t have enough current/voltage driving capability to drive a motor. For driving the motor in both directions (clockwise and anti-clockwise) we need to use an  H-Bridge . Please read our article  H-Bridge – DC Motor Driving  for more information. L298N is an integrated monolithic circuit with dual H-Bridge. It can be used to rotate the motor in both directions and to control the speed of the motor using  PWM  technique. Components Required Arduino Uno L298N Motor Driver 12V battery 2x DC Motors Jumper wires L298N Motor Driver Module L298N Motor Driver Connections Explained Specifications Output A, Output B – To connect two motors. Driver Power Input – Board can accept 5V to 35V which will act as the power

E-TECH SOLAR INVERTER SOLUTION

    DEEP CYCLE BATTERIES The reason many projects fail can mostly be put at the door step of batteries due to inadequate charging and low charge/discharge cycles. Our batteries are  among the very top 3 in the industry and capable of undergoing several cycle  of up to 5 years @ 20% DOD. Hence, we are confident of giving as much as 2 years warranty for some of our batteries SOLAR SYSTEM FOR DOMESTIC AND INDUSTRIAL USE  Many businesses such as hospitals, schools, shopping malls, hotels etc are beginning to embrace solar to completely eliminate unnecessary spending on diesels ……. why not come on board now and earn yourself some carbon credit apart from huge savings in millions of Naira.   SOLAR PANEL MODULES Our panels are carefully selected and tested from the very best among  the Tier one range of solar panels. We guarantee a sustainable yield for at least 25 years and 15 years warranty Inverter Our hybrid inverter chargers are super-powered with

Using the TLP250 Isolated MOSFET Driver

Using the TLP250 Isolated MOSFET Driver - Explanation and Example Circuits I’ve already shown how to drive an N-channel MOSFET (or even an IGBT) in both high-side and low-side configurations in a multitude of ways. I’ve also explained the principles of driving the MOSFETs in these configurations. The dedicated drivers I’ve shown so far are the TC427 and IR2110. Some people have requested me to write up on MOSFET drive using the very popular TLP250. And I’ll explain that here. The TLP250, like any driver, has an input stage, an output stage and a power supply connection. What’s special about the TLP250 is that the TLP250 is an optically isolated driver, meaning that the input and output are “optically isolated”. The isolation is optical – the input stage is an LED and the receiving output stage is light sensitive (think “photodetector”). Before delving any further, let’s look at the pin configuration and the truth table. Fig. 1 - TLP250 Pin Configuration Fig.