Ethernet Controlled LED
A starter project for those with Ethernet shields.
Components and supplies
1
Jumper wires (generic)
1
Arduino UNO
1
Breadboard (generic)
1
LED (generic)
1
Arduino Ethernet Rev. 3
1
Resistor 221 ohm
Apps and platforms
1
Arduino IDE
Project description
Code
The code
arduino
1#include <SPI.h> 2#include <Ethernet.h> 3 4// MAC address for shield 5byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; 6EthernetServer server(80); // Using port 80 7int led = 7; // LED attached to pin 7 8void setup() { 9 pinMode(led, OUTPUT); // Led set as an output 10 Ethernet.begin(mac); // Start the Ethernet shield 11 server.begin(); 12 Serial.begin(9600); // Start serial communication 13 Serial.println("Server address:"); // Print server address 14 // (Arduino shield) 15 Serial.println(Ethernet.localIP()); 16} 17void loop() { 18 EthernetClient client = server.available(); 19 if (client) { 20 boolean currentLineIsBlank = true; 21 String buffer = ""; 22 while (client.connected()) { 23 if (client.available()) { 24 char c = client.read(); // Read from the Ethernet shield 25 buffer += c; // Add character to string buffer 26 // Client sent request, now waiting for response 27 if (c == '\n' && currentLineIsBlank) { 28 client.println("HTTP/1.1 200 OK"); // HTTP response 29 client.println("Content-Type: text/html"); 30 client.println(); // HTML code 31 client.print("<center><br><h1>Internet Controlled Led </h1><br><br><br><FORM>"); 32 client.print("<P> <INPUT type=\\"submit\\" name=\\"status\\" value=\\"ON\\">"); 33 client.print("<P> <INPUT type=\\"submit\\" name=\\"status\\" value=\\"OFF\\">"); 34 client.print("</FORM></center>"); 35 break; 36 } 37 if (c == '\n') { 38 currentLineIsBlank = true; 39 buffer = ""; 40 } 41 else if (c == '\r') { // Command from webpage 42 // Did the on button get pressed 43 if (buffer.indexOf("GET /?status=ON") >= 0) 44 digitalWrite(led, HIGH); 45 // Did the off button get pressed 46 if (buffer.indexOf("GET /?status=OFF") >= 0) 47 digitalWrite(led, LOW); 48 } 49 else { 50 currentLineIsBlank = false; 51 } 52 } 53 } 54 client.stop(); // End server 55 } 56}
The code
arduino
1#include <SPI.h> 2#include <Ethernet.h> 3 4// MAC address for shield 5byte 6 mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; 7EthernetServer server(80); // 8 Using port 80 9int led = 7; // LED attached to pin 7 10void setup() { 11 pinMode(led, 12 OUTPUT); // Led set as an output 13 Ethernet.begin(mac); // Start the Ethernet 14 shield 15 server.begin(); 16 Serial.begin(9600); // Start serial communication 17 18 Serial.println("Server address:"); // Print server address 19 // (Arduino 20 shield) 21 Serial.println(Ethernet.localIP()); 22} 23void loop() { 24 EthernetClient 25 client = server.available(); 26 if (client) { 27 boolean currentLineIsBlank 28 = true; 29 String buffer = ""; 30 while (client.connected()) { 31 if 32 (client.available()) { 33 char c = client.read(); // Read from the Ethernet 34 shield 35 buffer += c; // Add character to string buffer 36 // Client 37 sent request, now waiting for response 38 if (c == '\ 39' && currentLineIsBlank) 40 { 41 client.println("HTTP/1.1 200 OK"); // HTTP response 42 client.println("Content-Type: 43 text/html"); 44 client.println(); // HTML code 45 client.print("<center><br><h1>Internet 46 Controlled Led </h1><br><br><br><FORM>"); 47 client.print("<P> <INPUT 48 type=\\"submit\\" name=\\"status\\" value=\\"ON\\">"); 49 client.print("<P> 50 <INPUT type=\\"submit\\" name=\\"status\\" value=\\"OFF\\">"); 51 client.print("</FORM></center>"); 52 53 break; 54 } 55 if (c == '\ 56') { 57 currentLineIsBlank 58 = true; 59 buffer = ""; 60 } 61 else if (c == '\ ') 62 { // Command from webpage 63 // Did the on button get pressed 64 if 65 (buffer.indexOf("GET /?status=ON") >= 0) 66 digitalWrite(led, HIGH); 67 68 // Did the off button get pressed 69 if (buffer.indexOf("GET 70 /?status=OFF") >= 0) 71 digitalWrite(led, LOW); 72 } 73 else 74 { 75 currentLineIsBlank = false; 76 } 77 } 78 } 79 80 client.stop(); // End server 81 } 82}
Downloadable files
The schematic
The schematic

Comments
Only logged in users can leave comments