{"id":254,"date":"2019-11-26T09:31:51","date_gmt":"2019-11-26T09:31:51","guid":{"rendered":"https:\/\/starthardware.org\/en\/?p=254"},"modified":"2020-01-06T12:14:20","modified_gmt":"2020-01-06T12:14:20","slug":"interactive-christmas-decoration-part-3-server-and-led","status":"publish","type":"post","link":"https:\/\/starthardware.org\/en\/interactive-christmas-decoration-part-3-server-and-led\/","title":{"rendered":"Interactive Christmas Decoration \u2013 Part 3: Server and LED"},"content":{"rendered":"\n<p>Now it it time to connect the NodeMCU to Wifi. We will use it as a server so we can connect it with the SmartPhone. The circuit stays the same, we will only change the code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Circuit<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"526\" src=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/11\/arduino-christmas-blink-1024x526.jpg\" alt=\"Simple Circuit with one LED\" class=\"wp-image-250\" srcset=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/11\/arduino-christmas-blink-1024x526.jpg 1024w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/11\/arduino-christmas-blink-300x154.jpg 300w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/11\/arduino-christmas-blink-768x395.jpg 768w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/11\/arduino-christmas-blink.jpg 1238w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Code to run the NodeMCU as a Server<\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-medium\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"154\" src=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/11\/nodemcu-reset-button-300x154.jpg\" alt=\"NodeMCU reset button\" class=\"wp-image-255\" srcset=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/11\/nodemcu-reset-button-300x154.jpg 300w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/11\/nodemcu-reset-button.jpg 546w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><figcaption>Reset Button<\/figcaption><\/figure><\/div>\n\n\n\n<p>The code is more complex. What happens is that the NodeMCU now acts as a webserver and is listening on the standard port 80. If you connect to its IP address it will show a webpage with a button. Press the button to switch the LED on and off. <\/p>\n\n\n\n<p>To make it run, fill in your Wifi name (SSID) and password, upload it to the NodeMCU and in the Arduino software open the Serial Monitor. You can find it at <em>Tools&gt;Serial Monitor<\/em>. Set the <em>baud rate <\/em>(bottom of the Serial Monitor window) to 115200 and press the reset button on the NodeMCU.<\/p>\n\n\n\n<p>The IP address is shown. Connect your smartphone to the same wifi and enter the IP in a browser window. The webpage will show a button to toggle the LED on the NodeMCU.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;ESP8266WiFi.h>\n#include &lt;ESP8266WebServer.h>\n\nconst char* ssid  = \"XXXXXXXX\";        \/\/ Wifi name (SSID)\nconst char* password = \"XXXXXXXX\";  \/\/ Wifi password\n\nESP8266WebServer server(80);    \/\/ Server port\nString Temp = \"\";\n\nint d7pin = D7;\nint d7state = 0;\n\n\/* ***** ***** ***** ***** HTML ***** ***** ***** ***** *\/\n\nconst char HTML_header[] PROGMEM = R\"=====(\n&lt;!DOCTYPE html>\n&lt;html>\n\n&lt;head>\n    &lt;meta content=\"text\/html; charset=ISO-8859-1\" http-equiv=\"content-type\">\n    &lt;meta http-equiv=\"refresh\" content=\"5\">\n    &lt;title>WebSchalter&lt;\/title>\n&lt;\/head>\n\n&lt;style>\n    html {\n        background-color: grey;\n    }\n\n    body {\n        margin: auto;\n        \n        margin-top: 60px;\n    }\n\n    button {\n        height: 120px;\n        width: 100%;\n        font-size: 48px;\n        background-color: cornflowerblue;\n        border: none;\n        color: white;\n        margin-bottom: 10px;\n    }\n    \n    button:active {\n        background-color: darkturquoise;\n        color: white;\n        border: none;\n    }\n    \n    button.on{\n        background-color: red;\n    }\n\n    a {\n        text-decoration: none;\n    }\n\n    \n    \n&lt;\/style>\n\n&lt;body>)=====\";\n\nString HTML_d7_off = \"&lt;a href=\\\"\/args?d7pin=1\\\">&lt;button>d7 - LED&lt;\/button>&lt;\/a>\";\nString HTML_d7_on = \"&lt;a href=\\\"\/args?d7pin=0\\\">&lt;button class=\\\"on\\\">d7 - LED&lt;\/button>&lt;\/a>\";\n\nconst char HTML_footer[] PROGMEM = R\"=====(\n&lt;\/body>\n&lt;\/html>\n)=====\";\n\n\/* ***** ***** ***** ***** Setup ***** ***** ***** ***** *\/\nvoid setup()\n{\n  pinMode(d7pin , OUTPUT);                  \/\/ D7 set as Output\n  Serial.begin(115200);                     \/\/ start serial communication\n  connectToWifi();                          \/\/ start wifi\n}\n\n\/* ***** Webpage: executed when \"http:\/\/&lt;ip address>\/\" is called ***** *\/\n\nvoid the_webpage() {\n  Temp = HTML_header;                        \/\/ top part of the html\n  \n  if (d7state == 1) {                        \/\/ d7 part of html page \u2013 the html code for a button\n    Temp += HTML_d7_on;\n  } else {\n    Temp += HTML_d7_off;\n  }\n  \n  Temp += HTML_footer;                       \/\/ bottom part of the html page\n  server.send(200, \"text\/html\", Temp);       \/\/ serving the html page\n}\n\n\/* ***** ***** ***** ***** Event Functions ***** ***** ***** ***** *\/\n\nvoid handleArgs() { \n  if (server.arg(\"d7pin\")!= \"\"){             \/\/ if specific argument is not empty\n    d7state = server.arg(\"d7pin\").toInt();   \/\/ set d7 pin to received argument\n  }\n  \n  the_webpage();                             \/\/ deliver webpage\n}\n\n\/* ***** ***** ***** Wifi Functions ***** ***** ***** *\/\n\nvoid showStates(){                           \/\/ map all states to pins\n  digitalWrite(d7pin, d7state);  \n}\n\n\/* ***** ***** ***** ***** Loop ***** ***** ***** ***** *\/\n\nvoid loop() {\n  server.handleClient();                     \/\/ update function for server\n  showStates();                              \/\/ map states to pins\n  delay(10);                                 \/\/ some time to process\n}\n\n\/* ***** ***** ***** Wifi Functions ***** ***** ***** *\/\n\nvoid connectToWifi() {  \n  Serial.println();\n  Serial.print(\"Connect to\"); Serial.println(ssid);   \n  WiFi.begin(ssid, password);\n  \n  while (WiFi.status() != WL_CONNECTED) {\n    delay(500);\n    Serial.print(\".\");             \n  }\n\n  Serial.println(\"\");\n  Serial.println(\"Wifi connected\");\n  Serial.println (\"Webserver running, waiting for ESP8266 ...\");\n\n  delay(2000);\n  Serial.println();\n  Serial.println(\"IP addresse of the web server is: \");\n  Serial.println(WiFi.localIP());   \/\/ output the ip\n  server.on(\"\/\", the_webpage);      \/\/ show website on root call \n  server.on(\"\/args\", handleArgs);   \/\/ associate the handler function to the path\n\n  server.begin();                   \/\/ start the server\n  Serial.println (\"Webserver running, waiting for ESP8266 ...\");\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">The Complete Tutorial<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/starthardware.org\/en\/interactive-christmas-decoration-part-1\/\">Interactive Christmas Decoration \u2013 Part 1: Preparation<\/a><\/li><li><a href=\"https:\/\/starthardware.org\/en\/interactive-christmas-decoration-part-2\/\">Interactive Christmas Decoration \u2013 Part 2: Make it blink<\/a><\/li><li><a href=\"https:\/\/starthardware.org\/en\/interactive-christmas-decoration-part-3-server-and-led\/\">Interactive Christmas Decoration \u2013 Part 3: Server and LED<\/a><\/li><li><a href=\"https:\/\/starthardware.org\/en\/interactive-christmas-decoration-part-4-server-led-rgb\/\">Interactive Christmas Decoration \u2013 Part 4: Server, LED, RGB<\/a><\/li><li><a href=\"https:\/\/starthardware.org\/en\/interactive-christmas-decoration-part-5-analog-input-with-ldr\/\">Interactive Christmas Decoration \u2013 Part 5: Analog Input with LDR<\/a><\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Dieser Beitrag ist auch auf Deutsch verf\u00fcgbar: <a href=\"https:\/\/starthardware.org\/workshop-interaktive-weihnachtsdekoration-teil-3-server-und-led\/\">Workshop: Interaktive Weihnachtsdekoration Teil 3 \u2013 Server und LED<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now it it time to connect the NodeMCU to Wifi. We will use it as a server so we can connect it with the SmartPhone. The circuit stays the same, we will only change the code. Circuit Code to run the NodeMCU as a Server The code is more complex. What happens is that the&hellip;&nbsp;<a href=\"https:\/\/starthardware.org\/en\/interactive-christmas-decoration-part-3-server-and-led\/\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Interactive Christmas Decoration \u2013 Part 3: Server and LED<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":256,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[5],"tags":[],"class_list":["post-254","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino-projects"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v18.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Interactive Christmas Decoration \u2013 Part 3: Server and LED - StartHardware - Tutorials for Arduino<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/starthardware.org\/en\/interactive-christmas-decoration-part-3-server-and-led\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Interactive Christmas Decoration \u2013 Part 3: Server and LED - StartHardware - Tutorials for Arduino\" \/>\n<meta property=\"og:description\" content=\"Now it it time to connect the NodeMCU to Wifi. We will use it as a server so we can connect it with the SmartPhone. The circuit stays the same, we will only change the code. Circuit Code to run the NodeMCU as a Server The code is more complex. What happens is that the&hellip;&nbsp;Read More &raquo;Interactive Christmas Decoration \u2013 Part 3: Server and LED\" \/>\n<meta property=\"og:url\" content=\"https:\/\/starthardware.org\/en\/interactive-christmas-decoration-part-3-server-and-led\/\" \/>\n<meta property=\"og:site_name\" content=\"StartHardware - Tutorials for Arduino\" \/>\n<meta property=\"article:published_time\" content=\"2019-11-26T09:31:51+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-01-06T12:14:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/11\/InteractiveChristmasDecoration-Part3-Server-LED.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"678\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Stefan Hermann\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/starthardware.org\/en\/#website\",\"url\":\"https:\/\/starthardware.org\/en\/\",\"name\":\"StartHardware - Tutorials for Arduino\",\"description\":\"Arduino, Electronics, Fun\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/starthardware.org\/en\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/starthardware.org\/en\/interactive-christmas-decoration-part-3-server-and-led\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/11\/InteractiveChristmasDecoration-Part3-Server-LED.jpg\",\"contentUrl\":\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/11\/InteractiveChristmasDecoration-Part3-Server-LED.jpg\",\"width\":1200,\"height\":678,\"caption\":\"interactive christmas decoration \u2013 server and led\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/starthardware.org\/en\/interactive-christmas-decoration-part-3-server-and-led\/#webpage\",\"url\":\"https:\/\/starthardware.org\/en\/interactive-christmas-decoration-part-3-server-and-led\/\",\"name\":\"Interactive Christmas Decoration \u2013 Part 3: Server and LED - StartHardware - Tutorials for Arduino\",\"isPartOf\":{\"@id\":\"https:\/\/starthardware.org\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/starthardware.org\/en\/interactive-christmas-decoration-part-3-server-and-led\/#primaryimage\"},\"datePublished\":\"2019-11-26T09:31:51+00:00\",\"dateModified\":\"2020-01-06T12:14:20+00:00\",\"author\":{\"@id\":\"https:\/\/starthardware.org\/en\/#\/schema\/person\/811b16fabcbfeef4210ea79cf0990a59\"},\"breadcrumb\":{\"@id\":\"https:\/\/starthardware.org\/en\/interactive-christmas-decoration-part-3-server-and-led\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/starthardware.org\/en\/interactive-christmas-decoration-part-3-server-and-led\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/starthardware.org\/en\/interactive-christmas-decoration-part-3-server-and-led\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/starthardware.org\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Interactive Christmas Decoration \u2013 Part 3: Server and LED\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/starthardware.org\/en\/#\/schema\/person\/811b16fabcbfeef4210ea79cf0990a59\",\"name\":\"Stefan Hermann\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/starthardware.org\/en\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/5b5a74ee1d07024fd1eff9b1f7137108089169010a93afaee907b9325ee579a6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/5b5a74ee1d07024fd1eff9b1f7137108089169010a93afaee907b9325ee579a6?s=96&d=mm&r=g\",\"caption\":\"Stefan Hermann\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Interactive Christmas Decoration \u2013 Part 3: Server and LED - StartHardware - Tutorials for Arduino","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/starthardware.org\/en\/interactive-christmas-decoration-part-3-server-and-led\/","og_locale":"en_US","og_type":"article","og_title":"Interactive Christmas Decoration \u2013 Part 3: Server and LED - StartHardware - Tutorials for Arduino","og_description":"Now it it time to connect the NodeMCU to Wifi. We will use it as a server so we can connect it with the SmartPhone. The circuit stays the same, we will only change the code. Circuit Code to run the NodeMCU as a Server The code is more complex. What happens is that the&hellip;&nbsp;Read More &raquo;Interactive Christmas Decoration \u2013 Part 3: Server and LED","og_url":"https:\/\/starthardware.org\/en\/interactive-christmas-decoration-part-3-server-and-led\/","og_site_name":"StartHardware - Tutorials for Arduino","article_published_time":"2019-11-26T09:31:51+00:00","article_modified_time":"2020-01-06T12:14:20+00:00","og_image":[{"width":1200,"height":678,"url":"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/11\/InteractiveChristmasDecoration-Part3-Server-LED.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"Written by":"Stefan Hermann","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/starthardware.org\/en\/#website","url":"https:\/\/starthardware.org\/en\/","name":"StartHardware - Tutorials for Arduino","description":"Arduino, Electronics, Fun","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/starthardware.org\/en\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https:\/\/starthardware.org\/en\/interactive-christmas-decoration-part-3-server-and-led\/#primaryimage","inLanguage":"en-US","url":"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/11\/InteractiveChristmasDecoration-Part3-Server-LED.jpg","contentUrl":"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2019\/11\/InteractiveChristmasDecoration-Part3-Server-LED.jpg","width":1200,"height":678,"caption":"interactive christmas decoration \u2013 server and led"},{"@type":"WebPage","@id":"https:\/\/starthardware.org\/en\/interactive-christmas-decoration-part-3-server-and-led\/#webpage","url":"https:\/\/starthardware.org\/en\/interactive-christmas-decoration-part-3-server-and-led\/","name":"Interactive Christmas Decoration \u2013 Part 3: Server and LED - StartHardware - Tutorials for Arduino","isPartOf":{"@id":"https:\/\/starthardware.org\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/starthardware.org\/en\/interactive-christmas-decoration-part-3-server-and-led\/#primaryimage"},"datePublished":"2019-11-26T09:31:51+00:00","dateModified":"2020-01-06T12:14:20+00:00","author":{"@id":"https:\/\/starthardware.org\/en\/#\/schema\/person\/811b16fabcbfeef4210ea79cf0990a59"},"breadcrumb":{"@id":"https:\/\/starthardware.org\/en\/interactive-christmas-decoration-part-3-server-and-led\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/starthardware.org\/en\/interactive-christmas-decoration-part-3-server-and-led\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/starthardware.org\/en\/interactive-christmas-decoration-part-3-server-and-led\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/starthardware.org\/en\/"},{"@type":"ListItem","position":2,"name":"Interactive Christmas Decoration \u2013 Part 3: Server and LED"}]},{"@type":"Person","@id":"https:\/\/starthardware.org\/en\/#\/schema\/person\/811b16fabcbfeef4210ea79cf0990a59","name":"Stefan Hermann","image":{"@type":"ImageObject","@id":"https:\/\/starthardware.org\/en\/#personlogo","inLanguage":"en-US","url":"https:\/\/secure.gravatar.com\/avatar\/5b5a74ee1d07024fd1eff9b1f7137108089169010a93afaee907b9325ee579a6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5b5a74ee1d07024fd1eff9b1f7137108089169010a93afaee907b9325ee579a6?s=96&d=mm&r=g","caption":"Stefan Hermann"}}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/posts\/254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/comments?post=254"}],"version-history":[{"count":7,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/posts\/254\/revisions"}],"predecessor-version":[{"id":291,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/posts\/254\/revisions\/291"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/media\/256"}],"wp:attachment":[{"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/media?parent=254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/categories?post=254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/tags?post=254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}