Websocket Hello World over an ethernet network
Dependencies: EthernetNetIf mbed DNSResolver
Revision 0:dac316268a0c, committed 2011-08-26
- Comitter:
- samux
- Date:
- Fri Aug 26 13:22:52 2011 +0000
- Child:
- 1:8bdf124446e1
- Commit message:
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ADXL345.lib Fri Aug 26 13:22:52 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/aberk/code/ADXL345/#bd8f0f20f433
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DNSResolver.lib Fri Aug 26 13:22:52 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/hlipka/code/DNSResolver/#097d4993dd1e
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EthernetNetIf.lib Fri Aug 26 13:22:52 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/donatien/code/EthernetNetIf/#bc7df6da7589
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Websocket.lib Fri Aug 26 13:22:52 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/samux/code/Websocket/#1f6b9451a608
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Wifly.lib Fri Aug 26 13:22:52 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/samux/code/Wifly/#984c1436dd42
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Aug 26 13:22:52 2011 +0000 @@ -0,0 +1,51 @@ +#include "mbed.h" +#include "Websocket.h" +#include "ADXL345.h" + +Serial pc(USBTX, USBRX); + +ADXL345 accelerometer(p5, p6, p7, p8); + +//Here, we create a Websocket instance in 'w' (write) mode +//on the 'samux' channel +Websocket ws("ws://sockets.mbed.org:444/mbed_demo/w/samux"); + +int main() { + char json_str[100]; + + int readings[3] = {0, 0, 0}; + + pc.printf("Starting ADXL345 test...\r\n"); + pc.printf("Device ID is: 0x%02x\r\n", accelerometer.getDevId()); + + //Go into standby mode to configure the device. + accelerometer.setPowerControl(0x00); + + //Full resolution, +/-16g, 4mg/LSB. + accelerometer.setDataFormatControl(0x0B); + + //3.2kHz data rate. + accelerometer.setDataRate(ADXL345_3200HZ); + + //Measurement mode. + accelerometer.setPowerControl(0x08); + + + while (1) { + + while(!ws.connect()) + pc.printf("cannot connect websocket, retrying\r\n"); + + while (1) { + wait(0.1); + + //we read accelerometers values + accelerometer.getOutput(readings); + + //Here, we format the string we will be sending to the server + //the format we are sending in is JSON + sprintf(json_str, "{\"id\":\"wifly_acc\",\"ax\":\"%d\",\"ay\":\"%d\",\"az\":\"%d\"}", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]); + ws.Send(json_str); + } + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Fri Aug 26 13:22:52 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912