Plymouth ELEC351 Group T / Mbed OS ELEC351_Group_T

Fork of ELEC351 by Plymouth ELEC351 Group T

Committer:
thomasmorris
Date:
Sun Jan 07 16:17:24 2018 +0000
Revision:
36:a0098306fc58
Parent:
35:26b0a9b55d82
Child:
37:7c4d7f206039
Working Network

Who changed what in which revision?

UserRevisionLine numberNew contents of line
thomasmorris 35:26b0a9b55d82 1 #include "NETWORK.hpp"
thomasmorris 35:26b0a9b55d82 2
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 35:26b0a9b55d82 9
thomasmorris 35:26b0a9b55d82 10
thomasmorris 35:26b0a9b55d82 11 void Network_Init()
thomasmorris 35:26b0a9b55d82 12 {
thomasmorris 35:26b0a9b55d82 13 printf("Basic HTTP server example\n");
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 35:26b0a9b55d82 19 //t1.start(Network_Thread);
thomasmorris 35:26b0a9b55d82 20 /* Open the server on ethernet stack */
thomasmorris 35:26b0a9b55d82 21 srv.open(&eth);
thomasmorris 35:26b0a9b55d82 22
thomasmorris 35:26b0a9b55d82 23 /* Bind the HTTP port (TCP 80) to the server */
thomasmorris 35:26b0a9b55d82 24 srv.bind(eth.get_ip_address(), 80);
thomasmorris 35:26b0a9b55d82 25
thomasmorris 35:26b0a9b55d82 26 /* Can handle 5 simultaneous connections */
thomasmorris 35:26b0a9b55d82 27 srv.listen(5);
thomasmorris 35:26b0a9b55d82 28 }
thomasmorris 36:a0098306fc58 29 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 30 {
thomasmorris 35:26b0a9b55d82 31 //using namespace std;
thomasmorris 35:26b0a9b55d82 32 //Block and wait on an incoming connection
thomasmorris 35:26b0a9b55d82 33 srv.accept(&clt_sock, &clt_addr);
thomasmorris 35:26b0a9b55d82 34 printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port());
thomasmorris 36:a0098306fc58 35 printf("In networking");
thomasmorris 35:26b0a9b55d82 36 //Uses a C++ string to make it easier to concatinate
thomasmorris 35:26b0a9b55d82 37 string response;
thomasmorris 35:26b0a9b55d82 38 //This is a C string
thomasmorris 36:a0098306fc58 39 char output1_str[64];
thomasmorris 36:a0098306fc58 40 char output2_str[64];
thomasmorris 36:a0098306fc58 41 char output3_str[64];
thomasmorris 36:a0098306fc58 42 char output4_str[64];
thomasmorris 36:a0098306fc58 43 char output5_str[64];
thomasmorris 36:a0098306fc58 44 char output6_str[64];
thomasmorris 36:a0098306fc58 45 char output7_str[64];
thomasmorris 36:a0098306fc58 46 char output8_str[64];
thomasmorris 36:a0098306fc58 47
thomasmorris 35:26b0a9b55d82 48
thomasmorris 36:a0098306fc58 49
thomasmorris 36:a0098306fc58 50 //Convert to a C String
thomasmorris 36:a0098306fc58 51 sprintf(output1_str, "%d/" , network_day);
thomasmorris 36:a0098306fc58 52 sprintf(output2_str, "%d/" , network_month);
thomasmorris 36:a0098306fc58 53 sprintf(output3_str, "%d " , network_year);
thomasmorris 36:a0098306fc58 54 sprintf(output4_str, "Time:%d:" , network_hours);
thomasmorris 36:a0098306fc58 55 sprintf(output5_str, "%d " , network_minute);
thomasmorris 36:a0098306fc58 56 sprintf(output6_str, "Temperature is : %1.1f " , network_temperature);
thomasmorris 36:a0098306fc58 57 sprintf(output7_str, "Pressure is : %1.1f " , network_pressure);
thomasmorris 36:a0098306fc58 58 sprintf(output8_str, "Light is : %5.3f " , network_light);
thomasmorris 35:26b0a9b55d82 59
thomasmorris 35:26b0a9b55d82 60
thomasmorris 35:26b0a9b55d82 61 //Build the C++ string response
thomasmorris 35:26b0a9b55d82 62 response = HTTP_MESSAGE_BODY1;
thomasmorris 36:a0098306fc58 63 response +=output1_str;
thomasmorris 36:a0098306fc58 64 response +=output2_str;
thomasmorris 36:a0098306fc58 65 response +=output3_str;
thomasmorris 36:a0098306fc58 66 response +=output4_str;
thomasmorris 36:a0098306fc58 67 response +=output5_str;
thomasmorris 36:a0098306fc58 68 response +=output6_str;
thomasmorris 36:a0098306fc58 69 response +=output7_str;
thomasmorris 36:a0098306fc58 70 response +=output8_str;
thomasmorris 35:26b0a9b55d82 71 response += HTTP_MESSAGE_BODY2;
thomasmorris 35:26b0a9b55d82 72
thomasmorris 35:26b0a9b55d82 73 //Send static HTML response (as a C string)
thomasmorris 35:26b0a9b55d82 74 clt_sock.send(response.c_str(), response.size()+6);
thomasmorris 35:26b0a9b55d82 75 }