This is the working code :
Code: Select all
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { your Arduino MAC address };
IPAddress ip( your Arduino IP address);
EthernetServer server(80);
String HTTP_req;
boolean Status5 = LOW;
boolean Status6 = LOW;
boolean Status7 = LOW;
boolean Status8 = LOW;
boolean Status9 = LOW;
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac, ip);
server.begin();
Serial.print("Server is at ");
Serial.println(Ethernet.localIP());
pinMode( 5, OUTPUT);
pinMode( 6, OUTPUT);
pinMode( 7, OUTPUT);
pinMode( 8, OUTPUT);
pinMode( 9, OUTPUT);
}
void loop()
{
EthernetClient client = server.available();
if (client) {
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
HTTP_req += c;
if (c == '\n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE html>");
client.println("<html>");
client.println("<head>");
client.println("<title>LED Controller</title>");
client.println("</head>");
client.println("<body>");
client.println("<h1>Arduino Ethernet LED Controller</h1>");
client.println("<p>Click to switch LEDS on and off.</p>");
client.println("<form method=\"get\">");
ProcessCheckbox(client);
client.println("</form>");
client.println("</body>");
client.println("</html>");
Serial.print(HTTP_req);
HTTP_req = "";
break;
}
if (c == '\n') {
currentLineIsBlank = true;
}
else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(2);
client.stop();
}
}
void ProcessCheckbox(EthernetClient cl)
{
Status5 = LOW;
cl.print("<input type=\"checkbox\" name=\"LED5\" value=\"2\" onclick=\"submit();\"");
if(HTTP_req.indexOf("LED5=2") > -1) {
Status5 = HIGH;
if(HTTP_req.indexOf("Referer:") > -1) {
if(HTTP_req.indexOf("LED5=2") > HTTP_req.indexOf("Referer:")) {
Status5 = LOW;
}
}
}
if(Status5) {
cl.print(" checked");
}
digitalWrite(5, Status5);
cl.println(">LED 5<br>");
Status6 = LOW;
cl.print("<input type=\"checkbox\" name=\"LED6\" value=\"2\" onclick=\"submit();\"");
if(HTTP_req.indexOf("LED6=2") > -1) {
Status6 = HIGH;
if(HTTP_req.indexOf("Referer:") > -1) {
if(HTTP_req.indexOf("LED6=2") > HTTP_req.indexOf("Referer:")) {
Status6 = LOW;
}
}
}
if(Status6) {
cl.print(" checked");
}
digitalWrite(6, Status6);
cl.println(">LED 6<br>");
Status7 = LOW;
cl.print("<input type=\"checkbox\" name=\"LED7\" value=\"2\" onclick=\"submit();\"");
if(HTTP_req.indexOf("LED7=2") > -1) {
Status7 = HIGH;
if(HTTP_req.indexOf("Referer:") > -1) {
if(HTTP_req.indexOf("LED7=2") > HTTP_req.indexOf("Referer:")) {
Status7 = LOW;
}
}
}
if(Status7) {
cl.print(" checked");
}
digitalWrite(7, Status7);
cl.println(">LED 7<br>");
Status8 = LOW;
cl.print("<input type=\"checkbox\" name=\"LED8\" value=\"2\" onclick=\"submit();\"");
if(HTTP_req.indexOf("LED8=2") > -1) {
Status8 = HIGH;
if(HTTP_req.indexOf("Referer:") > -1) {
if(HTTP_req.indexOf("LED8=2") > HTTP_req.indexOf("Referer:")) {
Status8 = LOW;
}
}
}
if(Status8) {
cl.print(" checked");
}
digitalWrite(8, Status8);
cl.println(">LED 8<br>");
Status9 = LOW;
cl.print("<input type=\"checkbox\" name=\"LED9\" value=\"2\" onclick=\"submit();\"");
if(HTTP_req.indexOf("LED9=2") > -1) {
Status9 = HIGH;
if(HTTP_req.indexOf("Referer:") > -1) {
if(HTTP_req.indexOf("LED9=2") > HTTP_req.indexOf("Referer:")) {
Status9 = LOW;
}
}
}
if(Status9) {
cl.print(" checked");
}
digitalWrite(9, Status9);
cl.println(">LED 9<br>");
}
I have determine that :
- 0 - Serial RX
1 - Serial TX
2 - ...
3 - ...
4 - SD Card
10 - Ethernet module
11 - Ethernet module
12 - Ethernet module
13 - Ethernet module
If someone can control more outputs on the Arduino Ethernet, can you share this knowledge too?