Time: 17:33 Date: 10/12/2017 Description: Task 1,7,8 Currently Functioning
Dependencies: BME280 BMP280 TextLCD
Working Repository
NETWORK.cpp@48:244d6d81bb52, 2018-01-09 (annotated)
- Committer:
- thomasmorris
- Date:
- Tue Jan 09 00:37:01 2018 +0000
- Revision:
- 48:244d6d81bb52
- Parent:
- 47:6d128e500875
HOLY SHIT IT WORKS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
thomasmorris | 35:26b0a9b55d82 | 1 | #include "NETWORK.hpp" |
thomasmorris | 37:7c4d7f206039 | 2 | #include <string> |
thomasmorris | 35:26b0a9b55d82 | 3 | AnalogIn ldr(PA_0); |
thomasmorris | 35:26b0a9b55d82 | 4 | |
thomasmorris | 35:26b0a9b55d82 | 5 | //Now setup a web server |
thomasmorris | 35:26b0a9b55d82 | 6 | TCPServer srv; //TCP/IP Server |
thomasmorris | 35:26b0a9b55d82 | 7 | TCPSocket clt_sock; //Socket for communication |
thomasmorris | 35:26b0a9b55d82 | 8 | SocketAddress clt_addr; //Address of incoming connection |
thomasmorris | 37:7c4d7f206039 | 9 | string GateWay_IP; |
thomasmorris | 35:26b0a9b55d82 | 10 | |
thomasmorris | 37:7c4d7f206039 | 11 | int Network_Init() |
thomasmorris | 35:26b0a9b55d82 | 12 | { |
thomasmorris | 35:26b0a9b55d82 | 13 | //Configure an ethernet connection |
thomasmorris | 35:26b0a9b55d82 | 14 | EthernetInterface eth; |
thomasmorris | 35:26b0a9b55d82 | 15 | eth.set_network(IP, NETMASK, GATEWAY); |
thomasmorris | 35:26b0a9b55d82 | 16 | eth.connect(); |
thomasmorris | 35:26b0a9b55d82 | 17 | printf("The target IP address is '%s'\n", eth.get_ip_address()); |
thomasmorris | 37:7c4d7f206039 | 18 | GateWay_IP = eth.get_ip_address(); |
thomasmorris | 37:7c4d7f206039 | 19 | |
thomasmorris | 37:7c4d7f206039 | 20 | if(GateWay_IP != "10.0.0.10") |
thomasmorris | 37:7c4d7f206039 | 21 | { |
thomasmorris | 37:7c4d7f206039 | 22 | return 1; |
thomasmorris | 37:7c4d7f206039 | 23 | //Error code here |
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 | 48:244d6d81bb52 | 37 | if(Log_Value==2){pc.printf("In Network Thread\n");} |
thomasmorris | 47:6d128e500875 | 38 | |
thomasmorris | 47:6d128e500875 | 39 | time_t Time = Data_Buffer[(Write_Pointer - 1)].get_time(); |
thomasmorris | 47:6d128e500875 | 40 | tm* Time_Pointer = localtime(&Time); |
thomasmorris | 47:6d128e500875 | 41 | int temp_day = Time_Pointer->tm_mday; |
thomasmorris | 47:6d128e500875 | 42 | int temp_month = (Time_Pointer->tm_mon+1);//Set to current month |
thomasmorris | 47:6d128e500875 | 43 | int temp_year = (Time_Pointer->tm_year+1900);//Set to current year |
thomasmorris | 47:6d128e500875 | 44 | |
thomasmorris | 47:6d128e500875 | 45 | int temp_hours = Time_Pointer->tm_hour; |
thomasmorris | 47:6d128e500875 | 46 | int temp_minute = Time_Pointer->tm_min; |
thomasmorris | 47:6d128e500875 | 47 | int temp_seconds = Time_Pointer->tm_sec; |
thomasmorris | 47:6d128e500875 | 48 | |
thomasmorris | 47:6d128e500875 | 49 | float temp_temperature = Data_Buffer[(Write_Pointer - 1)].get_temperature(); |
thomasmorris | 47:6d128e500875 | 50 | float temp_pressure = Data_Buffer[(Write_Pointer - 1)].get_pressure(); |
thomasmorris | 47:6d128e500875 | 51 | float temp_light = Data_Buffer[(Write_Pointer - 1)].get_light(); |
thomasmorris | 35:26b0a9b55d82 | 52 | //using namespace std; |
thomasmorris | 35:26b0a9b55d82 | 53 | //Block and wait on an incoming connection |
thomasmorris | 35:26b0a9b55d82 | 54 | srv.accept(&clt_sock, &clt_addr); |
thomasmorris | 45:875f7e18a386 | 55 | //printf("Networking connection accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port()); |
thomasmorris | 35:26b0a9b55d82 | 56 | //Uses a C++ string to make it easier to concatinate |
thomasmorris | 35:26b0a9b55d82 | 57 | string response; |
thomasmorris | 35:26b0a9b55d82 | 58 | //This is a C string |
thomasmorris | 36:a0098306fc58 | 59 | char output1_str[64]; |
thomasmorris | 36:a0098306fc58 | 60 | char output2_str[64]; |
thomasmorris | 36:a0098306fc58 | 61 | char output3_str[64]; |
thomasmorris | 36:a0098306fc58 | 62 | char output4_str[64]; |
thomasmorris | 36:a0098306fc58 | 63 | char output5_str[64]; |
thomasmorris | 36:a0098306fc58 | 64 | char output6_str[64]; |
thomasmorris | 36:a0098306fc58 | 65 | char output7_str[64]; |
thomasmorris | 36:a0098306fc58 | 66 | char output8_str[64]; |
thomasmorris | 45:875f7e18a386 | 67 | char output9_str[64]; |
thomasmorris | 36:a0098306fc58 | 68 | |
thomasmorris | 36:a0098306fc58 | 69 | //Convert to a C String |
thomasmorris | 47:6d128e500875 | 70 | sprintf(output1_str, "%02d/" , temp_day);//Print Day |
thomasmorris | 47:6d128e500875 | 71 | sprintf(output2_str, "%02d/" , temp_month);//Print Month |
thomasmorris | 47:6d128e500875 | 72 | sprintf(output3_str, "%d " , temp_year);//Print Year |
thomasmorris | 47:6d128e500875 | 73 | sprintf(output4_str, "Time:%02d:" , temp_hours);//Print Hours |
thomasmorris | 47:6d128e500875 | 74 | sprintf(output5_str, "%02d:" , temp_minute);//Print Minute |
thomasmorris | 47:6d128e500875 | 75 | sprintf(output6_str, "%02d " , temp_seconds);//Print Seconds |
thomasmorris | 47:6d128e500875 | 76 | sprintf(output7_str, "Temperature is : %1.1f " , temp_temperature);//Print temperature |
thomasmorris | 47:6d128e500875 | 77 | sprintf(output8_str, "Pressure is : %1.1f " , temp_pressure);//Print Pressure |
thomasmorris | 47:6d128e500875 | 78 | sprintf(output9_str, "Light is : %5.3f " , temp_light);//Print Light level |
thomasmorris | 35:26b0a9b55d82 | 79 | |
thomasmorris | 35:26b0a9b55d82 | 80 | |
thomasmorris | 35:26b0a9b55d82 | 81 | //Build the C++ string response |
thomasmorris | 35:26b0a9b55d82 | 82 | response = HTTP_MESSAGE_BODY1; |
thomasmorris | 36:a0098306fc58 | 83 | response +=output1_str; |
thomasmorris | 36:a0098306fc58 | 84 | response +=output2_str; |
thomasmorris | 36:a0098306fc58 | 85 | response +=output3_str; |
thomasmorris | 36:a0098306fc58 | 86 | response +=output4_str; |
thomasmorris | 36:a0098306fc58 | 87 | response +=output5_str; |
thomasmorris | 36:a0098306fc58 | 88 | response +=output6_str; |
thomasmorris | 36:a0098306fc58 | 89 | response +=output7_str; |
thomasmorris | 36:a0098306fc58 | 90 | response +=output8_str; |
thomasmorris | 45:875f7e18a386 | 91 | response +=output9_str; |
thomasmorris | 35:26b0a9b55d82 | 92 | response += HTTP_MESSAGE_BODY2; |
thomasmorris | 35:26b0a9b55d82 | 93 | |
thomasmorris | 48:244d6d81bb52 | 94 | |
thomasmorris | 48:244d6d81bb52 | 95 | if(Log_Value==2){pc.printf("Printing Network Data\n");} |
thomasmorris | 35:26b0a9b55d82 | 96 | //Send static HTML response (as a C string) |
thomasmorris | 35:26b0a9b55d82 | 97 | clt_sock.send(response.c_str(), response.size()+6); |
thomasmorris | 35:26b0a9b55d82 | 98 | } |