2018年4月22日 星期日

ESP8266 - LED Control from Web server

ESP8266 - LED Control from Web server

This tutorial explains how to control an LED connected with ESP8266 from Web Server.


Detailed Tutorial
1. Introduction:
In this tutorial, we explained how to get LED turn on an off that has connected to the Esp8266. The esp8266 has programmed from Arduino IDE to control the LED. This module will create a server using the router and we will set a web page for this server.
We will use the HTML commands to create the buttons on page and for printing LED status.
2. Hardware required
S.No.ItemQuantity
1.ESP8266 NodeMCU Amica wifi module 1
2.Breadboard 1
3.LED 1
4.Resistor 1K 1
5.Male to Male Jumper Wires 2
 3. Building Circuit
web_led_control

4. Programming:
Once the circuit part is done, Arduino is needed to be programmed.
//Robo India tutorial On Controlling LED on WEB 
//https://www.roboindia.com/tutorials


#include <ESP8266WiFi.h>
 
const char* ssid = "Your Network";
const char* password = "Network 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 Internet ");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected");
 
 /*-------- server started---------*/ 
  server.begin();
  Serial.println("Server started");
 
  /*------printing ip address--------*/
  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("/");
}
 
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);
    value = HIGH;
  }
  if (request.indexOf("/LED=OFF") != -1)  
  {
    digitalWrite(LED, LOW);
    value = LOW;
  }
 
/*------------------Creating html page---------------------*/

  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); 
  client.println("");
  client.println("");
 
  client.print("LED is: ");
 
  if(value == HIGH) 
  {
    client.print("ON");
  } 
  else 
  {
    client.print("OFF");
  }
  client.println("

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

Make sure to write ssid and password of your network before uploading the code.
5. Output:
After uploading the code, open the Serial monitor. Copy and paste the IP address of your network on the address bar of web browser. Now, control the LED connected, by clicking on the buttons on web page. 
If you have any query please write us at support@roboindia.com
Thanks and Regards
Content Development Team 
Robo India
https://roboindia.com

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...