Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of ELEC351 by
NETWORK.cpp@52:99915f5240b2, 2018-01-09 (annotated)
- Committer:
- thomasmorris
- Date:
- Tue Jan 09 22:27:49 2018 +0000
- Revision:
- 52:99915f5240b2
- Parent:
- 48:244d6d81bb52
ITS THE FINAL COMMIT MESSAGE DO DO DO DO DO
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| thomasmorris | 35:26b0a9b55d82 | 1 | #include "NETWORK.hpp" |
| thomasmorris | 35:26b0a9b55d82 | 2 | |
| thomasmorris | 35:26b0a9b55d82 | 3 | //Now setup a web server |
| thomasmorris | 35:26b0a9b55d82 | 4 | TCPServer srv; //TCP/IP Server |
| thomasmorris | 35:26b0a9b55d82 | 5 | TCPSocket clt_sock; //Socket for communication |
| thomasmorris | 35:26b0a9b55d82 | 6 | SocketAddress clt_addr; //Address of incoming connection |
| thomasmorris | 37:7c4d7f206039 | 7 | string GateWay_IP; |
| thomasmorris | 35:26b0a9b55d82 | 8 | |
| thomasmorris | 52:99915f5240b2 | 9 | //Mutex Locks |
| thomasmorris | 52:99915f5240b2 | 10 | |
| thomasmorris | 52:99915f5240b2 | 11 | Mutex NETWORK_Lock; |
| thomasmorris | 37:7c4d7f206039 | 12 | int Network_Init() |
| thomasmorris | 35:26b0a9b55d82 | 13 | { |
| thomasmorris | 35:26b0a9b55d82 | 14 | //Configure an ethernet connection |
| thomasmorris | 35:26b0a9b55d82 | 15 | EthernetInterface eth; |
| thomasmorris | 35:26b0a9b55d82 | 16 | eth.set_network(IP, NETMASK, GATEWAY); |
| thomasmorris | 35:26b0a9b55d82 | 17 | eth.connect(); |
| thomasmorris | 35:26b0a9b55d82 | 18 | printf("The target IP address is '%s'\n", eth.get_ip_address()); |
| thomasmorris | 37:7c4d7f206039 | 19 | GateWay_IP = eth.get_ip_address(); |
| thomasmorris | 37:7c4d7f206039 | 20 | |
| thomasmorris | 37:7c4d7f206039 | 21 | if(GateWay_IP != "10.0.0.10") |
| thomasmorris | 37:7c4d7f206039 | 22 | { |
| thomasmorris | 52:99915f5240b2 | 23 | return 1;//Makes an Error |
| thomasmorris | 37:7c4d7f206039 | 24 | } |
| thomasmorris | 35:26b0a9b55d82 | 25 | /* Open the server on ethernet stack */ |
| thomasmorris | 35:26b0a9b55d82 | 26 | srv.open(ð); |
| thomasmorris | 35:26b0a9b55d82 | 27 | |
| thomasmorris | 35:26b0a9b55d82 | 28 | /* Bind the HTTP port (TCP 80) to the server */ |
| thomasmorris | 35:26b0a9b55d82 | 29 | srv.bind(eth.get_ip_address(), 80); |
| thomasmorris | 35:26b0a9b55d82 | 30 | |
| thomasmorris | 35:26b0a9b55d82 | 31 | /* Can handle 5 simultaneous connections */ |
| thomasmorris | 35:26b0a9b55d82 | 32 | srv.listen(5); |
| thomasmorris | 37:7c4d7f206039 | 33 | return 0; |
| thomasmorris | 35:26b0a9b55d82 | 34 | } |
| thomasmorris | 47:6d128e500875 | 35 | void Networking() |
| thomasmorris | 35:26b0a9b55d82 | 36 | { |
| thomasmorris | 52:99915f5240b2 | 37 | if(Log_Value==2){pc.printf("In Network Thread\n");} //Logging statement for debugging |
| thomasmorris | 47:6d128e500875 | 38 | |
| thomasmorris | 52:99915f5240b2 | 39 | NETWORK_Lock.lock(); //Apply MUTEX Lock |
| thomasmorris | 52:99915f5240b2 | 40 | time_t Time = Data_Buffer[(Write_Pointer - 1)].get_time(); //Get current Data |
| thomasmorris | 52:99915f5240b2 | 41 | float temp_temperature = Data_Buffer[(Write_Pointer - 1)].get_temperature();//Set to current temperature |
| thomasmorris | 52:99915f5240b2 | 42 | float temp_pressure = Data_Buffer[(Write_Pointer - 1)].get_pressure(); //Set to current pressure |
| thomasmorris | 52:99915f5240b2 | 43 | float temp_light = Data_Buffer[(Write_Pointer - 1)].get_light(); //Set to current light level |
| thomasmorris | 52:99915f5240b2 | 44 | NETWORK_Lock.unlock(); //Release MUTEX Lock |
| thomasmorris | 47:6d128e500875 | 45 | |
| thomasmorris | 52:99915f5240b2 | 46 | tm* Time_Pointer = localtime(&Time); |
| thomasmorris | 52:99915f5240b2 | 47 | int temp_day = Time_Pointer->tm_mday; //Set to current day |
| thomasmorris | 52:99915f5240b2 | 48 | int temp_month = (Time_Pointer->tm_mon+1); //Set to current month |
| thomasmorris | 52:99915f5240b2 | 49 | int temp_year = (Time_Pointer->tm_year+1900); //Set to current year |
| thomasmorris | 47:6d128e500875 | 50 | |
| thomasmorris | 52:99915f5240b2 | 51 | int temp_hours = Time_Pointer->tm_hour; //Set to current hours |
| thomasmorris | 52:99915f5240b2 | 52 | int temp_minute = Time_Pointer->tm_min; //Set to current minute |
| thomasmorris | 52:99915f5240b2 | 53 | int temp_seconds = Time_Pointer->tm_sec; //Set to current seconds |
| thomasmorris | 52:99915f5240b2 | 54 | |
| thomasmorris | 35:26b0a9b55d82 | 55 | //Block and wait on an incoming connection |
| thomasmorris | 35:26b0a9b55d82 | 56 | srv.accept(&clt_sock, &clt_addr); |
| thomasmorris | 52:99915f5240b2 | 57 | |
| thomasmorris | 35:26b0a9b55d82 | 58 | //Uses a C++ string to make it easier to concatinate |
| thomasmorris | 35:26b0a9b55d82 | 59 | string response; |
| thomasmorris | 52:99915f5240b2 | 60 | //These are C strings |
| thomasmorris | 36:a0098306fc58 | 61 | char output1_str[64]; |
| thomasmorris | 36:a0098306fc58 | 62 | char output2_str[64]; |
| thomasmorris | 36:a0098306fc58 | 63 | char output3_str[64]; |
| thomasmorris | 36:a0098306fc58 | 64 | char output4_str[64]; |
| thomasmorris | 36:a0098306fc58 | 65 | char output5_str[64]; |
| thomasmorris | 36:a0098306fc58 | 66 | char output6_str[64]; |
| thomasmorris | 36:a0098306fc58 | 67 | char output7_str[64]; |
| thomasmorris | 36:a0098306fc58 | 68 | char output8_str[64]; |
| thomasmorris | 45:875f7e18a386 | 69 | char output9_str[64]; |
| thomasmorris | 36:a0098306fc58 | 70 | |
| thomasmorris | 36:a0098306fc58 | 71 | //Convert to a C String |
| thomasmorris | 52:99915f5240b2 | 72 | sprintf(output1_str, "%02d/" , temp_day); //Print Day |
| thomasmorris | 52:99915f5240b2 | 73 | sprintf(output2_str, "%02d/" , temp_month); //Print Month |
| thomasmorris | 52:99915f5240b2 | 74 | sprintf(output3_str, "%d " , temp_year); //Print Year |
| thomasmorris | 52:99915f5240b2 | 75 | sprintf(output4_str, "Time:%02d:" , temp_hours); //Print Hours |
| thomasmorris | 52:99915f5240b2 | 76 | sprintf(output5_str, "%02d:" , temp_minute); //Print Minute |
| thomasmorris | 52:99915f5240b2 | 77 | sprintf(output6_str, "%02d " , temp_seconds); //Print Seconds |
| thomasmorris | 52:99915f5240b2 | 78 | sprintf(output7_str, "Temperature is : %1.1f " , temp_temperature); //Print temperature |
| thomasmorris | 52:99915f5240b2 | 79 | sprintf(output8_str, "Pressure is : %1.1f " , temp_pressure); //Print Pressure |
| thomasmorris | 52:99915f5240b2 | 80 | sprintf(output9_str, "Light is : %5.3f " , temp_light); //Print Light level |
| thomasmorris | 35:26b0a9b55d82 | 81 | |
| thomasmorris | 35:26b0a9b55d82 | 82 | |
| thomasmorris | 35:26b0a9b55d82 | 83 | //Build the C++ string response |
| thomasmorris | 52:99915f5240b2 | 84 | response = HTTP_MESSAGE_BODY1; //Begin HTML Code |
| thomasmorris | 52:99915f5240b2 | 85 | response +=output1_str; //Print Day |
| thomasmorris | 52:99915f5240b2 | 86 | response +=output2_str; //Print Month |
| thomasmorris | 52:99915f5240b2 | 87 | response +=output3_str; //Print Year |
| thomasmorris | 52:99915f5240b2 | 88 | response +=output4_str; //Print Hours |
| thomasmorris | 52:99915f5240b2 | 89 | response +=output5_str; //Print Minute |
| thomasmorris | 52:99915f5240b2 | 90 | response +=output6_str; //Print Seconds |
| thomasmorris | 52:99915f5240b2 | 91 | response +=output7_str; //Print temperature |
| thomasmorris | 52:99915f5240b2 | 92 | response +=output8_str; //Print Pressure |
| thomasmorris | 52:99915f5240b2 | 93 | response +=output9_str; //Print Light level |
| thomasmorris | 52:99915f5240b2 | 94 | response += HTTP_MESSAGE_BODY2; //End HTML Code |
| thomasmorris | 35:26b0a9b55d82 | 95 | |
| thomasmorris | 48:244d6d81bb52 | 96 | |
| thomasmorris | 48:244d6d81bb52 | 97 | if(Log_Value==2){pc.printf("Printing Network Data\n");} |
| thomasmorris | 35:26b0a9b55d82 | 98 | //Send static HTML response (as a C string) |
| thomasmorris | 52:99915f5240b2 | 99 | clt_sock.send(response.c_str(), response.size()+6); //Send Response to Network |
| thomasmorris | 35:26b0a9b55d82 | 100 | } |
