Making an Arduino web server in under 3 minutes at Web Jam 11
Web Jam 11 was great fun and lots of interesting rants and presentations. Good to see two Arduino presentations but my talk on making an Arduino web server from scratch was the only one to show code the entire evening! Here's the code:
First off we need to listen on port 80 for any HTTP requests:
#include <Ethernet.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte ip[] = {10,0,0,20};
// listen on port 80 for connections
Server server(80);
void setup()
{
Ethernet.begin(mac, ip);
server.begin();
}
void loop()
{
Client client = server.available();
}
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte ip[] = {10,0,0,20};
// listen on port 80 for connections
Server server(80);
void setup()
{
Ethernet.begin(mac, ip);
server.begin();
}
void loop()
{
Client client = server.available();
}
Then we parse (and ignore) any HTTP headers coming in:
#include <Ethernet.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte ip[] = {10,0,0,20};
// listen on port 80 for connections
Server server(80);
void setup()
{
Ethernet.begin(mac, ip);
server.begin();
}
void loop()
{
Client client = server.available();
if (client) {
boolean blankline = false;
while (client.connected())
{
if (client.available())
{
char c = client.read();
// if line is blank, the http request has ended, so we can send a reply
if (c == '\n' && blankline) {
break;
}
// we're starting a new line
if (c == '\n') {
blankline = true;
}
else if (c != '\r'){
blankline = false;
}
}
}
client.flush();
client.stop();
}
}
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte ip[] = {10,0,0,20};
// listen on port 80 for connections
Server server(80);
void setup()
{
Ethernet.begin(mac, ip);
server.begin();
}
void loop()
{
Client client = server.available();
if (client) {
boolean blankline = false;
while (client.connected())
{
if (client.available())
{
char c = client.read();
// if line is blank, the http request has ended, so we can send a reply
if (c == '\n' && blankline) {
break;
}
// we're starting a new line
if (c == '\n') {
blankline = true;
}
else if (c != '\r'){
blankline = false;
}
}
}
client.flush();
client.stop();
}
}
Lastly we need to send some content back to the browser:
#include <Ethernet.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte ip[] = {10,0,0,20};
char* content[] = {"<html>",
"<body>",
"<h1>Web Jam 11</h1>",
"<p>Arduino powered Web Server</p>",
"<p>Vote for me SMS 2 to 0427075367</p>",
"</body>",
"</html>"
};
byte nolines = 7;
// listen on port 80 for connections
Server server(80);
void setup()
{
Ethernet.begin(mac, ip);
server.begin();
}
void loop()
{
Client client = server.available();
if (client) {
boolean blankline = false;
while (client.connected())
{
if (client.available())
{
char c = client.read();
// if line is blank, the http request has ended, so we can send a reply
if (c == '\n' && blankline) {
// Send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
// Send http content
for (int i =0; i < nolines; i++) {
client.println(content[i]);
}
break;
}
// we're starting a new line
if (c == '\n') {
blankline = true;
}
else if (c != '\r'){
blankline = false;
}
}
}
client.flush();
client.stop();
}
}
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte ip[] = {10,0,0,20};
char* content[] = {"<html>",
"<body>",
"<h1>Web Jam 11</h1>",
"<p>Arduino powered Web Server</p>",
"<p>Vote for me SMS 2 to 0427075367</p>",
"</body>",
"</html>"
};
byte nolines = 7;
// listen on port 80 for connections
Server server(80);
void setup()
{
Ethernet.begin(mac, ip);
server.begin();
}
void loop()
{
Client client = server.available();
if (client) {
boolean blankline = false;
while (client.connected())
{
if (client.available())
{
char c = client.read();
// if line is blank, the http request has ended, so we can send a reply
if (c == '\n' && blankline) {
// Send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
// Send http content
for (int i =0; i < nolines; i++) {
client.println(content[i]);
}
break;
}
// we're starting a new line
if (c == '\n') {
blankline = true;
}
else if (c != '\r'){
blankline = false;
}
}
}
client.flush();
client.stop();
}
}
What could be easier than that!
Shows you can make a web server in under 3 minutes even if you have microphone issues and don't have 3 arms (thanks for the help with the mike Lachlan).
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
[Add Comment]
# Posted By Andy Welsh
| 11/10/09 1:47 PM
# Posted By Justin Mclean
| 11/10/09 2:03 PM
[Add Comment]






