shauryacool
Published © GPL3+

Web server led control

Learn how to switch on leds with web server hosted by nodemcu v1.0

IntermediateProtip10,852
Web server led control

Things used in this project

Story

Read more

Schematics

Schematics

Code

Code

C/C++
//Nodemcu tutorial
//controlling led with web server.
//made by shauryacool in arduino ide
//tutorial link: https://create.arduino.cc/projecthub/shauryacool/web-server-led-control-with-nodemcu-and-nodemcu-setup-c2a84f
//copyrights reserved
#include <ESP8266WiFi.h>//We need it or compilers oxygen tank will explode.

const char* ssid = "ssid";//Write your network ssid
const char* password = "password";//Network password

int ledPin = D7;
WiFiServer server(80);

void setup() {
  
  pinMode(ledPin,OUTPUT);
  digitalWrite(ledPin,LOW);//We start with the led turned off.

  Serial.begin(115200);//remember to set the serial monitor to 115200 or you will see a lot of junk text.
  Serial.println();
  Serial.print("Wifi connecting to ");
  Serial.println( ssid );//your ssid

  WiFi.begin(ssid,password);//connecting to wifi

  Serial.println();
  Serial.print("Connecting");//connecting

  while( WiFi.status() != WL_CONNECTED ){
      delay(500);
      Serial.print(".");        
  }

  
  Serial.println();

  Serial.println("Wifi Connected Success!");//If network connection is success we will see these messages
  Serial.print("NodeMCU IP Address : ");
  Serial.println(WiFi.localIP() );

  server.begin();
  Serial.println("NodeMCU Server started");//server has 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("Hello New client");//Come on, we also have to welcome the client, but the client will not able to see it.
  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.println("<body>");
  client.println("<h1>LED Control</h1>");
  client.println("<p>Made by Shaurya Pratap Singh</p>");
 
  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("");
  
}

Credits

shauryacool
1 project • 1 follower

Comments