Simple code for comunication via TCP between the mbed and PC.

Dependencies:   EthernetInterface SimpleSocket mbed-rtos mbed

Fork of SimpleSocketExamples by Hiroshi Yamaguchi

Committer:
numeral369
Date:
Wed Dec 17 16:06:00 2014 +0000
Revision:
1:016774025718
Parent:
0:6dc3cfd058c6
A simple code for comunication via TCP between the Mbed and a PC

Who changed what in which revision?

UserRevisionLine numberNew 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 }