Skip to content

Instantly share code, notes, and snippets.

@sergiobuj
Created February 6, 2013 03:50
Show Gist options
  • Save sergiobuj/4720099 to your computer and use it in GitHub Desktop.
Save sergiobuj/4720099 to your computer and use it in GitHub Desktop.
Arduino web server
#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