pierre masala
/
MeARM
Mearm colorsorting with web application
main.cpp@4:62b6eaf030ab, 2017-11-30 (annotated)
- Committer:
- pierre11
- Date:
- Thu Nov 30 17:10:05 2017 +0000
- Revision:
- 4:62b6eaf030ab
- Parent:
- 3:f5e5af908b55
- Child:
- 5:36f1e4e1a427
working perfect;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
adustm | 1:ec61ea9f67de | 1 | #if !FEATURE_LWIP |
adustm | 1:ec61ea9f67de | 2 | #error [NOT_SUPPORTED] LWIP not supported for this target |
group-ST | 0:6e8275981824 | 3 | #endif |
group-ST | 0:6e8275981824 | 4 | |
group-ST | 0:6e8275981824 | 5 | #include "mbed.h" |
group-ST | 0:6e8275981824 | 6 | #include "EthernetInterface.h" |
group-ST | 0:6e8275981824 | 7 | #include "TCPServer.h" |
group-ST | 0:6e8275981824 | 8 | #include "TCPSocket.h" |
pierre11 | 3:f5e5af908b55 | 9 | uint8_t http[1024]; |
pierre11 | 3:f5e5af908b55 | 10 | //static uint8_t resp[1024]; |
pierre11 | 3:f5e5af908b55 | 11 | static void SendWebPage(float temperature); |
group-ST | 0:6e8275981824 | 12 | #define HTTP_STATUS_LINE "HTTP/1.0 200 OK" |
group-ST | 0:6e8275981824 | 13 | #define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8" |
group-ST | 0:6e8275981824 | 14 | #define HTTP_MESSAGE_BODY "" \ |
group-ST | 0:6e8275981824 | 15 | "<html>" "\r\n" \ |
group-ST | 0:6e8275981824 | 16 | " <body style=\"display:flex;text-align:center\">" "\r\n" \ |
group-ST | 0:6e8275981824 | 17 | " <div style=\"margin:auto\">" "\r\n" \ |
group-ST | 0:6e8275981824 | 18 | " <h1>Hello World</h1>" "\r\n" \ |
group-ST | 0:6e8275981824 | 19 | " <p>It works !</p>" "\r\n" \ |
group-ST | 0:6e8275981824 | 20 | " </div>" "\r\n" \ |
group-ST | 0:6e8275981824 | 21 | " </body>" "\r\n" \ |
group-ST | 0:6e8275981824 | 22 | "</html>" |
group-ST | 0:6e8275981824 | 23 | |
group-ST | 0:6e8275981824 | 24 | #define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n" \ |
group-ST | 0:6e8275981824 | 25 | HTTP_HEADER_FIELDS "\r\n" \ |
group-ST | 0:6e8275981824 | 26 | "\r\n" \ |
group-ST | 0:6e8275981824 | 27 | HTTP_MESSAGE_BODY "\r\n" |
pierre11 | 3:f5e5af908b55 | 28 | |
pierre11 | 3:f5e5af908b55 | 29 | |
group-ST | 0:6e8275981824 | 30 | |
group-ST | 0:6e8275981824 | 31 | int main() |
group-ST | 0:6e8275981824 | 32 | { |
pierre11 | 3:f5e5af908b55 | 33 | float v = 7; |
group-ST | 0:6e8275981824 | 34 | printf("Basic HTTP server example\n"); |
group-ST | 0:6e8275981824 | 35 | |
pierre11 | 3:f5e5af908b55 | 36 | |
group-ST | 0:6e8275981824 | 37 | EthernetInterface eth; |
group-ST | 0:6e8275981824 | 38 | eth.connect(); |
group-ST | 0:6e8275981824 | 39 | |
group-ST | 0:6e8275981824 | 40 | printf("The target IP address is '%s'\n", eth.get_ip_address()); |
group-ST | 0:6e8275981824 | 41 | |
group-ST | 0:6e8275981824 | 42 | TCPServer srv; |
group-ST | 0:6e8275981824 | 43 | TCPSocket clt_sock; |
adustm | 1:ec61ea9f67de | 44 | SocketAddress clt_addr; |
group-ST | 0:6e8275981824 | 45 | |
group-ST | 0:6e8275981824 | 46 | /* Open the server on ethernet stack */ |
group-ST | 0:6e8275981824 | 47 | srv.open(ð); |
group-ST | 0:6e8275981824 | 48 | |
group-ST | 0:6e8275981824 | 49 | /* Bind the HTTP port (TCP 80) to the server */ |
group-ST | 0:6e8275981824 | 50 | srv.bind(eth.get_ip_address(), 80); |
group-ST | 0:6e8275981824 | 51 | |
group-ST | 0:6e8275981824 | 52 | /* Can handle 5 simultaneous connections */ |
group-ST | 0:6e8275981824 | 53 | srv.listen(5); |
pierre11 | 3:f5e5af908b55 | 54 | |
group-ST | 0:6e8275981824 | 55 | while (true) { |
pierre11 | 3:f5e5af908b55 | 56 | |
pierre11 | 3:f5e5af908b55 | 57 | SendWebPage(v); |
pierre11 | 3:f5e5af908b55 | 58 | srv.accept(&clt_sock, &clt_addr); |
adustm | 1:ec61ea9f67de | 59 | printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port()); |
pierre11 | 3:f5e5af908b55 | 60 | //clt_sock.send(HTTP_RESPONSE, strlen(HTTP_RESPONSE)); |
pierre11 | 4:62b6eaf030ab | 61 | clt_sock.send(http, strlen((char *)http)); |
pierre11 | 3:f5e5af908b55 | 62 | v++; |
pierre11 | 4:62b6eaf030ab | 63 | |
group-ST | 0:6e8275981824 | 64 | } |
pierre11 | 3:f5e5af908b55 | 65 | |
group-ST | 0:6e8275981824 | 66 | } |
pierre11 | 3:f5e5af908b55 | 67 | |
pierre11 | 3:f5e5af908b55 | 68 | static void SendWebPage( float temperature) |
pierre11 | 3:f5e5af908b55 | 69 | { |
pierre11 | 3:f5e5af908b55 | 70 | uint8_t temp[50]; |
pierre11 | 3:f5e5af908b55 | 71 | //uint16_t SentDataLength; |
pierre11 | 3:f5e5af908b55 | 72 | //WIFI_Status_t ret; |
pierre11 | 3:f5e5af908b55 | 73 | |
pierre11 | 3:f5e5af908b55 | 74 | /* construct web page content */ |
pierre11 | 3:f5e5af908b55 | 75 | strcpy((char *)http, (char *)"HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n"); |
pierre11 | 3:f5e5af908b55 | 76 | strcat((char *)http, (char *)"<html>\r\n<body>\r\n"); |
pierre11 | 4:62b6eaf030ab | 77 | strcat((char *)http, (char *)"<head>\r\n<meta http-equiv=""refresh"" content=""30"">\r\n</head>\r\n"); |
pierre11 | 4:62b6eaf030ab | 78 | //strcat((char *)http, (char *)); |
pierre11 | 4:62b6eaf030ab | 79 | //strcat((char *)http, (char *)"</head>\r\n"); |
pierre11 | 3:f5e5af908b55 | 80 | strcat((char *)http, (char *)"<title>STM32 Web Server</title>\r\n"); |
pierre11 | 3:f5e5af908b55 | 81 | strcat((char *)http, (char *)"<h2>InventekSys : Web Server using Es-Wifi with STM32</h2>\r\n"); |
pierre11 | 3:f5e5af908b55 | 82 | strcat((char *)http, (char *)"<br /><hr>\r\n"); |
pierre11 | 3:f5e5af908b55 | 83 | strcat((char *)http, (char *)"<p><form method=\"POST\"><strong>Temp: <input type=\"text\" size=2 value=\""); |
pierre11 | 3:f5e5af908b55 | 84 | sprintf((char *)temp, "%f", temperature); |
pierre11 | 3:f5e5af908b55 | 85 | strcat((char *)http, (char *)temp); |
pierre11 | 3:f5e5af908b55 | 86 | strcat((char *)http, (char *)"</body>\r\n</html>\r\n"); |
pierre11 | 3:f5e5af908b55 | 87 | |
pierre11 | 3:f5e5af908b55 | 88 | //ret = WIFI_SendData(0, (uint8_t *)http, strlen((char *)http), &SentDataLength, WIFI_WRITE_TIMEOUT); |
pierre11 | 3:f5e5af908b55 | 89 | |
pierre11 | 3:f5e5af908b55 | 90 | // if((ret == WIFI_STATUS_OK) && (SentDataLength != strlen((char *)http))) |
pierre11 | 3:f5e5af908b55 | 91 | //{ |
pierre11 | 3:f5e5af908b55 | 92 | // ret = WIFI_STATUS_ERROR; |
pierre11 | 3:f5e5af908b55 | 93 | // } |
pierre11 | 3:f5e5af908b55 | 94 | |
pierre11 | 3:f5e5af908b55 | 95 | // return 0; |
pierre11 | 3:f5e5af908b55 | 96 | } |