Simple code for comunication via TCP between the mbed and PC.
Dependencies: EthernetInterface SimpleSocket mbed-rtos mbed
Fork of SimpleSocketExamples by
examples/webcontroller.cpp@0:6dc3cfd058c6, 2013-02-04 (annotated)
- Committer:
- yamaguch
- Date:
- Mon Feb 04 09:29:18 2013 +0000
- Revision:
- 0:6dc3cfd058c6
modified to use SimpleSocket/EthernetInterface
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yamaguch | 0:6dc3cfd058c6 | 1 | #include "SimpleSocket.h" |
yamaguch | 0:6dc3cfd058c6 | 2 | |
yamaguch | 0:6dc3cfd058c6 | 3 | void webcontroller() { |
yamaguch | 0:6dc3cfd058c6 | 4 | const char *response0 = |
yamaguch | 0:6dc3cfd058c6 | 5 | "HTTP/1.1 200 OK\r\n" |
yamaguch | 0:6dc3cfd058c6 | 6 | "Content-Type: text/html\r\n" |
yamaguch | 0:6dc3cfd058c6 | 7 | "\r\n" |
yamaguch | 0:6dc3cfd058c6 | 8 | "<html>\r\n" |
yamaguch | 0:6dc3cfd058c6 | 9 | "<head><title>mbed LED1 Controller</title></head>\r\n" |
yamaguch | 0:6dc3cfd058c6 | 10 | "<body>\r\n" |
yamaguch | 0:6dc3cfd058c6 | 11 | "<h4>LED1 Status & Change</h4>\r\n"; |
yamaguch | 0:6dc3cfd058c6 | 12 | |
yamaguch | 0:6dc3cfd058c6 | 13 | const char *response1 = |
yamaguch | 0:6dc3cfd058c6 | 14 | "<form method=\"GET\" action=\"/\">\r\n" |
yamaguch | 0:6dc3cfd058c6 | 15 | "<input type=\"radio\" name=\"LED\" value=\"1\" %s onclick=\"submit();\"/>ON\r\n" |
yamaguch | 0:6dc3cfd058c6 | 16 | "<input type=\"radio\" name=\"LED\" value=\"0\" %s onclick=\"submit();\"/>OFF\r\n" |
yamaguch | 0:6dc3cfd058c6 | 17 | "</form>\r\n"; |
yamaguch | 0:6dc3cfd058c6 | 18 | |
yamaguch | 0:6dc3cfd058c6 | 19 | const char *response2 = |
yamaguch | 0:6dc3cfd058c6 | 20 | "</body>\r\n" |
yamaguch | 0:6dc3cfd058c6 | 21 | "</html>\r\n"; |
yamaguch | 0:6dc3cfd058c6 | 22 | |
yamaguch | 0:6dc3cfd058c6 | 23 | DigitalOut led1(LED1); |
yamaguch | 0:6dc3cfd058c6 | 24 | |
yamaguch | 0:6dc3cfd058c6 | 25 | ServerSocket server(80); |
yamaguch | 0:6dc3cfd058c6 | 26 | |
yamaguch | 0:6dc3cfd058c6 | 27 | printf("webcontroller: %s\n", EthernetInterface::getIPAddress()); |
yamaguch | 0:6dc3cfd058c6 | 28 | |
yamaguch | 0:6dc3cfd058c6 | 29 | while (true) { |
yamaguch | 0:6dc3cfd058c6 | 30 | ClientSocket socket = server.accept(); |
yamaguch | 0:6dc3cfd058c6 | 31 | while (socket) { |
yamaguch | 0:6dc3cfd058c6 | 32 | if (socket.available()) { |
yamaguch | 0:6dc3cfd058c6 | 33 | char buf[512] = {}; |
yamaguch | 0:6dc3cfd058c6 | 34 | socket.read(buf, sizeof(buf) - 1); |
yamaguch | 0:6dc3cfd058c6 | 35 | printf("\r\n%s\r\n", buf); |
yamaguch | 0:6dc3cfd058c6 | 36 | led1 = strncmp("GET /?LED=1", buf, 11) == 0; |
yamaguch | 0:6dc3cfd058c6 | 37 | |
yamaguch | 0:6dc3cfd058c6 | 38 | printf("LED1 = %d\r\n\r\n", led1.read()); |
yamaguch | 0:6dc3cfd058c6 | 39 | printf(response0); |
yamaguch | 0:6dc3cfd058c6 | 40 | printf(response1, led1 ? "checked" : "", led1 ? "" : "checked"); |
yamaguch | 0:6dc3cfd058c6 | 41 | printf(response2); |
yamaguch | 0:6dc3cfd058c6 | 42 | |
yamaguch | 0:6dc3cfd058c6 | 43 | socket.printf(response0); |
yamaguch | 0:6dc3cfd058c6 | 44 | socket.printf(response1, led1 ? "checked" : "", led1 ? "" : "checked"); |
yamaguch | 0:6dc3cfd058c6 | 45 | socket.printf(response2); |
yamaguch | 0:6dc3cfd058c6 | 46 | socket.close(); |
yamaguch | 0:6dc3cfd058c6 | 47 | } |
yamaguch | 0:6dc3cfd058c6 | 48 | } |
yamaguch | 0:6dc3cfd058c6 | 49 | } |
yamaguch | 0:6dc3cfd058c6 | 50 | } |