Websockets demo sending mag and acc data to socket.io

Dependencies:   EthernetInterface FXOS8700Q WebSocketClient mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* FXOS8700Q Example Program
00002  * Copyright (c) 2014-2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 #include "FXOS8700Q.h"
00019 #include "EthernetInterface.h"
00020 #include "Websocket.h"
00021 
00022 Serial pc(USBTX, USBRX);
00023 I2C i2c(PTE25, PTE24);
00024 
00025 FXOS8700QAccelerometer acc(i2c, FXOS8700CQ_SLAVE_ADDR1);    // Configured for the FRDM-K64F with onboard sensors
00026 FXOS8700QMagnetometer mag(i2c, FXOS8700CQ_SLAVE_ADDR1);
00027 
00028 
00029 int main(void)
00030 {
00031 
00032     float faX, faY, faZ, fmX, fmY, fmZ;
00033     
00034     //Ethernet Interface
00035     EthernetInterface eth;
00036     eth.init();
00037     eth.connect();
00038     printf("IP Address is %s\n\r", eth.getIPAddress());
00039     
00040     //Sockets
00041     Websocket ws("ws://node-red-spookmx.c9.io/ws/inputs");
00042     ws.connect();
00043     
00044     acc.enable();
00045     mag.enable();
00046     ws.send("{'success':true, 'payload':'Starting Socket TX'}");
00047     printf("{'success':true, 'payload':'Starting Socket TX'}\n\r");
00048     while (true) {
00049         acc.getX(faX);
00050         acc.getY(faY);
00051         acc.getZ(faZ);
00052         mag.getX(fmX);
00053         mag.getY(fmY);
00054         mag.getZ(fmZ);
00055         char output[500];
00056         snprintf(output,500,"ACC: X=%1.4ff Y=%1.4ff Z=%1.4ff \t MAG: X=%4.1ff Y=%4.1ff Z=%4.1ff\r\n", faX, faY, faZ, fmX, fmY, fmZ);
00057         printf("ACC: X=%1.4ff Y=%1.4ff Z=%1.4ff \t MAG: X=%4.1ff Y=%4.1ff Z=%4.1ff\r\n", faX, faY, faZ, fmX, fmY, fmZ);
00058         ws.send(output);
00059         puts("");
00060         //wait(5.0f);
00061     }
00062 }