Example program streaming accelerometer data to a websocket server over a Sprint Mobile Broadband connection
Dependencies: MMA7660 SprintUSBModem WebSocketClient mbed-rtos mbed
Revision 4:c1ce97cb8d74, committed 2012-10-10
- Comitter:
- donatien
- Date:
- Wed Oct 10 14:56:48 2012 +0000
- Parent:
- 3:e4db133a4f59
- Child:
- 5:5aed2f21d249
- Commit message:
- Websocket demo
Changed in this revision
--- a/HTTPClient.lib Wed Oct 10 14:46:53 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/donatien/code/HTTPClient/#1f743885e7de
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WebSocketClient.lib Wed Oct 10 14:56:48 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/samux/code/WebSocketClient/#86e89a0369b9
--- a/main.cpp Wed Oct 10 14:46:53 2012 +0000 +++ b/main.cpp Wed Oct 10 14:56:48 2012 +0000 @@ -1,12 +1,12 @@ #include "mbed.h" #include "SprintUSBModem.h" -#include "HTTPClient.h" +#include "Websocket.h" void test(void const*) { - SprintUSBModem modem(p18); - HTTPClient http; - char str[512]; + SprintUSBModem modem(p18); + Websocket ws("ws://sockets.mbed.org:443/ws/demo/rw"); + char recv[128]; Thread::wait(5000); printf("Switching power on\r\n"); @@ -20,39 +20,30 @@ return; } - //GET data - printf("Trying to fetch page...\r\n"); - ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128); - if (!ret) + bool c = ws.connect(); + printf("Connect result: %s\r\n", c?"OK":"Failed"); + + for(int i = 0; i < 10000; i++) { - printf("Page fetched successfully - read %d characters\r\n", strlen(str)); - printf("Result: %s\r\n", str); - } - else - { - printf("Error - ret = %d - HTTP return code = %d\r\n", ret, http.getHTTPResponseCode()); + if(!(i%100)) + { + int ret = ws.send("WebSocket Hello World!"); + if(ret<0) + { + printf("Timeout\r\n"); + ws.close(); + c = ws.connect(); + printf("Connect result: %s\r\n", c?"OK":"Failed"); + } + } + + if (ws.read(recv)) { + printf("rcv: %s\r\n", recv); + } + } - //POST data - HTTPMap map; - HTTPText text(str, 512); - map.put("Hello", "World"); - map.put("test", "1234"); - printf("Trying to post data...\r\n"); - ret = http.post("http://httpbin.org/post", map, &text); - if (!ret) - { - printf("Executed POST successfully - read %d characters\r\n", strlen(str)); - printf("Result: %s\r\n", str); - } - else - { - printf("Error - ret = %d - HTTP return code = %d\r\n", ret, http.getHTTPResponseCode()); - } - - printf("Disconnecting\r\n"); - - modem.disconnect(); + modem.disconnect(); printf("Disconnected\r\n");