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@41:859b5e1e3d9a, 2018-01-08 (annotated)
- Committer:
- thomasmorris
- Date:
- Mon Jan 08 14:20:30 2018 +0000
- Revision:
- 41:859b5e1e3d9a
- Parent:
- 37:7c4d7f206039
- Child:
- 45:875f7e18a386
Working Looping SD card;
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 | 36:a0098306fc58 | 35 | void Networking(int network_day, int network_month, int network_year, int network_hours, int network_minute, float network_temperature, float network_pressure, float network_light) |
thomasmorris | 35:26b0a9b55d82 | 36 | { |
thomasmorris | 35:26b0a9b55d82 | 37 | //using namespace std; |
thomasmorris | 35:26b0a9b55d82 | 38 | //Block and wait on an incoming connection |
thomasmorris | 35:26b0a9b55d82 | 39 | srv.accept(&clt_sock, &clt_addr); |
thomasmorris | 41:859b5e1e3d9a | 40 | printf("Networking connection accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port()); |
thomasmorris | 35:26b0a9b55d82 | 41 | //Uses a C++ string to make it easier to concatinate |
thomasmorris | 35:26b0a9b55d82 | 42 | string response; |
thomasmorris | 35:26b0a9b55d82 | 43 | //This is a C string |
thomasmorris | 36:a0098306fc58 | 44 | char output1_str[64]; |
thomasmorris | 36:a0098306fc58 | 45 | char output2_str[64]; |
thomasmorris | 36:a0098306fc58 | 46 | char output3_str[64]; |
thomasmorris | 36:a0098306fc58 | 47 | char output4_str[64]; |
thomasmorris | 36:a0098306fc58 | 48 | char output5_str[64]; |
thomasmorris | 36:a0098306fc58 | 49 | char output6_str[64]; |
thomasmorris | 36:a0098306fc58 | 50 | char output7_str[64]; |
thomasmorris | 36:a0098306fc58 | 51 | char output8_str[64]; |
thomasmorris | 36:a0098306fc58 | 52 | |
thomasmorris | 36:a0098306fc58 | 53 | //Convert to a C String |
thomasmorris | 41:859b5e1e3d9a | 54 | sprintf(output1_str, "%d/" , network_day);//Print Day |
thomasmorris | 41:859b5e1e3d9a | 55 | sprintf(output2_str, "%d/" , network_month);//Print Month |
thomasmorris | 41:859b5e1e3d9a | 56 | sprintf(output3_str, "%d " , network_year);//Print Year |
thomasmorris | 41:859b5e1e3d9a | 57 | sprintf(output4_str, "Time:%d:" , network_hours);//Print Hours |
thomasmorris | 41:859b5e1e3d9a | 58 | sprintf(output5_str, "%d " , network_minute);//Print Minute |
thomasmorris | 41:859b5e1e3d9a | 59 | sprintf(output6_str, "Temperature is : %1.1f " , network_temperature);//Print temperature |
thomasmorris | 41:859b5e1e3d9a | 60 | sprintf(output7_str, "Pressure is : %1.1f " , network_pressure);//Print Pressure |
thomasmorris | 41:859b5e1e3d9a | 61 | sprintf(output8_str, "Light is : %5.3f " , network_light);//Print Light level |
thomasmorris | 35:26b0a9b55d82 | 62 | |
thomasmorris | 35:26b0a9b55d82 | 63 | |
thomasmorris | 35:26b0a9b55d82 | 64 | //Build the C++ string response |
thomasmorris | 35:26b0a9b55d82 | 65 | response = HTTP_MESSAGE_BODY1; |
thomasmorris | 36:a0098306fc58 | 66 | response +=output1_str; |
thomasmorris | 36:a0098306fc58 | 67 | response +=output2_str; |
thomasmorris | 36:a0098306fc58 | 68 | response +=output3_str; |
thomasmorris | 36:a0098306fc58 | 69 | response +=output4_str; |
thomasmorris | 36:a0098306fc58 | 70 | response +=output5_str; |
thomasmorris | 36:a0098306fc58 | 71 | response +=output6_str; |
thomasmorris | 36:a0098306fc58 | 72 | response +=output7_str; |
thomasmorris | 36:a0098306fc58 | 73 | response +=output8_str; |
thomasmorris | 35:26b0a9b55d82 | 74 | response += HTTP_MESSAGE_BODY2; |
thomasmorris | 35:26b0a9b55d82 | 75 | |
thomasmorris | 35:26b0a9b55d82 | 76 | //Send static HTML response (as a C string) |
thomasmorris | 35:26b0a9b55d82 | 77 | clt_sock.send(response.c_str(), response.size()+6); |
thomasmorris | 35:26b0a9b55d82 | 78 | } |