WebSocket client test for AbitUSBModem. see: http://developer.mbed.org/users/phsfan/notebook/abitusbmodem/

Dependencies:   AbitUSBModem USBHost WebSocketClient mbed

Committer:
phsfan
Date:
Wed Feb 25 07:15:03 2015 +0000
Revision:
0:0149c086ff75
1st build

Who changed what in which revision?

UserRevisionLine numberNew contents of line
phsfan 0:0149c086ff75 1 // http://developer.mbed.org/users/donatien/code/VodafoneUSBModemWebsocketTest/
phsfan 0:0149c086ff75 2 #include "mbed.h"
phsfan 0:0149c086ff75 3 #include "AbitUSBModem.h"
phsfan 0:0149c086ff75 4 #include "Websocket.h"
phsfan 0:0149c086ff75 5
phsfan 0:0149c086ff75 6 Serial pc(USBTX, USBRX);
phsfan 0:0149c086ff75 7 DigitalOut myled(LED1);
phsfan 0:0149c086ff75 8
phsfan 0:0149c086ff75 9 void test(void const*)
phsfan 0:0149c086ff75 10 {
phsfan 0:0149c086ff75 11 AbitUSBModem modem;
phsfan 0:0149c086ff75 12 Websocket ws("ws://sockets.mbed.org:443/ws/username/demo/rw");
phsfan 0:0149c086ff75 13 char recv[128];
phsfan 0:0149c086ff75 14
phsfan 0:0149c086ff75 15 printf("connect\r\n");
phsfan 0:0149c086ff75 16 int ret = modem.connect("prin", "prin");
phsfan 0:0149c086ff75 17 if(ret) {
phsfan 0:0149c086ff75 18 printf("Could not connect\r\n");
phsfan 0:0149c086ff75 19 return;
phsfan 0:0149c086ff75 20 }
phsfan 0:0149c086ff75 21
phsfan 0:0149c086ff75 22 printf("IP Address %s\r\n", modem.getIPAddress());
phsfan 0:0149c086ff75 23
phsfan 0:0149c086ff75 24 bool c = ws.connect();
phsfan 0:0149c086ff75 25 printf("Connect result: %s\r\n", c?"OK":"Failed");
phsfan 0:0149c086ff75 26
phsfan 0:0149c086ff75 27 for(int i = 0; i < 10000; i++)
phsfan 0:0149c086ff75 28 {
phsfan 0:0149c086ff75 29 if(!(i%100))
phsfan 0:0149c086ff75 30 {
phsfan 0:0149c086ff75 31 int ret = ws.send("WebSocket Hello World over the Y!mobile Network!");
phsfan 0:0149c086ff75 32 if(ret<0)
phsfan 0:0149c086ff75 33 {
phsfan 0:0149c086ff75 34 printf("Timeout\r\n");
phsfan 0:0149c086ff75 35 ws.close();
phsfan 0:0149c086ff75 36 c = ws.connect();
phsfan 0:0149c086ff75 37 printf("Connect result: %s\r\n", c?"OK":"Failed");
phsfan 0:0149c086ff75 38 }
phsfan 0:0149c086ff75 39 }
phsfan 0:0149c086ff75 40
phsfan 0:0149c086ff75 41 if (ws.read(recv)) {
phsfan 0:0149c086ff75 42 printf("rcv: %s\r\n", recv);
phsfan 0:0149c086ff75 43 }
phsfan 0:0149c086ff75 44 }
phsfan 0:0149c086ff75 45
phsfan 0:0149c086ff75 46 Thread::wait(1000);
phsfan 0:0149c086ff75 47 printf("exit\r\n");
phsfan 0:0149c086ff75 48 modem.disconnect();
phsfan 0:0149c086ff75 49 }
phsfan 0:0149c086ff75 50
phsfan 0:0149c086ff75 51 int main()
phsfan 0:0149c086ff75 52 {
phsfan 0:0149c086ff75 53 pc.baud(115200);
phsfan 0:0149c086ff75 54 printf("** PHS MQTT\r\n");
phsfan 0:0149c086ff75 55
phsfan 0:0149c086ff75 56 Thread testTask(test, NULL, osPriorityNormal, 1024 * 4);
phsfan 0:0149c086ff75 57 while(1) {
phsfan 0:0149c086ff75 58 myled = !myled;
phsfan 0:0149c086ff75 59 Thread::wait(1000);
phsfan 0:0149c086ff75 60 }
phsfan 0:0149c086ff75 61 }