Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Networkbits.cpp
00001 #include "sample_hardware.hpp" 00002 #include "Networkbits.hpp" 00003 00004 //Network thread - responsible for listening for connectinos and responding with updated tempature values 00005 void networktest() 00006 { 00007 //This only runs when BOTH switches are pressed down 00008 if ((SW1 == 0) || (SW2 == 0)) return; 00009 00010 lcd.cls(); 00011 lcd.printf("Basic HTTP server example\n"); 00012 00013 //Configure an ethernet connection 00014 EthernetInterface eth; 00015 eth.set_network(IP, NETMASK, GATEWAY); 00016 eth.connect(); 00017 lcd.printf("The target IP address is '%s'\n", eth.get_ip_address()); 00018 00019 //Now setup a web server 00020 TCPServer srv; //TCP/IP Server 00021 TCPSocket clt_sock; //Socket for communication 00022 SocketAddress clt_addr; //Address of incoming connection 00023 00024 /* Open the server on ethernet stack */ 00025 srv.open(ð); 00026 00027 /* Bind the HTTP port (TCP 80) to the server */ 00028 srv.bind(eth.get_ip_address(), 80); 00029 00030 /* Can handle 5 simultaneous connections */ 00031 srv.listen(5); 00032 00033 //KEEP RESPONDING WHILE THE SWITCHES ARE PRESSED 00034 while ((SW1 == 1) && (SW2 == 1)) { 00035 using namespace std; 00036 lcd.cls(); 00037 lcd.printf("Open 10.0.0.1 in a browser\n"); 00038 00039 //Block and wait on an incoming connection 00040 srv.accept(&clt_sock, &clt_addr); 00041 printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port()); 00042 00043 //Uses a C++ string to make it easier to concatinate 00044 string response; 00045 //This is a C string 00046 char ldr_str[64]; 00047 00048 //Read the temperature value - note that getParameters() is thread safe 00049 float temp = sensor.getTemperature(); 00050 00051 //Convert to a C String 00052 sprintf(ldr_str, "%5.3f", temp ); 00053 printf("LDR: %5.3f\n\r", temp); 00054 00055 //Build the C++ string response 00056 response = HTTP_MESSAGE_BODY1; 00057 response += ldr_str; 00058 response += HTTP_MESSAGE_BODY2; 00059 00060 //Send static HTML response (as a C string) 00061 clt_sock.send(response.c_str(), response.size()+6); 00062 } 00063 00064 printf("Release BOTH switches\n"); 00065 lcd.printf("Release BOTH switches\n"); 00066 while ((SW1 != 0) && (SW2 != 0)); 00067 wait(0.5); //debounce 00068 }
Generated on Sun Aug 7 2022 04:55:41 by
