Example program streaming accelerometer data to a websocket server over a Sprint Mobile Broadband connection
Dependencies: MMA7660 SprintUSBModem WebSocketClient mbed-rtos mbed
Diff: main.cpp
- Revision:
- 4:c1ce97cb8d74
- Parent:
- 2:9304861e65d6
- Child:
- 5:5aed2f21d249
--- 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");