I need help por my pc remote switch

Hi guys, I'm making a circuit for turning on my pc remotely with an arduino nano and an Enc28j60 Ethernet Shield. I got it to work, but there is a problem I would like to fix.
(I'm a begginer in this thing, so please excuse me if I explain something incorrectly).

So basically I based my code on the ethernet webServer example, and then I found some code on the internet that allows me to have buttons in the HTML page. I think that the button simply is an URL, and then the code checks the URL to see if a button has been pressed.

My problem is that when I press a button, the url stays as 192.168.0.50/?button#, which means that if I refresh the page, the action of pushing the button gets repeated and it turns off my pc.

Is there a workaround to this issue, or is there a better way of implementing the button?

Code:

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


byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 0, 69);
EthernetServer server(80);
String readString;


void setup() {
  
  //Ethernet.init(10);  
 
  Serial.begin(9600);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,INPUT);


 
  Serial.println("Wort world");

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true) {
      delay(1); 
    }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }

 
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {


  
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
   
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        
         if (readString.length() < 100) {
          //Almacena los caracteres a un String
          readString += c;
          
         }
       
        if (c == '\n' && currentLineIsBlank) {
          Serial.println(readString);
         
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.println("<body>");

          client.println("<p>Wort world 1.0</p>");

          client.println("<p>Server status: </p>");

            if(digitalRead(6)==HIGH){
              client.println("ON");
            }
            else if(digitalRead(6)==LOW){
              client.println("OFF");
            }
           
   
          client.println("<p><a href=\"/?button1\"\">POWER</a></p>");
       
          client.println("<p><a href=\"/?button2\"\">RESET</a></p>");

          client.println("<p><a href=\"/?button3\"\">HARD POWER OFF</a></p>");

          client.println("<p>RECUERDA NO ACTUALIZAR LA PAGINA DESPUES DE PRESIONAR UN BOTON</p>");
          client.println("<p>REINGRESA LA IP MANUALMENTE ANTES DE ACTUALIZAR, O APAGARAS EL SERVIDOR</p>");

           if (readString.indexOf("?button1") >0){
               digitalWrite(4, HIGH);
               delay(1000);
               digitalWrite(4, LOW);
                
          }
          if (readString.indexOf("?button2") >0){
               digitalWrite(5, HIGH);
               delay(1000);
               digitalWrite(5, LOW);
          }
          if (readString.indexOf("?button3") >0){
               digitalWrite(4, HIGH);
               delay(10000);
               digitalWrite(4, LOW);
          }
       
          client.println("</body>");
          client.println("</html>");

          delay(1);
          client.stop();
          
         
          

          



          readString="";
        }
        
      }
    }
    
   
    
    
}
}

Pin 4 is used for the power pin
Pin 5 is used for the reset pin
Pin 6 reads the status of the pc power led to know if the pc is on or off

Please excuse me if there is something wrong about the code, I'm still learning so I basically copied many things from different places and adapted them for my code.

Any feedback would be greatly appreciated!

Please Auto format your code in the IDE, use Copy for forum in the IDE and paste it here so that it is code tags to make it easier to read and copy for examination

Thanks, i was trying to figure out how to paste in correctly

Thanks

Did it take 2 attempts before pasting the code worked ?
If so, do you know what went wrong the first time ?

The first time I just copied the code straight from the IDE and pasted it, but for some reason it wasn't putting all of it inside the code box

The second time I clicked on the "copy for forum" option and then it worked

Thanks for the details. I noticed in your first attempt that only part of the code was shown in code blocks, but I don't know why

Explicitly using Copy for forum seems to work every time because the IDE adds the code block tags to the copied code

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.