networking library files

Dependents:   ELEC350_Project2

Committer:
Swabey89
Date:
Fri Dec 28 12:04:38 2018 +0000
Revision:
9:e22fd5795831
Parent:
8:8d2f71a08a31
Child:
10:b8214cc88e85
Updates

Who changed what in which revision?

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