Websocket client cellular hello world example

Dependencies:   C027_Support WebSocketClient mbed

Fork of Websocket_Ethernet_HelloWorld by Mbed

Committer:
mazgch
Date:
Tue May 27 09:17:25 2014 +0000
Revision:
8:c21f269f5d26
Parent:
7:6468bb94de0c
Child:
9:89538672454c
pull latest lib (update API)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 1:1c1802ec42a2 1 #include "mbed.h"
mazgch 6:828d29dc5562 2 #include "Websocket.h"
mazgch 8:c21f269f5d26 3
mazgch 8:c21f269f5d26 4 //------------------------------------------------------------------------------------
mazgch 8:c21f269f5d26 5 // You need to configure these cellular modem / SIM parameters.
mazgch 8:c21f269f5d26 6 // These parameters are ignored for LISA-C200 variants and can be left NULL.
mazgch 8:c21f269f5d26 7 //------------------------------------------------------------------------------------
mazgch 4:b33c0bce17dc 8 #include "MDM.h"
mazgch 8:c21f269f5d26 9 //! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual.
mazgch 4:b33c0bce17dc 10 #define SIMPIN NULL
mazgch 8:c21f269f5d26 11 /*! The APN of your network operator SIM, sometimes it is "internet" check your
mazgch 8:c21f269f5d26 12 contract with the network operator. You can also try to look-up your settings in
mazgch 8:c21f269f5d26 13 google: https://www.google.de/search?q=APN+list */
mazgch 4:b33c0bce17dc 14 #define APN "gprs.swisscom.ch"
mazgch 8:c21f269f5d26 15 //! Set the user name for your APN, or NULL if not needed
mazgch 4:b33c0bce17dc 16 #define USERNAME NULL
mazgch 8:c21f269f5d26 17 //! Set the password for your APN, or NULL if not needed
mazgch 4:b33c0bce17dc 18 #define PASSWORD NULL
mazgch 8:c21f269f5d26 19 //------------------------------------------------------------------------------------
mazgch 4:b33c0bce17dc 20
sam_grove 3:9bd22e5386cd 21 Ticker flash;
sam_grove 3:9bd22e5386cd 22 DigitalOut led(LED1);
sam_grove 3:9bd22e5386cd 23 void flashLED(void){led = !led;}
mazgch 4:b33c0bce17dc 24
sam_grove 3:9bd22e5386cd 25 int main()
sam_grove 3:9bd22e5386cd 26 {
sam_grove 3:9bd22e5386cd 27 flash.attach(&flashLED, 1.0f);
mazgch 4:b33c0bce17dc 28 MDMSerial mdm;
mazgch 8:c21f269f5d26 29 mdm.setDebug(4); // enable this for debugging issues
mazgch 8:c21f269f5d26 30 if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD))
mazgch 6:828d29dc5562 31 return -1;
sam_grove 3:9bd22e5386cd 32
mazgch 6:828d29dc5562 33 // view @ http://sockets.mbed.org/demo/viewer
mazgch 8:c21f269f5d26 34 printf("Websocket View at http://sockets.mbed.org/demo/viewer\n");
mazgch 6:828d29dc5562 35 Websocket ws("ws://sockets.mbed.org:443/ws/demo/rw");
mazgch 6:828d29dc5562 36
mazgch 6:828d29dc5562 37 printf("Websocket\n");
mazgch 6:828d29dc5562 38 ws.connect();
mazgch 6:828d29dc5562 39 char str[100];
mazgch 6:828d29dc5562 40 printf("Websocket connected\n");
sam_grove 3:9bd22e5386cd 41
mazgch 6:828d29dc5562 42 for(int i=0; i<0x7fffffff; ++i) {
mazgch 6:828d29dc5562 43 // string with a message
mazgch 6:828d29dc5562 44 sprintf(str, "%d WebSocket Hello World over Cellular", i);
mazgch 6:828d29dc5562 45 ws.send(str);
mazgch 6:828d29dc5562 46
mazgch 6:828d29dc5562 47 // clear the buffer and wait a sec...
mazgch 6:828d29dc5562 48 memset(str, 0, 100);
mazgch 6:828d29dc5562 49 wait(0.5f);
mazgch 6:828d29dc5562 50
mazgch 6:828d29dc5562 51 // websocket server should echo whatever we sent it
mazgch 6:828d29dc5562 52 if (ws.read(str)) {
mazgch 6:828d29dc5562 53 printf("rcv'd: %s\n", str);
sam_grove 3:9bd22e5386cd 54 }
samux 1:1c1802ec42a2 55 }
mazgch 6:828d29dc5562 56 ws.close();
mazgch 6:828d29dc5562 57
mazgch 6:828d29dc5562 58 mdm.disconnect();
mazgch 4:b33c0bce17dc 59 mdm.powerOff();
sam_grove 3:9bd22e5386cd 60
sam_grove 3:9bd22e5386cd 61 while(true);
samux 1:1c1802ec42a2 62 }