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-09
Revision:
4:b33c0bce17dc
Parent:
3:9bd22e5386cd
Child:
6:828d29dc5562

File content as of revision 4:b33c0bce17dc:

#include "mbed.h"
#include "C027.h"
#include "MDM.h"
#include "Websocket.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 and the GPS
    c027.mdmPower(true);
    c027.gpsPower(true);
    wait(2);
    // Create the modem object
    MDMSerial mdm;
    
    // initialize the modem 
    printf("Device Init\r\n");
    MDMParser::DevStatus devStatus;
    MDMParser::NetStatus netStatus;
    bool mdmOk = mdm.init(SIMPIN, &devStatus);
    if (mdmOk)
    {
        // wait until we are connected
        printf("Network Check\r\n");
        while (!mdm.checkNetStatus(&netStatus))
            wait_ms(1000);
    
        printf("Network Join\r\n");
        // join the internet connection 
        MDMParser::IP ip = mdm.join(APN,USERNAME,PASSWORD);
        if (ip != NOIP)
        {
            printf("  IP Address: " IPSTR "\r\n", IPNUM(ip));
            // 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);
    
    while(true);
}