{"id":13141,"date":"2017-02-03T21:02:44","date_gmt":"2017-02-04T02:02:44","guid":{"rendered":"http:\/\/embedded-lab.com\/blog\/?p=13141"},"modified":"2023-04-11T22:17:32","modified_gmt":"2023-04-12T02:17:32","slug":"tutorial-5-setting-esp8266-web-server","status":"publish","type":"post","link":"https:\/\/embedded-lab.com\/blog\/tutorial-5-setting-esp8266-web-server\/","title":{"rendered":"Tutorial 5: Setting up an ESP8266 Web Server"},"content":{"rendered":"<p>In this tutorial, we will explore how to setup an ESP8266 web server to\u00a0serve an webpage that can be displayed\u00a0on a client&#8217;s browser. The client can be any other computer, smartphone, or tablet connected to the same WiFi network. The webpage will also provide an user interface to allow you to toggle an I\/O pin of the ESP8266 hardware.<\/p>\n<div id=\"attachment_13124\" style=\"width: 918px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/WebServerT.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-13124\" class=\"size-full wp-image-13124\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/WebServerT.png\" alt=\"ESP8266 Webserver setup tutorial\" width=\"908\" height=\"552\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/WebServerT.png 908w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/WebServerT-580x353.png 580w\" sizes=\"auto, (max-width: 908px) 100vw, 908px\" \/><\/a><p id=\"caption-attachment-13124\" class=\"wp-caption-text\">ESP8266 Webserver setup tutorial<\/p><\/div>\n<h3>Hardware Setup<\/h3>\n<p>There is nothing much to do in the hardware setup of this experiment. In your EasyESP-1 board, all you need to do is to connect the LD1 pin to D1 pin using a jumper cable. This will connect the LD1 LED on board\u00a0to the D1 I\/O pin of the ESP8266.<\/p>\n<div id=\"attachment_13121\" style=\"width: 810px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/P_20170202_154535.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-13121\" class=\"size-full wp-image-13121\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/P_20170202_154535.jpg\" alt=\"Setup\" width=\"800\" height=\"617\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/P_20170202_154535.jpg 800w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/P_20170202_154535-580x447.jpg 580w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/a><p id=\"caption-attachment-13121\" class=\"wp-caption-text\">Hardware Setup<\/p><\/div>\n<h3>Software<\/h3>\n<p>As mentioned above, this tutorial illustrates how to\u00a0create a standalone local web server using EasyESP-1. The web server will respond to a client&#8217;s request with a web page that will allow the user to toggle the LED connected to the D1 I\/O pin. The first step to do that is to\u00a0create a server object, which is done with the following statement.<\/p>\n<div class=\"highlight highlight-source-c++\">\n<pre>WiFiServer <span class=\"pl-en\">server<\/span>(<span class=\"pl-c1\">80<\/span>);<\/pre>\n<\/div>\n<p>The server will respond to clients on port 80. You need to provide the credentials (network SSID and password) to connect to the chosen WiFi network. You can initiate the connection using <strong>WiFi.begin(ssid, password)<\/strong> function. Once connected to the network, you can find out the local IP address assigned to your ESP8266 using <strong>WiFi.localIP()<\/strong>. The complete code for this tutorial is posted below. The content of the webpage is defined in the string variable <strong>HTMLpage<\/strong>.<\/p>\n\n<div class=\"wp_syntax\"><table><tr><td class=\"code\"><pre class=\"c\" style=\"font-family:monospace;\"><span style=\"color: #339933;\">#include <\/span>\n<span style=\"color: #339933;\">#include <\/span>\n<span style=\"color: #339933;\">#include <\/span>\n<span style=\"color: #339933;\">#include <\/span>\n&nbsp;\nESP8266WebServer Webserver<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000dd;\">80<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n&nbsp;\n<span style=\"color: #666666; font-style: italic;\">\/\/ Replace with your network credentials<\/span>\n<span style=\"color: #993333;\">const<\/span> <span style=\"color: #993333;\">char<\/span><span style=\"color: #339933;\">*<\/span> ssid <span style=\"color: #339933;\">=<\/span> <span style=\"color: #ff0000;\">&quot;your network ssid&quot;<\/span><span style=\"color: #339933;\">;<\/span>\n<span style=\"color: #993333;\">const<\/span> <span style=\"color: #993333;\">char<\/span><span style=\"color: #339933;\">*<\/span> password <span style=\"color: #339933;\">=<\/span> <span style=\"color: #ff0000;\">&quot;password&quot;<\/span><span style=\"color: #339933;\">;<\/span>\n&nbsp;\nString HTMLpage <span style=\"color: #339933;\">=<\/span> <span style=\"color: #ff0000;\">&quot;&quot;<\/span><span style=\"color: #339933;\">;<\/span>\n<span style=\"color: #993333;\">int<\/span> LED <span style=\"color: #339933;\">=<\/span> D1<span style=\"color: #339933;\">;<\/span>\n&nbsp;\n<span style=\"color: #993333;\">void<\/span> setup<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #993333;\">void<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#123;<\/span>\n  HTMLpage <span style=\"color: #339933;\">+=<\/span> <span style=\"color: #ff0000;\">&quot;<\/span><\/pre><\/td><\/tr><\/table><\/div>\n\n<h3>ESP8266 Webserver Demo (Toggle LED)<\/h3>\n<p>LED <a href=\"\\&quot;ledON\\&quot;\"><button>ON<\/button><\/a>\u00a0<a href=\"\\&quot;ledOFF\\&quot;\"><button>OFF<\/button><\/a><\/p>\n\n<div class=\"wp_syntax\"><table><tr><td class=\"code\"><pre class=\"c\" style=\"font-family:monospace;\"><span style=\"color: #ff0000;\">&quot;;\n&nbsp;\n  pinMode(LED, OUTPUT);\n  digitalWrite(LED, LOW);\n  Serial.begin(115200);\n  WiFi.begin(ssid, password);\n  Serial.println(&quot;<\/span><span style=\"color: #ff0000;\">&quot;);\n&nbsp;\n  \/\/ Wait for connection\n  while (WiFi.status() != WL_CONNECTED) {\n    delay(500);\n    Serial.print(&quot;<\/span>.<span style=\"color: #ff0000;\">&quot;);\n  }\n  Serial.println(&quot;<\/span><span style=\"color: #ff0000;\">&quot;);\n  Serial.print(&quot;<\/span>Connected to <span style=\"color: #ff0000;\">&quot;);\n  Serial.println(ssid);\n  Serial.print(&quot;<\/span>IP address<span style=\"color: #339933;\">:<\/span> <span style=\"color: #ff0000;\">&quot;);\n  Serial.println(WiFi.localIP());\n&nbsp;\n  if (MDNS.begin(&quot;<\/span>esp8266<span style=\"color: #ff0000;\">&quot;, WiFi.localIP())) {\n    Serial.println(&quot;<\/span>MDNS responder started<span style=\"color: #ff0000;\">&quot;);\n  }\n&nbsp;\n  Webserver.on(&quot;<\/span><span style=\"color: #339933;\">\/<\/span><span style=\"color: #ff0000;\">&quot;, [](){\n    Webserver.send(200, &quot;<\/span>text<span style=\"color: #339933;\">\/<\/span>html<span style=\"color: #ff0000;\">&quot;, HTMLpage);\n  });\n  Webserver.on(&quot;<\/span><span style=\"color: #339933;\">\/<\/span>ledON<span style=\"color: #ff0000;\">&quot;, [](){\n    Webserver.send(200, &quot;<\/span>text<span style=\"color: #339933;\">\/<\/span>html<span style=\"color: #ff0000;\">&quot;, HTMLpage+&quot;<\/span><\/pre><\/td><\/tr><\/table><\/div>\n\n<p>LED is ON<\/p>\n\n<div class=\"wp_syntax\"><table><tr><td class=\"code\"><pre class=\"c\" style=\"font-family:monospace;\"><span style=\"color: #ff0000;\">&quot;);\n    digitalWrite(LED, HIGH);\n    delay(1000);\n  });\n  Webserver.on(&quot;<\/span><span style=\"color: #339933;\">\/<\/span>ledOFF<span style=\"color: #ff0000;\">&quot;, [](){\n    Webserver.send(200, &quot;<\/span>text<span style=\"color: #339933;\">\/<\/span>html<span style=\"color: #ff0000;\">&quot;, HTMLpage+&quot;<\/span><\/pre><\/td><\/tr><\/table><\/div>\n\n<p>LED is OFF<\/p>\n\n<div class=\"wp_syntax\"><table><tr><td class=\"code\"><pre class=\"c\" style=\"font-family:monospace;\"><span style=\"color: #ff0000;\">&quot;);\n    digitalWrite(LED, LOW);\n    delay(1000); \n  });\n&nbsp;\n  Webserver.begin();\n  Serial.println(&quot;<\/span>HTTP Webserver started<span style=\"color: #ff0000;\">&quot;);\n}\n&nbsp;\nvoid loop(void){\n  Webserver.handleClient();\n}<\/span><\/pre><\/td><\/tr><\/table><\/div>\n\n<p>Download\u00a0<strong><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/WebserverTutorial.zip\">Webserver Tutorial code<\/a><\/strong><\/p>\n<h3>Output<\/h3>\n<p>After uploading the web server code posted above (make sure you typed in your network credentials), open a Serial Monitor window for the IP address assigned to your EasyESP-1 board. If the connection to the selected WiFi network is successful, you will see the following output on the Serial Monitor window. The IP address displayed here is the IP address of the ESP8266 web server.<\/p>\n<div id=\"attachment_13127\" style=\"width: 1166px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/UART.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-13127\" class=\"size-full wp-image-13127\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/UART.png\" alt=\"Serial Monitor terminal\" width=\"1156\" height=\"668\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/UART.png 1156w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/UART-580x335.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/UART-1024x592.png 1024w\" sizes=\"auto, (max-width: 1156px) 100vw, 1156px\" \/><\/a><p id=\"caption-attachment-13127\" class=\"wp-caption-text\">Serial Monitor terminal output<\/p><\/div>\n<p>Now open a browser on any computer or tablet connected to the same WiFi network and type that IP address in the URL field and hit Go. You will see the web page served by the EasyESP-1 as follows.<\/p>\n<div id=\"attachment_13117\" style=\"width: 820px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/WebServer1.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-13117\" class=\"wp-image-13117 size-full\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/WebServer1.png\" alt=\"WebServer1\" width=\"810\" height=\"574\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/WebServer1.png 810w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/WebServer1-580x411.png 580w\" sizes=\"auto, (max-width: 810px) 100vw, 810px\" \/><\/a><p id=\"caption-attachment-13117\" class=\"wp-caption-text\">Web server home page<\/p><\/div>\n<p>Now if you press ON button, the browser will send that info to the server, and the ESP8266 will turn ON the LED connected to D1 pin. Similarly, clicking on OFF button will turn the LED OFF.<\/p>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/WebServer2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-13118\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/WebServer2.png\" alt=\"WebServer2\" width=\"812\" height=\"586\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/WebServer2.png 812w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/WebServer2-580x419.png 580w\" sizes=\"auto, (max-width: 812px) 100vw, 812px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/WebServer3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-13119\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/WebServer3.png\" alt=\"WebServer3\" width=\"812\" height=\"562\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/WebServer3.png 812w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/WebServer3-580x401.png 580w\" sizes=\"auto, (max-width: 812px) 100vw, 812px\" \/><\/a><\/p>\n<div id=\"attachment_13122\" style=\"width: 810px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/P_20170202_164121.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-13122\" class=\"size-full wp-image-13122\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/P_20170202_164121.jpg\" alt=\"Toggling LED via web server\" width=\"800\" height=\"673\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/P_20170202_164121.jpg 800w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/02\/P_20170202_164121-580x488.jpg 580w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/a><p id=\"caption-attachment-13122\" class=\"wp-caption-text\">Toggling LED via web server<\/p><\/div>\n<h3><a href=\"https:\/\/www.elecrow.com\/cde82664e.html\" target=\"_blank\" rel=\"noopener noreferrer\">Buy EasyESP-1 board<\/a><\/h3>\n<h4>More tutorials<\/h4>\n<h5 class=\"post-title\"><a href=\"http:\/\/embedded-lab.com\/blog\/easyesp-1-rapid-prototyping-development-board-esp8266\/\" target=\"_blank\" rel=\"noopener noreferrer\">EASYESP-1: A RAPID PROTOTYPING AND DEVELOPMENT BOARD FOR ESP8266<\/a><\/h5>\n<h5 class=\"post-title\"><a href=\"http:\/\/embedded-lab.com\/blog\/tutorial-1-setting-arduino-ide-easyesp-1\/\" target=\"_blank\" rel=\"noopener noreferrer\">TUTORIAL 1: SETTING UP THE ARDUINO IDE FOR EASYESP-1<\/a><\/h5>\n<h5 class=\"post-title\"><a href=\"http:\/\/embedded-lab.com\/blog\/tutorial-2-easyesp-1-hello-world-example\/\" target=\"_blank\" rel=\"noopener noreferrer\">TUTORIAL 2: EASYESP-1 \u201cHELLO WORLD\u201d EXAMPLE<\/a><\/h5>\n<h5 class=\"post-title\"><a href=\"http:\/\/embedded-lab.com\/blog\/tutorial-3-connecting-oled-display-esp8266\/\" target=\"_blank\" rel=\"noopener noreferrer\">TUTORIAL 3: CONNECTING AN OLED DISPLAY TO ESP8266<\/a><\/h5>\n<h5 class=\"post-title\"><a href=\"http:\/\/embedded-lab.com\/blog\/tutorial-4-working-esp8266-wifi-scan-class\/\" target=\"_blank\" rel=\"noopener noreferrer\">TUTORIAL 4: WORKING WITH ESP8266 WIFI SCAN CLASS<\/a><\/h5>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will explore how to setup an ESP8266 web server to\u00a0serve an webpage that can be displayed\u00a0on a client&#8217;s browser. The client can be any other computer, smartphone, or tablet connected to the same WiFi network. The webpage will also provide an user interface to allow you to toggle an I\/O pin of the ESP8266 hardware. Hardware Setup There is nothing much to do in the hardware setup of this experiment. In your EasyESP-1 board, all you need to do is to connect the LD1 pin to D1 pin using a jumper cable. This will connect the<\/p>\n","protected":false},"author":1,"featured_media":13124,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1603,1044,1464,958,1466],"tags":[],"class_list":["post-13141","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp8266-tutorials","category-esp8266","category-featured","category-internet-of-things","category-tutorials"],"_links":{"self":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/13141","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/comments?post=13141"}],"version-history":[{"count":6,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/13141\/revisions"}],"predecessor-version":[{"id":15140,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/13141\/revisions\/15140"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media\/13124"}],"wp:attachment":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media?parent=13141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/categories?post=13141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/tags?post=13141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}