Using a websocket to control a ShiftBrite LED and a digital compass
Dependencies: EthernetInterface HMC6352 WebSocketClient mbed-rtos mbed
Revision 0:a670e19e5f6a, committed 2013-10-17
- Comitter:
- wjenkins7
- Date:
- Thu Oct 17 20:02:56 2013 +0000
- Commit message:
- web socket example code
Changed in this revision
diff -r 000000000000 -r a670e19e5f6a EthernetInterface.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EthernetInterface.lib Thu Oct 17 20:02:56 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/EthernetInterface/#cba86db5ab96
diff -r 000000000000 -r a670e19e5f6a HMC6352.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HMC6352.lib Thu Oct 17 20:02:56 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/aberk/code/HMC6352/#83c0cb554099
diff -r 000000000000 -r a670e19e5f6a WebSocketClient.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WebSocketClient.lib Thu Oct 17 20:02:56 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/samux/code/WebSocketClient/#4567996414a5
diff -r 000000000000 -r a670e19e5f6a main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Oct 17 20:02:56 2013 +0000 @@ -0,0 +1,110 @@ +#include "mbed.h" +#include "EthernetInterface.h" +#include "Websocket.h" +#include "HMC6352.h" + +HMC6352 compass(p9, p10); +Serial pc(USBTX, USBRX); +SPI spi(p11, p12, p13); + +DigitalOut latch(p15); +DigitalOut enable(p16); + +//Function controlling the brightness of shiftbright leds +void RGB_LED(int redx, int greenx, int bluex) +{ + unsigned int low_color=0; + unsigned int high_color=0; + int red, green, blue; + red = ((redx - 48)*25)&0x3FF ; // subtract 48 from input to read ascii code correctly + blue = ((bluex - 48)*25)&0x3FF ; // scale by a factor of 25 so the LED's are brighter + green = ((greenx - 48)*25)&0x3FF ; // and with 0x3FF so the colors are no more than 10 bits + high_color=(blue<<4)|((red&0x3C0)>>6); + low_color=(((red&0x3F)<<10)|(green)); + spi.write(high_color); + spi.write(low_color); + latch=1; + enable=1; + latch=0; + enable=0; + wait(.25); +} + + +int main() +{ + // set the frequency and format of the spi connection to shiftbright + spi.format(16,0); + spi.frequency(500000); + + char *r, *b, *g; + int enable=0; + char r1=48,b1=48,g1=48; //Declare the colors zero or off in ascii code + char recv[3]; + + // run shiftbright function with zeros to start with shiftbright off + RGB_LED((int)r1, (int)g1, (int)b1); + + EthernetInterface eth; + eth.init(); //Use DHCP + eth.connect(); + //printf("IP Address is %s\n\r", eth.getIPAddress()); + + compass.setOpMode(HMC6352_CONTINUOUS, 1, 20); + + Websocket ws("ws://sockets.mbed.org:443/ws/demo/wo"); // set socket connection server address + ws.connect(); // connect socket server + int res = ws.send("MBED Connected"); // send msg confirming connection + + + + while (1) { + + // when the socket recieves information store it in recv + if (ws.read(recv)) { + + // if first character of the msg recieved is C send compass reading to socket + if(recv[0] == 'C') { + float compass_reading; + char reading[20]; + compass_reading = compass.sample() / 10.0; + snprintf(reading, sizeof(reading), "%f", compass_reading); + ws.send(reading); + ws.send("Heading is:"); + } + + // when r is input the character after a space is put as the brightness of the red LED + if(recv[0] == 'r') { + r= strtok(recv, " "); + r= strtok( NULL," "); + r1 = *r; + } + + // when b is input the character after a space is put as the brightness of the blue LED + if(recv[0] == 'b') { + b= strtok(recv, " "); + b= strtok( NULL," "); + b1 = *b; + } + + // when g is input the character after a space is put as the brightness of the green LED + if(recv[0] == 'g') { + g= strtok(recv, " "); + g= strtok( NULL," "); + g1 = *g; + } + + // when 0 is input the shiftbright turns all LED's off + if(recv[0] == '0') { + g1=48; + r1=48; + b1=48; + } + + // call on the LED function everytime an input comes from the socket + RGB_LED((int)r1, (int)g1, (int)b1); + + } + } + +} \ No newline at end of file
diff -r 000000000000 -r a670e19e5f6a mbed-rtos.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Thu Oct 17 20:02:56 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#ee87e782d34f
diff -r 000000000000 -r a670e19e5f6a mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Oct 17 20:02:56 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/a9913a65894f \ No newline at end of file