Plymouth ELEC351 Group T / Mbed OS ELEC351

Dependencies:   BME280 BMP280 TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NETWORK.cpp Source File

NETWORK.cpp

00001 #include "NETWORK.hpp"
00002 #include <string>
00003 AnalogIn ldr(PA_0);
00004 
00005 //Now setup a web server
00006 TCPServer srv;           //TCP/IP Server
00007 TCPSocket clt_sock;      //Socket for communication
00008 SocketAddress clt_addr;  //Address of incoming connection
00009 string GateWay_IP;
00010 
00011 int Network_Init()
00012 { 
00013     //Configure an ethernet connection
00014     EthernetInterface eth;
00015     eth.set_network(IP, NETMASK, GATEWAY);
00016     eth.connect();
00017     printf("The target IP address is '%s'\n", eth.get_ip_address());
00018     GateWay_IP = eth.get_ip_address();
00019     
00020     if(GateWay_IP != "10.0.0.10")
00021     {
00022         return 1;
00023         //Error code here   
00024     }
00025     /* Open the server on ethernet stack */
00026     srv.open(&eth);
00027     
00028     /* Bind the HTTP port (TCP 80) to the server */
00029     srv.bind(eth.get_ip_address(), 80);
00030     
00031     /* Can handle 5 simultaneous connections */
00032     srv.listen(5);
00033     return 0;
00034 }
00035 void Networking()
00036 {    
00037         if(Log_Value==2){pc.printf("In Network Thread\n");}
00038         
00039         time_t Time = Data_Buffer[(Write_Pointer - 1)].get_time();
00040         tm* Time_Pointer = localtime(&Time);
00041         int temp_day = Time_Pointer->tm_mday;
00042         int temp_month = (Time_Pointer->tm_mon+1);//Set to current month
00043         int temp_year = (Time_Pointer->tm_year+1900);//Set to current year
00044         
00045         int temp_hours = Time_Pointer->tm_hour;
00046         int temp_minute = Time_Pointer->tm_min;
00047         int temp_seconds = Time_Pointer->tm_sec;
00048         
00049         float temp_temperature = Data_Buffer[(Write_Pointer - 1)].get_temperature();
00050         float temp_pressure = Data_Buffer[(Write_Pointer - 1)].get_pressure();
00051         float temp_light = Data_Buffer[(Write_Pointer - 1)].get_light();
00052         //using namespace std;
00053         //Block and wait on an incoming connection
00054         srv.accept(&clt_sock, &clt_addr);
00055         //printf("Networking connection accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port());
00056         //Uses a C++ string to make it easier to concatinate
00057         string response;
00058         //This is a C string
00059         char output1_str[64];
00060         char output2_str[64];
00061         char output3_str[64];
00062         char output4_str[64];
00063         char output5_str[64];
00064         char output6_str[64];
00065         char output7_str[64];
00066         char output8_str[64];
00067         char output9_str[64];
00068         
00069         //Convert to a C String
00070         sprintf(output1_str, "%02d/" , temp_day);//Print Day
00071         sprintf(output2_str, "%02d/" , temp_month);//Print Month
00072         sprintf(output3_str, "%d   " , temp_year);//Print Year
00073         sprintf(output4_str, "Time:%02d:" , temp_hours);//Print Hours
00074         sprintf(output5_str, "%02d:" , temp_minute);//Print Minute
00075         sprintf(output6_str, "%02d   " , temp_seconds);//Print Seconds
00076         sprintf(output7_str, "Temperature is : %1.1f   " , temp_temperature);//Print temperature
00077         sprintf(output8_str, "Pressure is : %1.1f   " , temp_pressure);//Print Pressure
00078         sprintf(output9_str, "Light is : %5.3f   " , temp_light);//Print Light level
00079         
00080         
00081         //Build the C++ string response
00082         response = HTTP_MESSAGE_BODY1;
00083         response +=output1_str;
00084         response +=output2_str;
00085         response +=output3_str;
00086         response +=output4_str;
00087         response +=output5_str;
00088         response +=output6_str;
00089         response +=output7_str;
00090         response +=output8_str;
00091         response +=output9_str;
00092         response += HTTP_MESSAGE_BODY2;
00093         
00094         
00095         if(Log_Value==2){pc.printf("Printing Network Data\n");}
00096         //Send static HTML response (as a C string)
00097         clt_sock.send(response.c_str(), response.size()+6);    
00098 }