![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Update
Dependencies: MMA7660FC WebSocketClient WiflyInterface mbed
Fork of mbed_websocket_for_3Axis_Digiital_Accelerometer by
main.cpp@1:099ea08628f2, 2015-02-16 (annotated)
- Committer:
- lingtien5846
- Date:
- Mon Feb 16 09:18:48 2015 +0000
- Revision:
- 1:099ea08628f2
- Parent:
- 0:de962bd57d31
Update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedschool | 0:de962bd57d31 | 1 | #include "mbed.h" |
mbedschool | 0:de962bd57d31 | 2 | #include "WiflyInterface.h" |
mbedschool | 0:de962bd57d31 | 3 | #include "Websocket.h" |
mbedschool | 0:de962bd57d31 | 4 | #include "MMA7660FC.h" |
mbedschool | 0:de962bd57d31 | 5 | |
mbedschool | 0:de962bd57d31 | 6 | |
mbedschool | 0:de962bd57d31 | 7 | /* wifly interface: |
mbedschool | 0:de962bd57d31 | 8 | * - p9 and p10 are for the serial communication |
mbedschool | 0:de962bd57d31 | 9 | * - p19 is for the reset pin |
mbedschool | 0:de962bd57d31 | 10 | * - p26 is for the connection status |
mbedschool | 0:de962bd57d31 | 11 | * - "mbed" is the ssid of the network |
mbedschool | 0:de962bd57d31 | 12 | * - "password" is the password |
mbedschool | 0:de962bd57d31 | 13 | * - WPA is the security |
mbedschool | 0:de962bd57d31 | 14 | */ |
lingtien5846 | 1:099ea08628f2 | 15 | WiflyInterface wifly(p9, p10, p19, p26, "Nexus_5", "1234567890", WPA); |
mbedschool | 0:de962bd57d31 | 16 | DigitalOut myled1(LED1); |
mbedschool | 0:de962bd57d31 | 17 | DigitalOut myled2(LED2); |
mbedschool | 0:de962bd57d31 | 18 | DigitalOut myled3(LED3); |
mbedschool | 0:de962bd57d31 | 19 | DigitalOut myled4(LED4); |
lingtien5846 | 1:099ea08628f2 | 20 | |
mbedschool | 0:de962bd57d31 | 21 | #define ADDR_MMA7660 0x98 // I2C SLAVE ADDR MMA7660FC |
mbedschool | 0:de962bd57d31 | 22 | |
mbedschool | 0:de962bd57d31 | 23 | MMA7660FC Acc(p28, p27, ADDR_MMA7660); //sda, scl, Addr |
mbedschool | 0:de962bd57d31 | 24 | Serial pc(USBTX, USBRX); |
mbedschool | 0:de962bd57d31 | 25 | |
mbedschool | 0:de962bd57d31 | 26 | |
mbedschool | 0:de962bd57d31 | 27 | 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:de962bd57d31 | 28 | |
mbedschool | 0:de962bd57d31 | 29 | int main() { |
mbedschool | 0:de962bd57d31 | 30 | wifly.init(); //Use DHCP |
mbedschool | 0:de962bd57d31 | 31 | //wifly.init("192.168.21.33","255.255.255.0","192.168.21.2"); |
mbedschool | 0:de962bd57d31 | 32 | while (!wifly.connect()); |
lingtien5846 | 1:099ea08628f2 | 33 | myled1 = 1; |
mbedschool | 0:de962bd57d31 | 34 | printf("IP Address is %s\n\r", wifly.getIPAddress()); |
mbedschool | 0:de962bd57d31 | 35 | |
lingtien5846 | 1:099ea08628f2 | 36 | //Websocket ws("ws://192.168.0.153:8080/ws/accel/send"); |
lingtien5846 | 1:099ea08628f2 | 37 | //Websocket ws("ws://192.168.0.102:8888"); |
lingtien5846 | 1:099ea08628f2 | 38 | Websocket ws("ws://wot.city/object/accelerometer/send"); |
lingtien5846 | 1:099ea08628f2 | 39 | |
mbedschool | 0:de962bd57d31 | 40 | while (!ws.connect()); |
lingtien5846 | 1:099ea08628f2 | 41 | myled2 = 1; |
mbedschool | 0:de962bd57d31 | 42 | |
mbedschool | 0:de962bd57d31 | 43 | Acc.init(); // Initialization |
lingtien5846 | 1:099ea08628f2 | 44 | pc.printf("Value reg 0x06: %#x\n", Acc.read_reg( 0x06)); // Test the correct value of the register 0x06 |
mbedschool | 0:de962bd57d31 | 45 | pc.printf("Value reg 0x08: %#x\n", Acc.read_reg(0x08)); // Test the correct value of the register 0x08 |
mbedschool | 0:de962bd57d31 | 46 | pc.printf("Value reg 0x07: %#x\n\r", Acc.read_reg(0x07)); // Test the correct value of the register 0x07 |
mbedschool | 0:de962bd57d31 | 47 | |
mbedschool | 0:de962bd57d31 | 48 | while (1) { |
mbedschool | 0:de962bd57d31 | 49 | char data[256]; |
mbedschool | 0:de962bd57d31 | 50 | |
lingtien5846 | 1:099ea08628f2 | 51 | myled3 = 1; |
mbedschool | 0:de962bd57d31 | 52 | float x=0, y=0, z=0; |
mbedschool | 0:de962bd57d31 | 53 | float ax=0, ay=0, az=0; |
mbedschool | 0:de962bd57d31 | 54 | |
mbedschool | 0:de962bd57d31 | 55 | Acc.read_Tilt(&x, &y, &z); // Read the acceleration |
mbedschool | 0:de962bd57d31 | 56 | pc.printf("Tilt x: %2.2f degree \n", x); // Print the tilt orientation of the X axis |
mbedschool | 0:de962bd57d31 | 57 | pc.printf("Tilt y: %2.2f degree \n", y); // Print the tilt orientation of the Y axis |
mbedschool | 0:de962bd57d31 | 58 | pc.printf("Tilt z: %2.2f degree \n", z); // Print the tilt orientation of the Z axis |
mbedschool | 0:de962bd57d31 | 59 | |
mbedschool | 0:de962bd57d31 | 60 | wait_ms(100); |
mbedschool | 0:de962bd57d31 | 61 | |
mbedschool | 0:de962bd57d31 | 62 | ax = G_VALUE[Acc.read_x()]; |
mbedschool | 0:de962bd57d31 | 63 | ay = G_VALUE[Acc.read_y()]; |
mbedschool | 0:de962bd57d31 | 64 | az = G_VALUE[Acc.read_z()]; |
mbedschool | 0:de962bd57d31 | 65 | |
lingtien5846 | 1:099ea08628f2 | 66 | // if(ax<0){ |
lingtien5846 | 1:099ea08628f2 | 67 | // myled1=1; |
lingtien5846 | 1:099ea08628f2 | 68 | // }else{ |
lingtien5846 | 1:099ea08628f2 | 69 | // myled1=0; |
lingtien5846 | 1:099ea08628f2 | 70 | // } |
lingtien5846 | 1:099ea08628f2 | 71 | // if(ay<0){ |
lingtien5846 | 1:099ea08628f2 | 72 | // myled2=1; |
lingtien5846 | 1:099ea08628f2 | 73 | // }else{ |
lingtien5846 | 1:099ea08628f2 | 74 | // myled2=0; |
lingtien5846 | 1:099ea08628f2 | 75 | // } |
lingtien5846 | 1:099ea08628f2 | 76 | // if(az<0){ |
lingtien5846 | 1:099ea08628f2 | 77 | // myled3=1; |
lingtien5846 | 1:099ea08628f2 | 78 | // }else{ |
lingtien5846 | 1:099ea08628f2 | 79 | // myled3=0; |
lingtien5846 | 1:099ea08628f2 | 80 | // } |
mbedschool | 0:de962bd57d31 | 81 | |
lingtien5846 | 1:099ea08628f2 | 82 | wait(0.2); |
lingtien5846 | 1:099ea08628f2 | 83 | sprintf(data , "{\"ax\": %f, \"ay\": %f, \"az\": %f}", ax, ay, az); |
mbedschool | 0:de962bd57d31 | 84 | ws.send(data); |
mbedschool | 0:de962bd57d31 | 85 | |
mbedschool | 0:de962bd57d31 | 86 | //wait(1.0); |
mbedschool | 0:de962bd57d31 | 87 | } |
mbedschool | 0:de962bd57d31 | 88 | } |