ethernet web relay with reset/reboot option

Hi,

I’m trying to modify ProtectedLed sketch that i found.
I want to implement reboot option with relay. Need to get replay to turn on, then off one time with delay of a second. My current efforts to do that has not gone so well. Problem is that the on and off repeat is selfe 3 times.

I’m not so good with coding and I would be grateful for any help.

#include <EtherCard.h>

static byte mymac[] = { 0x00,0x01,0x01,0x01,0x01,0x01  };
static byte myip[] = { 192,168,1,13 };           // The IP board address.
static byte gwip[] = { 192,168,1,1 };            // The gateway router address.
static byte netmask[] = { 255, 255, 255, 0 };    // The subnet mask.
static byte dnsip[] = { 192, 168, 1, 1 };        // The DNS server address.

byte Ethernet::buffer[700];

const int led_pin = 8;
char* led_password = "david";
boolean led_status;

boolean reboot_status;

void setup () {
 
  Serial.begin(57600);
  Serial.println("Protected LED demo");
 
  if (!ether.begin(sizeof Ethernet::buffer, mymac, 10))
    Serial.println( "Failed to access Ethernet controller");
  else
    Serial.println("Ethernet controller initialized");
 
  Serial.println( "Getting static IP.");
  if (!ether.staticSetup(myip, gwip, netmask, dnsip)){
    Serial.println( "could not get a static IP");
  }

  ether.printIp("IP Address:\t", ether.myip);
  ether.printIp("Netmask:\t", ether.netmask);
  ether.printIp("Gateway:\t", ether.gwip);
  
  pinMode(led_pin, OUTPUT);
  digitalWrite(led_pin, HIGH);
  led_status = true;
}
  
void loop() {
 
  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);
  
  if(pos) {
    
    Serial.println();
    boolean password_valid = true; 
    
    // is it a POST request?
    if(strstr((char *)Ethernet::buffer + pos, "POST /") != 0) {
    
      Serial.println("New POST request!");
      
      // search and verify the password
      char password[20];      
      char* password_position = strstr((char *)Ethernet::buffer + pos, "&pwd=");
      if(password_position != 0) {
        strcpy(password, password_position + 5);
        Serial.print("Found password=");
        Serial.println(password);
        if(strcmp(password, led_password) == 0) Serial.println("Valid password :)");                  
        else {
          Serial.println("Wrong password :(");
          password_valid = false;
        }
      }
      
      // search for ON= or OFF= command
      if(password_valid) {
        
        // OFF command
        if(strstr((char *)Ethernet::buffer + pos, "OFF=") != 0) {
          Serial.println("Performing OFF command");
          digitalWrite(led_pin, LOW);
          led_status = false;


        
        // ON command
        } else if(strstr((char *)Ethernet::buffer + pos, "ON=") != 0) {
          Serial.println("Performing ON command");
          digitalWrite(led_pin, HIGH);
          led_status = true;          


        // REBOOT command
        } else if(strstr((char *)Ethernet::buffer + pos, "REBOOT=") != 0) {
          Serial.println("Performing REBOOT command");

          digitalWrite(led_pin, LOW);
          delay(1000);  // waits for a second
          reboot_status = true;
         digitalWrite(led_pin, HIGH);


        } else Serial.println("Unknown command :(");
      }
    }      
    
    // Output HTML page        
    BufferFiller bfill = ether.tcpOffset();
    bfill.emit_p(PSTR("HTTP/1.0 200 OK\r\n"
      "Content-Type: text/html\r\nPragma: no-cache\r\n\r\n"
      "<html><head><title>Protected LED</title></head>"
     
      "
<p><u><b><font size=7>NADZOR NAD RELEJI </font></b></u></p>
" 
    
      "<html><body><form method=\"POST\">"));

    
    // Enable / disable buttons based on the output status


  //if(led_status == true) bfill.emit_p(PSTR("<div><button name=\"ON\" disabled>ON</button><button name=\"OFF\">OFF</button></div>"));
    
    if(led_status == true) bfill.emit_p(PSTR("<div><button name=\"ON\" disabled style=\"width: 120; height: 30\">ON</button></button><button name=\"REBOOT\" style=\"width: 120; height: 30\">REBOOT</button><button name=\"OFF\" style=\"width: 120; height: 30\">OFF</button></div>"));


    else if(reboot_status == true) bfill.emit_p(PSTR("<div><button name=\"ON\" disabled style=\"width: 120; height: 30\">ON</button><button name=\"OFF\" style=\"width: 120; height: 30\">OFF</button></button><button name=\"REBOOT\" style=\"width: 120; height: 30\">REBOOT</button></div>"));

   
//    else bfill.emit_p(PSTR("<div><button name=\"ON\">ON</button><button name=\"OFF\" disabled>OFF</button></div>"));  

    else bfill.emit_p(PSTR("<div><button name=\"ON\" disabled style=\"width: 120; height: 30\">ON</button><button name=\"OFF\" style=\"width: 120; height: 30\">OFF</button></button><button name=\"REBOOT\" style=\"width: 120; height: 30\">REBOOT</button></div>"));
    


    
   ( "

");

    // A wrong password was entered?
//if(password_valid == true) bfill.emit_p(PSTR("

<div><input type=\"password\" name=\"pwd\"></div></form></body></html>"));

    if(password_valid == true) bfill.emit_p(PSTR("

<div><input name=\"pwd\" value=\"Pred spremembo vnesi geslo\" size=\"50\"></div></form></body></html>"));

    else bfill.emit_p(PSTR("

<div><input type=\"password\" name=\"pwd\">Wrong password</div></form></body></html>"));
      
    ether.httpServerReply(bfill.position());
  }
 
 }

This part of the code

        // REBOOT command
        } else if(strstr((char *)Ethernet::buffer + pos, "REBOOT=") != 0) {
          Serial.println("Performing REBOOT command");

          digitalWrite(led_pin, LOW);
          delay(1000);  // waits for a second
          reboot_status = true;
         digitalWrite(led_pin, HIGH);

does not work as I would like. Relay turns on and off 3x, Instead once.

I have tried to implement delay with millis() but I couldn’t make it work.

does not work as I would like. Relay turns on and off 3x, Instead once.

How many times is that snippet of code executed before the buffer changes?

From web interface should be triggered one time per request. On and Off command works as it should. Reboot command is triggered 3 times.

Also in serial monitor display there is a record that the post was requested 3 times

We can’t help you with snippets. Post ALL of your code AND your serial output.

I have posted the complete code, it is in the first part.

And this is the serial output when pressing reboot command.

Protected LED demo
Ethernet controller initialized
Getting static IP.
IP Address:	192.168.0.13
Netmask:	192.168.0.1
Gateway:	192.168.0.1



New POST request!
Found password=david
Valid password :)
Performing REBOOT command

New POST request!
Found password=david
Valid password :)
Performing REBOOT command

New POST request!
Found password=david
Valid password :)
Performing REBOOT command

I have posted the complete code, it is in the first part.

It was not there when you first posted. So, you make me look like an idiot for telling you that you need to post all of your code. For that, I move on.

#include <EtherCard.h>

static byte mymac[] = { 0x00,0x01,0x01,0x01,0x01,0x01  };
static byte myip[] = { 192,168,1,13 };           // The IP board address.
static byte gwip[] = { 192,168,1,1 };            // The gateway router address.
static byte netmask[] = { 255, 255, 255, 0 };    // The subnet mask.
static byte dnsip[] = { 192, 168, 1, 1 };        // The DNS server address.

byte Ethernet::buffer[700];

const int led_pin = 8;
char* led_password = "david";
boolean led_status;

boolean reboot_status;

void setup () {
 
  Serial.begin(57600);
  Serial.println("Protected LED demo");
 
  if (!ether.begin(sizeof Ethernet::buffer, mymac, 10))
    Serial.println( "Failed to access Ethernet controller");
  else
    Serial.println("Ethernet controller initialized");
 
  Serial.println( "Getting static IP.");
  if (!ether.staticSetup(myip, gwip, netmask, dnsip)){
    Serial.println( "could not get a static IP");
  }

  ether.printIp("IP Address:\t", ether.myip);
  ether.printIp("Netmask:\t", ether.netmask);
  ether.printIp("Gateway:\t", ether.gwip);
  
  pinMode(led_pin, OUTPUT);
  digitalWrite(led_pin, HIGH);
  led_status = true;
}
  
void loop() {
 
  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);
  
  if(pos) {
    
    Serial.println();
    boolean password_valid = true; 
    
    // is it a POST request?
    if(strstr((char *)Ethernet::buffer + pos, "POST /") != 0) {
    
      Serial.println("New POST request!");
      
      // search and verify the password
      char password[20];      
      char* password_position = strstr((char *)Ethernet::buffer + pos, "&pwd=");
      if(password_position != 0) {
        strcpy(password, password_position + 5);
        Serial.print("Found password=");
        Serial.println(password);
        if(strcmp(password, led_password) == 0) Serial.println("Valid password :)");                  
        else {
          Serial.println("Wrong password :(");
          password_valid = false;
        }
      }
      
      // search for ON= or OFF= command
      if(password_valid) {
        
        // OFF command
        if(strstr((char *)Ethernet::buffer + pos, "OFF=") != 0) {
          Serial.println("Performing OFF command");
          digitalWrite(led_pin, LOW);
          led_status = false;


        
        // ON command
        } else if(strstr((char *)Ethernet::buffer + pos, "ON=") != 0) {
          Serial.println("Performing ON command");
          digitalWrite(led_pin, HIGH);
          led_status = true;          


        // REBOOT command
        } else if(strstr((char *)Ethernet::buffer + pos, "REBOOT=") != 0) {
          Serial.println("Performing REBOOT command");

          digitalWrite(led_pin, LOW);
          delay(1000);  // waits for a second
          reboot_status = true;
         digitalWrite(led_pin, HIGH);


        } else Serial.println("Unknown command :(");
      }
    }      
    
    // Output HTML page        
    BufferFiller bfill = ether.tcpOffset();
    bfill.emit_p(PSTR("HTTP/1.0 200 OK\r\n"
      "Content-Type: text/html\r\nPragma: no-cache\r\n\r\n"
      "<html><head><title>Protected LED</title></head>"
     
      "
<p><u><b><font size=7>NADZOR NAD RELEJI </font></b></u></p>
" 
    
      "<html><body><form method=\"POST\">"));

    
    // Enable / disable buttons based on the output status


  //if(led_status == true) bfill.emit_p(PSTR("<div><button name=\"ON\" disabled>ON</button><button name=\"OFF\">OFF</button></div>"));
    
    if(led_status == true) bfill.emit_p(PSTR("<div><button name=\"ON\" disabled style=\"width: 120; height: 30\">ON</button></button><button name=\"REBOOT\" style=\"width: 120; height: 30\">REBOOT</button><button name=\"OFF\" style=\"width: 120; height: 30\">OFF</button></div>"));


    else if(reboot_status == true) bfill.emit_p(PSTR("<div><button name=\"ON\" disabled style=\"width: 120; height: 30\">ON</button><button name=\"OFF\" style=\"width: 120; height: 30\">OFF</button></button><button name=\"REBOOT\" style=\"width: 120; height: 30\">REBOOT</button></div>"));

   
//    else bfill.emit_p(PSTR("<div><button name=\"ON\">ON</button><button name=\"OFF\" disabled>OFF</button></div>"));  

    else bfill.emit_p(PSTR("<div><button name=\"ON\" disabled style=\"width: 120; height: 30\">ON</button><button name=\"OFF\" style=\"width: 120; height: 30\">OFF</button></button><button name=\"REBOOT\" style=\"width: 120; height: 30\">REBOOT</button></div>"));
    


    
   ( "

");

    // A wrong password was entered?
//if(password_valid == true) bfill.emit_p(PSTR("

<div><input type=\"password\" name=\"pwd\"></div></form></body></html>"));

    if(password_valid == true) bfill.emit_p(PSTR("

<div><input name=\"pwd\" value=\"Pred spremembo vnesi geslo\" size=\"50\"></div></form></body></html>"));

    else bfill.emit_p(PSTR("

<div><input type=\"password\" name=\"pwd\">Wrong password</div></form></body></html>"));
      
    ether.httpServerReply(bfill.position());
  }
 
 }

This is complet code and it is allready posted in the first part of original post.
As you see first message was not edited later.