ETHERNET ENC28J60 + 5 Relay Modules (1 Relay on Temperature Sensor)

This sketch requires etherShield, ETHER_28J60 and OneWire library.

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
#define RELAY2  23
#define RELAY3  24
#define RELAY4  25
#define RELAY5  26
#include <OneWire.h>
int DS18S20_Pin = 2;
OneWire ds(DS18S20_Pin);

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

ETHER_28J60 e;

float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius

byte data[12];
byte addr[8];

if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}

if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println(“CRC is not valid!”);
return -1000;
}

if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print(“Device is not recognized”);
return -1000;
}

ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end

byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad

for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}

ds.reset_search();

byte MSB = data[1];
byte LSB = data[0];

float tempRead = ((MSB << 8) | LSB); //using two’s compliment
float TemperatureSum = tempRead / 16;

return TemperatureSum;

}

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

}

void loop()

{

char* params;
if (params = e.serviceRequest())
{

float temperature = getTemp();
e.print(“TEMP: “);
e.print(temperature); e.print(“C”);
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();
}
float temperature = getTemp();
if (temperature >32) {
digitalWrite(26,LOW);
} else {
digitalWrite(26,HIGH);
}
}

Advertisements
This entry was posted in Arduino Project. 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