An example program that establishes a read-only web socket connection, and echos everything it sees to the LCD
Dependencies: C12832_lcd SprintUSBModem WebSocketClient mbed-rtos mbed
Fork of SprintUSBModemWebsocketTest-Temp by
main.cpp@3:7d9749651663, 2012-11-01 (annotated)
- Committer:
- chris
- Date:
- Thu Nov 01 03:45:23 2012 +0000
- Revision:
- 3:7d9749651663
- Parent:
- 2:72c06f2902d5
First commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chris | 2:72c06f2902d5 | 1 | #include "mbed.h" |
chris | 2:72c06f2902d5 | 2 | #include "SprintUSBModem.h" |
chris | 2:72c06f2902d5 | 3 | #include "Websocket.h" |
chris | 3:7d9749651663 | 4 | #include "C12832_lcd.h" |
chris | 2:72c06f2902d5 | 5 | |
chris | 2:72c06f2902d5 | 6 | void test(void const*) |
chris | 2:72c06f2902d5 | 7 | { |
chris | 2:72c06f2902d5 | 8 | |
chris | 2:72c06f2902d5 | 9 | SprintUSBModem modem; |
chris | 3:7d9749651663 | 10 | // Send messages at http://sockets.mbed.org/demo/sender |
chris | 3:7d9749651663 | 11 | Websocket ws("ws://sockets.mbed.org:443/ws/demo/ro"); |
chris | 2:72c06f2902d5 | 12 | |
chris | 2:72c06f2902d5 | 13 | char recv[128]; |
chris | 2:72c06f2902d5 | 14 | |
chris | 2:72c06f2902d5 | 15 | modem.power(true); |
chris | 2:72c06f2902d5 | 16 | |
chris | 2:72c06f2902d5 | 17 | int ret = modem.connect(); |
chris | 2:72c06f2902d5 | 18 | if(ret) { |
chris | 2:72c06f2902d5 | 19 | printf("Could not connect\r\n"); |
chris | 2:72c06f2902d5 | 20 | return; |
chris | 2:72c06f2902d5 | 21 | } |
chris | 2:72c06f2902d5 | 22 | |
chris | 2:72c06f2902d5 | 23 | bool c = ws.connect(); |
chris | 2:72c06f2902d5 | 24 | printf("Connect result: %s\r\n", c?"OK":"Failed"); |
chris | 2:72c06f2902d5 | 25 | |
chris | 3:7d9749651663 | 26 | C12832_LCD lcd; |
chris | 3:7d9749651663 | 27 | lcd.cls(); |
chris | 3:7d9749651663 | 28 | |
chris | 2:72c06f2902d5 | 29 | while (1) { |
chris | 2:72c06f2902d5 | 30 | |
chris | 3:7d9749651663 | 31 | if (ws.read(recv)) { |
chris | 3:7d9749651663 | 32 | printf("rcv: %s\r\n", recv); |
chris | 3:7d9749651663 | 33 | lcd.cls(); |
chris | 3:7d9749651663 | 34 | lcd.locate(0,3); |
chris | 3:7d9749651663 | 35 | lcd.printf(recv); |
chris | 2:72c06f2902d5 | 36 | } |
chris | 3:7d9749651663 | 37 | |
chris | 2:72c06f2902d5 | 38 | Thread::wait(1000); |
chris | 2:72c06f2902d5 | 39 | } |
chris | 2:72c06f2902d5 | 40 | |
chris | 2:72c06f2902d5 | 41 | modem.disconnect(); |
chris | 2:72c06f2902d5 | 42 | |
chris | 2:72c06f2902d5 | 43 | printf("Disconnected\r\n"); |
chris | 2:72c06f2902d5 | 44 | |
chris | 2:72c06f2902d5 | 45 | modem.power(false); |
chris | 2:72c06f2902d5 | 46 | |
chris | 2:72c06f2902d5 | 47 | printf("Powered off\r\n"); |
chris | 2:72c06f2902d5 | 48 | |
chris | 2:72c06f2902d5 | 49 | } |
chris | 2:72c06f2902d5 | 50 | |
chris | 2:72c06f2902d5 | 51 | |
chris | 2:72c06f2902d5 | 52 | int main() |
chris | 2:72c06f2902d5 | 53 | { |
chris | 2:72c06f2902d5 | 54 | DBG_INIT(); |
chris | 2:72c06f2902d5 | 55 | DBG_SET_SPEED(115200); |
chris | 2:72c06f2902d5 | 56 | DBG_SET_NEWLINE("\r\n"); |
chris | 2:72c06f2902d5 | 57 | Thread testTask(test, NULL, osPriorityNormal, 1024 * 5); |
chris | 2:72c06f2902d5 | 58 | DigitalOut led(LED1); |
chris | 2:72c06f2902d5 | 59 | while(1) { |
chris | 2:72c06f2902d5 | 60 | led=!led; |
chris | 2:72c06f2902d5 | 61 | Thread::wait(1000); |
chris | 2:72c06f2902d5 | 62 | } |
chris | 2:72c06f2902d5 | 63 | |
chris | 2:72c06f2902d5 | 64 | return 0; |
chris | 2:72c06f2902d5 | 65 | } |