Home Automation | Arduino Web Server using Ethernet Module

Arduino web server is used to control the home appliances from the remote location using the internet browser. For controlling this home automation project, we need arduino uno and arduino ethernet module enc28j60. We have used the enc28j60 ethernet module to connect arduino to the local ethernet.

There are different home automation project available using arduino, avr or raspberry pie. We have also developed bluetooth home automation using the AVR microcontroller. But this project can be controlled from any web browser locally by entering the IP address of the web server.

We have downloaded the EtherCard Library from github. Unzip the library in the Arduino/libraries folder.

How to Control Devices using Arduino Web Server

To understand this concept, we need some basic knowledge of the arduino and some terminology’s used in the internet world. While controlling the home devices, we use the relay to trigger the AC load. And this triggering signal comes from some kind of microcontroller devices. Here we are using the arduino uno to trigger the relay. But the question is how to control the devices over the internet locally.

For this, we connected the enc28j60 ethernet module to the arduino board with the SPI interface.

The arduino will work as the web server. We have written the code for HTML page into the arduino with some MAC and IP address. Now we have the arduino with ethernet connectivity and a web page.

Now open the web browser from any local network. In the URL section where we generally type the website address, type the IP address as given in the arduino web server program. Here this browser acts as a client and requesting the server to open the web page. Here we have arduino as a mini web server. And it will through the HTML page to the browser.

We took arduino web server project from http://www.winkleink.com. Where two led’s were controlled over the ethernet.

We are not using that long PHP code, modified little bit and mad simple to use

EtherCard

EtherCard is a driver for the ENC28J60 chip, compatible with Arduino IDE. Adapted and extended from code written by Guido Socher and Pascal Stang.

License: GPL2

The documentation for this library is at http://jeelabs.net/pub/docs/ethercard/.

Requirements

  • Hardware: This library only supports the ENC28J60 chip.
  • Hardware: Only AVR-based microcontrollers are supported, such as:
    • Arduino Uno
    • Arduino Mega
    • Arduino Leonardo
    • Arduino Nano/Pro/Fio/Pro-mini/LiliPad/Duemilanove
    • Any other Arduino clone using an AVR microcontroller should work
  • Hardware: Non-AVR boards are NOT currently supported (101/Zero/Due) #211
  • Hardware: Depending on the size of the buffer for packets, this library uses about 1k of Arduino RAM. Large strings and other global variables can easily push the limits of smaller microcontrollers.
  • Hardware: This library uses the SPI interface of the microcontroller, and will require at least one dedicated pin for CS, plus the SO, SI, and SCK pins of the SPI interface.
  • Software: Any Arduino IDE >= 1.0.0 should be fine

Arduino ENC28j60 Connection

We have connected the arduino ethernet module using the SPI interface.

 

Arduino Pins ENC28j60 Pins
MISO Digital Pin 12
MOSI Digital Pin 11
SCK Digital Pin 13
CS Digital Pin 10
VCC 5V or 3V3 (Check for Your Module)
GND Gnd

Arduino Circuit with ENCJ60 Ethernet Module

We know that for controlling the devices we need the relay. We are not using the relay in the circuit to make it look simple. But in actual we use the relay.

After connecting the arduino with ethernet module, we write the code. The IP address of the server is 192.168.1.5 and gateway is 192.168.1.1

static byte myip[] = { 192,168,1,5 };

static byte gwip[] = { 192,168,1,1 };

You can change the both according to your router and LAN configuration.

Now enter the IP address into the browser. You will get the simple HTML page by the arduino web server.

Now we can control the devices by pressing the link available at the HTML page. A get request goes to the server when we press the link. And server takes decision accordingly to ON the device.

Arduino Web Server Code

/*Source:Winlkeink : August 2012 : http://www.winkleink.com/2012/08/arduino-ethernet-ethercard-xamp-web.html*/
#include <EtherCard.h>

// ethernet mac address - must be unique on your network
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
// ethernet interface ip address

static byte myip[] = { 192,168,1,5 };
// gateway ip address
static byte gwip[] = { 192,168,1,1 };

byte Ethernet::buffer[500]; // tcp/ip send and receive buffer

// Using a Variable for the Pin, but it is not necessary 
const int ledPin2 = 2;
const int ledPin4 = 4;

// Some stuff for responding to the request
char* on = "ON";
char* off = "OFF";
char* statusLabel;
char* buttonLabel;

// Small web page to return so the request is completed
const char page[] PROGMEM =
"HTTP/1.0 503 Service Unavailable
"
"Content-Type: text/html
"
"Retry-After: 600
"
"
"
"<html>"
  "<head><title>"
    "www.maxphi.com Arduino 192.168.1.5"
  "</title></head>"
  "<body>"    
    "<h3>Home Automation www.maxphi.com</h3>" 
    "<p>FAN</p>"   
    "<a href='http://192.168.1.5/?FAN=ON'>ON</a><br>"
    "<a href='http://192.168.1.5/?FAN=OFF'>OFF</a><br>"
    "<br>"
    "<p>BULB</p>"   
    "<a href='http://192.168.1.5/?BULB=ON'>ON</a><br>"
    "<a href='http://192.168.1.5/?BULB=OFF'>OFF</a><br>"           
  "</body>"
"</html>"
;

void setup(){
// Set Pin2 to be an Output
  pinMode(ledPin2, OUTPUT);
// Set Pin4 to be an Output
  pinMode(ledPin4, OUTPUT);

// Scary complex intializing of the EtherCard - I don't understand this stuff (yet0  
  ether.begin(sizeof Ethernet::buffer, mymac);
// Set IP using Static
  ether.staticSetup(myip, gwip);
}

void loop(){  
  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);

// IF LED2=ON turn it ON
  if(strstr((char *)Ethernet::buffer + pos, "GET /?FAN=ON") != 0) {
      Serial.println("Received ON command");
      digitalWrite(ledPin2, HIGH);
    }

// IF LED2=OFF turn it OFF  
    if(strstr((char *)Ethernet::buffer + pos, "GET /?FAN=OFF") != 0) {
      Serial.println("Received OFF command");
      digitalWrite(ledPin2, LOW);
    }

// IF LED4=ON turn it ON
  if(strstr((char *)Ethernet::buffer + pos, "GET /?BULB=ON") != 0) {
      Serial.println("Received ON command");
      digitalWrite(ledPin4, HIGH);
    }

// IF LED4=OFF turn it OFF  
    if(strstr((char *)Ethernet::buffer + pos, "GET /?BULB=OFF") != 0) {
      Serial.println("Received OFF command");
      digitalWrite(ledPin4, LOW);
    }

//Return a page so the request is completed.
    memcpy_P(ether.tcpOffset(), page, sizeof page);
    ether.httpServerReply(sizeof page - 1);  
}