A simple example of ethernet webserver for mbed-os.

Getting started with Ethernet webserver

This is the Ethernet webserver example for mbed OS.

Files at this revision

API Documentation at this revision

Comitter:
c_jin
Date:
Tue Feb 19 11:48:03 2019 +0000
Commit message:
A simple example of Ethernet webserver demo for mbed-os.

Changed in this revision

CONTRIBUTING.md Show annotated file Show diff for this revision Revisions of this file
README.md Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 621fd985b960 CONTRIBUTING.md
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CONTRIBUTING.md	Tue Feb 19 11:48:03 2019 +0000
@@ -0,0 +1,5 @@
+# Contributing to Mbed OS
+
+Mbed OS is an open-source, device software platform for the Internet of Things. Contributions are an important part of the platform, and our goal is to make it as simple as possible to become a contributor.
+
+To encourage productive collaboration, as well as robust, consistent and maintainable code, we have a set of guidelines for [contributing to Mbed OS](https://os.mbed.com/docs/mbed-os/latest/contributing/index.html).
diff -r 000000000000 -r 621fd985b960 README.md
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md	Tue Feb 19 11:48:03 2019 +0000
@@ -0,0 +1,24 @@
+# Getting started example for GD32 Ethernet web server demo
+
+###
+
+This demo realize webserver application:
+  Users can visit the board through Internet Explorer, the board acts as a webserver, and the url is the local 
+ip address of the board("192.168.57.156").
+  If DHCP function is needed, it can be configured by change DEMO_DHCP_ENABLE to 1 in main.cpp. This 
+function is closed by default. Users can use a router to connect the board, and use the HyperTerminal
+to print the automatic allocated ip address. Users can visit the board through Internet Explorer.
+
+```
+
+## Related Links
+
+* [Mbed OS Stats API](https://os.mbed.com/docs/latest/apis/mbed-statistics.html)
+* [Mbed OS Configuration](https://os.mbed.com/docs/latest/reference/configuration.html)
+* [Mbed OS Serial Communication](https://os.mbed.com/docs/latest/tutorials/serial-communication.html)
+
+### License and contributions
+
+The software is provided under Apache-2.0 license. Contributions to this project are accepted under the same license. Please see contributing.md for more info.
+
+This project contains code from other projects. The original license text is included in those source files. They must comply with our license guide.
diff -r 000000000000 -r 621fd985b960 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Feb 19 11:48:03 2019 +0000
@@ -0,0 +1,105 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2018 GigaDevice Limited
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include "mbed.h"
+#include "EthernetInterface.h"
+#include "TCPServer.h"
+#include "TCPSocket.h"
+
+/* 1 = Use DHCP */
+/* 0 = Use default IP */
+#define DEMO_DHCP_ENABLE        0
+
+#if (0 == DEMO_DHCP_ENABLE)
+#define DEVICE_IP       "192.168.57.156"
+#define DEVICE_MASK     "255.255.255.0"
+#define DEVICE_GATEWAY  "192.168.57.1"
+#endif
+    
+const char html_contex[] = {
+/* HTTP header */
+/* "HTTP/1.0 200 OK
+" (17 bytes) */
+0x48,0x54,0x54,0x50,0x2f,0x31,0x2e,0x30,0x20,0x32,0x30,0x30,0x20,0x4f,0x4b,0x0d,
+0x0a,
+/* "Content-type: text/html
+" (27 bytes) */
+0x43,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x2d,0x74,0x79,0x70,0x65,0x3a,0x20,0x74,0x65,
+0x78,0x74,0x2f,0x68,0x74,0x6d,0x6c,0x0d,0x0a,0x0d,0x0a,
+/* raw file data (380 bytes) */
+0x3c,0x21,0x44,0x4f,0x43,0x54,0x59,0x50,0x45,0x20,0x68,0x74,0x6d,0x6c,0x20,0x50,
+0x55,0x42,0x4c,0x49,0x43,0x20,0x22,0x2d,0x2f,0x2f,0x57,0x33,0x43,0x2f,0x2f,0x44,
+0x54,0x44,0x20,0x58,0x48,0x54,0x4d,0x4c,0x20,0x31,0x2e,0x30,0x20,0x54,0x72,0x61,
+0x6e,0x73,0x69,0x74,0x69,0x6f,0x6e,0x61,0x6c,0x2f,0x2f,0x45,0x4e,0x22,0x20,0x22,
+0x68,0x74,0x74,0x70,0x3a,0x2f,0x2f,0x77,0x77,0x77,0x2e,0x77,0x33,0x2e,0x6f,0x72,
+0x67,0x2f,0x54,0x52,0x2f,0x78,0x68,0x74,0x6d,0x6c,0x31,0x2f,0x44,0x54,0x44,0x2f,
+0x78,0x68,0x74,0x6d,0x6c,0x31,0x2d,0x74,0x72,0x61,0x6e,0x73,0x69,0x74,0x69,0x6f,
+0x6e,0x61,0x6c,0x2e,0x64,0x74,0x64,0x22,0x3e,0x0d,0x0a,0x3c,0x68,0x74,0x6d,0x6c,
+0x20,0x78,0x6d,0x6c,0x6e,0x73,0x3d,0x22,0x68,0x74,0x74,0x70,0x3a,0x2f,0x2f,0x77,
+0x77,0x77,0x2e,0x77,0x33,0x2e,0x6f,0x72,0x67,0x2f,0x31,0x39,0x39,0x39,0x2f,0x78,
+0x68,0x74,0x6d,0x6c,0x22,0x3e,0x0d,0x0a,0x3c,0x68,0x65,0x61,0x64,0x3e,0x0d,0x0a,
+0x3c,0x6d,0x65,0x74,0x61,0x20,0x68,0x74,0x74,0x70,0x2d,0x65,0x71,0x75,0x69,0x76,
+0x3d,0x22,0x43,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x2d,0x54,0x79,0x70,0x65,0x22,0x20,
+0x63,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x3d,0x22,0x74,0x65,0x78,0x74,0x2f,0x68,0x74,
+0x6d,0x6c,0x3b,0x20,0x63,0x68,0x61,0x72,0x73,0x65,0x74,0x3d,0x75,0x74,0x66,0x2d,
+0x38,0x22,0x20,0x2f,0x3e,0x0d,0x0a,0x3c,0x2f,0x68,0x65,0x61,0x64,0x3e,0x0d,0x0a,
+0x3c,0x62,0x6f,0x64,0x79,0x3e,0x0d,0x0a,0x3c,0x64,0x69,0x76,0x20,0x61,0x6c,0x69,
+0x67,0x6e,0x3d,0x22,0x63,0x65,0x6e,0x74,0x65,0x72,0x22,0x3e,0x0d,0x0a,0x20,0x20,
+0x3c,0x68,0x31,0x3e,0x3c,0x73,0x74,0x72,0x6f,0x6e,0x67,0x3e,0x48,0x65,0x6c,0x6c,
+0x6f,0x20,0x57,0x6f,0x72,0x6c,0x64,0x0d,0x0a,0x20,0x20,0x3c,0x2f,0x73,0x74,0x72,
+0x6f,0x6e,0x67,0x3e,0x3c,0x2f,0x68,0x31,0x3e,0x0d,0x0a,0x3c,0x2f,0x64,0x69,0x76,
+0x3e,0x0d,0x0a,0x3c,0x68,0x72,0x20,0x2f,0x3e,0x0d,0x0a,0x3c,0x70,0x3e,0x26,0x6e,
+0x62,0x73,0x70,0x3b,0x3c,0x2f,0x70,0x3e,0x0d,0x0a,0x3c,0x2f,0x62,0x6f,0x64,0x79,
+0x3e,0x0d,0x0a,0x3c,0x2f,0x68,0x74,0x6d,0x6c,0x3e,0x0d,0x0a,};
+    
+EthernetInterface enet;
+
+int main()
+{
+    nsapi_size_or_error_t result;
+    
+    printf("Mbed OS HTTP server example start \r\n");
+    
+#if (0 == DEMO_DHCP_ENABLE)
+    /* Disable DHCP */
+    enet.set_dhcp(0);
+    enet.set_network(DEVICE_IP, DEVICE_MASK, DEVICE_GATEWAY);
+#else
+    /* Enable DHCP */
+    enet.set_dhcp(1);
+#endif
+    
+    if(enet.connect() != 0){
+        printf("Network initialization failed \r\n");
+        while(1);
+    }else{
+        printf("The device IP address is '%s' \r\n", enet.get_ip_address());
+        printf("The device NetMask is '%s' \r\n", enet.get_netmask());
+        printf("The device GateWay is '%s' \r\n", enet.get_gateway());
+    }
+    
+    TCPServer http_srv;
+    TCPSocket cli_sock;
+    SocketAddress cli_addr;
+    
+    result = http_srv.open(&enet);
+    if (result != 0) {
+        printf("Error! http_srv.open() returned: %d\n", result);
+    }
+    
+    /* Bind socket to the HTTP port(80) */
+    result = http_srv.bind(enet.get_ip_address(), 80);
+    if (result != 0) {
+        printf("Error! http_srv.bind() returned: %d\n", result);
+    }
+    
+    http_srv.listen(5);
+    
+    while (1) {
+        http_srv.accept(&cli_sock, &cli_addr);
+        printf("Server accept successful, remote is %s:%d\n", cli_addr.get_ip_address(), cli_addr.get_port());
+        cli_sock.send(html_contex, sizeof(html_contex));
+    }
+}
diff -r 000000000000 -r 621fd985b960 mbed-os.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Tue Feb 19 11:48:03 2019 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#ecb3c8c837162c73537bd0f3592c6e2a42994045
diff -r 000000000000 -r 621fd985b960 mbed_app.json
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_app.json	Tue Feb 19 11:48:03 2019 +0000
@@ -0,0 +1,11 @@
+{
+    "target_overrides": {
+        "*": {
+            "platform.stack-stats-enabled": true,
+            "platform.heap-stats-enabled": true,
+            "platform.cpu-stats-enabled": true,
+            "platform.thread-stats-enabled": true,
+            "platform.sys-stats-enabled": true
+        }
+    }
+}