Websocket example

Dependencies:   C027 UbloxUSBModem WebSocketClient mbed

Fork of VodafoneUSBModemWebsocketTest by Donatien Garnier

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "C027.h"
00003 #include "UbloxUSBGSMModem.h"
00004 #include "UbloxUSBCDMAModem.h"
00005 
00006 #include "Websocket.h"
00007 
00008 C027 c027;
00009 
00010 void test(void const*)
00011 {
00012     c027.mdmPower(true);
00013     c027.mdmReset();
00014     c027.mdmWakeup();
00015     UbloxUSBCDMAModem modem(NC, true, 1); // for LISA-C use the UbloxUSBCDMAModem instead
00016     modem.power(true);
00017     Thread::wait(1000);
00018     
00019     // See the output on http://sockets.mbed.org/demo/viewer
00020     Websocket ws("ws://sockets.mbed.org:443/ws/demo/rw");
00021     char msg[512] = {0};
00022     Timer t;
00023     t.start();
00024     
00025     int ret = modem.connect("internet"); // eventaully set another apn here
00026     if(ret) {
00027         printf("Could not connect\n");
00028         return;
00029     }
00030 
00031     bool c = ws.connect();
00032     printf("Connect result: %s\n", c?"OK":"Failed");
00033 
00034     for(int i = 0; i < 10000; i++) 
00035     {
00036         ws.connect();
00037         // create json string with acc/tmp data
00038         sprintf(msg, "Testing mbed Websockets Loop: %d", i);
00039         ws.send(msg);    
00040         wait(0.5f);
00041         memset(msg, 0, 512);
00042         
00043         if (ws.read(msg))
00044         {
00045             printf("rcv: %s\r\n", msg);
00046         }
00047         else
00048         {
00049             printf("Loop %d ws.read() returns 0\n \t %s, line %d @ %6.2f seconds\n", i, __FILE__, __LINE__, t.read());
00050             wait(5.0f);
00051         }
00052         ws.close();
00053 
00054     }
00055 
00056     modem.disconnect();
00057     c027.mdmPower(false);
00058 
00059     while(1) {
00060     }
00061 }
00062 
00063 
00064 int main()
00065 {
00066     Thread testTask(test, NULL, osPriorityNormal, 1024 * 4);
00067     DigitalOut led(LED); // on rev A you should reasign the signal to A0
00068     while(1) {
00069         led=!led;
00070         Thread::wait(1000);
00071     }
00072 
00073     return 0;
00074 }