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.
main.cpp
00001 #include "mbed.h" 00002 #include "EthernetInterface.h" 00003 #include "TCPSocket.h" 00004 #include "http_server.h" 00005 #include "LocalFileSystem.h" 00006 00007 #define PORT 1000 00008 #define SOCKET_TIMEOUT_MS 2000 00009 00010 //Serial interface 00011 Serial pc(USBTX, USBRX,115200); 00012 00013 //Initialize network interface 00014 EthernetInterface eth; 00015 00016 nsapi_error_t status; 00017 int n_bytes; 00018 SocketAddress eth_addr; 00019 TCPSocket socket; 00020 TCPSocket* client; 00021 int i; 00022 Thread socket_thread; 00023 Thread web_server_thread; 00024 char recv_buffer[40]; 00025 char html_page[10000]; 00026 int html_page_size; 00027 00028 HttpServer web_server; 00029 00030 DigitalOut heartbeat(LED1); 00031 DigitalOut socket_led(LED2); 00032 DigitalOut webserver_led(LED3); 00033 00034 //There is a configuration file that can be accessed through usb in order to configure the device 00035 LocalFileSystem local("local"); //Create the local filesystem under the name "local" 00036 00037 FILE *html_file; //Pointer to HTML file 00038 00039 void socket_port() 00040 { 00041 if(socket.open(ð)==NSAPI_ERROR_OK) 00042 pc.printf("Socket Opened\n\r"); 00043 else 00044 pc.printf("Error opening socket\n\r"); 00045 00046 if(socket.bind(PORT)==NSAPI_ERROR_OK) 00047 pc.printf("Socket Bind Success\n\r"); 00048 else 00049 pc.printf("Bind Error\n\r"); 00050 00051 if(socket.listen(QUEUED_REQUESTS)==NSAPI_ERROR_OK) 00052 pc.printf("Socket Listen Success\n\r"); 00053 else 00054 pc.printf("Listen Error\n\r"); 00055 00056 while(1) 00057 { 00058 client = socket.accept(&status); 00059 00060 pc.printf("After accept\n\r"); 00061 socket_led = !socket_led; 00062 if(status==NSAPI_ERROR_OK) 00063 { 00064 client->set_timeout(SOCKET_TIMEOUT_MS); 00065 n_bytes = client->recv(recv_buffer,40); 00066 if(n_bytes>0) 00067 { 00068 for(i=0;i<n_bytes;i++) 00069 pc.putc(recv_buffer[i]); 00070 switch(recv_buffer[0]) 00071 { 00072 case 'a': 00073 pc.printf("a received\n\r"); 00074 client->send("a=2\n\r",5); 00075 break; 00076 case 'b': 00077 pc.printf("b received\n\r"); 00078 client->send("b=5\n\r",5); 00079 break; 00080 default: 00081 pc.printf("ERROR>NOT-A-CMD\n\r"); 00082 client->send("ERROR>NOT-A-CMD\n\r",17); 00083 } 00084 ThisThread::sleep_for(2000); 00085 } 00086 else 00087 pc.printf("Receiving Error: %d\n\r",n_bytes); 00088 client->close(); 00089 } 00090 else 00091 pc.printf("Accept Error: %d\n\r",status); 00092 } 00093 } 00094 00095 void web_server_func() 00096 { 00097 web_server.init(ð,html_page,html_page_size); 00098 while(1) 00099 { 00100 web_server.run(); 00101 webserver_led = !webserver_led; 00102 } 00103 } 00104 00105 int main() 00106 { 00107 pc.printf("Starting Firmware\n\r"); 00108 00109 // Initializng webserver 00110 html_file = fopen("/local/index.htm","r"); // Open "index.html" on the local file system for reading 00111 if(html_file==NULL){ //If file doesnt exist 00112 pc.printf("File not found: index.htm\n\r"); 00113 } 00114 else 00115 { //File exists 00116 pc.printf("HMTL file found\n\r"); 00117 fseek(html_file, 0, SEEK_SET); 00118 html_page_size = 0; 00119 while (!feof(html_file)) 00120 html_page[html_page_size++]= getc(html_file); 00121 if(feof(html_file)) 00122 html_page_size--; 00123 } 00124 00125 // Initialising Ethernet Interface 00126 do{ 00127 status = eth.connect(); 00128 if(status == NSAPI_ERROR_OK || status == NSAPI_ERROR_IS_CONNECTED) 00129 pc.printf("Internet connection established\n\r"); 00130 else 00131 pc.printf("No internet connnection: Error Code: %d\n\r", status); 00132 }while(status != NSAPI_ERROR_OK && status != NSAPI_ERROR_IS_CONNECTED); 00133 00134 // Checking IP Address (DHCP Mode) 00135 if(eth.get_ip_address(ð_addr)==NSAPI_ERROR_OK) 00136 pc.printf("IP address: %s\n\r", eth_addr.get_ip_address() ? eth_addr.get_ip_address() : "None"); 00137 else 00138 pc.printf("DHCP failure - acquiring the IP address has failed\n\r"); 00139 00140 //while(1){} 00141 00142 00143 socket_thread.start(socket_port); 00144 web_server_thread.start(web_server_func); 00145 00146 while (true) { 00147 heartbeat = !heartbeat; 00148 ThisThread::sleep_for(1000); 00149 } 00150 }
Generated on Thu Sep 8 2022 01:18:45 by
1.7.2