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

Hardware:
ethernet shield
relays
leds
resistors
connecting wires
breadboard
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
      Website: http://www.ihacklab.blogspot.com
      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() {

  Serial.begin(9600);
  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
   digitalWrite(CH1,LOW);
   digitalWrite(CH2,LOW);
   delay(2000); 
  
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("my server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {   
      if (client.available()) {
        char c = client.read();
     
        //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') {          
           Serial.println(readString); 
     
           client.println("HTTP/1.1 200 OK"); //start of your HTML
           client.println("Content-Type: text/html");
           client.println();     
           client.println("<!DOCTYPE HTML>");
           client.println("<HTML>");
           client.println("<HEAD>");
          client.println("<TITLE>iHackLab</TITLE>");
           client.println("</HEAD>");
           client.println("<center>");
           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("www.myhackspaceproject.blogspot.com/>"); 
           client.println("<hr />");
           client.println("<br />"); 
             client.println("</center>");
           client.println("</BODY>");
           client.println("</HTML>");   //end of your HTML
     
           delay(1);
           //stopping client
           client.stop();
           
           if (readString.indexOf("one") >0){
               Serial.println("Relay 1");
        digitalWrite(CH1, HIGH);
       delay(1000);
        digitalWrite(CH1, LOW); 
        delay(1000);
    
         
           }
           if (readString.indexOf("two") >0){
                Serial.println("Relay 2");
       digitalWrite(CH2, HIGH);
       delay(1000);
        digitalWrite(CH2, LOW); 
        delay(1000);
           }
           if (readString.indexOf("bth") >0){
                 Serial.println("Relays 1,2");
        digitalWrite(CH1, HIGH);
        delay(1000);
        digitalWrite(CH1, LOW); 
        delay(1000);
        digitalWrite(CH2, HIGH);
        delay(1000);
        digitalWrite(CH2, LOW); 
        delay(1000);
           }
           if (readString.indexOf("stp") >0){
               Serial.println("Deactivate");
         digitalWrite(CH1,HIGH);
         digitalWrite(CH2,HIGH);
         digitalWrite(gLed, LOW);
         digitalWrite(rLed, LOW);
         delay(1000);
           }
            
            readString="";  
           
         }
       }
    }
}
}

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

Challenge: 

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



Actual Layout:






Procedure:
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