Arduino nano, ENC28J60 Ethernet shield and 4 channel relay

i have already made this project previuosly using arduino uno, wiznet w5100 ethernet shield and 4 channel relay shield. But using Arduino nano + ENC29J60 lot cheaper. not mention it has smaller size. Once you get this badboy online on your network, you can control every device in your house using your smartphone or any gadget you have(that rely on your coding skill, ofcourse).

let’s the fun begin 🙂

wait, as usual i don’t do lots of talk so here’s the schematic and arduino code.

#include <UIPEthernet.h>

EthernetServer server = EthernetServer(23);
boolean alreadyConnected = false; // whether or not the client was connected previously
String commandString;
String commandStringTmp;

int ledPinOne = 3;
int ledPinTwo = 4;
int ledPinThree = 5;
int ledPinFour = 6;
int val;

void setup()
{
  pinMode(ledPinOne, OUTPUT);      // sets the digital pin 1 as output
  pinMode(ledPinTwo, OUTPUT);      // sets the digital pin 1 as output
  pinMode(ledPinThree, OUTPUT);      // sets the digital pin 1 as output
  pinMode(ledPinFour, OUTPUT);      // sets the digital pin 1 as output
  digitalWrite(ledPinOne, HIGH);
  digitalWrite(ledPinTwo, HIGH);
  digitalWrite(ledPinThree, HIGH);
  digitalWrite(ledPinFour, HIGH);
  
  //Serial.begin(9600);

  uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05};
  IPAddress myIP(192,168,200,36);
  IPAddress gateway(192,168,200,18);

  Ethernet.begin(mac,myIP,gateway);

  server.begin();
}

void loop() {
  // wait for a new client:
  EthernetClient client = server.available();

  // when the client sends the first byte, say hello:
  if (client) {
    if (!alreadyConnected) {
      // clear out the input buffer:
      client.flush();    
      commandString = ""; //clear the commandString variable

      //server.println("--> Please type your command and hit Return...");
      alreadyConnected = true;
    } 

    while (client.available()) {      
      // read the bytes incoming from the client:
      char newChar = client.read();

     if (newChar == 0x0D)  //If a 0x0D is received, a Carriage Return, then evaluate the command
     {
         //server.print("Received this command: ");
         //server.println(commandString);
         //if (commandString.indexOf("quit") > -1){
         commandStringTmp = commandString;
         commandStringTmp.replace("\n","");
         commandStringTmp.replace("\r","");
         if (commandStringTmp.equals("quit")) {
           server.println();
           server.print("disconnecting\r\n");
           client.stop();
         } else {   
           processCommand(commandString);
         }
     } else
     {
         //Serial.println(newChar);
         commandString += newChar;
     }

    }
  }
}

void processCommand(String command)
{
  command.replace("","");
  command.replace("\r","");
  //server.println("Processing command ");
  //server.println(command);
 
// ONE
  if (command.equals("ledoneon")) {
    //server.println("LED 1 On command received"); 
    digitalWrite(ledPinOne, LOW);   // sets the LED one on
    //server.println("LED 1 was turned on");
    commandString = "";
    return;  
  }
   
  
  if (command.equals("ledoneoff")) {
    //server.println("LED 1 Off command received"); 
    digitalWrite(ledPinOne, HIGH);   // sets the LED one off
    //server.println("LED 1 was turned off");
    commandString = "";
    return;
  } 

// TWO
  if (command.equals("ledtwoon")) {
    //server.println("LED 2 On command received"); 
    digitalWrite(ledPinTwo, LOW);   // sets the LED one on
    //server.println("LED 2 was turned on");
    commandString = "";
    return;  
  }
 
  if (command.equals("ledtwooff")) {
    //server.println("LED 2 Off command received"); 
    digitalWrite(ledPinTwo, HIGH);   // sets the LED one off
    //server.println("LED 2 was turned off");
    commandString = "";
    return;
  } 

// THREE
  if (command.equals("ledthreeon")) {
    //server.println("LED 3 On command received"); 
    digitalWrite(ledPinThree, LOW);   // sets the LED one on
    //server.println("LED 3 was turned on");
    commandString = "";
    return;  
  }
 
  if (command.equals("ledthreeoff")) {
    //server.println("LED 3 Off command received"); 
    digitalWrite(ledPinThree, HIGH);   // sets the LED one off
    //server.println("LED 3 was turned off");
    commandString = "";
    return;
  } 

// FOUR
  if (command.equals("ledfouron")) {
    //server.println("LED 4 On command received"); 
    digitalWrite(ledPinFour, LOW);   // sets the LED one on
    //server.println("LED 4 was turned on");
    commandString = "";
    return;  
  }
 
  if (command.equals("ledfouroff")) {
    //server.println("LED 4 Off command received"); 
    digitalWrite(ledPinFour, HIGH);   // sets the LED one off
    //server.println("LED 4 was turned off");
    commandString = "";
    return;
  } 

// STATUS
// ONE
  if (command.equals("ledonestatus")) {
    //server.println("LED One status command received"); 
    val = digitalRead(ledPinOne);   // sets the LED two on
    server.print("LED 1 was turned ");
    if (val == 0) {
      server.println("on");
    } else {
      server.println("off");
    }
    commandString = "";
    return;
  } 
  
// TWO
  if (command.equals("ledtwostatus")) {
    //server.println("LED Two status command received"); 
    val = digitalRead(ledPinTwo);   // sets the LED two on
    server.print("LED 2 was turned ");
    if (val == 0) {
      server.println("on");
    } else {
      server.println("off");
    }
    commandString = "";
    return;
  }

// THREE
  if (command.equals("ledthreestatus")) {
    //server.println("LED Three status command received"); 
    val = digitalRead(ledPinThree);   // sets the LED two on
    server.print("LED 3 was turned ");
    if (val == 0) {
      server.println("on");
    } else {
      server.println("off");
    }
    commandString = "";
    return;
  } 

// FOUR
  if (command.equals("ledfourstatus")) {
    //server.println("LED Four status command received"); 
    val = digitalRead(ledPinFour);   // sets the LED two on
    server.print("LED 4 was turned ");
    if (val == 0) {
      server.println("on");
    } else {
      server.println("off");
    }
    commandString = "";
    return;
  } 
  
  commandString = "";
}

you can test it, using telnet command to ip address assigned to ethernet shield.

example

telnet 192.168.200.36 23
ledoneon <- enter
ledoneoff <- enter
ledonestatus <- enter

commands are:

To turn relays on and off
- ledoneon
turn relay no 1 on
- ledoneoff
turn relay no 1 off
- ledtwoon
turn relay no 2 on
- ledtwooff
turn relay no 2 off
- ledthreeon
turn relay no 3 on
- ledthreeoff
turn relay no 3 off
- ledfouron
turn relay no 4 on
- ledfouroff
turn relay no 4 off

To view the state of relays

- leonestatus
- ledtwostatus
- ledthreestatus
- ledfourstatus

have fun 🙂

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *