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!