My first serious Arduino project: Ethernet Relay Controller – Day 4

So, if you remember the three days/posts I spent working on this project and how it diverted from controlling a relay switch using Ethernet and Arduino to just sending humidity and temperature, well, I’m back to relay switch controlling through Ethernet. And hopefully, this will ends this project.

So, I sketched this circuit using the following parts:

  • 1 x 1N4004 diode
  • 1 x 2n2222 transistor
  • 1 x 5v DC – 250v AC Relay switch
  • Arduino Uno Rev 3
  • Arduino Ethernet Shield

circuit

Explaining the diagram; connect the power source from the 5v pin from the Arduino board (If you have an external source, that’s even better) to both Diode and one of the relay switch’s coil nodes, then connect the other node with the Diode’s other node and the collector’s node in the Transistor. Connect the Transistor’s base to Pin #2 (Recommending adding a resistor. Perhaps a 1KΩ?) and connect the Transistor’s emitter to the ground.

Easy?

Well, here’s the code:

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 
  192, 168, 1, 15 };
byte gateway[] = { 
  192, 168, 1, 1 };
byte subnet[] = { 
  255, 255, 255, 0 };
EthernetServer server(80);

String readString;
int pinSwitch = 0;


void setup()
{

  pinMode(2, OUTPUT);
  Serial.begin(9600);

  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
}

void loop()
{
  EthernetClient client = server.available();
  if (client) 
  {
    while (client.connected())
    {
      if (client.available()) 
      {
        char c = client.read();

        if (readString.length() < 100) 
        {
          readString += c;
        }

        if (c == '\n') 
        {

          ProcessGet(readString);
          Serial.println(pinSwitch);

          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("  <HEAD>");
          client.println("    <TITLE>Relay switch test</TITLE>");
          client.println("  </HEAD>");
          client.println("<BODY>");
          client.println("  <H1>Relay switch test</H1>");
          client.println("  <hr />");
          client.println("  <br />");

          client.println("  <form method=\"GET\">");
          client.print("    <label>Current state (");
          if (pinSwitch == 0)
            client.println("OFF)</label><br/>");
          else
            client.println("ON)</label><br/>");

          client.print("    <input type=\"hidden\" name=\"switch\" value=\"");
          if (pinSwitch == 0)
            client.println("1\"/>");
          else
            client.println("0\"/>");
          
          client.println("    <input type=\"submit\" value=\"Change state\"/>");
          client.println("  </form>");

          client.println("</BODY>");
          client.println("</HTML>");

          delay(1);
          client.stop();
          readString = "";

        }
      }
    }
  }
}

void ProcessGet(String strGet)
{
  Serial.println(strGet);
  String par = strGet.substring(6,13);
  String val = strGet.substring(13,14);
  int X = 0;

  if (par == "switch=")
  {
    X = val.toInt();
    if (X == 0)
      SwitchTrigger(0);
    else if (X == 1)
      SwitchTrigger(1);    
  }
}

void SwitchTrigger(int state)
{
  if (state == 0)
  {
    digitalWrite(2, LOW);
    pinSwitch = state;
  }
  else if (state == 1)
  {
    digitalWrite(2, HIGH);
    pinSwitch = state;
  }
   
}

Here’s a video of how this looks like:

I’m not sure if I’ll add a real light to the end or not. But let’s hope I end up doing so 😛

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.