Hello,
I am working on a project to control a tower light via Ethernet shield working as a server.
I do not understand why the LEDs light up before I send the command, I want them to be off before press ON and after I press OFF on my browser.
Any help would be greatly appreciated.
Here is the code that I have so far:
/*
Arduino with Ethernet Shield
*/
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 0, 178 }; // ip in lan (that's what you need to use in your browser. ("192.168.0.178")
byte gateway[] = { 192, 168, 0, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
String readString;
int led;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
Ethernet.begin(mac);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}
//if HTTP request has ended
if (c == '\n') {
Serial.println(readString); //print to serial monitor for debuging
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("");
client.println("");
client.println("Control Colonne Lumineuse");
client.println("");
client.println("");
client.println("
Control Colonne Lumineuse
");client.println("
");
client.println("<a href="/?1ON"">LED1ON");
client.println("<a href="/?1OFF"">LED1OFF");
client.println("<a href="/?2ON"">LED2ON");
client.println("<a href="/?2OFF"">LED2OFF");
client.println("<a href="/?3ON"">LED3ON");
client.println("<a href="/?3OFF"">LED3OFF");
client.println("
");
client.println("");
client.println("");
delay(1);
//stopping client
client.stop();
//controls the Arduino if you press the buttons
if (readString.indexOf("?1ON") > 0) {
led = 3;
digitalWrite(led, HIGH);
return;
}
if (readString.indexOf("?1OFF") > 0) {
led = 3;
digitalWrite(led, LOW);
return;
}
if (readString.indexOf("?2ON") > 0) {
led = 4;
digitalWrite(led, HIGH);
return;
}
if (readString.indexOf("?2OFF") > 0) {
led = 4;
digitalWrite(led, LOW);
return;
}
if (readString.indexOf("?3ON") > 0) {
led = 5;
digitalWrite(led, HIGH);
return;
}
if (readString.indexOf("?3OFF") > 0) {
led = 5;
digitalWrite(led, LOW);
return;
}
readString = "";
}
}
}
}
}
TowerLightcontrol.ino (3.34 KB)