This program listens to a websocket and when the server sends the appropriate ASCII combinations through the websocket, the appropriate LED lights up (red, blue, or green).
Dependencies: WebSocketClient mbed-rtos mbed
main.cpp@0:181c1ea775ea, 2015-01-29 (annotated)
- Committer:
- brianh
- Date:
- Thu Jan 29 01:12:59 2015 +0000
- Revision:
- 0:181c1ea775ea
Very first commit of the LED_Remote program. It is a functional Websocket program and has been proven to work with Node.js.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
brianh | 0:181c1ea775ea | 1 | /******************************************************************************* |
brianh | 0:181c1ea775ea | 2 | * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved. |
brianh | 0:181c1ea775ea | 3 | * |
brianh | 0:181c1ea775ea | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
brianh | 0:181c1ea775ea | 5 | * copy of this software and associated documentation files (the "Software"), |
brianh | 0:181c1ea775ea | 6 | * to deal in the Software without restriction, including without limitation |
brianh | 0:181c1ea775ea | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
brianh | 0:181c1ea775ea | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
brianh | 0:181c1ea775ea | 9 | * Software is furnished to do so, subject to the following conditions: |
brianh | 0:181c1ea775ea | 10 | * |
brianh | 0:181c1ea775ea | 11 | * The above copyright notice and this permission notice shall be included |
brianh | 0:181c1ea775ea | 12 | * in all copies or substantial portions of the Software. |
brianh | 0:181c1ea775ea | 13 | * |
brianh | 0:181c1ea775ea | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
brianh | 0:181c1ea775ea | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
brianh | 0:181c1ea775ea | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
brianh | 0:181c1ea775ea | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
brianh | 0:181c1ea775ea | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
brianh | 0:181c1ea775ea | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
brianh | 0:181c1ea775ea | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
brianh | 0:181c1ea775ea | 21 | * |
brianh | 0:181c1ea775ea | 22 | * Except as contained in this notice, the name of Maxim Integrated |
brianh | 0:181c1ea775ea | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
brianh | 0:181c1ea775ea | 24 | * Products, Inc. Branding Policy. |
brianh | 0:181c1ea775ea | 25 | * |
brianh | 0:181c1ea775ea | 26 | * The mere transfer of this software does not imply any licenses |
brianh | 0:181c1ea775ea | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
brianh | 0:181c1ea775ea | 28 | * trademarks, maskwork rights, or any other form of intellectual |
brianh | 0:181c1ea775ea | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
brianh | 0:181c1ea775ea | 30 | * ownership rights. |
brianh | 0:181c1ea775ea | 31 | ******************************************************************************* |
brianh | 0:181c1ea775ea | 32 | * |
brianh | 0:181c1ea775ea | 33 | * LEDRemote version 1.0 is a program that listens for ACII characters from a |
brianh | 0:181c1ea775ea | 34 | * websocket. If the correct ASCII characters are sent, the onboard LED on |
brianh | 0:181c1ea775ea | 35 | * a Freescale Freedom board (K64F) will light an LED. For example, if "R1" or |
brianh | 0:181c1ea775ea | 36 | * "R2" is received the RED LED gets exercised. "B1" and "B0" exercises the blue |
brianh | 0:181c1ea775ea | 37 | * LED, and "G1" and "G0" exercises the green LED. |
brianh | 0:181c1ea775ea | 38 | * |
brianh | 0:181c1ea775ea | 39 | */ |
brianh | 0:181c1ea775ea | 40 | |
brianh | 0:181c1ea775ea | 41 | #include "mbed.h" |
brianh | 0:181c1ea775ea | 42 | #include "EthernetInterface.h" |
brianh | 0:181c1ea775ea | 43 | #include "Websocket.h" |
brianh | 0:181c1ea775ea | 44 | |
brianh | 0:181c1ea775ea | 45 | DigitalOut ledr(PTB22); |
brianh | 0:181c1ea775ea | 46 | DigitalOut ledg(PTE26); |
brianh | 0:181c1ea775ea | 47 | DigitalOut ledb(PTB21); |
brianh | 0:181c1ea775ea | 48 | //DigitalIn sw(PTA4); |
brianh | 0:181c1ea775ea | 49 | |
brianh | 0:181c1ea775ea | 50 | int main() { |
brianh | 0:181c1ea775ea | 51 | int res; |
brianh | 0:181c1ea775ea | 52 | char buf[100]={0}; |
brianh | 0:181c1ea775ea | 53 | char comparebuf[2]={0}; |
brianh | 0:181c1ea775ea | 54 | char msg[100]; |
brianh | 0:181c1ea775ea | 55 | ledg = 1; |
brianh | 0:181c1ea775ea | 56 | ledr = 1; |
brianh | 0:181c1ea775ea | 57 | ledb = 1; |
brianh | 0:181c1ea775ea | 58 | bool startup = true; |
brianh | 0:181c1ea775ea | 59 | int startupmode = 0; |
brianh | 0:181c1ea775ea | 60 | int startupledcount = 0; |
brianh | 0:181c1ea775ea | 61 | |
brianh | 0:181c1ea775ea | 62 | //bool pb = 1; |
brianh | 0:181c1ea775ea | 63 | |
brianh | 0:181c1ea775ea | 64 | //BW_MPU_CESR_VLD(0);// k64f workaround mentioned here (to turn off MPU): https://community.freescale.com/thread/324825 |
brianh | 0:181c1ea775ea | 65 | //wait(1); |
brianh | 0:181c1ea775ea | 66 | //clock_manager_set_gate(kClockModuleENET,0,true); // turn on clock |
brianh | 0:181c1ea775ea | 67 | |
brianh | 0:181c1ea775ea | 68 | printf("\r\n---- Maxim's WebSocket example with the k64f board ----\r\n"); |
brianh | 0:181c1ea775ea | 69 | EthernetInterface eth; |
brianh | 0:181c1ea775ea | 70 | eth.init(); |
brianh | 0:181c1ea775ea | 71 | eth.connect(); |
brianh | 0:181c1ea775ea | 72 | //wait(1.0); |
brianh | 0:181c1ea775ea | 73 | printf("IP Address is %s\r\n", eth.getIPAddress()); |
brianh | 0:181c1ea775ea | 74 | wait(1.0); |
brianh | 0:181c1ea775ea | 75 | |
brianh | 0:181c1ea775ea | 76 | // Change YOURLOGIN for your mbed login so you can communicate to the board |
brianh | 0:181c1ea775ea | 77 | // using the mbed websocket at: |
brianh | 0:181c1ea775ea | 78 | // http://sockets.mbed.org/YOURLOGIN |
brianh | 0:181c1ea775ea | 79 | //Websocket ws("ws://sockets.mbed.org/ws/brianh/ro"); // over port 80 (html) |
brianh | 0:181c1ea775ea | 80 | //Websocket ws("ws://sockets.mbed.org:443/ws/brianh/rw"); // secure TLS |
brianh | 0:181c1ea775ea | 81 | |
brianh | 0:181c1ea775ea | 82 | //Websocket ws("ws://192.168.1.12:8080/"); // home -- notice forward slash is necessary |
brianh | 0:181c1ea775ea | 83 | Websocket ws("ws://10.17.245.247:8080/"); // Dallas -- Mbed needs the slash at the end or the websocket won't connect properly without a path |
brianh | 0:181c1ea775ea | 84 | //Websocket ws("ws://10.33.245.13:8080/"); // California -- Mbed needs the slash at the end or the websocket won't connect properly without a path |
brianh | 0:181c1ea775ea | 85 | //Websocket ws("ws://dal-lt-BHindman.maxim-ic.internal:8080/"); // laptop |
brianh | 0:181c1ea775ea | 86 | //wait(1.0); |
brianh | 0:181c1ea775ea | 87 | |
brianh | 0:181c1ea775ea | 88 | ws.connect(); |
brianh | 0:181c1ea775ea | 89 | wait(1.0); |
brianh | 0:181c1ea775ea | 90 | |
brianh | 0:181c1ea775ea | 91 | //ws.send("st"); |
brianh | 0:181c1ea775ea | 92 | //while (pb) |
brianh | 0:181c1ea775ea | 93 | while(1) |
brianh | 0:181c1ea775ea | 94 | { |
brianh | 0:181c1ea775ea | 95 | if (ws.is_connected()) |
brianh | 0:181c1ea775ea | 96 | { |
brianh | 0:181c1ea775ea | 97 | if (startup) |
brianh | 0:181c1ea775ea | 98 | { |
brianh | 0:181c1ea775ea | 99 | startupledcount++; // count number of loops while websocket is connected |
brianh | 0:181c1ea775ea | 100 | if (startupledcount > 20) |
brianh | 0:181c1ea775ea | 101 | { |
brianh | 0:181c1ea775ea | 102 | startupledcount = 0; |
brianh | 0:181c1ea775ea | 103 | startupmode++; |
brianh | 0:181c1ea775ea | 104 | if (startupmode == 1) ws.send("st"); // request server to send state of red led |
brianh | 0:181c1ea775ea | 105 | if (startupmode == 2) ws.send("su"); // request server to send state of green led |
brianh | 0:181c1ea775ea | 106 | if (startupmode == 3) ws.send("sv"); // request server to send state of blue led |
brianh | 0:181c1ea775ea | 107 | if (startupmode == 3) startup = false; |
brianh | 0:181c1ea775ea | 108 | } |
brianh | 0:181c1ea775ea | 109 | |
brianh | 0:181c1ea775ea | 110 | } |
brianh | 0:181c1ea775ea | 111 | //printf("is_connected() is true \r\n"); |
brianh | 0:181c1ea775ea | 112 | // read from websocket the led that should be turned on. |
brianh | 0:181c1ea775ea | 113 | if(!ws.read(buf)) |
brianh | 0:181c1ea775ea | 114 | { |
brianh | 0:181c1ea775ea | 115 | //printf("%c",buf[0]); |
brianh | 0:181c1ea775ea | 116 | // is it red? |
brianh | 0:181c1ea775ea | 117 | if(buf[0] == 'r') |
brianh | 0:181c1ea775ea | 118 | { |
brianh | 0:181c1ea775ea | 119 | if(buf[1] == '0') |
brianh | 0:181c1ea775ea | 120 | { |
brianh | 0:181c1ea775ea | 121 | ledr = 0; |
brianh | 0:181c1ea775ea | 122 | if ((comparebuf[0] != buf[0]) || (comparebuf[1] != buf[1])) printf("Turned red LED on\n\r"); |
brianh | 0:181c1ea775ea | 123 | } |
brianh | 0:181c1ea775ea | 124 | else |
brianh | 0:181c1ea775ea | 125 | { |
brianh | 0:181c1ea775ea | 126 | ledr = 1; |
brianh | 0:181c1ea775ea | 127 | if ((comparebuf[0] != buf[0]) || (comparebuf[1] != buf[1])) printf("Turned red LED off\n\r"); |
brianh | 0:181c1ea775ea | 128 | } |
brianh | 0:181c1ea775ea | 129 | } |
brianh | 0:181c1ea775ea | 130 | // is it green? |
brianh | 0:181c1ea775ea | 131 | if(buf[0] == 'g') |
brianh | 0:181c1ea775ea | 132 | { |
brianh | 0:181c1ea775ea | 133 | //printf("g! \r\n"); |
brianh | 0:181c1ea775ea | 134 | if(buf[1] == '0') |
brianh | 0:181c1ea775ea | 135 | { |
brianh | 0:181c1ea775ea | 136 | ledg = 0; |
brianh | 0:181c1ea775ea | 137 | if ((comparebuf[0] != buf[0]) || (comparebuf[1] != buf[1])) printf("Turned green LED on\n\r"); |
brianh | 0:181c1ea775ea | 138 | } |
brianh | 0:181c1ea775ea | 139 | else |
brianh | 0:181c1ea775ea | 140 | { |
brianh | 0:181c1ea775ea | 141 | ledg = 1; |
brianh | 0:181c1ea775ea | 142 | if ((comparebuf[0] != buf[0]) || (comparebuf[1] != buf[1])) printf("Turned green LED off\n\r"); |
brianh | 0:181c1ea775ea | 143 | } |
brianh | 0:181c1ea775ea | 144 | } |
brianh | 0:181c1ea775ea | 145 | // is it blue? |
brianh | 0:181c1ea775ea | 146 | if(buf[0] == 'b') |
brianh | 0:181c1ea775ea | 147 | { |
brianh | 0:181c1ea775ea | 148 | if(buf[1] == '0') |
brianh | 0:181c1ea775ea | 149 | { |
brianh | 0:181c1ea775ea | 150 | ledb = 0; |
brianh | 0:181c1ea775ea | 151 | if ((comparebuf[0] != buf[0]) || (comparebuf[1] != buf[1])) printf("Turned blue LED on\n\r"); |
brianh | 0:181c1ea775ea | 152 | } |
brianh | 0:181c1ea775ea | 153 | else |
brianh | 0:181c1ea775ea | 154 | { |
brianh | 0:181c1ea775ea | 155 | ledb = 1; |
brianh | 0:181c1ea775ea | 156 | if ((comparebuf[0] != buf[0]) || (comparebuf[1] != buf[1])) printf("Turned blue LED off\n\r"); |
brianh | 0:181c1ea775ea | 157 | } |
brianh | 0:181c1ea775ea | 158 | } |
brianh | 0:181c1ea775ea | 159 | comparebuf[0] = buf[0]; |
brianh | 0:181c1ea775ea | 160 | comparebuf[1] = buf[1]; |
brianh | 0:181c1ea775ea | 161 | |
brianh | 0:181c1ea775ea | 162 | } |
brianh | 0:181c1ea775ea | 163 | //pb = sw; |
brianh | 0:181c1ea775ea | 164 | } |
brianh | 0:181c1ea775ea | 165 | else |
brianh | 0:181c1ea775ea | 166 | { |
brianh | 0:181c1ea775ea | 167 | printf("trying to make websocket connection\n\r"); |
brianh | 0:181c1ea775ea | 168 | ws.close(); |
brianh | 0:181c1ea775ea | 169 | ws.connect(); |
brianh | 0:181c1ea775ea | 170 | //wait(1.0); |
brianh | 0:181c1ea775ea | 171 | } |
brianh | 0:181c1ea775ea | 172 | }// end while |
brianh | 0:181c1ea775ea | 173 | //ws.close(); |
brianh | 0:181c1ea775ea | 174 | //eth.disconnect(); |
brianh | 0:181c1ea775ea | 175 | //ledg = 1; |
brianh | 0:181c1ea775ea | 176 | //ledr = 1; |
brianh | 0:181c1ea775ea | 177 | //ledb = 1; |
brianh | 0:181c1ea775ea | 178 | //while(1) |
brianh | 0:181c1ea775ea | 179 | //{ |
brianh | 0:181c1ea775ea | 180 | //} |
brianh | 0:181c1ea775ea | 181 | } |