Problem - remote control relays (ethernet shield)

Working code on message 3

when i send a http request to the arduino server with some number (doesn’t matter what) its response to the browser :

Turning off pin 2 Turning off pin 3 Turning off pin 4 Turning off pin 5 Turning off pin 6 Turning off pin 7 Turning off pin 8 Turning off pin 9

evrey time,
and i can’t control the relays…

i can’t find any problem with the code, do you see the problem??

#include <Ethernet.h>
#include <SPI.h>
boolean reading = false;

////////////////////////////////////////////////////////////////////////
//CONFIGURE
////////////////////////////////////////////////////////////////////////

/*-----( Declare Constants )-----*/
#define RELAY_ON 0
#define RELAY_OFF 1
/*-----( Declare objects )-----*/
/*-----( Declare Variables )-----*/
#define Relay_1  2  // Arduino Digital I/O pin number
#define Relay_2  3
#define Relay_3  4
#define Relay_4  5
#define Relay_5  6  // Arduino Digital I/O pin number
#define Relay_6  7
#define Relay_7  9
//#define Relay_8  9  

byte ip[] = { 192, 168, 0, 101 };   //Manual setup only
  byte gateway[] = { 192, 168, 0, 1 }; //Manual setup only
  byte subnet[] = { 255, 255, 255, 0 }; //Manual setup only

  // if need to change the MAC address (Very Rare)
  byte mac[] = { 0xDE, 0xAD, 0xBE, 0xED, 0xFE, 0xED };

  EthernetServer server = EthernetServer(80); //port 80
  
  int status2 = 0;
  int status3 = 0;
  int status4 = 0;
  int status5 = 0;
  int status6 = 0;
  int status7 = 0;
  int status8 = 0;
  int status9 = 0;
  
////////////////////////////////////////////////////////////////////////

void setup(){
  Serial.begin(9600);

   digitalWrite(Relay_1, RELAY_OFF);
  digitalWrite(Relay_2, RELAY_OFF);
  digitalWrite(Relay_3, RELAY_OFF);
  digitalWrite(Relay_4, RELAY_OFF);  
  digitalWrite(Relay_5, RELAY_OFF);
  digitalWrite(Relay_6, RELAY_OFF);
  digitalWrite(Relay_7, RELAY_OFF);
 // digitalWrite(Relay_8, RELAY_OFF); 
  
//---( THEN set pins as outputs )----  
  pinMode(Relay_1, OUTPUT);   
  pinMode(Relay_2, OUTPUT);  
  pinMode(Relay_3, OUTPUT);  
  pinMode(Relay_4, OUTPUT);    
  pinMode(Relay_5, OUTPUT);   
  pinMode(Relay_6, OUTPUT);  
  pinMode(Relay_7, OUTPUT);  
//  pinMode(Relay_8, OUTPUT);    
  delay(4000); //Check that all relays are inactive at Reset
  //Ethernet.begin(mac);
  Ethernet.begin(mac, ip, gateway, subnet); //for manual setup

  server.begin();
  Serial.println(Ethernet.localIP());

}

void loop(){

  // listen for incoming clients, and process qequest.
  checkForClient();

}

void checkForClient(){

  EthernetClient client = server.available();

  if (client) {

    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    boolean sentHeader = false;

    while (client.connected()) {
      if (client.available()) {

        if(!sentHeader){
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          sentHeader = true;
        }

        char c = client.read();

        if(reading && c == ' ') reading = false;
        if(c == '?') reading = true; //found the ?, begin reading the info

        if(reading){
          Serial.print(c);

           switch (c) {
            case '2':
              //add code here to trigger on 2
              triggerPin(2, client);
              break;
            case '3':
            //add code here to trigger on 3
              triggerPin(3, client);
              break;
            case '4':
            //add code here to trigger on 4
              triggerPin(4, client);
              break;
            case '5':
            //add code here to trigger on 5
              triggerPin(5, client);
              break;
            case '6':
            //add code here to trigger on 6
              triggerPin(6, client);
              break;
            case '7':
            //add code here to trigger on 7
              triggerPin(7, client);
              break;
            case '8':
            //add code here to trigger on 8
              triggerPin(8, client);
              break;
            case '9':
            //add code here to trigger on 9
              triggerPin(9, client);
              break;
          }

        }

        if (c == '\n' && currentLineIsBlank)  break;

        if (c == '\n') {
          currentLineIsBlank = true;
        }else if (c != '\r') {
          currentLineIsBlank = false;
        }

      }
    }

    delay(1); // give the web browser time to receive the data
    client.stop(); // close the connection:

  } 

}

void triggerPin(int pin, EthernetClient client){
  if (pin = 2) {
  if (status2 = 0) {
   triggerRelay(pin, 1);
   status2 = 1;
   client.print("Turning on pin ");
   client.println(pin);
  }
  else if (status2 = 1) {
   triggerRelay(pin, 0);
   status2 = 0;
   client.print("Turning off pin ");
   client.println(pin);
  }
 }

 if (pin = 3) {
  if (status3 = 0) {
   triggerRelay(pin, 1);
   status3 = 1;
   client.print("Turning on pin ");
   client.println(pin);
  }
  else if (status3 = 1) {
   triggerRelay(pin, 0);
   status3 = 0;
   client.print("Turning off pin ");
   client.println(pin);
  }
 }

 if (pin = 4) {
  if (status4 = 0) {
   triggerRelay(pin, 1);
   status4 = 1;
   client.print("Turning on pin ");
   client.println(pin);
  }
  else if (status4 = 1) {
   triggerRelay(pin, 0);
   status4 = 0;
   client.print("Turning off pin ");
   client.println(pin);
  }
 }

 if (pin = 5) {
  if (status5 = 0) {
   triggerRelay(pin, 1);
   status5 = 1;
   client.print("Turning on pin ");
   client.println(pin);
  }
  else if (status5 = 1) {
   triggerRelay(pin, 0);
   status5 = 0;
   client.print("Turning off pin ");
   client.println(pin);
  }
 }

 if (pin = 6) {
  if (status6 = 0) {
   triggerRelay(pin, 1);
   status6 = 1;
   client.print("Turning on pin ");
   client.println(pin);
  }
  else if (status6 = 1) {
   triggerRelay(pin, 0);
   status6 = 0;
   client.print("Turning off pin ");
   client.println(pin);
  }
 }

 if (pin = 7) {
  if (status7 = 0) {
   triggerRelay(pin, 1);
   status7 = 1;
   client.print("Turning on pin ");
   client.println(pin);
  }
  else if (status7 = 1) {
   triggerRelay(pin, 0);
   status7 = 0;
   client.print("Turning off pin ");
   client.println(pin);
  }
 }

 if (pin = 8) {
  if (status8 = 0) {
   triggerRelay(pin, 1);
   status8 = 1;
   client.print("Turning on pin ");
   client.println(pin);
  }
  else if (status8 = 1) {
   triggerRelay(pin, 0);
   status8 = 0;
   client.print("Turning off pin ");
   client.println(pin);
  }
 }

 if (pin = 9) {
  if (status9 = 0) {
   triggerRelay(pin, 1);
   status9 = 1;
   client.print("Turning on pin ");
   client.println(pin);
  }
  else if (status9 = 1) {
   triggerRelay(pin, 0);
   status9 = 0;
   client.print("Turning off pin ");
   client.println(pin);
  }
 }


}

void triggerRelay(int pin, int stat){
  if (stat == 1) {
  digitalWrite(pin,  RELAY_ON);
  }
  else {
    digitalWrite(pin,  RELAY_OFF);
  }
  
}

You are assigning values, not comparing here. Change “=” to “==” in the comparisons.

void triggerPin(int pin, EthernetClient client){
  if (pin = 2) {
  if (status2 = 0) {
   triggerRelay(pin, 1);
   status2 = 1;
   client.print("Turning on pin ");
   client.println(pin);
  }
  else if (status2 = 1) {
   triggerRelay(pin, 0);
   status2 = 0;
   client.print("Turning off pin ");
   client.println(pin);
  }
 }

Like this:

void triggerPin(int pin, EthernetClient client){
  if (pin == 2) {
  if (status2 == 0) {
   triggerRelay(pin, 1);
   status2 = 1;
   client.print("Turning on pin ");
   client.println(pin);
  }
  else if (status2 == 1) {
   triggerRelay(pin, 0);
   status2 = 0;
   client.print("Turning off pin ");
   client.println(pin);
  }
 }

SurferTim: You are assigning values, not comparing here. Change "=" to "==" in the comparisons.

void triggerPin(int pin, EthernetClient client){
  if (pin = 2) {
  if (status2 = 0) {
   triggerRelay(pin, 1);
   status2 = 1;
   client.print("Turning on pin ");
   client.println(pin);
  }
  else if (status2 = 1) {
   triggerRelay(pin, 0);
   status2 = 0;
   client.print("Turning off pin ");
   client.println(pin);
  }
 }

Like this:

void triggerPin(int pin, EthernetClient client){
  if (pin == 2) {
  if (status2 == 0) {
   triggerRelay(pin, 1);
   status2 = 1;
   client.print("Turning on pin ");
   client.println(pin);
  }
  else if (status2 == 1) {
   triggerRelay(pin, 0);
   status2 = 0;
   client.print("Turning off pin ");
   client.println(pin);
  }
 }

thank you very much.. its worked

here is the working code if anyone want :

#include <Ethernet.h>
#include <SPI.h>
boolean reading = false;

////////////////////////////////////////////////////////////////////////
//CONFIGURE
////////////////////////////////////////////////////////////////////////

/*-----( Declare Constants )-----*/
#define RELAY_ON 0
#define RELAY_OFF 1
/*-----( Declare objects )-----*/
/*-----( Declare Variables )-----*/
#define Relay_1  2  // Arduino Digital I/O pin number
#define Relay_2  3
#define Relay_3  4
#define Relay_4  5
#define Relay_5  6  // Arduino Digital I/O pin number
#define Relay_6  7
#define Relay_7  9
//#define Relay_8  9  

byte ip[] = { 192, 168, 0, 101 };   //Manual setup only
  byte gateway[] = { 192, 168, 0, 1 }; //Manual setup only
  byte subnet[] = { 255, 255, 255, 0 }; //Manual setup only

  // if need to change the MAC address (Very Rare)
  byte mac[] = { 0xDE, 0xAD, 0xBE, 0xED, 0xFE, 0xED };

  EthernetServer server = EthernetServer(80); //port 80
  
  int status2 = 0;
  int status3 = 0;
  int status4 = 0;
  int status5 = 0;
  int status6 = 0;
  int status7 = 0;
  int status8 = 0;
  int status9 = 0;
  
////////////////////////////////////////////////////////////////////////

void setup(){
  Serial.begin(9600);

   digitalWrite(Relay_1, RELAY_OFF);
  digitalWrite(Relay_2, RELAY_OFF);
  digitalWrite(Relay_3, RELAY_OFF);
  digitalWrite(Relay_4, RELAY_OFF);  
  digitalWrite(Relay_5, RELAY_OFF);
  digitalWrite(Relay_6, RELAY_OFF);
  digitalWrite(Relay_7, RELAY_OFF);
 // digitalWrite(Relay_8, RELAY_OFF); 
  
//---( THEN set pins as outputs )----  
  pinMode(Relay_1, OUTPUT);   
  pinMode(Relay_2, OUTPUT);  
  pinMode(Relay_3, OUTPUT);  
  pinMode(Relay_4, OUTPUT);    
  pinMode(Relay_5, OUTPUT);   
  pinMode(Relay_6, OUTPUT);  
  pinMode(Relay_7, OUTPUT);  
//  pinMode(Relay_8, OUTPUT);    
  delay(4000); //Check that all relays are inactive at Reset
  //Ethernet.begin(mac);
  Ethernet.begin(mac, ip, gateway, subnet); //for manual setup

  server.begin();
  Serial.println(Ethernet.localIP());

}

void loop(){

  // listen for incoming clients, and process qequest.
  checkForClient();

}

void checkForClient(){

  EthernetClient client = server.available();

  if (client) {
    
    Serial.println ("/n/n client found/n");

    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    boolean sentHeader = false;

    while (client.connected()) {
      if (client.available()) {

        if(!sentHeader){
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          sentHeader = true;
        }

        char c = client.read();
        if(reading && c == ' ') reading = false;
        if(c == '?') reading = true; //found the ?, begin reading the info

        if(reading){
          Serial.print(c);
          Serial.println ("/n/n");

           switch (c) {
            case '2':
              //add code here to trigger on 2
              triggerPin(2, client);
              Serial.println ("recieved 2");
              break;
            case '3':
            //add code here to trigger on 3
              triggerPin(3, client);
              Serial.println ("recieved 3");
              break;
            case '4':
            //add code here to trigger on 4
              triggerPin(4, client);
              Serial.println ("recieved 4");
              break;
            case '5':
            //add code here to trigger on 5
              triggerPin(5, client);
              Serial.println ("recieved 5");
              break;
            case '6':
            //add code here to trigger on 6
              triggerPin(6, client);
              Serial.println ("recieved 6");
              break;
            case '7':
            //add code here to trigger on 7
              triggerPin(7, client);
              Serial.println ("recieved 7");
              break;
            case '8':
            //add code here to trigger on 8
              triggerPin(8, client);
              Serial.println ("recieved 8");
              break;
            case '9':
            //add code here to trigger on 9
              triggerPin(9, client);
              Serial.println ("recieved 9");
              break;
          }

        }

        if (c == '\n' && currentLineIsBlank)  break;

        if (c == '\n') {
          currentLineIsBlank = true;
        }else if (c != '\r') {
          currentLineIsBlank = false;
        }

      }
    }

    delay(1); // give the web browser time to receive the data
    client.stop(); // close the connection:

  } 

}

void triggerPin(int pin, EthernetClient client){
  Serial.println ("triggier pin : ");
  Serial.println (pin);
  Serial.println ("/n");
  
   if (pin == 2) {
  if (status2 == 0) {
   triggerRelay(pin, 1);
   status2 = 1;
   client.print("Turning on pin ");
   client.println(pin);
  }
  else if (status2 == 1) {
   triggerRelay(pin, 0);
   status2 = 0;
   client.print("Turning off pin ");
   client.println(pin);
  }
 }

 if (pin == 3) {
  if (status3 == 0) {
   triggerRelay(pin, 1);
   status3 = 1;
   client.print("Turning on pin ");
   client.println(pin);
  }
  else if (status3 == 1) {
   triggerRelay(pin, 0);
   status3 = 0;
   client.print("Turning off pin ");
   client.println(pin);
  }
 }

 if (pin == 4) {
  if (status4 == 0) {
   triggerRelay(pin, 1);
   status4 = 1;
   client.print("Turning on pin ");
   client.println(pin);
  }
  else if (status4 == 1) {
   triggerRelay(pin, 0);
   status4 = 0;
   client.print("Turning off pin ");
   client.println(pin);
  }
 }

 if (pin == 5) {
  if (status5 == 0) {
   triggerRelay(pin, 1);
   status5 = 1;
   client.print("Turning on pin ");
   client.println(pin);
  }
  else if (status5 == 1) {
   triggerRelay(pin, 0);
   status5 = 0;
   client.print("Turning off pin ");
   client.println(pin);
  }
 }

 if (pin == 6) {
  if (status6 == 0) {
   triggerRelay(pin, 1);
   status6 = 1;
   client.print("Turning on pin ");
   client.println(pin);
  }
  else if (status6 == 1) {
   triggerRelay(pin, 0);
   status6 = 0;
   client.print("Turning off pin ");
   client.println(pin);
  }
 }

 if (pin == 7) {
  if (status7 == 0) {
   triggerRelay(pin, 1);
   status7 = 1;
   client.print("Turning on pin ");
   client.println(pin);
  }
  else if (status7 == 1) {
   triggerRelay(pin, 0);
   status7 = 0;
   client.print("Turning off pin ");
   client.println(pin);
  }
 }

 if (pin == 8) {
  if (status8 == 0) {
   triggerRelay(pin, 1);
   status8 = 1;
   client.print("Turning on pin ");
   client.println(pin);
  }
  else if (status8 == 1) {
   triggerRelay(pin, 0);
   status8 = 0;
   client.print("Turning off pin ");
   client.println(pin);
  }
 }

 if (pin == 9) {
  if (status9 == 0) {
   triggerRelay(pin, 1);
   status9 = 1;
   client.print("Turning on pin ");
   client.println(pin);
  }
  else if (status9 == 1) {
   triggerRelay(pin, 0);
   status9 = 0;
   client.print("Turning off pin ");
   client.println(pin);
  }
 }

}

void triggerRelay(int pin, int stat){
  if (stat == 1) {
  digitalWrite(pin,  RELAY_ON);
  Serial.println ("  status 1  ");
  }
  else {
    digitalWrite(pin,  RELAY_OFF);
    
  Serial.println ("  status 0  ");
  }
  
}