temp

Committer:
BenRJG
Date:
Thu Dec 06 15:38:09 2018 +0000
Revision:
0:2a4af0cb6e8d
Imported Code from Kiel; Added button functionality; Added set DateTime Functionality

Who changed what in which revision?

UserRevisionLine numberNew contents of line
BenRJG 0:2a4af0cb6e8d 1 #include "NetWorking.hpp"
BenRJG 0:2a4af0cb6e8d 2
BenRJG 0:2a4af0cb6e8d 3 // Thread handels networking and updates a conected PC whenever the webpage is refreshed
BenRJG 0:2a4af0cb6e8d 4 void NetWorkingThread(void)
BenRJG 0:2a4af0cb6e8d 5 {
BenRJG 0:2a4af0cb6e8d 6 // Configure an ethernet connection
BenRJG 0:2a4af0cb6e8d 7 EthernetInterface eth;
BenRJG 0:2a4af0cb6e8d 8 eth.set_network(IP, NETMASK, GATEWAY);
BenRJG 0:2a4af0cb6e8d 9 eth.connect();
BenRJG 0:2a4af0cb6e8d 10
BenRJG 0:2a4af0cb6e8d 11 // Now setup a web server
BenRJG 0:2a4af0cb6e8d 12 TCPServer srv; // TCP/IP Server
BenRJG 0:2a4af0cb6e8d 13 TCPSocket clt_sock; // Socket for communication
BenRJG 0:2a4af0cb6e8d 14 SocketAddress clt_addr; // Address of incoming connection
BenRJG 0:2a4af0cb6e8d 15
BenRJG 0:2a4af0cb6e8d 16 // Open the server on ethernet stack
BenRJG 0:2a4af0cb6e8d 17 srv.open(&eth);
BenRJG 0:2a4af0cb6e8d 18
BenRJG 0:2a4af0cb6e8d 19 // Bind the HTTP port (TCP 80) to the server
BenRJG 0:2a4af0cb6e8d 20 srv.bind(eth.get_ip_address(), 80);
BenRJG 0:2a4af0cb6e8d 21
BenRJG 0:2a4af0cb6e8d 22 // Set maximum simultanious conections
BenRJG 0:2a4af0cb6e8d 23 srv.listen(5);
BenRJG 0:2a4af0cb6e8d 24
BenRJG 0:2a4af0cb6e8d 25 while (true) {
BenRJG 0:2a4af0cb6e8d 26 using namespace std;
BenRJG 0:2a4af0cb6e8d 27
BenRJG 0:2a4af0cb6e8d 28 // Block and wait on an incoming connection (if page is accessed)
BenRJG 0:2a4af0cb6e8d 29 srv.accept(&clt_sock, &clt_addr);
BenRJG 0:2a4af0cb6e8d 30
BenRJG 0:2a4af0cb6e8d 31 // Get most up to date enviromental values
BenRJG 0:2a4af0cb6e8d 32 float LIGHT = 0.0f;
BenRJG 0:2a4af0cb6e8d 33 float TEMP = 0.0f;
BenRJG 0:2a4af0cb6e8d 34 float PRES = 0.0f;
BenRJG 0:2a4af0cb6e8d 35
BenRJG 0:2a4af0cb6e8d 36 // Convert to a C String
BenRJG 0:2a4af0cb6e8d 37 char Light_str[8];
BenRJG 0:2a4af0cb6e8d 38 char Temp_str[8];
BenRJG 0:2a4af0cb6e8d 39 char Pres_str[8];
BenRJG 0:2a4af0cb6e8d 40 sprintf(Light_str, "%5.3f", LIGHT );
BenRJG 0:2a4af0cb6e8d 41 sprintf(Temp_str, "%5.3f", TEMP );
BenRJG 0:2a4af0cb6e8d 42 sprintf(Pres_str, "%5.3f", PRES );
BenRJG 0:2a4af0cb6e8d 43
BenRJG 0:2a4af0cb6e8d 44 string response;
BenRJG 0:2a4af0cb6e8d 45
BenRJG 0:2a4af0cb6e8d 46 // Build the C++ string response
BenRJG 0:2a4af0cb6e8d 47 response = HTTP_MESSAGE_BODY1;
BenRJG 0:2a4af0cb6e8d 48 response += Light_str;
BenRJG 0:2a4af0cb6e8d 49 response += HTTP_MESSAGE_BODY2;
BenRJG 0:2a4af0cb6e8d 50 response += Temp_str;
BenRJG 0:2a4af0cb6e8d 51 response += HTTP_MESSAGE_BODY3;
BenRJG 0:2a4af0cb6e8d 52 response += Pres_str;
BenRJG 0:2a4af0cb6e8d 53 response += HTTP_CLOSE;
BenRJG 0:2a4af0cb6e8d 54
BenRJG 0:2a4af0cb6e8d 55 // Send static HTML response (as a C string)
BenRJG 0:2a4af0cb6e8d 56 int debug = clt_sock.send(response.c_str(), strlen(response.c_str())+6);
BenRJG 0:2a4af0cb6e8d 57 }
BenRJG 0:2a4af0cb6e8d 58 }
BenRJG 0:2a4af0cb6e8d 59