An example program that uses a Wifly module to send messages over web sockets
Dependencies: WebSocketClient WiflyInterface mbed LM75B MMA7660
Fork of Websocket_Wifly_HelloWorld by
main.cpp@4:79f9caf3d109, 2013-02-08 (annotated)
- Committer:
- samux
- Date:
- Fri Feb 08 12:09:33 2013 +0000
- Revision:
- 4:79f9caf3d109
- Parent:
- 3:d1d4b7d317d1
- Child:
- 5:ab84dbbe7366
websocket demo using wifly and on board sensors (app board) (acc - temp)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 1:31e50fea8be8 | 1 | #include "mbed.h" |
samux | 1:31e50fea8be8 | 2 | #include "WiflyInterface.h" |
samux | 1:31e50fea8be8 | 3 | #include "Websocket.h" |
samux | 4:79f9caf3d109 | 4 | #include "LM75B.h" |
samux | 4:79f9caf3d109 | 5 | #include "MMA7660.h" |
samux | 1:31e50fea8be8 | 6 | |
samux | 1:31e50fea8be8 | 7 | |
samux | 1:31e50fea8be8 | 8 | /* wifly interface: |
samux | 1:31e50fea8be8 | 9 | * - p9 and p10 are for the serial communication |
chris | 3:d1d4b7d317d1 | 10 | * - p30 is for the reset pin |
chris | 3:d1d4b7d317d1 | 11 | * - p29 is for the connection status |
samux | 1:31e50fea8be8 | 12 | * - "mbed" is the ssid of the network |
samux | 1:31e50fea8be8 | 13 | * - "password" is the password |
samux | 1:31e50fea8be8 | 14 | * - WPA is the security |
samux | 1:31e50fea8be8 | 15 | */ |
chris | 3:d1d4b7d317d1 | 16 | |
chris | 3:d1d4b7d317d1 | 17 | WiflyInterface wifly(p9, p10, p30, p29, "mbed", "password", WPA); |
samux | 1:31e50fea8be8 | 18 | |
samux | 4:79f9caf3d109 | 19 | // accelerometer |
samux | 4:79f9caf3d109 | 20 | MMA7660 acc(p28, p27); |
samux | 4:79f9caf3d109 | 21 | |
samux | 4:79f9caf3d109 | 22 | // temperature sensor |
samux | 4:79f9caf3d109 | 23 | LM75B tmp(p28,p27); |
samux | 4:79f9caf3d109 | 24 | |
samux | 4:79f9caf3d109 | 25 | |
chris | 3:d1d4b7d317d1 | 26 | int main() |
chris | 3:d1d4b7d317d1 | 27 | { |
samux | 4:79f9caf3d109 | 28 | char json_str[100]; |
chris | 3:d1d4b7d317d1 | 29 | |
samux | 1:31e50fea8be8 | 30 | wifly.init(); //Use DHCP |
samux | 1:31e50fea8be8 | 31 | while (!wifly.connect()); |
samux | 1:31e50fea8be8 | 32 | printf("IP Address is %s\n\r", wifly.getIPAddress()); |
samux | 1:31e50fea8be8 | 33 | |
chris | 3:d1d4b7d317d1 | 34 | // See the output on http://sockets.mbed.org/app-board/viewer |
samux | 4:79f9caf3d109 | 35 | Websocket ws("ws://sockets.mbed.org:443/ws/sensors/wo"); |
samux | 4:79f9caf3d109 | 36 | |
samux | 4:79f9caf3d109 | 37 | // connect WS server |
samux | 1:31e50fea8be8 | 38 | while (!ws.connect()); |
samux | 1:31e50fea8be8 | 39 | |
samux | 1:31e50fea8be8 | 40 | while (1) { |
samux | 4:79f9caf3d109 | 41 | // create json string with acc/tmp data |
samux | 4:79f9caf3d109 | 42 | sprintf(json_str, "{\"id\":\"app_board_EW2013\",\"ax\":%d,\"ay\":%d,\"az\":%d, \"tmp\":%d}", (int)(acc.x()*360), (int)(acc.y()*360), (int)(acc.z()*360), (int)tmp.read()); |
samux | 4:79f9caf3d109 | 43 | |
samux | 4:79f9caf3d109 | 44 | // send str |
samux | 4:79f9caf3d109 | 45 | ws.send(json_str); |
samux | 4:79f9caf3d109 | 46 | |
samux | 4:79f9caf3d109 | 47 | wait(0.1); |
samux | 1:31e50fea8be8 | 48 | } |
samux | 0:64c683256184 | 49 | } |