
Simple example using WebSocket to send accelerometer data
Dependencies: EthernetInterface MMA7660 WebSocketClient mbed-rtos mbed
main.cpp@0:11bb5faeb547, 2012-10-26 (annotated)
- Committer:
- samux
- Date:
- Fri Oct 26 09:11:00 2012 +0000
- Revision:
- 0:11bb5faeb547
send accelerometer data over websocket
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 0:11bb5faeb547 | 1 | #include "mbed.h" |
samux | 0:11bb5faeb547 | 2 | #include "EthernetInterface.h" |
samux | 0:11bb5faeb547 | 3 | #include "Websocket.h" |
samux | 0:11bb5faeb547 | 4 | #include "MMA7660.h" |
samux | 0:11bb5faeb547 | 5 | |
samux | 0:11bb5faeb547 | 6 | MMA7660 MMA(p28, p27); |
samux | 0:11bb5faeb547 | 7 | |
samux | 0:11bb5faeb547 | 8 | DigitalOut l1(LED1); |
samux | 0:11bb5faeb547 | 9 | |
samux | 0:11bb5faeb547 | 10 | int main() { |
samux | 0:11bb5faeb547 | 11 | char json_str[100]; |
samux | 0:11bb5faeb547 | 12 | |
samux | 0:11bb5faeb547 | 13 | if (MMA.testConnection()) |
samux | 0:11bb5faeb547 | 14 | l1 = 1; |
samux | 0:11bb5faeb547 | 15 | |
samux | 0:11bb5faeb547 | 16 | EthernetInterface eth; |
samux | 0:11bb5faeb547 | 17 | eth.init(); //Use DHCP |
samux | 0:11bb5faeb547 | 18 | eth.connect(); |
samux | 0:11bb5faeb547 | 19 | printf("IP Address is %s\n\r", eth.getIPAddress()); |
samux | 0:11bb5faeb547 | 20 | |
samux | 0:11bb5faeb547 | 21 | Websocket ws("ws://sockets.mbed.org/ws/demo/wo"); |
samux | 0:11bb5faeb547 | 22 | ws.connect(); |
samux | 0:11bb5faeb547 | 23 | |
samux | 0:11bb5faeb547 | 24 | while (1) { |
samux | 0:11bb5faeb547 | 25 | sprintf(json_str, "{\"id\":\"app_board_acc\",\"ax\":\"%d\",\"ay\":\"%d\",\"az\":\"%d\"}", (int)(MMA.x()*200), (int)(MMA.y()*200), (int)(MMA.z()*200)); |
samux | 0:11bb5faeb547 | 26 | ws.send(json_str); |
samux | 0:11bb5faeb547 | 27 | wait(0.2); |
samux | 0:11bb5faeb547 | 28 | } |
samux | 0:11bb5faeb547 | 29 | } |