websocket client for gyro and 3-Axio Accelerometer
Dependencies: ITG3200 MMA7660FC WebSocketClient WiflyInterface mbed
main.cpp@0:a4c49388bc5e, 2015-02-04 (annotated)
- Committer:
- mbedschool
- Date:
- Wed Feb 04 13:40:01 2015 +0000
- Revision:
- 0:a4c49388bc5e
- Child:
- 1:f43651a4e987
websocket client for gyro and 3-Axio
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedschool | 0:a4c49388bc5e | 1 | #include "mbed.h" |
mbedschool | 0:a4c49388bc5e | 2 | #include "WiflyInterface.h" |
mbedschool | 0:a4c49388bc5e | 3 | #include "Websocket.h" |
mbedschool | 0:a4c49388bc5e | 4 | #include "ITG3200.h" |
mbedschool | 0:a4c49388bc5e | 5 | #include "MMA7660FC.h" |
mbedschool | 0:a4c49388bc5e | 6 | |
mbedschool | 0:a4c49388bc5e | 7 | /* wifly interface: |
mbedschool | 0:a4c49388bc5e | 8 | * - p9 and p10 are for the serial communication |
mbedschool | 0:a4c49388bc5e | 9 | * - p19 is for the reset pin |
mbedschool | 0:a4c49388bc5e | 10 | * - p26 is for the connection status |
mbedschool | 0:a4c49388bc5e | 11 | * - "mbed" is the ssid of the network |
mbedschool | 0:a4c49388bc5e | 12 | * - "password" is the password |
mbedschool | 0:a4c49388bc5e | 13 | * - WPA is the security |
mbedschool | 0:a4c49388bc5e | 14 | */ |
mbedschool | 0:a4c49388bc5e | 15 | WiflyInterface wifly(p13, p14, p19, p26, "WWNet", "mmmmmmmm", WPA); |
mbedschool | 0:a4c49388bc5e | 16 | DigitalOut led1(LED1); |
mbedschool | 0:a4c49388bc5e | 17 | DigitalOut led2(LED1); |
mbedschool | 0:a4c49388bc5e | 18 | //gyrc |
mbedschool | 0:a4c49388bc5e | 19 | ITG3200 gyro(p9, p10, 0x68); |
mbedschool | 0:a4c49388bc5e | 20 | //3Axis |
mbedschool | 0:a4c49388bc5e | 21 | #define ADDR_MMA7660 0x98 // I2C SLAVE ADDR MMA7660FC |
mbedschool | 0:a4c49388bc5e | 22 | MMA7660FC Acc(p28, p27, ADDR_MMA7660); //sda, scl, Addr |
mbedschool | 0:a4c49388bc5e | 23 | float G_VALUE[64] = {0, 0.047, 0.094, 0.141, 0.188, 0.234, 0.281, 0.328, 0.375, 0.422, 0.469, 0.516, 0.563, 0.609, 0.656, 0.703, 0.750, 0.797, 0.844, 0.891, 0.938, 0.984, 1.031, 1.078, 1.125, 1.172, 1.219, 1.266, 1.313, 1.359, 1.406, 1.453, -1.500, -1.453, -1.406, -1.359, -1.313, -1.266, -1.219, -1.172, -1.125, -1.078, -1.031, -0.984, -0.938, -0.891, -0.844, -0.797, -0.750, -0.703, -0.656, -0.609, -0.563, -0.516, -0.469, -0.422, -0.375, -0.328, -0.281, -0.234, -0.188, -0.141, -0.094, -0.047}; |
mbedschool | 0:a4c49388bc5e | 24 | |
mbedschool | 0:a4c49388bc5e | 25 | |
mbedschool | 0:a4c49388bc5e | 26 | int main() { |
mbedschool | 0:a4c49388bc5e | 27 | wifly.init(); //Use DHCP |
mbedschool | 0:a4c49388bc5e | 28 | while (!wifly.connect()); |
mbedschool | 0:a4c49388bc5e | 29 | led1=1; |
mbedschool | 0:a4c49388bc5e | 30 | Websocket ws("ws://sockets.mbed.org/ws/mbedschool/viewer"); |
mbedschool | 0:a4c49388bc5e | 31 | while (!ws.connect()); |
mbedschool | 0:a4c49388bc5e | 32 | led1=2; |
mbedschool | 0:a4c49388bc5e | 33 | |
mbedschool | 0:a4c49388bc5e | 34 | |
mbedschool | 0:a4c49388bc5e | 35 | //gyro |
mbedschool | 0:a4c49388bc5e | 36 | int x = 0, y = 0, z = 0, temp = 0; |
mbedschool | 0:a4c49388bc5e | 37 | //Set highest bandwidth. |
mbedschool | 0:a4c49388bc5e | 38 | gyro.setLpBandwidth(LPFBW_42HZ); |
mbedschool | 0:a4c49388bc5e | 39 | //3Axio |
mbedschool | 0:a4c49388bc5e | 40 | Acc.init(); // Initialization |
mbedschool | 0:a4c49388bc5e | 41 | |
mbedschool | 0:a4c49388bc5e | 42 | while (1) { |
mbedschool | 0:a4c49388bc5e | 43 | char data[256]; |
mbedschool | 0:a4c49388bc5e | 44 | wait(0.1f); |
mbedschool | 0:a4c49388bc5e | 45 | |
mbedschool | 0:a4c49388bc5e | 46 | //gyro |
mbedschool | 0:a4c49388bc5e | 47 | x = gyro.getGyroX(); |
mbedschool | 0:a4c49388bc5e | 48 | y = gyro.getGyroY(); |
mbedschool | 0:a4c49388bc5e | 49 | z = gyro.getGyroZ(); |
mbedschool | 0:a4c49388bc5e | 50 | temp = gyro.getTemperature(); |
mbedschool | 0:a4c49388bc5e | 51 | |
mbedschool | 0:a4c49388bc5e | 52 | //3-Axio |
mbedschool | 0:a4c49388bc5e | 53 | float x=0, y=0, z=0; |
mbedschool | 0:a4c49388bc5e | 54 | float ax=0, ay=0, az=0; |
mbedschool | 0:a4c49388bc5e | 55 | |
mbedschool | 0:a4c49388bc5e | 56 | Acc.read_Tilt(&x, &y, &z); // Read the acceleration |
mbedschool | 0:a4c49388bc5e | 57 | wait_ms(100); |
mbedschool | 0:a4c49388bc5e | 58 | ax = G_VALUE[Acc.read_x()]; |
mbedschool | 0:a4c49388bc5e | 59 | ay = G_VALUE[Acc.read_y()]; |
mbedschool | 0:a4c49388bc5e | 60 | az = G_VALUE[Acc.read_z()]; |
mbedschool | 0:a4c49388bc5e | 61 | |
mbedschool | 0:a4c49388bc5e | 62 | |
mbedschool | 0:a4c49388bc5e | 63 | |
mbedschool | 0:a4c49388bc5e | 64 | //websocket send data |
mbedschool | 0:a4c49388bc5e | 65 | sprintf( data , "{\"temp\": %d, \"x\": %d, \"y\": %d, \"z\": %d\n , \"ax\": %d\n, \"ay\": %d\n, \"az\": %d\n}", temp, x, y, z ,ax,ay,az); |
mbedschool | 0:a4c49388bc5e | 66 | ws.send(data); |
mbedschool | 0:a4c49388bc5e | 67 | |
mbedschool | 0:a4c49388bc5e | 68 | wait(1.0); |
mbedschool | 0:a4c49388bc5e | 69 | } |
mbedschool | 0:a4c49388bc5e | 70 | } |
mbedschool | 0:a4c49388bc5e | 71 |