Plymouth ELEC351 Group T / Mbed OS ELEC351

Dependencies:   BME280 BMP280 TextLCD

Committer:
thomasmorris
Date:
Tue Dec 19 13:26:54 2017 +0000
Revision:
8:0e4481b64353
Child:
12:536eca338ae8
Added DATA Class structure and networking example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
thomasmorris 8:0e4481b64353 1 #include "NETWORK.hpp"
thomasmorris 8:0e4481b64353 2
thomasmorris 8:0e4481b64353 3 AnalogIn ldr(PA_0);
thomasmorris 8:0e4481b64353 4
thomasmorris 8:0e4481b64353 5
thomasmorris 8:0e4481b64353 6 int NETWORK_Print()
thomasmorris 8:0e4481b64353 7 {
thomasmorris 8:0e4481b64353 8 printf("Basic HTTP server example\n");
thomasmorris 8:0e4481b64353 9
thomasmorris 8:0e4481b64353 10 //Configure an ethernet connection
thomasmorris 8:0e4481b64353 11 EthernetInterface eth;
thomasmorris 8:0e4481b64353 12 eth.set_network(IP, NETMASK, GATEWAY);
thomasmorris 8:0e4481b64353 13 eth.connect();
thomasmorris 8:0e4481b64353 14 printf("The target IP address is '%s'\n", eth.get_ip_address());
thomasmorris 8:0e4481b64353 15
thomasmorris 8:0e4481b64353 16 //Now setup a web server
thomasmorris 8:0e4481b64353 17 TCPServer srv; //TCP/IP Server
thomasmorris 8:0e4481b64353 18 TCPSocket clt_sock; //Socket for communication
thomasmorris 8:0e4481b64353 19 SocketAddress clt_addr; //Address of incoming connection
thomasmorris 8:0e4481b64353 20
thomasmorris 8:0e4481b64353 21 /* Open the server on ethernet stack */
thomasmorris 8:0e4481b64353 22 srv.open(&eth);
thomasmorris 8:0e4481b64353 23
thomasmorris 8:0e4481b64353 24 /* Bind the HTTP port (TCP 80) to the server */
thomasmorris 8:0e4481b64353 25 srv.bind(eth.get_ip_address(), 80);
thomasmorris 8:0e4481b64353 26
thomasmorris 8:0e4481b64353 27 /* Can handle 5 simultaneous connections */
thomasmorris 8:0e4481b64353 28 srv.listen(5);
thomasmorris 8:0e4481b64353 29
thomasmorris 8:0e4481b64353 30 while (true) {
thomasmorris 8:0e4481b64353 31 using namespace std;
thomasmorris 8:0e4481b64353 32 //Block and wait on an incoming connection
thomasmorris 8:0e4481b64353 33 srv.accept(&clt_sock, &clt_addr);
thomasmorris 8:0e4481b64353 34 printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port());
thomasmorris 8:0e4481b64353 35
thomasmorris 8:0e4481b64353 36 //Uses a C++ string to make it easier to concatinate
thomasmorris 8:0e4481b64353 37 string response;
thomasmorris 8:0e4481b64353 38 //This is a C string
thomasmorris 8:0e4481b64353 39 char ldr_str[64];
thomasmorris 8:0e4481b64353 40
thomasmorris 8:0e4481b64353 41 //Read the LDR value
thomasmorris 8:0e4481b64353 42 float u = ldr;
thomasmorris 8:0e4481b64353 43
thomasmorris 8:0e4481b64353 44 //Convert to a C String
thomasmorris 8:0e4481b64353 45 sprintf(ldr_str, "%5.3f", u );
thomasmorris 8:0e4481b64353 46 printf("LDR: %5.3f\n\r", u);
thomasmorris 8:0e4481b64353 47
thomasmorris 8:0e4481b64353 48 //Build the C++ string response
thomasmorris 8:0e4481b64353 49 response = HTTP_MESSAGE_BODY1;
thomasmorris 8:0e4481b64353 50 response += ldr_str;
thomasmorris 8:0e4481b64353 51 response += HTTP_MESSAGE_BODY2;
thomasmorris 8:0e4481b64353 52
thomasmorris 8:0e4481b64353 53 //Send static HTML response (as a C string)
thomasmorris 8:0e4481b64353 54 clt_sock.send(response.c_str(), response.size()+6);
thomasmorris 8:0e4481b64353 55 }
thomasmorris 8:0e4481b64353 56 }