Mearm colorsorting with web application

Dependencies:   TCS3200

Committer:
pierre11
Date:
Thu Nov 30 16:10:23 2017 +0000
Revision:
3:f5e5af908b55
Parent:
1:ec61ea9f67de
Child:
4:62b6eaf030ab
refresh;

Who changed what in which revision?

UserRevisionLine numberNew 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(&eth);
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 3:f5e5af908b55 61 clt_sock.send(http, strlen((char *)http));
pierre11 3:f5e5af908b55 62
pierre11 3:f5e5af908b55 63 v++;
pierre11 3:f5e5af908b55 64 SendWebPage(v);
pierre11 3:f5e5af908b55 65 clt_sock.send(http, strlen((char *)http));
group-ST 0:6e8275981824 66 }
pierre11 3:f5e5af908b55 67
group-ST 0:6e8275981824 68 }
pierre11 3:f5e5af908b55 69
pierre11 3:f5e5af908b55 70 static void SendWebPage( float temperature)
pierre11 3:f5e5af908b55 71 {
pierre11 3:f5e5af908b55 72 uint8_t temp[50];
pierre11 3:f5e5af908b55 73 //uint16_t SentDataLength;
pierre11 3:f5e5af908b55 74 //WIFI_Status_t ret;
pierre11 3:f5e5af908b55 75
pierre11 3:f5e5af908b55 76 /* construct web page content */
pierre11 3:f5e5af908b55 77 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 78 strcat((char *)http, (char *)"<html>\r\n<body>\r\n");
pierre11 3:f5e5af908b55 79 strcat((char *)http, (char *)"<head>\r\n");
pierre11 3:f5e5af908b55 80 strcat((char *)http, (char *)"<meta http-equiv="refresh" content="30">\r\n");
pierre11 3:f5e5af908b55 81 strcat((char *)http, (char *)"</head>\r\n");
pierre11 3:f5e5af908b55 82 strcat((char *)http, (char *)"<title>STM32 Web Server</title>\r\n");
pierre11 3:f5e5af908b55 83 strcat((char *)http, (char *)"<h2>InventekSys : Web Server using Es-Wifi with STM32</h2>\r\n");
pierre11 3:f5e5af908b55 84 strcat((char *)http, (char *)"<br /><hr>\r\n");
pierre11 3:f5e5af908b55 85 strcat((char *)http, (char *)"<p><form method=\"POST\"><strong>Temp: <input type=\"text\" size=2 value=\"");
pierre11 3:f5e5af908b55 86 sprintf((char *)temp, "%f", temperature);
pierre11 3:f5e5af908b55 87 strcat((char *)http, (char *)temp);
pierre11 3:f5e5af908b55 88 strcat((char *)http, (char *)"</body>\r\n</html>\r\n");
pierre11 3:f5e5af908b55 89
pierre11 3:f5e5af908b55 90 //ret = WIFI_SendData(0, (uint8_t *)http, strlen((char *)http), &SentDataLength, WIFI_WRITE_TIMEOUT);
pierre11 3:f5e5af908b55 91
pierre11 3:f5e5af908b55 92 // if((ret == WIFI_STATUS_OK) && (SentDataLength != strlen((char *)http)))
pierre11 3:f5e5af908b55 93 //{
pierre11 3:f5e5af908b55 94 // ret = WIFI_STATUS_ERROR;
pierre11 3:f5e5af908b55 95 // }
pierre11 3:f5e5af908b55 96
pierre11 3:f5e5af908b55 97 // return 0;
pierre11 3:f5e5af908b55 98 }