Using a websocket to control a ShiftBrite LED and a digital compass
Dependencies: EthernetInterface HMC6352 WebSocketClient mbed-rtos mbed
main.cpp@0:a670e19e5f6a, 2013-10-17 (annotated)
- Committer:
- wjenkins7
- Date:
- Thu Oct 17 20:02:56 2013 +0000
- Revision:
- 0:a670e19e5f6a
web socket example code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wjenkins7 | 0:a670e19e5f6a | 1 | #include "mbed.h" |
wjenkins7 | 0:a670e19e5f6a | 2 | #include "EthernetInterface.h" |
wjenkins7 | 0:a670e19e5f6a | 3 | #include "Websocket.h" |
wjenkins7 | 0:a670e19e5f6a | 4 | #include "HMC6352.h" |
wjenkins7 | 0:a670e19e5f6a | 5 | |
wjenkins7 | 0:a670e19e5f6a | 6 | HMC6352 compass(p9, p10); |
wjenkins7 | 0:a670e19e5f6a | 7 | Serial pc(USBTX, USBRX); |
wjenkins7 | 0:a670e19e5f6a | 8 | SPI spi(p11, p12, p13); |
wjenkins7 | 0:a670e19e5f6a | 9 | |
wjenkins7 | 0:a670e19e5f6a | 10 | DigitalOut latch(p15); |
wjenkins7 | 0:a670e19e5f6a | 11 | DigitalOut enable(p16); |
wjenkins7 | 0:a670e19e5f6a | 12 | |
wjenkins7 | 0:a670e19e5f6a | 13 | //Function controlling the brightness of shiftbright leds |
wjenkins7 | 0:a670e19e5f6a | 14 | void RGB_LED(int redx, int greenx, int bluex) |
wjenkins7 | 0:a670e19e5f6a | 15 | { |
wjenkins7 | 0:a670e19e5f6a | 16 | unsigned int low_color=0; |
wjenkins7 | 0:a670e19e5f6a | 17 | unsigned int high_color=0; |
wjenkins7 | 0:a670e19e5f6a | 18 | int red, green, blue; |
wjenkins7 | 0:a670e19e5f6a | 19 | red = ((redx - 48)*25)&0x3FF ; // subtract 48 from input to read ascii code correctly |
wjenkins7 | 0:a670e19e5f6a | 20 | blue = ((bluex - 48)*25)&0x3FF ; // scale by a factor of 25 so the LED's are brighter |
wjenkins7 | 0:a670e19e5f6a | 21 | green = ((greenx - 48)*25)&0x3FF ; // and with 0x3FF so the colors are no more than 10 bits |
wjenkins7 | 0:a670e19e5f6a | 22 | high_color=(blue<<4)|((red&0x3C0)>>6); |
wjenkins7 | 0:a670e19e5f6a | 23 | low_color=(((red&0x3F)<<10)|(green)); |
wjenkins7 | 0:a670e19e5f6a | 24 | spi.write(high_color); |
wjenkins7 | 0:a670e19e5f6a | 25 | spi.write(low_color); |
wjenkins7 | 0:a670e19e5f6a | 26 | latch=1; |
wjenkins7 | 0:a670e19e5f6a | 27 | enable=1; |
wjenkins7 | 0:a670e19e5f6a | 28 | latch=0; |
wjenkins7 | 0:a670e19e5f6a | 29 | enable=0; |
wjenkins7 | 0:a670e19e5f6a | 30 | wait(.25); |
wjenkins7 | 0:a670e19e5f6a | 31 | } |
wjenkins7 | 0:a670e19e5f6a | 32 | |
wjenkins7 | 0:a670e19e5f6a | 33 | |
wjenkins7 | 0:a670e19e5f6a | 34 | int main() |
wjenkins7 | 0:a670e19e5f6a | 35 | { |
wjenkins7 | 0:a670e19e5f6a | 36 | // set the frequency and format of the spi connection to shiftbright |
wjenkins7 | 0:a670e19e5f6a | 37 | spi.format(16,0); |
wjenkins7 | 0:a670e19e5f6a | 38 | spi.frequency(500000); |
wjenkins7 | 0:a670e19e5f6a | 39 | |
wjenkins7 | 0:a670e19e5f6a | 40 | char *r, *b, *g; |
wjenkins7 | 0:a670e19e5f6a | 41 | int enable=0; |
wjenkins7 | 0:a670e19e5f6a | 42 | char r1=48,b1=48,g1=48; //Declare the colors zero or off in ascii code |
wjenkins7 | 0:a670e19e5f6a | 43 | char recv[3]; |
wjenkins7 | 0:a670e19e5f6a | 44 | |
wjenkins7 | 0:a670e19e5f6a | 45 | // run shiftbright function with zeros to start with shiftbright off |
wjenkins7 | 0:a670e19e5f6a | 46 | RGB_LED((int)r1, (int)g1, (int)b1); |
wjenkins7 | 0:a670e19e5f6a | 47 | |
wjenkins7 | 0:a670e19e5f6a | 48 | EthernetInterface eth; |
wjenkins7 | 0:a670e19e5f6a | 49 | eth.init(); //Use DHCP |
wjenkins7 | 0:a670e19e5f6a | 50 | eth.connect(); |
wjenkins7 | 0:a670e19e5f6a | 51 | //printf("IP Address is %s\n\r", eth.getIPAddress()); |
wjenkins7 | 0:a670e19e5f6a | 52 | |
wjenkins7 | 0:a670e19e5f6a | 53 | compass.setOpMode(HMC6352_CONTINUOUS, 1, 20); |
wjenkins7 | 0:a670e19e5f6a | 54 | |
wjenkins7 | 0:a670e19e5f6a | 55 | Websocket ws("ws://sockets.mbed.org:443/ws/demo/wo"); // set socket connection server address |
wjenkins7 | 0:a670e19e5f6a | 56 | ws.connect(); // connect socket server |
wjenkins7 | 0:a670e19e5f6a | 57 | int res = ws.send("MBED Connected"); // send msg confirming connection |
wjenkins7 | 0:a670e19e5f6a | 58 | |
wjenkins7 | 0:a670e19e5f6a | 59 | |
wjenkins7 | 0:a670e19e5f6a | 60 | |
wjenkins7 | 0:a670e19e5f6a | 61 | while (1) { |
wjenkins7 | 0:a670e19e5f6a | 62 | |
wjenkins7 | 0:a670e19e5f6a | 63 | // when the socket recieves information store it in recv |
wjenkins7 | 0:a670e19e5f6a | 64 | if (ws.read(recv)) { |
wjenkins7 | 0:a670e19e5f6a | 65 | |
wjenkins7 | 0:a670e19e5f6a | 66 | // if first character of the msg recieved is C send compass reading to socket |
wjenkins7 | 0:a670e19e5f6a | 67 | if(recv[0] == 'C') { |
wjenkins7 | 0:a670e19e5f6a | 68 | float compass_reading; |
wjenkins7 | 0:a670e19e5f6a | 69 | char reading[20]; |
wjenkins7 | 0:a670e19e5f6a | 70 | compass_reading = compass.sample() / 10.0; |
wjenkins7 | 0:a670e19e5f6a | 71 | snprintf(reading, sizeof(reading), "%f", compass_reading); |
wjenkins7 | 0:a670e19e5f6a | 72 | ws.send(reading); |
wjenkins7 | 0:a670e19e5f6a | 73 | ws.send("Heading is:"); |
wjenkins7 | 0:a670e19e5f6a | 74 | } |
wjenkins7 | 0:a670e19e5f6a | 75 | |
wjenkins7 | 0:a670e19e5f6a | 76 | // when r is input the character after a space is put as the brightness of the red LED |
wjenkins7 | 0:a670e19e5f6a | 77 | if(recv[0] == 'r') { |
wjenkins7 | 0:a670e19e5f6a | 78 | r= strtok(recv, " "); |
wjenkins7 | 0:a670e19e5f6a | 79 | r= strtok( NULL," "); |
wjenkins7 | 0:a670e19e5f6a | 80 | r1 = *r; |
wjenkins7 | 0:a670e19e5f6a | 81 | } |
wjenkins7 | 0:a670e19e5f6a | 82 | |
wjenkins7 | 0:a670e19e5f6a | 83 | // when b is input the character after a space is put as the brightness of the blue LED |
wjenkins7 | 0:a670e19e5f6a | 84 | if(recv[0] == 'b') { |
wjenkins7 | 0:a670e19e5f6a | 85 | b= strtok(recv, " "); |
wjenkins7 | 0:a670e19e5f6a | 86 | b= strtok( NULL," "); |
wjenkins7 | 0:a670e19e5f6a | 87 | b1 = *b; |
wjenkins7 | 0:a670e19e5f6a | 88 | } |
wjenkins7 | 0:a670e19e5f6a | 89 | |
wjenkins7 | 0:a670e19e5f6a | 90 | // when g is input the character after a space is put as the brightness of the green LED |
wjenkins7 | 0:a670e19e5f6a | 91 | if(recv[0] == 'g') { |
wjenkins7 | 0:a670e19e5f6a | 92 | g= strtok(recv, " "); |
wjenkins7 | 0:a670e19e5f6a | 93 | g= strtok( NULL," "); |
wjenkins7 | 0:a670e19e5f6a | 94 | g1 = *g; |
wjenkins7 | 0:a670e19e5f6a | 95 | } |
wjenkins7 | 0:a670e19e5f6a | 96 | |
wjenkins7 | 0:a670e19e5f6a | 97 | // when 0 is input the shiftbright turns all LED's off |
wjenkins7 | 0:a670e19e5f6a | 98 | if(recv[0] == '0') { |
wjenkins7 | 0:a670e19e5f6a | 99 | g1=48; |
wjenkins7 | 0:a670e19e5f6a | 100 | r1=48; |
wjenkins7 | 0:a670e19e5f6a | 101 | b1=48; |
wjenkins7 | 0:a670e19e5f6a | 102 | } |
wjenkins7 | 0:a670e19e5f6a | 103 | |
wjenkins7 | 0:a670e19e5f6a | 104 | // call on the LED function everytime an input comes from the socket |
wjenkins7 | 0:a670e19e5f6a | 105 | RGB_LED((int)r1, (int)g1, (int)b1); |
wjenkins7 | 0:a670e19e5f6a | 106 | |
wjenkins7 | 0:a670e19e5f6a | 107 | } |
wjenkins7 | 0:a670e19e5f6a | 108 | } |
wjenkins7 | 0:a670e19e5f6a | 109 | |
wjenkins7 | 0:a670e19e5f6a | 110 | } |