{"id":34701,"date":"2017-03-06T07:32:45","date_gmt":"2017-03-06T07:32:45","guid":{"rendered":"http:\/\/randomnerdtutorials.com\/?p=34701"},"modified":"2019-04-02T10:23:29","modified_gmt":"2019-04-02T10:23:29","slug":"arduino-ethernet-web-server-with-relay","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/arduino-ethernet-web-server-with-relay\/","title":{"rendered":"Arduino Ethernet Web Server with Relay"},"content":{"rendered":"<p>This post shows how to build an Arduino Ethernet web server that controls a relay that is attached to a lamp.<\/p>\n<p>You can access your\u00a0web server with any device that has a browser and it\u2019s connected to the same network.<!--more--><\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/arduinowebserver-GIF.gif?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" \/><\/p>\n<p><strong>Recommended resources:<\/strong><\/p>\n<ul>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/guide-for-relay-module-with-arduino\/\">Guide for Relay Module with Arduino<\/a><\/li>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/arduino-webserver-with-an-arduino-ethernet-shield\/\">Arduino \u2013 Webserver with an Arduino + Ethernet Shield<\/a><\/li>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-web-server-with-arduino-ide\/\">ESP8266 Web Server with Arduino IDE<\/a><\/li>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-web-server\/\">ESP8266 Web Server Tutorial<\/a><\/li>\n<\/ul>\n<p>If you like Arduino projects\u00a0and you want to build more, I recommend getting my <a href=\"https:\/\/randomnerdtutorials.com\/arduino-step-by-step-projects\/\" target=\"_blank\" rel=\"noopener noreferrer\">Arduino Step-by-step Projects &#8211; Build 25 Projects course<\/a>.<\/p>\n<p><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-29199\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/warning-m-1.png?resize=700%2C200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"700\" height=\"200\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/warning-m-1.png?w=700&amp;quality=100&amp;strip=all&amp;ssl=1 700w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/warning-m-1.png?resize=150%2C43&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/warning-m-1.png?resize=300%2C86&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/warning-m-1.png?resize=80%2C23&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/warning-m-1.png?resize=220%2C63&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/warning-m-1.png?resize=250%2C71&amp;quality=100&amp;strip=all&amp;ssl=1 250w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/warning-m-1.png?resize=280%2C80&amp;quality=100&amp;strip=all&amp;ssl=1 280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/warning-m-1.png?resize=510%2C146&amp;quality=100&amp;strip=all&amp;ssl=1 510w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<p><strong>Note:\u00a0if you\u2019re not comfortable dealing with mains voltage, but you still want to try to do the project, \u00a0you can replace the relay module with an LED, for example. The code and the schematics are\u00a0very similar.<\/strong><\/p>\n<h2>Ethernet shield<\/h2>\n<p>The Arduino Ethernet shield connects your Arduino to the internet in a simple way. Just mount this module onto your Arduino board, connect it to your network with an RJ45 cable and follow a few simple steps to start controlling your projects\u00a0through the web.<\/p>\n<p><strong>Note: <\/strong>you must connect an Ethernet cable from your router to your Ethernet shield as shown in the following\u00a0figure.<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter size-full wp-image-34713\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Ethernet-Shield.jpg?resize=700%2C478&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"700\" height=\"478\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Ethernet-Shield.jpg?w=700&amp;quality=100&amp;strip=all&amp;ssl=1 700w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Ethernet-Shield.jpg?resize=150%2C102&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Ethernet-Shield.jpg?resize=300%2C205&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Ethernet-Shield.jpg?resize=80%2C55&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Ethernet-Shield.jpg?resize=220%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Ethernet-Shield.jpg?resize=146%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 146w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Ethernet-Shield.jpg?resize=349%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 349w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Ethernet-Shield.jpg?resize=608%2C415&amp;quality=100&amp;strip=all&amp;ssl=1 608w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<h3>Pin usage<\/h3>\n<p>When the Arduino is connected to an Ethernet shield, you can\u2019t use Digital pins from 10 to 13,\u00a0because they are being used in order to establish a communication between the\u00a0Arduino and the Ethernet shield.<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter size-full wp-image-34714\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Ethernet-shield-pinout.png?resize=448%2C357&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"448\" height=\"357\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Ethernet-shield-pinout.png?w=448&amp;quality=100&amp;strip=all&amp;ssl=1 448w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Ethernet-shield-pinout.png?resize=150%2C120&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Ethernet-shield-pinout.png?resize=300%2C239&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Ethernet-shield-pinout.png?resize=80%2C64&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Ethernet-shield-pinout.png?resize=220%2C175&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Ethernet-shield-pinout.png?resize=125%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 125w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Ethernet-shield-pinout.png?resize=188%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 188w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Ethernet-shield-pinout.png?resize=299%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 299w\" sizes=\"(max-width: 448px) 100vw, 448px\" \/><\/p>\n<h3>Relay module<\/h3>\n<p><a href=\"https:\/\/randomnerdtutorials.com\/guide-for-relay-module-with-arduino\/\">A relay<\/a> is an electrically operated switch. It means that it can be turned on or off, letting the current going through or not. The relay module is the one in the figure below.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-34718\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/relay-module.jpg?resize=210%2C197&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"210\" height=\"197\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/relay-module.jpg?w=210&amp;quality=100&amp;strip=all&amp;ssl=1 210w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/relay-module.jpg?resize=150%2C141&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/relay-module.jpg?resize=80%2C75&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/relay-module.jpg?resize=107%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 107w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/relay-module.jpg?resize=160%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 160w\" sizes=\"(max-width: 210px) 100vw, 210px\" \/><\/p>\n<p>This particular relay module comes with two relays (those blue cubes).<\/p>\n<p>About\u00a0mains voltage, relays have 3 possible connections:<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-34717\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/relay-labeled.jpg?resize=372%2C342&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"372\" height=\"342\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/relay-labeled.jpg?w=372&amp;quality=100&amp;strip=all&amp;ssl=1 372w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/relay-labeled.jpg?resize=150%2C138&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/relay-labeled.jpg?resize=300%2C276&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/relay-labeled.jpg?resize=80%2C74&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/relay-labeled.jpg?resize=220%2C202&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/relay-labeled.jpg?resize=109%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 109w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/relay-labeled.jpg?resize=163%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 163w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/relay-labeled.jpg?resize=259%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 259w\" sizes=\"(max-width: 372px) 100vw, 372px\" \/><\/p>\n<ul>\n<li><strong>COM<\/strong>:<strong> common pin<\/strong><\/li>\n<li><strong>NO<\/strong>: <strong>normally open<\/strong> \u2013 there is no contact between the common pin and the normally open pin. So, when you trigger the relay, it connects to the COM pin and power is provided to the load (a lamp, in our case).<\/li>\n<li><strong>NC:<\/strong> <strong>normally closed<\/strong> \u2013 there is contact between the common pin and the normally closed pin. There is always contact between the COM and NC pins, even when the relay is turned off. When you trigger the relay, the circuit is opened and there is no power provided to the load.<\/li>\n<\/ul>\n<p>Relating this project,\u00a0it is better to use a\u00a0<strong>normally open circuit<\/strong>, because we want to light up the lamp occasionally. <a href=\"https:\/\/randomnerdtutorials.com\/guide-for-relay-module-with-arduino\/\">Read this tutorial<\/a> to learn more about using a relay module with the Arduino board.<\/p>\n<p>The connections between the relay and the Arduino are really simple:<\/p>\n<p><strong><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-34715\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/IMG_6361-279x300.jpg?resize=279%2C300&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"279\" height=\"300\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/IMG_6361-279x300.jpg?resize=279%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 279w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/IMG_6361-279x300.jpg?resize=140%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 140w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/IMG_6361-279x300.jpg?resize=74%2C80&amp;quality=100&amp;strip=all&amp;ssl=1 74w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/IMG_6361-279x300.jpg?resize=205%2C220&amp;quality=100&amp;strip=all&amp;ssl=1 205w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/IMG_6361-279x300.jpg?resize=93%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 93w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/IMG_6361-279x300.jpg?resize=221%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 221w\" sizes=\"(max-width: 279px) 100vw, 279px\" \/><\/strong><\/p>\n<ul>\n<li><strong>GND<\/strong>: goes to ground<\/li>\n<li><strong>IN1<\/strong>: controls the first relay. Should be connected to an Arduino digital pin<\/li>\n<li><strong>IN2<\/strong>: controls the second relay. Should be connected to an Arduino digital pin<\/li>\n<li><strong>VCC<\/strong>: goes to 5V<\/li>\n<\/ul>\n<h2>Parts required<\/h2>\n<p>Here&#8217;s a complete list of the components you need for this project:<\/p>\n<ul>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/compatible-arduino-uno-r3-board\/\" target=\"_blank\" rel=\"noopener noreferrer\">Arduino UNO<\/a>\u00a0\u2013 read\u00a0<a href=\"https:\/\/makeradvisor.com\/best-arduino-starter-kits\/\" target=\"_blank\" rel=\"noopener noreferrer\">Best Arduino Starter Kits<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/ethernet-shield-w5100-arduino-shield\/\" target=\"_blank\" rel=\"noopener noreferrer\">Ethernet Shield<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/5v-2-channel-relay-module-optocoupler\/\" target=\"_blank\" rel=\"noopener noreferrer\">Relay module<\/a><\/li>\n<li>Lamp cord set (<a href=\"http:\/\/ebay.to\/2feHELc\" target=\"_blank\" rel=\"noopener noreferrer\">view on eBay<\/a>)<\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noopener noreferrer\">Breadboard<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noopener noreferrer\">Jumper Wires\u00a0<\/a><\/li>\n<\/ul>\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n<h2>Code<\/h2>\n<p>Copy the following code to your Arduino IDE and before uploading it to your Arduino board read the <strong>&#8220;Configuring your network&#8221;<\/strong> section below.<\/p>\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n * Rui Santos \n * Complete Project Details http:\/\/randomnerdtutorials.com\n*\/\n\n#include &lt;SPI.h&gt;\n#include &lt;Ethernet.h&gt;\n\n\/\/ Enter a MAC address and IP address for your controller below.\n\/\/ The IP address will be dependent on your local network:\nbyte mac[] = { \n  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };\nIPAddress ip(192,168,1, 111);\n\n\/\/ Initialize the Ethernet server library\n\/\/ with the IP address and port you want to use \n\/\/ (port 80 is default for HTTP):\nEthernetServer server(80);\n\n\/\/ Relay state and pin\nString relay1State = &quot;Off&quot;;\nconst int relay = 7;\n\n\/\/ Client variables \nchar linebuf[80];\nint charcount=0;\n\nvoid setup() { \n  \/\/ Relay module prepared \n  pinMode(relay, OUTPUT);\n  digitalWrite(relay, HIGH);\n  \n  \/\/ Open serial communication at a baud rate of 9600\n  Serial.begin(9600);\n  \n  \/\/ start the Ethernet connection and the server:\n  Ethernet.begin(mac, ip);\n  server.begin();\n  Serial.print(&quot;server is at &quot;);\n  Serial.println(Ethernet.localIP());\n}\n\n\/\/ Display dashboard page with on\/off button for relay\n\/\/ It also print Temperature in C and F\nvoid dashboardPage(EthernetClient &amp;client) {\n  client.println(&quot;&lt;!DOCTYPE HTML&gt;&lt;html&gt;&lt;head&gt;&quot;);\n  client.println(&quot;&lt;meta name=\\&quot;viewport\\&quot; content=\\&quot;width=device-width, initial-scale=1\\&quot;&gt;&lt;\/head&gt;&lt;body&gt;&quot;);                                                             \n  client.println(&quot;&lt;h3&gt;Arduino Web Server - &lt;a href=\\&quot;\/\\&quot;&gt;Refresh&lt;\/a&gt;&lt;\/h3&gt;&quot;);\n  \/\/ Generates buttons to control the relay\n  client.println(&quot;&lt;h4&gt;Relay 1 - State: &quot; + relay1State + &quot;&lt;\/h4&gt;&quot;);\n  \/\/ If relay is off, it shows the button to turn the output on          \n  if(relay1State == &quot;Off&quot;){\n    client.println(&quot;&lt;a href=\\&quot;\/relay1on\\&quot;&gt;&lt;button&gt;ON&lt;\/button&gt;&lt;\/a&gt;&quot;);\n  }\n  \/\/ If relay is on, it shows the button to turn the output off         \n  else if(relay1State == &quot;On&quot;){\n    client.println(&quot;&lt;a href=\\&quot;\/relay1off\\&quot;&gt;&lt;button&gt;OFF&lt;\/button&gt;&lt;\/a&gt;&quot;);                                                                    \n  }\n  client.println(&quot;&lt;\/body&gt;&lt;\/html&gt;&quot;); \n}\n\n\nvoid loop() {\n  \/\/ listen for incoming clients\n  EthernetClient client = server.available();\n  if (client) {\n    Serial.println(&quot;new client&quot;);\n    memset(linebuf,0,sizeof(linebuf));\n    charcount=0;\n    \/\/ an http request ends with a blank line\n    boolean currentLineIsBlank = true;\n    while (client.connected()) {\n      if (client.available()) {\n       char c = client.read();\n       \/\/read char by char HTTP request\n        linebuf[charcount]=c;\n        if (charcount&lt;sizeof(linebuf)-1) charcount++;\n        \/\/ if you've gotten to the end of the line (received a newline\n        \/\/ character) and the line is blank, the http request has ended,\n        \/\/ so you can send a reply\n        if (c == '\\n' &amp;&amp; currentLineIsBlank) {\n          dashboardPage(client);\n          break;\n        }\n        if (c == '\\n') {\n          if (strstr(linebuf,&quot;GET \/relay1off&quot;) &gt; 0){\n            digitalWrite(relay, HIGH);\n            relay1State = &quot;Off&quot;;\n          }\n          else if (strstr(linebuf,&quot;GET \/relay1on&quot;) &gt; 0){\n            digitalWrite(relay, LOW);\n            relay1State = &quot;On&quot;;\n          }\n          \/\/ you're starting a new line\n          currentLineIsBlank = true;\n          memset(linebuf,0,sizeof(linebuf));\n          charcount=0;          \n        } \n        else if (c != '\\r') {\n          \/\/ you've gotten a character on the current line\n          currentLineIsBlank = false;\n        }\n      }\n    }\n    \/\/ give the web browser time to receive the data\n    delay(1);\n    \/\/ close the connection:\n    client.stop();\n    Serial.println(&quot;client disonnected&quot;);\n  }\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/arduino_ethernet_web_server.ino\" target=\"_blank\">View raw code<\/a><\/p>\n<h3>Configuring your network<\/h3>\n<p>Take a look at the configuring your network code snippet:<\/p>\n<pre>byte mac[] = {\r\n0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };\r\nIPAddress ip(192,168,1,<span style=\"color: #ff0000;\"><strong>XXX<\/strong><\/span>);<\/pre>\n<p><strong>Important:<\/strong>\u00a0you actually might need to replace that\u00a0variable highlighted in red with appropriate values that are suitable for your network, otherwise your\u00a0Arduino will not establish a connection with your\u00a0network.<\/p>\n<p>Replace the following line with an IP that is available and suitable for your network:<\/p>\n<pre><strong>IPAddress ip(<span style=\"color: #ff0000;\">X<\/span>, <span style=\"color: #ff0000;\">X<\/span>, <span style=\"color: #ff0000;\">X<\/span>, <span style=\"color: #ff0000;\">X<\/span>);<\/strong><\/pre>\n<p>In my case, my IP range is\u00a0<strong>192.168.1.<span style=\"color: #ff0000;\">X<\/span><\/strong> and with the software <a href=\"http:\/\/angryip.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">Angry IP Scanner<\/a> I know that the IP\u00a0<strong>192.168.1.111<\/strong>\u00a0is available in my network, because it doesn\u2019t have any active device in my network with that exact same IP address:<\/p>\n<pre><strong>IPAddress ip(192, 168, 1, <span style=\"color: #ff0000;\">111<\/span>);<\/strong><\/pre>\n<h2><strong>Schematics<\/strong><\/h2>\n<p>Wire you circuit accordingly to the schematic below:<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-34712 aligncenter\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Schematicsfinal.png?resize=996%2C660&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"996\" height=\"660\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Schematicsfinal.png?w=996&amp;quality=100&amp;strip=all&amp;ssl=1 996w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Schematicsfinal.png?resize=150%2C99&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Schematicsfinal.png?resize=300%2C199&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Schematicsfinal.png?resize=768%2C509&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Schematicsfinal.png?resize=80%2C53&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Schematicsfinal.png?resize=220%2C146&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Schematicsfinal.png?resize=151%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 151w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Schematicsfinal.png?resize=226%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 226w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Schematicsfinal.png?resize=359%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 359w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Schematicsfinal.png?resize=626%2C415&amp;quality=100&amp;strip=all&amp;ssl=1 626w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Schematicsfinal.png?resize=735%2C487&amp;quality=100&amp;strip=all&amp;ssl=1 735w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Schematicsfinal.png?resize=898%2C595&amp;quality=100&amp;strip=all&amp;ssl=1 898w\" sizes=\"(max-width: 996px) 100vw, 996px\" \/><\/p>\n<h2>Demonstration<\/h2>\n<p>Your Arduino Web Sever looks like the figure below:<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-34722\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/arduino_web_server_ethernet_browser.png?resize=524%2C384&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"524\" height=\"384\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/arduino_web_server_ethernet_browser.png?w=524&amp;quality=100&amp;strip=all&amp;ssl=1 524w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/arduino_web_server_ethernet_browser.png?resize=150%2C110&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/arduino_web_server_ethernet_browser.png?resize=300%2C220&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/arduino_web_server_ethernet_browser.png?resize=80%2C59&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/arduino_web_server_ethernet_browser.png?resize=220%2C161&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/arduino_web_server_ethernet_browser.png?resize=136%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 136w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/arduino_web_server_ethernet_browser.png?resize=205%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 205w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/arduino_web_server_ethernet_browser.png?resize=325%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 325w\" sizes=\"(max-width: 524px) 100vw, 524px\" \/><\/p>\n<p>Here&#8217;s a demonstration showing what you have at the end of this project:<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-34721\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/arduinowebserver-GIF.gif?resize=480%2C270&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"480\" height=\"270\" \/><\/p>\n<h2>Wrapping up<\/h2>\n<p>With this project you built an Arduino web server that turns a relay on and off.<\/p>\n<p>Now, you can use this project to control any electronics appliance you want.<\/p>\n<p>This project is a simplified version of one of my projects in the \u00a0<a href=\"https:\/\/randomnerdtutorials.com\/arduino-step-by-step-projects\/\" target=\"_blank\" rel=\"noopener noreferrer\">Arduino Step-by-step Projects<\/a>. If you liked this project make sure you check out the <a href=\"https:\/\/randomnerdtutorials.com\/arduino-step-by-step-projects\/\" target=\"_blank\" rel=\"noopener noreferrer\">course<\/a>\u00a0that includes 23 Arduino projects.<\/p>\n<p>Thanks for reading,<\/p>\n<p>Rui and Sara<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post shows how to build an Arduino Ethernet web server that controls a relay that is attached to a lamp. You can access your\u00a0web server with any device that &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Arduino Ethernet Web Server with Relay\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/arduino-ethernet-web-server-with-relay\/#more-34701\" aria-label=\"Read more about Arduino Ethernet Web Server with Relay\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":34712,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[303,2,267,245,225,264,10,257],"tags":[],"class_list":["post-34701","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-0-arduino","category-arduino","category-arduino-project","category-arduino-ide","category-home-automation","category-project","category-a-tutorials","category-web-server-2"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/03\/Schematicsfinal.png?fit=996%2C660&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/34701","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=34701"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/34701\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/34712"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=34701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=34701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=34701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}