Websocket client cellular hello world example

Dependencies:   C027_Support WebSocketClient mbed

Fork of Websocket_Ethernet_HelloWorld by Mbed

main.cpp

Committer:
mazgch
Date:
2014-05-13
Revision:
7:6468bb94de0c
Parent:
6:828d29dc5562
Child:
8:c21f269f5d26

File content as of revision 7:6468bb94de0c:

#include "mbed.h"
#include "Websocket.h"
#include "C027.h"
#include "MDM.h"

//----------------------------------------------------------------------
// You may need to configure these parameters

/** Set your secret SIM pin here "1234"
*/
#define SIMPIN      NULL

/** The APN of your network operator, sometimes it is "internet" 
    check your contract with the network operator
*/
#define APN         "gprs.swisscom.ch"

/** Set the user name for your APN, or NULL if not needed
*/
#define USERNAME    NULL

/** Set the password for your APN, or NULL if not needed
*/
#define PASSWORD    NULL 

Ticker flash;
DigitalOut led(LED1);
void flashLED(void){led = !led;}
C027 c027;
    
int main() 
{
    flash.attach(&flashLED, 1.0f);
    // turn on the supplies of the Modem
    c027.mdmPower(true);
    printf("Modem Initialize\r\n");
    MDMSerial mdm;
    if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD, true))
        return -1;
    
    // view @ http://sockets.mbed.org/demo/viewer
    Websocket ws("ws://sockets.mbed.org:443/ws/demo/rw");
    
    printf("Websocket\n");
    ws.connect();
    char str[100];
    printf("Websocket connected\n");
    
    for(int i=0; i<0x7fffffff; ++i) {
        // string with a message
        sprintf(str, "%d WebSocket Hello World over Cellular", i);
        ws.send(str);
    
        // clear the buffer and wait a sec...
        memset(str, 0, 100);
        wait(0.5f);
    
        // websocket server should echo whatever we sent it
        if (ws.read(str)) {
            printf("rcv'd: %s\n", str);
        }
    }
    ws.close();

    mdm.disconnect();
    mdm.powerOff();
    c027.mdmPower(false);
    printf("Done\n");
    
    while(true);
}