ETHERNET ENC28J60 + 4 Relay Modules (simple sketch)

web4relay

Ok so this is the easiest way to control 4 relays on a webserver controlled by ENC28J60.

You can download the library here

https://docs.google.com/file/d/0B4qM0hq-OmC1RHhEcXRNSmZ1YW8/

 

// A simple web server that turn a Relay on or off”

#include “etherShield.h”
#include “ETHER_28J60.h”
#define RELAY1  22 //Digital Pin 22
#define RELAY2  23
#define RELAY3  24
#define RELAY4  25
#define RELAY5  26
#define RELAY6  27

static uint8_t mac[6] = {0x54, 0x55, 0x58, 0x10, 0x00, 0x24};   // this just needs to be unique for your network,

static uint8_t ip[4] = {192, 168, 1, 3}; // IP address for the webserver

static uint16_t port = 240; // Use port 80 – the standard for HTTP – MAX 240port

ETHER_28J60 e;

void setup()
{
e.setup(mac, ip, port);
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(RELAY3, OUTPUT);
pinMode(RELAY4, OUTPUT);
digitalWrite(RELAY1,HIGH);
digitalWrite(RELAY2,HIGH);
digitalWrite(RELAY3,HIGH);
digitalWrite(RELAY4,HIGH);
}

void loop()

{

char* params;
if (params = e.serviceRequest())
{
e.print(“<h1><a href=’/?led1=off&led2=off’>Relay Control Panel</a></h1>”);
e.print(“<h1><a href=’/?rele3=off&rele4=off’>Relay Control Panel 2</a></h1>”);

if (strcmp(params, “?led1=on&led2=off”) == 0)
{
pinMode(RELAY1, HIGH);
pinMode(RELAY2, LOW);
e.print(“<a href=’?led1=off&led2=off’><button style=’border: 1px solid #00FF00; border-left: 10px solid #00FF00′ type=’button’>RELAY 1 IS ON</button></a><a href=’?led1=on&led2=on’><br/><button style=’border: 1px solid #FF0000; border-left: 10px solid #FF0000′ type=’button’>RELAY 2 IS OFF</button></a>”);
}
else if (strcmp(params, “?led1=off&led2=on”) == 0)
{
pinMode(RELAY1, LOW);
pinMode(RELAY2, HIGH);
e.print(“<a href=’?led1=on&led2=on’><button style=’border: 1px solid #FF0000; border-left: 10px solid #FF0000′ type=’button’>RELAY 1 IS OFF</button></a><a href=’?led1=off&led2=off’><br/><button style=’border: 1px solid #00FF00; border-left: 10px solid #00FF00′ type=’button’>RELAY 2 IS ON</button></a>”);
}
else if (strcmp(params, “?led1=off&led2=off”) == 0)
{
pinMode(RELAY1, LOW);
pinMode(RELAY2, LOW);
e.print(“<a href=’?led1=on&led2=off’><button style=’border: 1px solid #FF0000; border-left: 10px solid #FF0000′ type=’button’>RELAY 1 IS OFF</button></a><a href=’?led1=off&led2=on’><br/><button style=’border: 1px solid #FF0000; border-left: 10px solid #FF0000′ type=’button’>RELAY 2 IS OFF</button></a>”);
}
else if (strcmp(params, “?led1=on&led2=on”) == 0)
{
pinMode(RELAY1, HIGH);
pinMode(RELAY2, HIGH);
e.print(“<a href=’?led1=off&led2=on’><button style=’border: 1px solid #00FF00; border-left: 10px solid #00FF00′ type=’button’>RELAY 1 IS ON</button></a><a href=’?led1=on&led2=off’><br/><button style=’border: 1px solid #00FF00; border-left: 10px solid #00FF00′ type=’button’>RELAY 2 IS ON</button></a>”);
}
else if (strcmp(params, “?rele3=on&rele4=off”) == 0)
{
pinMode(RELAY3, HIGH);
pinMode(RELAY4, LOW);
e.print(“<a href=’?rele3=off&rele4=off’><button style=’border: 1px solid #00FF00; border-left: 10px solid #00FF00′ type=’button’>RELAY 3 IS ON</button></a><a href=’?rele3=on&rele4=on’><br/><button style=’border: 1px solid #FF0000; border-left: 10px solid #FF0000′ type=’button’>RELAY 4 IS OFF</button></a>”);
}
else if (strcmp(params, “?rele3=off&rele4=on”) == 0)
{
pinMode(RELAY3, LOW);
pinMode(RELAY4, HIGH);
e.print(“<a href=’?rele3=on&rele4=on’><button style=’border: 1px solid #FF0000; border-left: 10px solid #FF0000′ type=’button’>RELAY 3 IS OFF</button></a><a href=’?rele3=off&rele4=off’><br/><button style=’border: 1px solid #00FF00; border-left: 10px solid #00FF00′ type=’button’>RELAY 4 IS ON</button></a>”);
}
else if (strcmp(params, “?rele3=off&rele4=off”) == 0)
{
pinMode(RELAY3, LOW);
pinMode(RELAY4, LOW);
e.print(“<a href=’?rele3=on&rele4=off’><button style=’border: 1px solid #FF0000; border-left: 10px solid #FF0000′ type=’button’>RELAY 3 IS OFF</button></a><a href=’?rele3=off&rele4=on’><br/><button style=’border: 1px solid #FF0000; border-left: 10px solid #FF0000′ type=’button’>RELAY 4 IS OFF</button></a>”);
}
else if (strcmp(params, “?rele3=on&rele4=on”) == 0)
{
pinMode(RELAY3, HIGH);
pinMode(RELAY4, HIGH);
e.print(“<a href=’?rele3=off&rele4=on’><button style=’border: 1px solid #00FF00; border-left: 10px solid #00FF00′ type=’button’>RELAY 3 IS ON</button></a><a href=’?rele3=on&rele4=off’><br/><button style=’border: 1px solid #00FF00; border-left: 10px solid #00FF00′ type=’button’>RELAY 4 IS ON</button></a>”);
}

e.respond();
}
}

Advertisements
This entry was posted in Arduino Project and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s