webserver relay switch

Dears

below codes using EtherCard form control relay in pins 6,7,8,9
and its work fine in web
leds ON when i clicking on Relais 1 then become OFF when i second clicking on Relais 1
but i need to modify codes to switch ON LED after clicking the become of automatic after 2 sec with out second clicking

/*
 * arduWebRelays.ino
 *
 * Created: 07/09/2013 15:55:00
 *  Author: Guillaume Carriere - guillaume.carriere@gmail.com
 *
 */ 
#include <EtherCard.h>

#define STATIC 1  // set to 1 to disable DHCP (adjust myip/gwip values below)

#if STATIC
// ethernet interface ip address
static byte myip[] = { 192,168,1,200 };
// gateway ip address
static byte gwip[] = { 192,168,1,1 };
#endif

// ethernet mac address - must be unique on your network
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

#define BUFFER_SIZE   500
byte Ethernet::buffer[BUFFER_SIZE];
BufferFiller bfill;

#define CS_PIN       10

#define RELAIS_1     8
#define RELAIS_2     9
#define RELAIS_3     7
#define RELAIS_4     6

bool relais1Status = false;
bool relais2Status = false;
bool relais3Status = false;
bool relais4Status = false;


const char http_OK[] PROGMEM =
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/html\r\n"
"Pragma: no-cache\r\n\r\n";

const char http_Found[] PROGMEM =
"HTTP/1.0 302 Found\r\n"
"Location: /\r\n\r\n";

const char http_Unauthorized[] PROGMEM =
"HTTP/1.0 401 Unauthorized\r\n"
"Content-Type: text/html\r\n\r\n"
"<h1>401 Unauthorized</h1>";

void homePage()
{
  bfill.emit_p(PSTR("$F"
    "<title>Arduino Relais Webserver</title>" 
    "Relais 1: <a href=\"?relais1=$F\">$F</a>
"
    "Relais 2: <a href=\"?relais2=$F\">$F</a>
"
    "Relais 3: <a href=\"?relais3=$F\">$F</a>
"
    "Relais 4: <a href=\"?relais4=$F\">$F</a>"),
    
  http_OK,
  relais1Status?PSTR("off"):PSTR("on"),
  relais1Status?PSTR("<font color=\"green\"><b>ON</b></font>"):PSTR("<font color=\"red\">OFF</font>"),
  relais2Status?PSTR("off"):PSTR("on"),
  relais2Status?PSTR("<font color=\"green\"><b>ON</b></font>"):PSTR("<font color=\"red\">OFF</font>"),
  relais3Status?PSTR("off"):PSTR("on"),
  relais3Status?PSTR("<font color=\"green\"><b>ON</b></font>"):PSTR("<font color=\"red\">OFF</font>"),
  relais4Status?PSTR("off"):PSTR("on"),
  relais4Status?PSTR("<font color=\"green\"><b>ON</b></font>"):PSTR("<font color=\"red\">OFF</font>"));
}

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

  pinMode(RELAIS_1, OUTPUT);
  pinMode(RELAIS_2, OUTPUT);
  pinMode(RELAIS_3, OUTPUT);
  pinMode(RELAIS_4, OUTPUT);

  if (ether.begin(BUFFER_SIZE, mymac, CS_PIN) == 0)
    Serial.println("Cannot initialise ethernet.");
  else
    Serial.println("Ethernet initialised.");

  ether.staticSetup(myip);
    
  Serial.println("Setting up DHCP");
  
  
  ether.printIp("My IP: ", ether.myip);
  ether.printIp("GW IP: ", ether.gwip);
  ether.printIp("DNS IP: ", ether.dnsip);

}

void loop()
{
  digitalWrite(RELAIS_1, relais1Status); 
  digitalWrite(RELAIS_2, relais2Status); 
  digitalWrite(RELAIS_3, relais3Status);
  digitalWrite(RELAIS_4, relais4Status); 


  delay(1);   // necessary for my system
  word len = ether.packetReceive();   // check for ethernet packet
  word pos = ether.packetLoop(len);   // check for tcp packet

  if (pos) {
    bfill = ether.tcpOffset();
    char *data = (char *) Ethernet::buffer + pos;
    if (strncmp("GET /", data, 5) != 0) {
      // Unsupported HTTP request
      // 304 or 501 response would be more appropriate
      bfill.emit_p(http_Unauthorized);
    }
    else {
      Serial.print("----");
      Serial.print(data);
      Serial.println("----");
      data += 5;

      if (data[0] == ' ') {
        // Return home page
        homePage();
      }
      else if (strncmp("?relais1=on ", data, 12) == 0) {
        relais1Status = true;        
        bfill.emit_p(http_Found);
      }
      else if (strncmp("?relais2=on ", data, 12) == 0) {
        relais2Status = true;        
        bfill.emit_p(http_Found);
      }
      else if (strncmp("?relais3=on ", data, 12) == 0) {
        relais3Status = true;        
        bfill.emit_p(http_Found);
      }
      else if (strncmp("?relais4=on ", data, 12) == 0) {
        relais4Status = true;        
        bfill.emit_p(http_Found);
      }
      else if (strncmp("?relais1=off ", data, 13) == 0) {
        relais1Status = false;        
        bfill.emit_p(http_Found);
      }
      else if (strncmp("?relais2=off ", data, 13) == 0) {
        relais2Status = false;        
        bfill.emit_p(http_Found);
      }
      else if (strncmp("?relais3=off ", data, 13) == 0) {
        relais3Status = false;        
        bfill.emit_p(http_Found);
      }
      else if (strncmp("?relais4=off ", data, 13) == 0) {
        relais4Status = false;        
        bfill.emit_p(http_Found);
      }
      
      else {
        // Page not found
        bfill.emit_p(http_Unauthorized);
      }
    }

    ether.httpServerReply(bfill.position());    // send http response
  }
}

Regards

but i need to modify codes to switch ON LED after clicking the become of automatic after 2 sec with out second clicking

So, what is the problem?

After checking for the client connection, and dealing with what the client wanted, add a loop to check each pin. If it is on, and enough time has passed, turn it off.

Set up an array to hold the time at which each relay was switched on, then in your loop check each element with millis() to decide which relay to turn off.

i made modification like below in Relay4 but its add delay to all other switch

void loop()
{
  digitalWrite(RELAIS_1, relais1Status); 
  digitalWrite(RELAIS_2, relais2Status); 
  digitalWrite(RELAIS_3, relais3Status);
  digitalWrite(RELAIS_4, relais4Status);
  digitalWrite(RELAIS_4, relais4Status);
  delay(1000);
  relais4Status = false;
  digitalWrite(RELAIS_4, relais4Status);

can you please write i modification statements

i made modification like below in Relay4

Where did anyone suggest using delay()? Nowhere. Clearly that is the wrong solution.

Think about how YOU would turn the relays off, if they were supposed to be on for 2 hours, +/- 10 seconds.

You have to record when you turned them on. You don't have any place in the code yet to do that.

Periodically, you need to see if a relay is on (that will be true if the on time is not zero). If so, then you need to determine if it is time to turn the relay off. Clearly, you need to know what time it is in order to make that decision. The millis() function is how the Arduino "checks it's watch".

If the relay is on, and it is time to turn it off, turn it off and set the on time back to zero.

You need to re-read what I said.

You need a time stored for each relay, so if you have 5 relays, you need to store 5 times. Those times need filling in with millis() when they get switched.

You then test each of those times against millis() in your loop to see how much time has passed.

you are totally right but i can't do it with my low experience can you mange that

can you mange that

Someone drag a cat in?

Look at the blink without delay example. It shows how to record when an event occurs. It shows how to determine if the interval between then and now exceeds some threshold. It shows how to take action when that happens.

That is everything you need to do. The only real difference will be where the "turn the relay on" events happen and where the "has the relay been on long enough" test happens.

thanks i made it this modify will work in relay 1 with delay(1000)

else if (strncmp("?relais1=on ", data, 12) == 0) {
        relais1Status = true;        
        digitalWrite(RELAIS_1, relais1Status);
        delay(1000);
        relais1Status = false;        
        bfill.emit_p(http_Found);

thanks for all

Now press a few buttons on your website and see if all the relays turn on as they should. Do they?

tammytam: Now press a few buttons on your website and see if all the relays turn on as they should. Do they?

And, how long does it take for a relay to turn on, after you press the icon on the web page.

You have been told more than once that delay() is NOT the answer. Someday, you'll accept that.

my project is to make relay work for (2-3) sec with above code i found a little confuse when i increase the delay to 5 sec relay take to order each one with 5 sec delay

i couldn't solve it

is there any error code make this duplicate order

i couldn't solve it

Because you are going the wrong way. Read ALL the responses again.