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:
1#include <Ethernet.h>
2
3byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
4byte ip[] = {10,0,0,20};
5
6// listen on port 80 for connections
7Server server(80);
8
9void setup()
10{
11 Ethernet.begin(mac, ip);
12 server.begin();
13}
14
15void loop()
16{
17 Client client = server.available();
18}
2
3byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
4byte ip[] = {10,0,0,20};
5
6// listen on port 80 for connections
7Server server(80);
8
9void setup()
10{
11 Ethernet.begin(mac, ip);
12 server.begin();
13}
14
15void loop()
16{
17 Client client = server.available();
18}
Then we parse (and ignore) any HTTP headers coming in:
1#include <Ethernet.h>
2
3byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
4byte ip[] = {10,0,0,20};
5
6// listen on port 80 for connections
7Server server(80);
8
9void setup()
10{
11 Ethernet.begin(mac, ip);
12 server.begin();
13}
14
15void loop()
16{
17 Client client = server.available();
18
19 if (client) {
20 boolean blankline = false;
21
22 while (client.connected())
23 {
24 if (client.available())
25 {
26 char c = client.read();
27
28 // if line is blank, the http request has ended, so we can send a reply
29 if (c == '\n' && blankline) {
30
31 break;
32 }
33 // we're starting a new line
34 if (c == '\n') {
35 blankline = true;
36 }
37 else if (c != '\r'){
38 blankline = false;
39 }
40 }
41 }
42
43 client.flush();
44 client.stop();
45 }
46}
2
3byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
4byte ip[] = {10,0,0,20};
5
6// listen on port 80 for connections
7Server server(80);
8
9void setup()
10{
11 Ethernet.begin(mac, ip);
12 server.begin();
13}
14
15void loop()
16{
17 Client client = server.available();
18
19 if (client) {
20 boolean blankline = false;
21
22 while (client.connected())
23 {
24 if (client.available())
25 {
26 char c = client.read();
27
28 // if line is blank, the http request has ended, so we can send a reply
29 if (c == '\n' && blankline) {
30
31 break;
32 }
33 // we're starting a new line
34 if (c == '\n') {
35 blankline = true;
36 }
37 else if (c != '\r'){
38 blankline = false;
39 }
40 }
41 }
42
43 client.flush();
44 client.stop();
45 }
46}
Lastly we need to send some content back to the browser:
1#include <Ethernet.h>
2
3byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
4byte ip[] = {10,0,0,20};
5
6char* content[] = {"<html>",
7"<body>",
8"<h1>Web Jam 11</h1>",
9"<p>Arduino powered Web Server</p>",
10"<p>Vote for me SMS 2 to 0427075367</p>",
11"</body>",
12"</html>"
13};
14
15byte nolines = 7;
16
17// listen on port 80 for connections
18Server server(80);
19
20void setup()
21{
22 Ethernet.begin(mac, ip);
23 server.begin();
24}
25
26void loop()
27{
28 Client client = server.available();
29
30 if (client) {
31 boolean blankline = false;
32
33 while (client.connected())
34 {
35 if (client.available())
36 {
37 char c = client.read();
38
39 // if line is blank, the http request has ended, so we can send a reply
40 if (c == '\n' && blankline) {
41
42 // Send a standard http response header
43 client.println("HTTP/1.1 200 OK");
44 client.println("Content-Type: text/html");
45 client.println();
46
47 // Send http content
48 for (int i =0; i < nolines; i++) {
49 client.println(content[i]);
50 }
51
52 break;
53 }
54 // we're starting a new line
55 if (c == '\n') {
56 blankline = true;
57 }
58 else if (c != '\r'){
59 blankline = false;
60 }
61 }
62 }
63
64 client.flush();
65 client.stop();
66 }
67}
2
3byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
4byte ip[] = {10,0,0,20};
5
6char* content[] = {"<html>",
7"<body>",
8"<h1>Web Jam 11</h1>",
9"<p>Arduino powered Web Server</p>",
10"<p>Vote for me SMS 2 to 0427075367</p>",
11"</body>",
12"</html>"
13};
14
15byte nolines = 7;
16
17// listen on port 80 for connections
18Server server(80);
19
20void setup()
21{
22 Ethernet.begin(mac, ip);
23 server.begin();
24}
25
26void loop()
27{
28 Client client = server.available();
29
30 if (client) {
31 boolean blankline = false;
32
33 while (client.connected())
34 {
35 if (client.available())
36 {
37 char c = client.read();
38
39 // if line is blank, the http request has ended, so we can send a reply
40 if (c == '\n' && blankline) {
41
42 // Send a standard http response header
43 client.println("HTTP/1.1 200 OK");
44 client.println("Content-Type: text/html");
45 client.println();
46
47 // Send http content
48 for (int i =0; i < nolines; i++) {
49 client.println(content[i]);
50 }
51
52 break;
53 }
54 // we're starting a new line
55 if (c == '\n') {
56 blankline = true;
57 }
58 else if (c != '\r'){
59 blankline = false;
60 }
61 }
62 }
63
64 client.flush();
65 client.stop();
66 }
67}
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).
TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
[Add Comment]
[Subscribe to Comments]
$20 if you can put railo on there ;)
# Posted By Andy Welsh
| 11/10/09 1:47 PM
Well I was thinking of ColdFusion but see what I can do :-)
# Posted By Justin Mclean
| 11/10/09 2:03 PM
[Add Comment]