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