Saturday, October 10, 2015

Arduino Ethernet Shield

by J.B. Wylzan

Project 29:  Ethernet Web Server

This project shows how to set up the Arduino Ethernet Shield, 
design a simple HTML code, and use the site to control an LED and a relay.

The Ethernet Shield
The Web Server
The Relay

ethernet shield
connecting wires
Arduino R3 UNO board

Block Diagram:

Replace the servo with a relay instead.

NOTE: Before you try the sketch below , make sure to test your ethernet shield using the example provided  by Arduino.  Go to File > Examples > Ethernet > Webserver.

Code # 29:

/* ===============================================================
      Project #29: 2 Channels 5V Relay Shield
      Author: J. B. Wylzan with some help from the Arduino community
      Abstract: Control relays via an ethernet web server.
================================================================== */

#include <SPI.h>
#include <Ethernet.h>

#define CH1 6
#define CH2 7
#define gLed 8
#define rLed 9

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //Your mac address
byte ip[] = { 192, 168, x, x };                      //Your ip address
byte gateway[] = { 192, 168, x, x };                   //Your gateaway address
byte subnet[] = { 255, 255, x, x };                  //Your subnet address
EthernetServer server(80);                             //Your server port
String readString;

void setup() {

  pinMode(CH1, OUTPUT);  
  pinMode(CH2, OUTPUT); 
  pinMode(gLed, OUTPUT); 
  pinMode(rLed, OUTPUT);
  //Provide power to both LEDs
   digitalWrite(gLed, HIGH);
   digitalWrite(rLed, HIGH);
   //Turn OFF any power to the Relay channels
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, gateway, subnet);
  Serial.print("my server is at ");

void loop() {
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {   
      if (client.available()) {
        char c =;
        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string
          readString = readString + c;
          //if HTTP request has ended
         if (c == '\n') {          
           client.println("HTTP/1.1 200 OK"); //start of your HTML
           client.println("Content-Type: text/html");
           client.println("<!DOCTYPE HTML>");
           client.println("<H1>IHackLab Homotronics Website</H1>");
           client.println("<hr />");
           client.println("<br />");  
           client.println("<H2>Automation using Arduino Ethernet Shield</H2>");
           client.println("<br />");  
           client.println("<a href=\"/stp\"\">Deactivate</a><br />"); 
           client.println("<br />");     
           client.println("<br />"); 
           client.println("<a href=\"/one\"\">Activate Relay 1</a><br />");
              client.println("<br />");   
               client.println("<br />");  
           client.println("<a href=\"/two\"\">Activate Relay 2</a><br />");   
           client.println("<br />"); 
            client.println("<br />");  
           client.println("<a href=\"/bth\"\">Activate Both Relays 1, 2</a><br />");
              client.println("<br />");   
               client.println("<br />");  
           client.println("<br />");   
           client.println("<br />"); 
           client.println("<hr />");
           client.println("<hr />");
           client.println("<br />"); 
           client.println("</HTML>");   //end of your HTML
           //stopping client
           if (readString.indexOf("one") >0){
               Serial.println("Relay 1");
        digitalWrite(CH1, HIGH);
        digitalWrite(CH1, LOW); 
           if (readString.indexOf("two") >0){
                Serial.println("Relay 2");
       digitalWrite(CH2, HIGH);
        digitalWrite(CH2, LOW); 
           if (readString.indexOf("bth") >0){
                 Serial.println("Relays 1,2");
        digitalWrite(CH1, HIGH);
        digitalWrite(CH1, LOW); 
        digitalWrite(CH2, HIGH);
        digitalWrite(CH2, LOW); 
           if (readString.indexOf("stp") >0){
         digitalWrite(gLed, LOW);
         digitalWrite(rLed, LOW);

/* =============================================================== */


Make a sketch that will turn On and Off any appliances in your house controlled by your webserver.

Actual Layout:

1. Build the prototype as shown above
2. Run the Arduino Interface
3. Select File > New
4. Copy Code #29 above
5. Paste Code #29
6. Click File > Save
7. Click Verify
8. Click Upload
9. Type your ip address on the url slot
10.  Click On or Off to activate the relay and the led

Project # 23:  Robotics

Disclaimer:  We shall not be liable for any loss or damage of whatever nature - direct, indirect, consequential, or otherwise - which may arise as a result of your use of any information on this website. However, if you are interested in using any of the projects for personal or educational purposes, please inform the author by email. 

Public Domain Notice: Copyright (c) 2000. All rights reserved. This article is part of a book entitled iHackRobot. Copies are welcome to be shared or distributed publicly as long proper citations are observed. Please cite as follows: A Journal of a Creative Mind, Joey Lawsin, 1988, USA.

"The Last Human on Earth will no longer be Human." ~ Joey Lawsin
The Homotronics® and Homodruinos® logos are registered trademarks.
Copyright Biotronics© Inc. iHackRobot®. All Rights Reserved.
Patent Pending. 2000 © ®


No comments:

Post a Comment