How to Make LAN/Ethernet Relay Switch Using Arduino UNO

How to Make LAN/Ethernet Relay Switch Using Arduino UNO © GPL3+

How to make LAN/Ethernet relay switch using Arduino UNO and ethernet shield ENC28j60.

  • 12,075 views
  • 2 comments
  • 5 respects

Components and supplies

About this project

How to make LAN/Ethernet relay switch using Arduino UNO and ethernet shield ENC28j60. This is a simple tutorial, remote control over the LAN.

To control it, I used HTTP and JSON. This electronic circuit can be used as part of a larger system, IoT Internet of things.

See more: How to make LAN/Ethernet relay switch using Arduino UNO.

Step 1: Components

Step 2: Schematic

Step 3: Ethernet modules ENC28J60

Connection ENC28J60 from Arduino UNO.

Step 4: Software

This software supports two relays, but it's not a problem to increase their number. IP device is permanently assigned to 192.168.0.101. If you had more devices working on the same network, you'd have to remember about unique IP and MAC addresses.

#include <UIPEthernet.h> // Used for Ethernet  #define PIN_LED 2  #define PIN_RELAY_0 A5 #define PIN_RELAY_1 A4    // **** ETHERNET SETTING **** // Ethernet MAC address - must be unique on your network byte mac[] = { 0x41, 0x44, 0x41, 0x43, 0x48, 0x41 }; //Ethernet interface IP address (unique in your network) IPAddress ip(192, 168, 0, 101);                         //ethernet interface IP port (80 = http) EthernetServer server(80); EthernetClient client; String str = "";   void setup() {   Serial.begin(115200);   Serial.print("START");    pinMode(PIN_RELAY_0, OUTPUT);   pinMode(PIN_RELAY_1, OUTPUT);    SW(0, LOW);   SW(1, LOW);    // start the Ethernet connection and the server:   Ethernet.begin(mac, ip);   server.begin();    Serial.print("IP Address: ");   Serial.println(Ethernet.localIP());   Serial.println(); }  void loop() {   client = server.available();    if( client ){     Serial.println("BEGIN-------------->\n");      boolean currentLineIsBlank = true;     String line = "";     bool firstLine = true;          while (client.connected()){              if (client.available()) {         char c = client.read();         Serial.print(c);          if (c == '\n' && currentLineIsBlank) {           String html = "{";           for(int i=0; i<2; i++){             if( SW_state(i) ){               html += "\"sw_"+String(i)+"\":1 ";             }else{               html += "\"sw_"+String(i)+"\":0 ";             }              if( i<1 ) html += ",";           }           html += "}";                      client.println(html);           break;         }          if (c == '\n') {                      currentLineIsBlank = true;           if( firstLine ) {             firstLine = false;             Serial.println("-------------------------------" + line);             if( line.indexOf( "sw_on=0" ) >= 0 ){               SW(0, HIGH);             }else if( line.indexOf( "sw_on=1" ) >= 0 ){               SW(1, HIGH);             }else if( line.indexOf( "sw_off=0" ) >= 0 ) {               SW(0, LOW);             }else if( line.indexOf( "sw_off=1" ) >= 0 ){               SW(1, LOW);             }           }         }else if (c != '\r') {           currentLineIsBlank = false;           if( firstLine ) {             line = line + c;           }         }       }     }      delay(10);      client.stop();     Serial.println("END-------------->\n");   } }  bool SW_state(byte num){   bool val;   switch(num){     case 0: val = digitalRead(PIN_RELAY_0); break;     case 1: val = digitalRead(PIN_RELAY_1); break;   }   return !val; }  void SW(byte num, bool val){   val = !val;   switch(num){     case 0: digitalWrite(PIN_RELAY_0, val); break;     case 1: digitalWrite(PIN_RELAY_1, val); break;   } }

Step 5: Testing

To turn on the relay, just enter the link in the browser. http://192.168.0.101/sw_on=1, "1" is the relay number. To turn off the relay http://192.168.0.101/sw_off=1. In response we get a JSON relay state {"sw_0":0 ,"sw_1":0 }.

Schematics

schematic_Z7ODI4K3Rs.jpg
Schematic z7odi4k3rs

Comments

Similar projects you might like

Arduino UNO and Vibration Sensor with Relay

Project tutorial by MichDragstar

  • 286 views
  • 0 comments
  • 5 respects

Simple Arduino Uno - ESP 8266 Integration

Project tutorial by circuito.io team

  • 93,140 views
  • 23 comments
  • 75 respects

Communication Between Arduino UNO

by Giovanni Gentile

  • 2,528 views
  • 1 comment
  • 4 respects

Add WiFi to Arduino UNO

by Jefferson Paredes

  • 145,150 views
  • 58 comments
  • 93 respects
Add projectSign up / Login