something

Dependencies:   BMP280 Task671-mbedos-FZ429-TCP-dynamic

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

Committer:
FranciscoSalle
Date:
Tue Jan 09 17:39:01 2018 +0000
Revision:
2:f7c18fc24082
.;

Who changed what in which revision?

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