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