Arduino Web Control of Relay and Lights with Ethernet Shield
Here is the Arduino Uno Code for above Web Server with Ethernet Shield for controlling Relay and Lights via Internet and or LAN.
/*
Arduino Ethernet Shield Prototyping,V2.0. Nitin William 14/01/2014
SPI.h, Ethernet.h Library from Aruduino.cc
This example code is in public domain.
References:
http://bildr.org/2011/06/arduino-ethernet-pin-control/
http://www.jackbarber.co.uk/notes/arduino-web-server-led-control
http://blog.startingelectronics.com/?p=546
*/
#include <SPI.h>
#include <Ethernet.h>
EthernetServer server(7412);// Server port
const byte mainlightPin = 5;// Select pin for Main Light
const byte chandelierPin = 6;// Select pin for Chandelier Light
const byte fanPin = 7;// Select pin for Fan
const byte auxPin = 8;// Select pin for Auxillary Power Light
byte mac[] = { 0x00, 0x08, 0xDC, 0xAB, 0xCD, 0xEF };// Physical MAC address
byte ip[] = { 192, 168, 1, 3 };// Fixed IP address
byte gateway[] = { 192, 168, 1, 1 };// Router Gateway Internet access
byte subnet[] = { 255, 255, 255, 0 };// Subnet mask
String readString;
void setup()
{
delay(300);// Delay for Ethernet shield initialization (Arduino has 65mS Power Up delay and W5100 reset logic has 280mS)
pinMode(mainlightPin, OUTPUT);// Define pin for Main Light as Output
pinMode(chandelierPin, OUTPUT);// Define pin for Chandelier as Output
pinMode(fanPin, OUTPUT);// Define pin for Fan as Output
pinMode(auxPin, OUTPUT);// Define pin for Auxillary Power as Output
Serial.begin(9600);// Initialize serial communications at 9600 bps
Serial.println(F("Arduino UNO R3 with Ethernet Shiled W5100 - VU3GAO"));// Display Arduino title
Ethernet.begin(mac, ip, gateway, subnet);// Start Ethernet
server.begin();
Serial.print(F("Ethernet Shield initialized. Local IP address is:"));
Serial.println(Ethernet.localIP());// Print Server IP address
}
void loop()
{
EthernetClient client = server.available();// Create a client connection
if (client == true)
{
while (client.connected())
{
if (client.available())
{
char c = client.read();// Read char by char HTTP request
if (readString.length() < 100)
{
readString = readString + c;// Store characters to string
}
if (c == '\n')
{
Serial.println(readString);
client.println(F("http/1.1 200 ok"));// Send standard http headers
client.println(F("content-type: text/html"));
client.println();
client.println(F("<!doctype html><html>"));
client.println(F("<body bgcolor='blue'>"));
client.println(F("<center><head><title>Home Web Control</title></head>"));
client.println(F("<h2>Arduino UNO R3 Ethernet Shield Web Server(v1)</h2>"));
client.println(F("<h4>VU3GAO - Nitin William</h4>"));
client.println(F("<hr/><p> Click the Buttons to turn On and OFF <p/><hr/>"));
client.print(F("<input type=button value='Main Light ON' onmousedown=location.href='/?ML_on'>"));
client.println(F("<input type=button value='Main Light OFF' onmousedown=location.href='/?ML_off'><br/><br/>"));
client.print(F("<input type=button value='Chandelier ON' onmousedown=location.href='/?Ch_on'>"));
client.println(F("<input type=button value='Chandelier OFF' onmousedown=location.href='/?Ch_off'><br/><br/>"));
client.print(F("<input type=button value='Ceiling Fan ON' onmousedown=location.href='/?FN_on'>"));
client.println(F("<input type=button value='Ceiling Fan OFF' onmousedown=location.href='/?FN_off'><br/><br/>"));
client.print(F("<input type=button value='Auxillary Power ON' onmousedown=location.href='/?AP_on'>"));
client.println(F("<input type=button value='Auxillary Power OFF' onmousedown=location.href='/?AP_off'><br/><hr/>"));
client.println(F("</body></html>"));
delay(1);// Page loading delay
client.stop();// Stopping client
if(readString.indexOf("/?ML_on") > 0) digitalWrite(mainlightPin, HIGH);// Switch on Main Light
if(readString.indexOf("/?ML_off") > 0) digitalWrite(mainlightPin, LOW);// Switch off Main Light
if(readString.indexOf("/?Ch_on") > 0) digitalWrite(chandelierPin, HIGH);// Switch on Chandelier Light
if(readString.indexOf("/?Ch_off") > 0) digitalWrite(chandelierPin, LOW);// Switch off Chandelier Light
if(readString.indexOf("/?FN_on") > 0) digitalWrite(fanPin, HIGH);// Switch on Fan
if(readString.indexOf("/?FN_off") > 0) digitalWrite(fanPin, LOW);// Switch off Fan
if(readString.indexOf("/?AP_on") > 0) digitalWrite(auxPin, HIGH);// Switch on Auxillary Power
if(readString.indexOf("/?AP_off") > 0) digitalWrite(auxPin, LOW);// Switch off Auxillary Power
readString = "";// Clearing string for next read
}// End of line reached
}// End of client available
}// End of client connected
}// End of client connection
}// End of loop