ESP8266 (ESP12E) web server with ARDUINO IDE

This example is first step to make IoT project. We will make local web server for ESP12E, it will connect to Local Wifi network (let's say, your home Wifi network), then we can control ON/OFF LED (which is in GPIO16) through phone


Step 1. Upload firmware
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
 
MDNSResponder mdns;
 
ESP8266WebServer server(80);
String webPage;
const char* ssid     = "NguyenTuong";      //wifi name
const char* password = "123456789";  //wifi password
 
void setup() {
 
  pinMode(16, OUTPUT);  //led pin 16
 
  webPage += "<h1>ESP8266 Web Server</h1><p>Socket #1 ";
  webPage += "<a href=\"socket1On\"><button>ON</button></a>&nbsp;";
  webPage += "<a href=\"socket1Off\"><button>OFF</button></a></p>";
  
  Serial.begin(74880);
  delay(100);
 
 
  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");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  
  if (mdns.begin("esp8266", WiFi.localIP())) 
    Serial.println("MDNS responder started");
 
  server.on("/", [](){
    server.send(200, "text/html", webPage);
  });
  server.on("/socket1On", [](){
    server.send(200, "text/html", webPage);
    // Turn off LED
    digitalWrite(16, HIGH);
    delay(1000);
  });
  server.on("/socket1Off", [](){
    server.send(200, "text/html", webPage);
    //Turn on LED
    digitalWrite(16, LOW);
    delay(1000); 
  });

  server.begin();
  Serial.println("HTTP server started");
}
 
void loop() {
  server.handleClient();
}

This case, ESP12E is programmed by Arduino IDE. To know to program, please visit this article Link here

Step 2. Test
After load firmware successful, change ESP12E to RUN mode, open Monitor screen, result will show immediately as:

See result screen, we will know ESP12E local address, this case is 192.168.1.98
Now we can use mobile phone, or computer access to Local web by typing address 192.168.1.98 to web browser. A local web server will be shown. From this, we can turn ON or OFF LED on GPIO16



4 comments:

  1. An author must have a vast knowledge of vocabulary. The dictionary of a writer must be full of new english vocabulary to make their work more attractive. Use of new words makes their work more valuable and graceful.
    QNAP TS X51 Series

    ReplyDelete
  2. Thanks for your sharing

    ReplyDelete
  3. Pretty projects and great idea,Thank you for the tutorial and for sharing !

    หนังออนไลน์

    ReplyDelete