• LOGIN
  • No products in the cart.

Introduction

In this tutorial, we will be Controlling Esp12E NodeMCU with LED from website . We will create a web app with three buttons for three leds for turning it on and off. We will see step wise process along with source code for the implementation. Note that the web app which we will make can control your LED of  ESP12E NodeMCU Wifi Development Board through local network only. Your ESP12E module and your laptop in which the web page is there should be connected to same WiFi network, or same hot-spot. The IP address which is assigned to ESP12E Module is local. It is connected to internet via single tunnel. But don’t worry, I will write a tutorial on controlling the LED connected to ESP12E NodeMCU from any where in the world 🙂 . So lets begin.

 

 

 

This is the first part of my video which is based on TCP/IP protocol for communication. Here you will get the detailed explanation of the coding part.

 

Here is the last part of my video and the hardware connectivity is also going to developed in the later part. 

Program to do it.

#include <ESP8266WiFi.h>

const char* ssid = “****”;
const char* password = “****”;

int ledPin = 13; // GPIO13
WiFiServer server(80);

void setup() {
Serial.begin(9600);
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(“<a href=\”/LED=ON\”\”><button>Turn On </button></a>”);
client.println(“<a href=\”/LED=OFF\”\”><button>Turn Off </button></a><br />”);
client.println(“</html>”);

delay(1);
Serial.println(“Client disonnected”);
Serial.println(“”);

}

 

August 16, 2018

4 responses on "Setting Up TCP/IP Based ESP8266 Webserver Step by Step Using Arduino IDE II"

  1. Content is just awesome Sir, it gave me a brief idea how to do that. Thanks a lot

  2. what is the use of:
    1) client.flush()
    2) request.indexOf(“/LED=ON”)
    3) client.println(“”); // do not forget this one

  3. Everything is perfectly described in the first and the second video section. @bappaditya

Leave a Message

Your email address will not be published. Required fields are marked *

X