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