I'm working on a project to turn off and on Relay on Web Server using Arduino UnoR3 and W5100 Ethernet Shield.
When I connect to http://172.30.x.x and press the button, the link changes to http://172.30.x.x/?7conv; and refresh the web page, there is a problem that the relay switches without pressing the button.
Is there a way to prevent this?
#include <SPI.h>
#include <Ethernet.h>
int relayPro = 7;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 172, 30, x, x };
byte gateway[] = { 172, 30, x, x };
byte subnet[] = { 255, 255, x, x };
EthernetServer server(80);
String readString;
void setup() {
pinMode(relayPro, OUTPUT);
digitalWrite(relayPro, HIGH);
Ethernet.begin(mac, ip, gateway, gateway, subnet);
server.begin();
Serial.begin(9600);
Serial.println("REMOTE CONTROL SYSTEM");
}
void loop() {
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (readString.length() < 100) {
readString += c;
Serial.print(c);
}
if (c == '\n') {
Serial.println(readString);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<META CHARSET=\"UTF-8\">");
client.println("<TITLE>REMOTE CH CONTROL</TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<H1>REMOTE CH CONTROL</H1>");
client.println("<input type=submit value='Switch' style=width:100px;height:45px onClick=location.href='/?7conv;'>");
client.println("</BODY>");
client.println("</HTML>");
delay(1);
client.stop();
if (readString.indexOf('7') > 0) {
digitalWrite(relayPro, LOW);
Serial.println("Relay ON");
delay(1500);
digitalWrite(relayPro, HIGH);
Serial.println("Relay OFF");
}
readString = "";
}
}
}
}
}