fbpx

Control ESP8266 GPIO’S Form webserver .

Esp8266 act as web server, Esp8266 hold a web page which helps client(Mobile) to get a information and update information to web server.

Through this concept, client can control the ESP8266.The following example show how client can control ESP 8266 gpio Webserver – ESP8266 hold a web page with a user interface to toggle a led which is displayed in client(Mobile) device.

Program

 include <ESP8266WiFi.h>
 include <WiFiClient.h>
 include <ESP8266WebServer.h>
 const char* ssid = " smart_config ";
 const char* password = "imake";
 int value = HIGH;
 int LED = 2; // internal led connected to gpio 2
 WiFiServer server(80);
 void setup()
 {
 Serial.begin(115200);
 pinMode(LED, OUTPUT);
 digitalWrite(LED, HIGH);
 WiFi.softAP(ssid, password);
 Serial.println();
 Serial.print("Server IP address: ");
 Serial.println(WiFi.softAPIP());
 Serial.print("Server MAC address: ");
 Serial.println(WiFi.softAPmacAddress());
 server.begin(); // Starts the Server
 Serial.println("Server started");
 }
 void loop()
 {
 WiFiClient client = server.available();
 if (!client)
 {
 return;
 }
 while(!client.available())
 {
 delay(1);
 }
 String Get_data = client.readStringUntil('\r');
 Serial.println(Get_data);
 client.flush();
 Update_Led(Get_data);
 /------------------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("");
 client.println("");
 client.print("LED: ");
 if(value == LOW)
 {
 client.print("ON");
 }
 else
 {
 client.print("OFF");
 }
 client.println("

");
 client.println("ON");
 client.println("OFF
");
 client.println("");
 Serial.print("client Disconnected");
 }
 void Update_Led(String Status)
 {
 if(Status.indexOf("/LED=ON") != -1)
 {
 digitalWrite(LED, LOW); // Turn ON LED
 value = LOW;
 }
 if(Status.indexOf("/LED=OFF") != -1)
 {
 digitalWrite(LED, HIGH); // Turn OFF LED
 value = HIGH;
 }
 } 

one uploaded, Open the arduino serial monitor to get the ip address of esp8266

In PC or Mobile connect to smart_config network

open the browser and enter to ip address printed in serial terminal.. Mostly default IP of ESP8266 is 192.168.4.1

browser will display the dashboard to control the led

when you click on button internal led of esp8266 will turn off on

when you click off button internal led of esp8266 will turn off

Leave a Reply

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