V4
Dependencies: BMP280
Fork of Thread_Communication_V4 by
Diff: main.cpp
- Revision:
- 12:a244f6f9d2fe
- Parent:
- 11:19135c83c208
- Child:
- 13:089f3adb3813
diff -r 19135c83c208 -r a244f6f9d2fe main.cpp --- a/main.cpp Thu Jan 04 20:33:17 2018 +0000 +++ b/main.cpp Fri Jan 05 12:01:04 2018 +0000 @@ -115,8 +115,15 @@ //Format samples, send to FIFO buffer head memset(data_buffer[sample_h],NULL,64); + char sampleTime[20]; + memset(sampleTime, NULL,20); + time( &raw_time ); + sample_epoch = localtime( &raw_time ); + strftime(sampleTime,20,"%d/%m/%Y, %X",sample_epoch); + + dataLock.lock(); //lock critical section - sprintf(data_buffer[sample_h],"%s, %2.2f, %4.2f, %.4f\n\r", TIME, TEMP, PRES, LDR); + sprintf(data_buffer[sample_h],"%s, %2.2f, %4.2f, %.4f\n\r", sampleTime, TEMP, PRES, LDR); dataLock.unlock(); //unlock critical section @@ -160,12 +167,6 @@ TEMP = Sensor.getTemperature(); PRES = Sensor.getPressure(); - - memset(TIME, NULL,20); - time( &raw_time ); - sample_epoch = localtime( &raw_time ); - strftime(TIME,20,"%d/%m/%Y, %X",sample_epoch); - dataLock.unlock(); // Exiting Critical Section Green_int = !Green_int; // debugging @@ -468,7 +469,7 @@ pc.puts("\tREAD ALL - Read All previous samples held in memory\n\r"); pc.puts("\tSETTIME hh:mm::ss - Set time in 24hr format\n\r"); pc.puts("\tSETDATE dd/mm/yyyy - Set time in specified format\n\r"); - pc.puts("\tDELETE ALL - Delete all sampled held in internal memory\n\n\n\r"); + pc.puts("\tDELETE ALL - Delete all sampled held in internal memory\n\r"); pc.puts("\tSETT - Set sample period. must be an integer in range 0 < T < 61\n\r"); } else if (strstr(rx_buffer,"tell me a joke")) { @@ -575,10 +576,11 @@ /*--------------------------------------------------------------------------*/ /*---------------------------Networking Thread------------------------------*/ -void Network1 () { - +void Network1 () +{ + printf("Setting up server\n\r"); - + //Configure an ethernet connection EthernetInterface eth; eth.set_network(IP, NETMASK, GATEWAY); @@ -586,79 +588,76 @@ if (eth.get_ip_address() == NULL) { pc.printf("Error - Can't get IP. Network not setup\n\r"); pc.printf("Reset Required. Make sure network cables are plugged in\n\r"); - } - else { - printf("The target IP address is '%s'\n\r", eth.get_ip_address()); - - //Now setup a web server - TCPServer srv; //TCP/IP Server - - SocketAddress clt_addr; //Address of incoming connection - - /* Open the server on ethernet stack */ - srv.open(ð); - - /* Bind the HTTP port (TCP 80) to the server */ - srv.bind(eth.get_ip_address(), 80); - - /* Can handle 5 simultaneous connections */ - srv.listen(5); - - TCPSocket clt_sock; //Socket for communication - pc.printf("Server Ready\n\r"); - while (true) { - - - using namespace std; - //Block and wait on an incoming connection - srv.accept(&clt_sock, &clt_addr); - //printf("accept %s:%d\n\r", clt_addr.get_ip_address(), clt_addr.get_port()); - - //Uses a C++ string to make it easier to concatinate - string response; - string strL = "LDR:"; - string strP = ", Pressure(mBar): "; - string strT = ", Temp(C): "; - string strDT = ", Date/Time: "; + } + else { + printf("The target IP address is '%s'\n\r", eth.get_ip_address()); + + //Now setup a web server + TCPServer srv; //TCP/IP Server + + SocketAddress clt_addr; //Address of incoming connection + + /* Open the server on ethernet stack */ + srv.open(ð); + + /* Bind the HTTP port (TCP 80) to the server */ + srv.bind(eth.get_ip_address(), 80); + + /* Can handle 5 simultaneous connections */ + srv.listen(5); + + TCPSocket clt_sock; //Socket for communication + pc.printf("Server Ready\n\r"); - //This is a C string - char l_str[64]; - char p_str[64]; - char t_str[64]; - - //Read the LDR value - dataLock.lock(); // add watchdog? - float L = LDR ; - float T = TEMP; - float P = PRES; - char DT[20] = TIME; - dataLock.unlock(); - - //Convert to a C String - sprintf(l_str, "%1.3f", L ); // try \n\r?? - sprintf(t_str, "%2.2f", T); - sprintf(p_str, "%4.2f", P); - - - - //Build the C++ string response - response = HTTP_MESSAGE_BODY1; - // response += strL; - response += l_str; - response += strT; - response += t_str; - response += strP; - response += p_str; - response += strDT; - response += DT; - response += HTTP_MESSAGE_BODY2; - - //Send static HTML response (as a C string) - clt_sock.send(response.c_str(), response.size()+6); - - } - } -} + while (true) { + + using namespace std; + //Block and wait on an incoming connection + srv.accept(&clt_sock, &clt_addr); + //printf("accept %s:%d\n\r", clt_addr.get_ip_address(), clt_addr.get_port()); + + //Uses a C++ string to make it easier to concatinate + string response; + string strL = "LDR:"; + string strP = ", Pressure(mBar): "; + string strT = ", Temp(C): "; + + //This is a C string + char l_str[64]; + char p_str[64]; + char t_str[64]; + + //Read the LDR value + dataLock.lock(); // add watchdog? + float L = LDR ; + float T = TEMP; + float P = PRES; + + dataLock.unlock(); + + //Convert to a C String + sprintf(l_str, "%1.3f", L ); // try \n\r?? + sprintf(t_str, "%2.2f", T); + sprintf(p_str, "%4.2f", P); + + + + //Build the C++ string response + response = HTTP_MESSAGE_BODY1; + // response += strL; + response += l_str; + response += strT; + response += t_str; + response += strP; + response += p_str; + response += HTTP_MESSAGE_BODY2; + + //Send static HTML response (as a C string) + clt_sock.send(response.c_str(), response.size()+6); + + }// end While + }//end if +}// end thread /*---------------------------POST--------------------------------------------*/ void POST () { pc.printf(" Basic POST\n\r");