No changes
Networkbits.cpp@9:4be9eeb39a36, 2018-11-30 (annotated)
- Committer:
- O_Thom
- Date:
- Fri Nov 30 13:32:06 2018 +0000
- Revision:
- 9:4be9eeb39a36
- Parent:
- 8:df979097cc71
Minor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 8:df979097cc71 | 1 | #include "sample_hardware.hpp" |
noutram | 8:df979097cc71 | 2 | #include "Networkbits.hpp" |
noutram | 8:df979097cc71 | 3 | |
noutram | 8:df979097cc71 | 4 | //Network thread - responsible for listening for connectinos and responding with updated tempature values |
noutram | 8:df979097cc71 | 5 | void networktest() |
noutram | 8:df979097cc71 | 6 | { |
noutram | 8:df979097cc71 | 7 | //This only runs when BOTH switches are pressed down |
noutram | 8:df979097cc71 | 8 | if ((SW1 == 0) || (SW2 == 0)) return; |
noutram | 8:df979097cc71 | 9 | |
noutram | 8:df979097cc71 | 10 | lcd.cls(); |
noutram | 8:df979097cc71 | 11 | lcd.printf("Basic HTTP server example\n"); |
noutram | 8:df979097cc71 | 12 | |
noutram | 8:df979097cc71 | 13 | //Configure an ethernet connection |
noutram | 8:df979097cc71 | 14 | EthernetInterface eth; |
noutram | 8:df979097cc71 | 15 | eth.set_network(IP, NETMASK, GATEWAY); |
noutram | 8:df979097cc71 | 16 | eth.connect(); |
noutram | 8:df979097cc71 | 17 | lcd.printf("The target IP address is '%s'\n", eth.get_ip_address()); |
noutram | 8:df979097cc71 | 18 | |
noutram | 8:df979097cc71 | 19 | //Now setup a web server |
noutram | 8:df979097cc71 | 20 | TCPServer srv; //TCP/IP Server |
noutram | 8:df979097cc71 | 21 | TCPSocket clt_sock; //Socket for communication |
noutram | 8:df979097cc71 | 22 | SocketAddress clt_addr; //Address of incoming connection |
noutram | 8:df979097cc71 | 23 | |
noutram | 8:df979097cc71 | 24 | /* Open the server on ethernet stack */ |
noutram | 8:df979097cc71 | 25 | srv.open(ð); |
noutram | 8:df979097cc71 | 26 | |
noutram | 8:df979097cc71 | 27 | /* Bind the HTTP port (TCP 80) to the server */ |
noutram | 8:df979097cc71 | 28 | srv.bind(eth.get_ip_address(), 80); |
noutram | 8:df979097cc71 | 29 | |
noutram | 8:df979097cc71 | 30 | /* Can handle 5 simultaneous connections */ |
noutram | 8:df979097cc71 | 31 | srv.listen(5); |
noutram | 8:df979097cc71 | 32 | |
noutram | 8:df979097cc71 | 33 | //KEEP RESPONDING WHILE THE SWITCHES ARE PRESSED |
noutram | 8:df979097cc71 | 34 | while ((SW1 == 1) && (SW2 == 1)) { |
noutram | 8:df979097cc71 | 35 | using namespace std; |
noutram | 8:df979097cc71 | 36 | lcd.cls(); |
noutram | 8:df979097cc71 | 37 | lcd.printf("Open 10.0.0.1 in a browser\n"); |
noutram | 8:df979097cc71 | 38 | |
noutram | 8:df979097cc71 | 39 | //Block and wait on an incoming connection |
noutram | 8:df979097cc71 | 40 | srv.accept(&clt_sock, &clt_addr); |
noutram | 8:df979097cc71 | 41 | printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port()); |
noutram | 8:df979097cc71 | 42 | |
noutram | 8:df979097cc71 | 43 | //Uses a C++ string to make it easier to concatinate |
noutram | 8:df979097cc71 | 44 | string response; |
noutram | 8:df979097cc71 | 45 | //This is a C string |
noutram | 8:df979097cc71 | 46 | char ldr_str[64]; |
noutram | 8:df979097cc71 | 47 | |
noutram | 8:df979097cc71 | 48 | //Read the temperature value - note that getParameters() is thread safe |
noutram | 8:df979097cc71 | 49 | float temp = sensor.getTemperature(); |
noutram | 8:df979097cc71 | 50 | |
noutram | 8:df979097cc71 | 51 | //Convert to a C String |
noutram | 8:df979097cc71 | 52 | sprintf(ldr_str, "%5.3f", temp ); |
noutram | 8:df979097cc71 | 53 | printf("LDR: %5.3f\n\r", temp); |
noutram | 8:df979097cc71 | 54 | |
noutram | 8:df979097cc71 | 55 | //Build the C++ string response |
noutram | 8:df979097cc71 | 56 | response = HTTP_MESSAGE_BODY1; |
noutram | 8:df979097cc71 | 57 | response += ldr_str; |
noutram | 8:df979097cc71 | 58 | response += HTTP_MESSAGE_BODY2; |
noutram | 8:df979097cc71 | 59 | |
noutram | 8:df979097cc71 | 60 | //Send static HTML response (as a C string) |
noutram | 8:df979097cc71 | 61 | clt_sock.send(response.c_str(), response.size()+6); |
noutram | 8:df979097cc71 | 62 | } |
noutram | 8:df979097cc71 | 63 | |
noutram | 8:df979097cc71 | 64 | printf("Release BOTH switches\n"); |
noutram | 8:df979097cc71 | 65 | lcd.printf("Release BOTH switches\n"); |
noutram | 8:df979097cc71 | 66 | while ((SW1 != 0) && (SW2 != 0)); |
noutram | 8:df979097cc71 | 67 | wait(0.5); //debounce |
noutram | 8:df979097cc71 | 68 | } |