Plymouth ELEC351 Group T / Mbed OS ELEC351

Dependencies:   BME280 BMP280 TextLCD

Committer:
thomasmorris
Date:
Sun Jan 07 19:21:11 2018 +0000
Revision:
37:7c4d7f206039
Parent:
36:a0098306fc58
Child:
41:859b5e1e3d9a
Working Revision network working;

Who changed what in which revision?

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