Websocket Hello World over a wifi network
Dependencies: EthernetNetIf mbed DNSResolver
main.cpp
- Committer:
- samux
- Date:
- 2011-08-26
- Revision:
- 2:e7fba3d516b5
- Parent:
- 0:e10d21debdaa
- Child:
- 3:a1ea35e2c7b3
File content as of revision 2:e7fba3d516b5:
#include "mbed.h" #include "Wifly.h" #include "Websocket.h" #include "ADXL345.h" Serial pc(USBTX, USBRX); ADXL345 accelerometer(p5, p6, p7, p8); //Here, we create an instance, with pins 9 and 10 connecting to the //WiFly's TX and RX pins, and pin 21 to RESET. We are connecting to the //NETGEAR network, password bananna2, and we are using WPA. Wifly wifly(p9, p10, p21, "mbed", "bananna2", true); //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", &wifly); 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 (1) { while (!wifly.Join()) //we connect to the network wifly.reset(); if (!ws.connect()) //we connect to the server wifly.reset(); else break; } 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); //And we send the string } } }