Created
February 6, 2013 03:50
-
-
Save sergiobuj/4720099 to your computer and use it in GitHub Desktop.
Arduino web server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include <Ethernet.h> | |
int port = 80; | |
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; | |
IPAddress ip(192, 168, 1, 99); | |
EthernetServer server = EthernetServer(port); | |
void setup() | |
{ | |
Serial.begin(9600); | |
delay(1000); | |
Ethernet.begin(mac, ip); | |
server.begin(); | |
Serial.println(Ethernet.localIP()); | |
} | |
void loop() | |
{ | |
EthernetClient client = server.available(); | |
if (client) { | |
boolean currentLineIsBlank = true; | |
while (client.connected()) { | |
if (client.available()) { | |
char c = client.read(); | |
Serial.write(c); | |
if (c == '\n' && currentLineIsBlank) { | |
client.println("HTTP/1.1 200 OK"); | |
client.println("Content-Type: text/html"); | |
client.println("Connnection: close"); | |
client.println(); | |
client.println("<!DOCTYPE HTML>"); | |
client.println("<head>"); | |
client.println(" <link rel='stylesheet' type='text/css' href='http://show_docs.cloudfoundry.com/css/arduinito.css'>"); | |
client.println("</head>"); | |
client.println("<html>"); | |
client.println(" <form action='on' method='POST' id='on-form'>"); | |
client.println(" <button form='on-form' class='on'>"); | |
client.println(" <font>ON</font>"); | |
client.println(" </button>"); | |
client.println(" </form>"); | |
client.println(" <form action='off' method='POST' id='off-form'>"); | |
client.println(" <button form='off-form' class='off'>"); | |
client.println(" <font>OFF</font>"); | |
client.println(" </button>"); | |
client.println(" </form>"); | |
client.println(" <form action='blink' method='POST' id='blink-form'>"); | |
client.println(" <button form='blink-form' class='blink'>"); | |
client.println(" <font>BLINK</font>"); | |
client.println(" </button>"); | |
client.println(" </form>"); | |
client.println("</html>"); | |
break; | |
} | |
if (c == '\n') { | |
currentLineIsBlank = true; | |
} else if (c != '\r') { | |
currentLineIsBlank = false; | |
} | |
} | |
} | |
delay(1); | |
client.stop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment