Hello World program demonstrating HTML5 Websockets connecting via Ethernet

Dependencies:   WebSocketClient

Fork of Websocket_Ethernet_HelloWorld by mbed official

Files at this revision

API Documentation at this revision

Comitter:
mbed_official
Date:
Thu Mar 16 21:15:06 2017 +0000
Parent:
5:bd0a76c1d9d0
Child:
7:26199246b2b4
Commit message:
Updated example to utilize mbed-os and the new websocket client library

Changed in this revision

EthernetInterface.lib Show diff for this revision Revisions of this file
WebSocketClient.lib 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-rtos.lib Show diff for this revision Revisions of this file
mbed.bld Show diff for this revision Revisions of this file
--- a/EthernetInterface.lib	Wed May 14 15:26:34 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/mbed_official/code/EthernetInterface/#097a9996f8d5
--- a/WebSocketClient.lib	Wed May 14 15:26:34 2014 +0000
+++ b/WebSocketClient.lib	Thu Mar 16 21:15:06 2017 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/samux/code/WebSocketClient/#4567996414a5
+https://developer.mbed.org/teams/mbed_example/code/WebSocketClient/#efa2c147bee1
--- a/main.cpp	Wed May 14 15:26:34 2014 +0000
+++ b/main.cpp	Thu Mar 16 21:15:06 2017 +0000
@@ -1,47 +1,51 @@
+/* Copyright C2014 ARM, MIT License
+ *
+ * Author: Doug Anson (doug.anson@arm.com)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files the "Software", to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
 #include "mbed.h"
 #include "EthernetInterface.h"
 #include "Websocket.h"
 
-Ticker flash;
+// Blinky
 DigitalOut led(LED1);
-void flashLED(void){led = !led;}
- 
- 
-int main() 
-{
-    flash.attach(&flashLED, 1.0f);
+
+int main() {   
+
+    // announce
+    printf("Websocket Example v1.0.0\r\n");
     
-    EthernetInterface ethernet;
-    ethernet.init();    // connect with DHCP
-    int ret_val = ethernet.connect();
-
-    if (0 == ret_val) {
-        printf("IP Address: %s\n", ethernet.getIPAddress());
-    } else {
-        error("ethernet failed to connect: %d.\n", ret_val);
-    }
+    // Create a network interface and connect
+    EthernetInterface eth;
+    eth.connect();
+    printf("IP Address is %s\n\r", eth.get_ip_address());
 
-    // view @ http://sockets.mbed.org/demo/viewer
-    Websocket ws("ws://sockets.mbed.org:443/ws/demo/rw");
-    ws.connect();
-    char str[100];
-    
-    for(int i=0; i<0x7fffffff; ++i) {
-        // string with a message
-        sprintf(str, "%d WebSocket Hello World over Ethernet", i);
-        ws.send(str);
+    // Create a websocket instance
+    Websocket ws("ws://example.com:8080/", &eth);
+    int connect_error = ws.connect();
     
-        // clear the buffer and wait a sec...
-        memset(str, 0, 100);
-        wait(0.5f);
-    
-        // websocket server should echo whatever we sent it
-        if (ws.read(str)) {
-            printf("rcv'd: %s\n", str);
-        }
+    // begin main loop
+    while (true) {
+
+        // blink... 
+        led = !led; 
+        wait(0.5);
+        
+        int error_c = ws.send("Hello World\r\n");
     }
-    ws.close();
-    ethernet.disconnect();
-    
-    while(true);
 }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Thu Mar 16 21:15:06 2017 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#f4864dc6429e1ff5474111d4e0f6bee36a759b1c
--- a/mbed-rtos.lib	Wed May 14 15:26:34 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/mbed_official/code/mbed-rtos/#5dfe422a963d
--- a/mbed.bld	Wed May 14 15:26:34 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/8a40adfe8776
\ No newline at end of file