Control LED on Inernet using Arduino & ENC28J60 Ethernet Module

Control LED on Inernet using Arduino & ENC28J60 (Last Updated On: December 1, 2018)

Introduction:

In this Article we will learn how to Control LED on Inernet using Arduino & ENC28J60 Ethernet Module to establish communication between computer to Arduino in a LAN or Wireless Network. We are interfacing the ENC28J60 Ethernet controller to Arduino so that our Arduino will be the one of member of that network. Once it will done the arduino can Control LED on Inernet using Arduino & ENC28J60 Ethernet Module. Interfacing ENC28J60 Ethernet Module with Arduino and will further gives us the freedom of using the internet on Arduino.




ENC28J60 Ethernet Module:

Introduction to ENC28J60:

(Buy this ENC28J60 online from Amazon)

The ENC28J60 Ethernet Module utilizes the Microchip ENC28J60 Stand-Alone Ethernet Controller IC featuring a host of features to handle most of the network protocol requirements. The board connects directly to most microcontrollers with a standard SPI interface with a transfer speed of up to 20MHz.

Control LED on Inernet using Arduino & ENC28J60

This Ethernet Breakout-Module is simplest way to add LAN connectivity to your microcontroller based products and projects.

1. Use this module to enable Ethernet interface for your product. It works with any microcontroller operating at 3. 3V or 5V.
2. This module works at 3.3V and is compatible with 5V interface lines.
3. Use SPI process to interface with it.
4. Host web server, ping the module or add it to home automation via internet.
5. Heart of this module is ENC28J60 Ethernet controller from Microchip.
6. Use of RJ45 with Integrated magnetics has made it possible to reduce the size of board.

Features:

1. Ethernet LAN Module for Arduino/AVR/LPC/STM3
2. ENC28J60 Ethernet chips
3. Can be easily mounted with the MCU
4. Network Interface: HR911105A
5. Supply Voltage: 3.3 V (5V Tolerant DIO)
6. 25Mhz crystal oscillator
7. Size (L x W x H): Approx. 2.3 x 1.3 x 0.7 inch / 58 x 34 x 17 mm


Block Diagram & Connections:

This is the simple block diagram that explains how an Arduino & ENC28J60 module can be interfaced with Router and Computer so that LED can be controlled for switching on & off.

Control LED on Inernet using Arduino & ENC28J60
Figure: Block Diagram


The Router should be connected with LAN and should have multiple input output port. From the router one ethernet cable is connected to Computer System and other ethernet cable should be connected to ENC28J60 Module. Similarly the ENC28J60 Module is connected to Arduino. The connection between Arduino & ENC28J60 is given below.

Control LED on Inernet using Arduino & ENC28J60


Control LED on Inernet using Arduino & ENC28J60 Ethernet Module:

1. Download the Library for ENC28J60 from here: ENC28J60 Library

2. Add the library files to your Arduino IDE library:

3. Copy the Code from below and paste it to Arduino IDE.

4. Compile & then upload the code to Arduino UNO Board:
Control LED on Inernet using Arduino & ENC28J60

5. Click on Serial Monitor and copy the IP Address:
Control LED on Inernet using Arduino & ENC28J60

6. Open Google Chrome or any other web browser and paste the code and hit enter:
Control LED on Inernet using Arduino & ENC28J60



7. Copy the syntax code. Modify the syntax code by editing it with your IP adress number. Then hit enter:
Control LED on Inernet using Arduino & ENC28J60

8. Edit the code with ON & OFF command to switch on & switch off the LED:
Control LED on Inernet using Arduino & ENC28J60


Source Code:

#include <EtherCard.h>

#define STATIC 0 // set to 1 to disable DHCP (adjust myip/gwip values below)

// mac address
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
// ethernet interface ip address
static byte myip[] = { 192,168,0,200 };
// gateway ip address
static byte gwip[] = { 192,168,0,1 };

// LED to control output
int ledPin10 = 9;

byte Ethernet::buffer[700];

char const page[] PROGMEM =
"HTTP/1.0 503 Service Unavailable\r\n"
"Content-Type: text/html\r\n"
"Retry-After: 600\r\n"
"\r\n"
"<html>"
"<head><title>"
"Service Temporarily Unavailable"
"</title></head>"
"<body>"
"<h3>This page is used behind the scene</h3>"
"<p><em>"
"Commands to control LED are transferred to Arduino.<br />"
"The syntax: http://192.168.0.XX/?LED10=OFF or ON"
"</em></p>"
"</body>"
"</html>"
;

void setup () {
pinMode(ledPin10, OUTPUT);

Serial.begin(9600);
Serial.println("Trying to get an IP...");

Serial.print("MAC: ");
for (byte i = 0; i < 6; ++i) {
Serial.print(mymac[i], HEX);
if (i < 5)
Serial.print(':');
}
Serial.println();

if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
{
Serial.println( "Failed to access Ethernet controller");
}
else
{
Serial.println("Ethernet controller access: OK");
}
;

#if STATIC
Serial.println( "Getting static IP.");
if (!ether.staticSetup(myip, gwip)){
Serial.println( "could not get a static IP");
blinkLed(); // blink forever to indicate a problem
}
#else

Serial.println("Setting up DHCP");
if (!ether.dhcpSetup()){
Serial.println( "DHCP failed");
blinkLed(); // blink forever to indicate a problem
}
#endif

ether.printIp("My IP: ", ether.myip);
ether.printIp("Netmask: ", ether.netmask);
ether.printIp("GW IP: ", ether.gwip);
ether.printIp("DNS IP: ", ether.dnsip);
}

void loop () {

word len = ether.packetReceive();
word pos = ether.packetLoop(len);

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

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

// show some data to the user
memcpy_P(ether.tcpOffset(), page, sizeof page);
ether.httpServerReply(sizeof page - 1);
}

void blinkLed(){
while (true){
digitalWrite(ledPin10, HIGH);
delay(500);
digitalWrite(ledPin10, LOW);
delay(500);
}
}

Video Explanation & Demonstration:




Recommended For You

About the Author: Alex Newton

Ask A Question .......

Close