Need help with my code. relay board + Ethernet Shield

Hey everybody!

I have a little issue I believe with my code. I connected all my devices/light on my NO contact but as soon as I power on the boards my NO contact are ON. I would like to know how I could start my board with my NO contact OFF. Also I would like to know how I could make like a timer/toggle on for my contact. Let say I turn on the light and I want after 5 second to turn off by itself.

Here’s my code:

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

//Setup Connection

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x03 };
byte ip[] = { 192, 168, 0, 4 };
byte gateway[] = { 192, 168, 0, 1 };
byte subnet[] = { 255, 255, 255, 0 };
EthernetServer server(80);
String readString;

//Setup Pins on the board

void setup(){  

  pinMode(2, OUTPUT); // Pin Assignment through which relay will be controlled
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  
  //start Ethernet
  
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  //enable serial data print

  Serial.begin(9600);
  Serial.println("server LED test 1.0"); // so that we can know what is getting loaded
}

void loop(){  

  // Create a client connection

  EthernetClient client = server.available();

  if (client) {

    while (client.connected()) {

      if (client.available()) {

        char c = client.read();

        //read char by char HTTP request

        if (readString.length() < 100) {

          //store characters to string

          readString += c;
          
          //Serial.print(c);
        }
        //if HTTP request has ended

        if (c == '\n') {
          Serial.println(readString); //print to serial monitor for debuging

          /* Start OF HTML Section. Here Keep everything as it is unless you understands its working */
      
          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("</HEAD>");

         // Relay Control Code

          client.println("<a href=\"/?relay1on\"\">Turn On Light 1</a>");
          client.println("<a href=\"/?relay1off\"\">Turn Off Light 1</a>
"); 
          client.println("
");
          client.println("
");
          client.println("<a href=\"/?relay2on\"\">Turn On Light 2</a>");
          client.println("<a href=\"/?relay2off\"\">Turn Off Light 2</a>
");
          client.println("
");
          client.println("
");
          client.println("<a href=\"/?relay3on\"\">Turn On Light 3</a>");
          client.println("<a href=\"/?relay3off\"\">Turn Off Light 3</a>
");
          client.println("
");
          client.println("
");
          client.println("<a href=\"/?relay4on\"\">Turn On Light 4</a>");
          client.println("<a href=\"/?relay4off\"\">Turn Off Light 4</a>
");
          client.println("
");
          client.println("
");
          client.println("<a href=\"/?relay5on\"\">Turn On Light 5</a>");
          client.println("<a href=\"/?relay5off\"\">Turn Off Light 5</a>
"); 
          client.println("
");
          client.println("
");
          client.println("<a href=\"/?relay6on\"\">Turn On Light 6</a>");
          client.println("<a href=\"/?relay6off\"\">Turn Off Light 6</a>
"); 
          client.println("
");
          client.println("
");
          client.println("<a href=\"/?relay7on\"\">Turn On Light 7</a>");
          client.println("<a href=\"/?relay7off\"\">Turn Off Light 7</a>
"); 
          client.println("
");
          client.println("
");
          client.println("<a href=\"/?relay8on\"\">Turn On Light 8</a>");
          client.println("<a href=\"/?relay8off\"\">Turn Off Light 8</a>
"); 
          client.println("
");
          client.println("
");

            // control arduino pin via ethernet Start //

          if(readString.indexOf("?relay1on") >0)//checks for on
          {
            digitalWrite(6, HIGH);    // set pin 4 high
            Serial.println("Led On");
            client.println("
");
        }
          else{
          if(readString.indexOf("?relay1off") >0)//checks for off
          {
            digitalWrite(6, LOW);    // set pin 4 low
            Serial.println("Led Off");
            client.println("
");
        }
          }
          if(readString.indexOf("?relay2on") >0)
          {
            digitalWrite(7, HIGH);
            Serial.println("Led On");
            client.println("
");
          }
          else{
          if(readString.indexOf("?relay2off") >0)
          {
            digitalWrite(7, LOW);
            Serial.println("Led Off");
            client.println("
");
          }
          }
           if(readString.indexOf("?relay3on") >0)
          {
            digitalWrite(8, HIGH);
            Serial.println("Led On");
            client.println("
");
          }
          else{
          if(readString.indexOf("?relay3off") >0)
          {
            digitalWrite(8, LOW);
            Serial.println("Led Off");
            client.println("
");
          }
          }
           if(readString.indexOf("?relay4on") >0)
          {
            digitalWrite(9, HIGH);
            Serial.println("Led On");
            client.println("
");
          }
          else{
          if(readString.indexOf("?relay4off") >0)
          {
            digitalWrite(9, LOW);
            Serial.println("Led Off");
            client.println("
");
          }
          }
      if(readString.indexOf("?relay5on") >0)
          {
            digitalWrite(5, HIGH);
            Serial.println("Led On");
            client.println("
");
          }
          else{
          if(readString.indexOf("?relay5off") >0)
          {
            digitalWrite(5, LOW);
            Serial.println("Led Off");
            client.println("
");
          }
          }
      if(readString.indexOf("?relay6on") >0)
          {
            digitalWrite(4, HIGH);
            Serial.println("Led On");
            client.println("
");
        }
          else{
          if(readString.indexOf("?relay6off") >0)
          {
            digitalWrite(4, LOW);
            Serial.println("Led Off");
            client.println("
");
        }
          }
      if(readString.indexOf("?relay7on") >0)
          {
            digitalWrite(3, HIGH);
            Serial.println("Led On");
            client.println("
");
        }
          else{
          if(readString.indexOf("?relay7off") >0)
          {
            digitalWrite(3, LOW);
            Serial.println("Led Off");
            client.println("
");
        }
          }
      if(readString.indexOf("?relay8on") >0)
          {
            digitalWrite(2, HIGH);
            Serial.println("Led On");
            client.println("
");
        }
          else{
          if(readString.indexOf("?relay8off") >0)
          {
            digitalWrite(2, LOW);
            Serial.println("Led Off");
            client.println("
");
        }
          }
      
          //clearing string for next read
          readString="";
          client.println("</body>");
          client.println("</HTML>");
          delay(1);

          //stopping client
          client.stop();
        }
      }
    }
  }
}

Thanks for your help!

PS: I am a beginner on Arduino Product this is my first project.
PS2: I know I mixed my High and Low / On and Off (When i click off it’s on and when i click on its off lol) I will fix my html later for that.

Are you sure you have your relay outputs wired correctly? You might set the arduino output pins low in the setup section of your code.

zoomkat:
Are you sure you have your relay outputs wired correctly? You might set the arduino output pins low in the setup section of your code.

What do you mean? All my wires are setup correctly though. I can control all my relays with the web interface.

stangatx:
What do you mean? All my wires are setup correctly though. I can control all my relays with the web interface.

Maybe something like below in the setup area. The digital output pins may be floating until you assign them a condition.

void setup(){ 

  pinMode(2, OUTPUT); // Pin Assignment through which relay will be controlled
  digitalWrite(2, LOW);    // set pin 2 low
  pinMode(3, OUTPUT);
  digitalWrite(3, LOW);    
  pinMode(4, OUTPUT);
  digitalWrite(4, LOW);    
  pinMode(5, OUTPUT);
  digitalWrite(5, LOW);    
  pinMode(6, OUTPUT);
  digitalWrite(6, LOW);    
  pinMode(7, OUTPUT);
  digitalWrite(7, LOW);    
  pinMode(8, OUTPUT);
  digitalWrite(8, LOW);    
  pinMode(9, OUTPUT);
  digitalWrite(9, LOW);

zoomkat:
Maybe something like below in the setup area. The digital output pins may be floating until you assign them a condition.

void setup(){ 

pinMode(2, OUTPUT); // Pin Assignment through which relay will be controlled
  digitalWrite(2, LOW);    // set pin 2 low
  pinMode(3, OUTPUT);
  digitalWrite(3, LOW);   
  pinMode(4, OUTPUT);
  digitalWrite(4, LOW);   
  pinMode(5, OUTPUT);
  digitalWrite(5, LOW);   
  pinMode(6, OUTPUT);
  digitalWrite(6, LOW);   
  pinMode(7, OUTPUT);
  digitalWrite(7, LOW);   
  pinMode(8, OUTPUT);
  digitalWrite(8, LOW);   
  pinMode(9, OUTPUT);
  digitalWrite(9, LOW);

Thanks, I actually thought about something like that but still after I uploaded the code my relays are still ON even though I'm NO.

Without knowing your wiring or relay setup, all I can think of is you wired to NC instead of NO on the relay outputs.

zoomkat:
Without knowing your wiring or relay setup, all I can think of is you wired to NC instead of NO on the relay outputs.

Nevermind I got it. It wasn’t my wiring it was my coding haha. Do you know how can I add a delay when I turn on the light? Let’s say when I turn on the light after 5 seconds I would like it turns off by itself. I tried the delay function but it didn’t work…

Here’s my updatedcode:

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

//Setup Connection

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x03 };
byte ip[] = { 192, 168, 0, 4 };
byte gateway[] = { 192, 168, 0, 1 };
byte subnet[] = { 255, 255, 255, 0 };
EthernetServer server(80);
String readString;

//Setup Pins on the board

void setup(){  

  pinMode(2, OUTPUT); // Pin Assignment through which relay will be controlled
  digitalWrite(2, HIGH);    // set pin 2 low
  pinMode(3, OUTPUT);
  digitalWrite(3, HIGH);   
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);   
  pinMode(5, OUTPUT);
  digitalWrite(5, HIGH);   
  pinMode(6, OUTPUT);
  digitalWrite(6, HIGH);   
  pinMode(7, OUTPUT);
  digitalWrite(7, HIGH);   
  pinMode(8, OUTPUT);
  digitalWrite(8, HIGH);   
  pinMode(9, OUTPUT);
  digitalWrite(9, HIGH);
  
  //start Ethernet
  
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  //enable serial data print

  Serial.begin(9600);
  Serial.println("server LED test 1.0"); // so that we can know what is getting loaded
}

void loop(){  

  // Create a client connection

  EthernetClient client = server.available();

  if (client) {

    while (client.connected()) {

      if (client.available()) {

        char c = client.read();

        //read char by char HTTP request

        if (readString.length() < 100) {

          //store characters to string

          readString += c;
          
          //Serial.print(c);
        }
        //if HTTP request has ended

        if (c == '\n') {
          Serial.println(readString); //print to serial monitor for debuging

          /* Start OF HTML Section. Here Keep everything as it is unless you understands its working */
      
          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("</HEAD>");

         // Relay Control Code

          client.println("<a href=\"/?relay1on\"\">Turn On Light 1</a>");
          client.println("<a href=\"/?relay1off\"\">Turn Off Light 1</a>
"); 
          client.println("
");
          client.println("
");
          client.println("<a href=\"/?relay2on\"\">Turn On Light 2</a>");
          client.println("<a href=\"/?relay2off\"\">Turn Off Light 2</a>
");
          client.println("
");
          client.println("
");
          client.println("<a href=\"/?relay3on\"\">Turn On Light 3</a>");
          client.println("<a href=\"/?relay3off\"\">Turn Off Light 3</a>
");
          client.println("
");
          client.println("
");
          client.println("<a href=\"/?relay4on\"\">Turn On Light 4</a>");
          client.println("<a href=\"/?relay4off\"\">Turn Off Light 4</a>
");
          client.println("
");
          client.println("
");
          client.println("<a href=\"/?relay5on\"\">Turn On Light 5</a>");
          client.println("<a href=\"/?relay5off\"\">Turn Off Light 5</a>
"); 
          client.println("
");
          client.println("
");
          client.println("<a href=\"/?relay6on\"\">Turn On Light 6</a>");
          client.println("<a href=\"/?relay6off\"\">Turn Off Light 6</a>
"); 
          client.println("
");
          client.println("
");
          client.println("<a href=\"/?relay7on\"\">Turn On Light 7</a>");
          client.println("<a href=\"/?relay7off\"\">Turn Off Light 7</a>
"); 
          client.println("
");
          client.println("
");
          client.println("<a href=\"/?relay8on\"\">Turn On Light 8</a>");
          client.println("<a href=\"/?relay8off\"\">Turn Off Light 8</a>
"); 
          client.println("
");
          client.println("
");

            // control arduino pin via ethernet Start //

          if(readString.indexOf("?relay1on") >0)//checks for on
          {
            digitalWrite(6, LOW);    // set pin 4 high
            Serial.println("Led On");
            client.println("
");
        }
          else{
          if(readString.indexOf("?relay1off") >0)//checks for off
          {
            digitalWrite(6, HIGH);    // set pin 4 low
            Serial.println("Led Off");
            client.println("
");
        }
          }
          if(readString.indexOf("?relay2on") >0)
          {
            digitalWrite(7, LOW);
            Serial.println("Led On");
            client.println("
");
          }
          else{
          if(readString.indexOf("?relay2off") >0)
          {
            digitalWrite(7, HIGH);
            Serial.println("Led Off");
            client.println("
");
          }
          }
           if(readString.indexOf("?relay3on") >0)
          {
            digitalWrite(8, LOW);
            Serial.println("Led On");
            client.println("
");
          }
          else{
          if(readString.indexOf("?relay3off") >0)
          {
            digitalWrite(8, HIGH);
            Serial.println("Led Off");
            client.println("
");
          }
          }
           if(readString.indexOf("?relay4on") >0)
          {
            digitalWrite(9, LOW);
            Serial.println("Led On");
            client.println("
");
          }
          else{
          if(readString.indexOf("?relay4off") >0)
          {
            digitalWrite(9, HIGH);
            Serial.println("Led Off");
            client.println("
");
          }
          }
      if(readString.indexOf("?relay5on") >0)
          {
            digitalWrite(5, LOW);
            Serial.println("Led On");
            client.println("
");
          }
          else{
          if(readString.indexOf("?relay5off") >0)
          {
            digitalWrite(5, HIGH);
            Serial.println("Led Off");
            client.println("
");
          }
          }
      if(readString.indexOf("?relay6on") >0)
          {
            digitalWrite(4, LOW);
            Serial.println("Led On");
            client.println("
");
        }
          else{
          if(readString.indexOf("?relay6off") >0)
          {
            digitalWrite(4, HIGH);
            Serial.println("Led Off");
            client.println("
");
        }
          }
      if(readString.indexOf("?relay7on") >0)
          {
            digitalWrite(3, LOW);
            Serial.println("Led On");
            client.println("
");
        }
          else{
          if(readString.indexOf("?relay7off") >0)
          {
            digitalWrite(3, HIGH);
            Serial.println("Led Off");
            client.println("
");
        }
          }
      if(readString.indexOf("?relay8on") >0)
          {
            digitalWrite(2, LOW);
            Serial.println("Led On");
            client.println("
");
        }
          else{
          if(readString.indexOf("?relay8off") >0)
          {
            digitalWrite(2, HIGH);
            Serial.println("Led Off");
            client.println("
");
        }
          }
      
          //clearing string for next read
          readString="";
          client.println("</body>");
          client.println("</HTML>");
          delay(1);

          //stopping client
          client.stop();
        }
      }
    }
  }
}