HTML stuff almost there may need altering

Fork of Task671-mbedos-FZ429-TCP-dynamic by University of Plymouth - Stages 1, 2 and 3

Committer:
bMcDonnell
Date:
Tue Dec 26 11:26:35 2017 +0000
Revision:
2:edfdb8e455b9
Parent:
1:76bd6f78cabc
Working HTML except for 10's instead of values read from sensors and date/time. May need to update as Date/time is days/months and hours/mins

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:65ff7ad381e8 1 #if !FEATURE_LWIP
noutram 0:65ff7ad381e8 2 #error [NOT_SUPPORTED] LWIP not supported for this target
noutram 0:65ff7ad381e8 3 #endif
noutram 0:65ff7ad381e8 4
noutram 0:65ff7ad381e8 5 #include "mbed.h"
noutram 0:65ff7ad381e8 6 #include "EthernetInterface.h"
noutram 0:65ff7ad381e8 7 #include "TCPServer.h"
noutram 0:65ff7ad381e8 8 #include "TCPSocket.h"
noutram 0:65ff7ad381e8 9 #include <iostream>
noutram 0:65ff7ad381e8 10 #include <string>
noutram 0:65ff7ad381e8 11
noutram 0:65ff7ad381e8 12 #define HTTP_STATUS_LINE "HTTP/1.0 200 OK"
noutram 0:65ff7ad381e8 13 #define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8"
noutram 0:65ff7ad381e8 14 #define HTTP_MESSAGE_BODY1 "" \
noutram 0:65ff7ad381e8 15 "<html>" "\r\n" \
noutram 0:65ff7ad381e8 16 " <body style=\"display:flex;text-align:center\">" "\r\n" \
noutram 0:65ff7ad381e8 17 " <div style=\"margin:auto\">" "\r\n" \
bMcDonnell 2:edfdb8e455b9 18 " <h1>Team-Q Elec351</h1>" "\r\n" \
bMcDonnell 2:edfdb8e455b9 19 " <p>Time : " \
noutram 0:65ff7ad381e8 20
noutram 0:65ff7ad381e8 21 #define HTTP_MESSAGE_BODY2 "" \
bMcDonnell 2:edfdb8e455b9 22 "&nbsp Date : " \
bMcDonnell 2:edfdb8e455b9 23
bMcDonnell 2:edfdb8e455b9 24 #define HTTP_MESSAGE_BODY3 "" \
bMcDonnell 2:edfdb8e455b9 25 "</p>" "\r\n" \
bMcDonnell 2:edfdb8e455b9 26 " <p>Light levels are : " "\r\n" \
bMcDonnell 2:edfdb8e455b9 27
bMcDonnell 2:edfdb8e455b9 28 #define HTTP_MESSAGE_BODY4 "" \
bMcDonnell 2:edfdb8e455b9 29 "</p>" "\r\n" \
bMcDonnell 2:edfdb8e455b9 30 " <p>Current Temperature : " "\r\n" \
bMcDonnell 2:edfdb8e455b9 31
bMcDonnell 2:edfdb8e455b9 32 #define HTTP_MESSAGE_BODY5 "" \
bMcDonnell 2:edfdb8e455b9 33 "</p>" "\r\n" \
bMcDonnell 2:edfdb8e455b9 34 " <p>Current Pressure : " "\r\n" \
bMcDonnell 2:edfdb8e455b9 35
bMcDonnell 2:edfdb8e455b9 36 #define HTTP_MESSAGE_BODY6 "" \
noutram 0:65ff7ad381e8 37 "</p>" "\r\n" \
noutram 0:65ff7ad381e8 38 " </div>" "\r\n" \
noutram 0:65ff7ad381e8 39 " </body>" "\r\n" \
noutram 0:65ff7ad381e8 40 "</html>"
noutram 0:65ff7ad381e8 41
noutram 0:65ff7ad381e8 42 #define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n" \
noutram 0:65ff7ad381e8 43 HTTP_HEADER_FIELDS "\r\n" \
noutram 0:65ff7ad381e8 44 "\r\n" \
noutram 0:65ff7ad381e8 45 HTTP_MESSAGE_BODY "\r\n"
noutram 0:65ff7ad381e8 46
noutram 0:65ff7ad381e8 47 #define IP "10.0.0.10"
noutram 0:65ff7ad381e8 48 #define NETMASK "255.0.0.0"
noutram 0:65ff7ad381e8 49 #define GATEWAY "10.0.0.1"
noutram 0:65ff7ad381e8 50
noutram 1:76bd6f78cabc 51 AnalogIn ldr(PA_0);
noutram 0:65ff7ad381e8 52
noutram 0:65ff7ad381e8 53
noutram 0:65ff7ad381e8 54 int main()
noutram 0:65ff7ad381e8 55 {
noutram 0:65ff7ad381e8 56 printf("Basic HTTP server example\n");
noutram 0:65ff7ad381e8 57
noutram 0:65ff7ad381e8 58 //Configure an ethernet connection
noutram 0:65ff7ad381e8 59 EthernetInterface eth;
noutram 0:65ff7ad381e8 60 eth.set_network(IP, NETMASK, GATEWAY);
noutram 0:65ff7ad381e8 61 eth.connect();
noutram 0:65ff7ad381e8 62 printf("The target IP address is '%s'\n", eth.get_ip_address());
noutram 0:65ff7ad381e8 63
noutram 0:65ff7ad381e8 64 //Now setup a web server
noutram 0:65ff7ad381e8 65 TCPServer srv; //TCP/IP Server
noutram 0:65ff7ad381e8 66 TCPSocket clt_sock; //Socket for communication
noutram 0:65ff7ad381e8 67 SocketAddress clt_addr; //Address of incoming connection
noutram 0:65ff7ad381e8 68
noutram 0:65ff7ad381e8 69 /* Open the server on ethernet stack */
noutram 0:65ff7ad381e8 70 srv.open(&eth);
noutram 0:65ff7ad381e8 71
noutram 0:65ff7ad381e8 72 /* Bind the HTTP port (TCP 80) to the server */
noutram 0:65ff7ad381e8 73 srv.bind(eth.get_ip_address(), 80);
noutram 0:65ff7ad381e8 74
noutram 0:65ff7ad381e8 75 /* Can handle 5 simultaneous connections */
noutram 0:65ff7ad381e8 76 srv.listen(5);
noutram 0:65ff7ad381e8 77
noutram 0:65ff7ad381e8 78 while (true) {
noutram 0:65ff7ad381e8 79 using namespace std;
noutram 0:65ff7ad381e8 80 //Block and wait on an incoming connection
noutram 0:65ff7ad381e8 81 srv.accept(&clt_sock, &clt_addr);
noutram 0:65ff7ad381e8 82 printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port());
noutram 0:65ff7ad381e8 83
noutram 1:76bd6f78cabc 84 //Uses a C++ string to make it easier to concatinate
bMcDonnell 2:edfdb8e455b9 85 //"response" is the string that makes up the whole web page
noutram 0:65ff7ad381e8 86 string response;
noutram 1:76bd6f78cabc 87 //This is a C string
bMcDonnell 2:edfdb8e455b9 88 //make space to save variables as strings
bMcDonnell 2:edfdb8e455b9 89 char time_str[64];
bMcDonnell 2:edfdb8e455b9 90 char date_str[64];
noutram 1:76bd6f78cabc 91 char ldr_str[64];
bMcDonnell 2:edfdb8e455b9 92 char temp_str[64];
bMcDonnell 2:edfdb8e455b9 93 char pres_str[64];
noutram 1:76bd6f78cabc 94
bMcDonnell 2:edfdb8e455b9 95 //Read the time/date/sensor values
bMcDonnell 2:edfdb8e455b9 96 //use sensor readings instead of 10's
bMcDonnell 2:edfdb8e455b9 97 float a = 10;
bMcDonnell 2:edfdb8e455b9 98 float e = 10;
bMcDonnell 2:edfdb8e455b9 99 float i = ldr;
bMcDonnell 2:edfdb8e455b9 100 float o = 10;
bMcDonnell 2:edfdb8e455b9 101 float u = 10;
noutram 0:65ff7ad381e8 102
noutram 1:76bd6f78cabc 103 //Convert to a C String
bMcDonnell 2:edfdb8e455b9 104 sprintf(time_str, "%5.3f", a );
bMcDonnell 2:edfdb8e455b9 105 sprintf(date_str, "%5.3f", e );
bMcDonnell 2:edfdb8e455b9 106 sprintf(ldr_str, "%5.3f", i );
bMcDonnell 2:edfdb8e455b9 107 sprintf(temp_str, "%5.3f", o );
bMcDonnell 2:edfdb8e455b9 108 sprintf(pres_str, "%5.3f", u );
noutram 1:76bd6f78cabc 109
noutram 1:76bd6f78cabc 110 //Build the C++ string response
noutram 0:65ff7ad381e8 111 response = HTTP_MESSAGE_BODY1;
bMcDonnell 2:edfdb8e455b9 112 response += time_str;
noutram 0:65ff7ad381e8 113 response += HTTP_MESSAGE_BODY2;
bMcDonnell 2:edfdb8e455b9 114 response += date_str;
bMcDonnell 2:edfdb8e455b9 115 response += HTTP_MESSAGE_BODY3;
bMcDonnell 2:edfdb8e455b9 116 response += ldr_str;
bMcDonnell 2:edfdb8e455b9 117 response += HTTP_MESSAGE_BODY4;
bMcDonnell 2:edfdb8e455b9 118 response += temp_str;
bMcDonnell 2:edfdb8e455b9 119 response += HTTP_MESSAGE_BODY5;
bMcDonnell 2:edfdb8e455b9 120 response += pres_str;
bMcDonnell 2:edfdb8e455b9 121 response += HTTP_MESSAGE_BODY6;
bMcDonnell 2:edfdb8e455b9 122
noutram 1:76bd6f78cabc 123 //Send static HTML response (as a C string)
noutram 0:65ff7ad381e8 124 clt_sock.send(response.c_str(), response.size()+6);
noutram 0:65ff7ad381e8 125 }
noutram 0:65ff7ad381e8 126 }