Network, SD card, Serial, LCD and sensors all work! :) ** Don't Press the User Button without an SD Card inserted!! **
Dependencies: BMP280
Fork of Thread_Communication_V2 by
Diff: main.cpp
- Revision:
- 9:b838c5787ed7
- Parent:
- 8:ab6322afa341
- Child:
- 10:c10d1337d754
--- a/main.cpp Fri Dec 29 17:50:30 2017 +0000 +++ b/main.cpp Sat Dec 30 15:12:09 2017 +0000 @@ -17,7 +17,7 @@ void readISR(); void circBuff(); void writeRemove_SD(); - +void Network1(); // USER_BUTTON ISRs (Debounce) @@ -37,6 +37,7 @@ Thread _PrintLCD, _serialCMD, _circBuff; Thread _sensorRead (osPriorityRealtime); //dataLock Thread Thread _writeRemove_SD; +Thread _Network1; /* GLOBAL DATA */ volatile float LDR = 0; @@ -86,6 +87,7 @@ _sensorRead.start(sensorRead); _circBuff.start(circBuff); _writeRemove_SD.start(writeRemove_SD); + _Network1.start(Network1); userButton.rise(&userButtonRise); read.attach(readISR, SAMPLING_PERIOD); @@ -546,3 +548,72 @@ }// End While }// End Thread +void Network1 () { + + printf("Basic HTTP server example\n"); + + //Configure an ethernet connection + EthernetInterface eth; + eth.set_network(IP, NETMASK, GATEWAY); + eth.connect(); + printf("The target IP address is '%s'\n", 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); + + while (true) { + + TCPSocket clt_sock; //Socket for communication + 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(); + float L = LDR ; + float T = TEMP; + float P = PRES; + dataLock.unlock(); + + //Convert to a C String + sprintf(l_str, "%1.3f", L ); + 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); + } +}