Websocket client cellular hello world example
Dependencies: C027_Support WebSocketClient mbed
Fork of Websocket_Ethernet_HelloWorld by
Diff: main.cpp
- Revision:
- 4:b33c0bce17dc
- Parent:
- 3:9bd22e5386cd
- Child:
- 6:828d29dc5562
--- a/main.cpp Fri Oct 25 00:07:04 2013 +0000 +++ b/main.cpp Fri May 09 17:50:56 2014 +0000 @@ -1,47 +1,89 @@ #include "mbed.h" -#include "EthernetInterface.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; - EthernetInterface ethernet; - ethernet.init(); // connect with DHCP - int ret_val = ethernet.connect(); - - if (0 == ret_val) { - printf("IP Address: %s\n", ethernet.getIPAddress()); - } else { - error("ethernet failed to connect: %d.\n", ret_val); - } - - // view @ http://sockets.mbed.org/demo/viewer - Websocket ws("ws://sockets.mbed.org:443/ws/demo/rw"); - ws.connect(); - char str[100]; + // 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); - for(int i=0; i<0x7fffffff; ++i) { - // string with a message - sprintf(str, "%d WebSocket Hello World over Ethernet", 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); + 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(); } } - ws.close(); - ethernet.disconnect(); + mdm.powerOff(); + c027.mdmPower(false); while(true); } \ No newline at end of file