SimpleSocket 1.0 examples

Dependencies:   EthernetNetIf SimpleSocket 1.0 mbed

examples/webserver.cpp

Committer:
yamaguch
Date:
2013-02-04
Revision:
40:84182fc63956
Parent:
webserver.cpp@ 39:108499af2b53

File content as of revision 40:84182fc63956:

#include "EthernetNetIf.h"
#include "SimpleSocket.h"

void webserver() {
    const char *response0 =
        "HTTP/1.1 200 OK\r\n"
        "Content-Type: text/html\r\n"
        "\r\n"
        "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"5\">\r\n"
        "<html>\r\n"
        "<head>\r\n"
        "<title>mbed web server</title>\r\n"
        "</head>\r\n"
        "<body>\r\n"
        "<h2>Analog Input</h2>\r\n"
        "<table cellpadding=\"5\">\r\n";
    const char *response1 =
        "<tr style=\"background:#ccccff\">"
        "<th>pin</th><th>value</th>"
        "</tr>\r\n";
    const char *response2 =
        "<tr style=\"background:#cccccc\">"
        "<td>p%d</td><td align=\"center\">%f</td>"
        "</tr>\r\n";
    const char *response3 =
        "</table>\r\n"
        "</body>\r\n"
        "</html>\r\n";

    EthernetNetIf eth;
    eth.setup();

    ServerSocket server(80);

    while (true) {
        ClientSocket socket = server.accept();
        while (socket) {
            if (socket.available()) {
                while (socket.available())
                    socket.read();
                socket.printf(response0);
                socket.printf(response1);
                AnalogIn analogPin[] = {p15, p16, p17, p18, p19, p20};
                for (int i = 0; i < 6; i++)
                    socket.printf(response2, 15 + i, analogPin[i].read());
                socket.printf(response3);
                socket.close();
            }
        }
    }
}