Updated

Dependents:   PROJECTTEST

Committer:
Swabey89
Date:
Sat Jan 05 15:02:17 2019 +0000
Revision:
0:9df0dd20731a
Updated

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Swabey89 0:9df0dd20731a 1 #include "Networking.hpp"
Swabey89 0:9df0dd20731a 2
Swabey89 0:9df0dd20731a 3 //Network thread - responsible for listening for connectinos and responding with updated tempature values
Swabey89 0:9df0dd20731a 4 void network()
Swabey89 0:9df0dd20731a 5 {
Swabey89 0:9df0dd20731a 6 EthernetInterface eth;
Swabey89 0:9df0dd20731a 7 eth.set_network(IP, NETMASK, GATEWAY);
Swabey89 0:9df0dd20731a 8 eth.connect();
Swabey89 0:9df0dd20731a 9
Swabey89 0:9df0dd20731a 10 printQueue.call(printf, "The target IP address is '%s'\n\n\n\r", eth.get_ip_address());
Swabey89 0:9df0dd20731a 11
Swabey89 0:9df0dd20731a 12 //Now setup a web server
Swabey89 0:9df0dd20731a 13 TCPServer srv; //TCP/IP Server
Swabey89 0:9df0dd20731a 14 TCPSocket clt_sock; //Socket for communication
Swabey89 0:9df0dd20731a 15 SocketAddress clt_addr; //Address of incoming connection
Swabey89 0:9df0dd20731a 16
Swabey89 0:9df0dd20731a 17 /* Open the server on ethernet stack */
Swabey89 0:9df0dd20731a 18 srv.open(&eth);
Swabey89 0:9df0dd20731a 19
Swabey89 0:9df0dd20731a 20 /* Bind the HTTP port (TCP 80) to the server */
Swabey89 0:9df0dd20731a 21 srv.bind(eth.get_ip_address(), 80);
Swabey89 0:9df0dd20731a 22
Swabey89 0:9df0dd20731a 23 /* Can handle 5 simultaneous connections */
Swabey89 0:9df0dd20731a 24 srv.listen(5);
Swabey89 0:9df0dd20731a 25
Swabey89 0:9df0dd20731a 26 while(true)
Swabey89 0:9df0dd20731a 27 {
Swabey89 0:9df0dd20731a 28 //Block and wait on an incoming connection
Swabey89 0:9df0dd20731a 29 srv.accept(&clt_sock, &clt_addr);
Swabey89 0:9df0dd20731a 30
Swabey89 0:9df0dd20731a 31 network_tout.attach(network_toutISR,TOUT_TIME_DEF);
Swabey89 0:9df0dd20731a 32
Swabey89 0:9df0dd20731a 33 if(logging)
Swabey89 0:9df0dd20731a 34 {
Swabey89 0:9df0dd20731a 35 printQueue.call(printf,"Incoming connection accepted on %s:%d\r\n", clt_addr.get_ip_address(), clt_addr.get_port());
Swabey89 0:9df0dd20731a 36 }
Swabey89 0:9df0dd20731a 37
Swabey89 0:9df0dd20731a 38 //Uses a C++ string to make it easier to concatinate
Swabey89 0:9df0dd20731a 39 {
Swabey89 0:9df0dd20731a 40 string response;
Swabey89 0:9df0dd20731a 41 char temp_str[10];
Swabey89 0:9df0dd20731a 42 char press_str[10];
Swabey89 0:9df0dd20731a 43 char light_str[10];
Swabey89 0:9df0dd20731a 44
Swabey89 0:9df0dd20731a 45 bufferLock.lock();
Swabey89 0:9df0dd20731a 46 double temp = buffer[newestIndex].gettemp();
Swabey89 0:9df0dd20731a 47 double press = buffer[newestIndex].getpress();
Swabey89 0:9df0dd20731a 48 float light = buffer[newestIndex].getlight()*100;
Swabey89 0:9df0dd20731a 49 string time_str = buffer[newestIndex].getTime();
Swabey89 0:9df0dd20731a 50 bufferLock.unlock();
Swabey89 0:9df0dd20731a 51
Swabey89 0:9df0dd20731a 52 //Convert to a C String
Swabey89 0:9df0dd20731a 53 sprintf(temp_str, "%5.2f", temp);
Swabey89 0:9df0dd20731a 54 sprintf(press_str, "%5.2f", press);
Swabey89 0:9df0dd20731a 55 sprintf(light_str, "%5.2f", light);
Swabey89 0:9df0dd20731a 56
Swabey89 0:9df0dd20731a 57
Swabey89 0:9df0dd20731a 58 //Build the C++ string response
Swabey89 0:9df0dd20731a 59 response = HTTP_RESPONSE;
Swabey89 0:9df0dd20731a 60
Swabey89 0:9df0dd20731a 61 response += HTTP_MESSAGE_TIME;
Swabey89 0:9df0dd20731a 62 response += time_str;
Swabey89 0:9df0dd20731a 63
Swabey89 0:9df0dd20731a 64 response += HTTP_MESSAGE_TEMP;
Swabey89 0:9df0dd20731a 65 response += temp_str;
Swabey89 0:9df0dd20731a 66
Swabey89 0:9df0dd20731a 67 response += HTTP_MESSAGE_PRESSURE;
Swabey89 0:9df0dd20731a 68 response += press_str;
Swabey89 0:9df0dd20731a 69
Swabey89 0:9df0dd20731a 70 response += HTTP_MESSAGE_LIGHT;
Swabey89 0:9df0dd20731a 71 response += light_str;
Swabey89 0:9df0dd20731a 72
Swabey89 0:9df0dd20731a 73 response += HTTP_MESSAGE_FOOT;
Swabey89 0:9df0dd20731a 74
Swabey89 0:9df0dd20731a 75 response += "\r\n";
Swabey89 0:9df0dd20731a 76
Swabey89 0:9df0dd20731a 77 //Send static HTML response (as a C string)
Swabey89 0:9df0dd20731a 78 clt_sock.send(response.c_str(), response.size());
Swabey89 0:9df0dd20731a 79
Swabey89 0:9df0dd20731a 80 if(logging)
Swabey89 0:9df0dd20731a 81 {
Swabey89 0:9df0dd20731a 82 printQueue.call(printf, "Network thread responded with time: %s, temperature: %s, pressure:%s and light level: %s\r\n\n",time_str, temp_str, press_str, light_str);
Swabey89 0:9df0dd20731a 83 }
Swabey89 0:9df0dd20731a 84 }
Swabey89 0:9df0dd20731a 85 network_tout.detach();
Swabey89 0:9df0dd20731a 86 }
Swabey89 0:9df0dd20731a 87 }
Swabey89 0:9df0dd20731a 88
Swabey89 0:9df0dd20731a 89 void network_toutISR(void)
Swabey89 0:9df0dd20731a 90 {
Swabey89 0:9df0dd20731a 91 threadstates |= NETWORK;
Swabey89 0:9df0dd20731a 92 }