Websocket client cellular hello world example
Dependencies: C027_Support WebSocketClient mbed
Fork of Websocket_Ethernet_HelloWorld by
main.cpp@6:828d29dc5562, 2014-05-12 (annotated)
- Committer:
- mazgch
- Date:
- Mon May 12 14:01:26 2014 +0000
- Revision:
- 6:828d29dc5562
- Parent:
- 4:b33c0bce17dc
- Child:
- 7:6468bb94de0c
use latest lib
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 1:1c1802ec42a2 | 1 | #include "mbed.h" |
mazgch | 6:828d29dc5562 | 2 | #include "Websocket.h" |
mazgch | 4:b33c0bce17dc | 3 | #include "C027.h" |
mazgch | 4:b33c0bce17dc | 4 | #include "MDM.h" |
sam_grove | 3:9bd22e5386cd | 5 | |
mazgch | 4:b33c0bce17dc | 6 | //---------------------------------------------------------------------- |
mazgch | 4:b33c0bce17dc | 7 | // You may need to configure these parameters |
mazgch | 4:b33c0bce17dc | 8 | |
mazgch | 4:b33c0bce17dc | 9 | /** Set your secret SIM pin here "1234" |
mazgch | 4:b33c0bce17dc | 10 | */ |
mazgch | 4:b33c0bce17dc | 11 | #define SIMPIN NULL |
mazgch | 4:b33c0bce17dc | 12 | |
mazgch | 4:b33c0bce17dc | 13 | /** The APN of your network operator, sometimes it is "internet" |
mazgch | 4:b33c0bce17dc | 14 | check your contract with the network operator |
mazgch | 4:b33c0bce17dc | 15 | */ |
mazgch | 4:b33c0bce17dc | 16 | #define APN "gprs.swisscom.ch" |
mazgch | 4:b33c0bce17dc | 17 | |
mazgch | 4:b33c0bce17dc | 18 | /** Set the user name for your APN, or NULL if not needed |
mazgch | 4:b33c0bce17dc | 19 | */ |
mazgch | 4:b33c0bce17dc | 20 | #define USERNAME NULL |
mazgch | 4:b33c0bce17dc | 21 | |
mazgch | 4:b33c0bce17dc | 22 | /** Set the password for your APN, or NULL if not needed |
mazgch | 4:b33c0bce17dc | 23 | */ |
mazgch | 4:b33c0bce17dc | 24 | #define PASSWORD NULL |
mazgch | 4:b33c0bce17dc | 25 | |
sam_grove | 3:9bd22e5386cd | 26 | Ticker flash; |
sam_grove | 3:9bd22e5386cd | 27 | DigitalOut led(LED1); |
sam_grove | 3:9bd22e5386cd | 28 | void flashLED(void){led = !led;} |
mazgch | 4:b33c0bce17dc | 29 | C027 c027; |
mazgch | 4:b33c0bce17dc | 30 | |
sam_grove | 3:9bd22e5386cd | 31 | int main() |
sam_grove | 3:9bd22e5386cd | 32 | { |
sam_grove | 3:9bd22e5386cd | 33 | flash.attach(&flashLED, 1.0f); |
mazgch | 4:b33c0bce17dc | 34 | // turn on the supplies of the Modem and the GPS |
mazgch | 4:b33c0bce17dc | 35 | c027.mdmPower(true); |
mazgch | 6:828d29dc5562 | 36 | printf("Modem Initialize\r\n"); |
mazgch | 4:b33c0bce17dc | 37 | MDMSerial mdm; |
mazgch | 6:828d29dc5562 | 38 | if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD, true)) |
mazgch | 6:828d29dc5562 | 39 | return -1; |
sam_grove | 3:9bd22e5386cd | 40 | |
mazgch | 6:828d29dc5562 | 41 | // view @ http://sockets.mbed.org/demo/viewer |
mazgch | 6:828d29dc5562 | 42 | Websocket ws("ws://sockets.mbed.org:443/ws/demo/rw"); |
mazgch | 6:828d29dc5562 | 43 | |
mazgch | 6:828d29dc5562 | 44 | printf("Websocket\n"); |
mazgch | 6:828d29dc5562 | 45 | ws.connect(); |
mazgch | 6:828d29dc5562 | 46 | char str[100]; |
mazgch | 6:828d29dc5562 | 47 | printf("Websocket connected\n"); |
sam_grove | 3:9bd22e5386cd | 48 | |
mazgch | 6:828d29dc5562 | 49 | for(int i=0; i<0x7fffffff; ++i) { |
mazgch | 6:828d29dc5562 | 50 | // string with a message |
mazgch | 6:828d29dc5562 | 51 | sprintf(str, "%d WebSocket Hello World over Cellular", i); |
mazgch | 6:828d29dc5562 | 52 | ws.send(str); |
mazgch | 6:828d29dc5562 | 53 | |
mazgch | 6:828d29dc5562 | 54 | // clear the buffer and wait a sec... |
mazgch | 6:828d29dc5562 | 55 | memset(str, 0, 100); |
mazgch | 6:828d29dc5562 | 56 | wait(0.5f); |
mazgch | 6:828d29dc5562 | 57 | |
mazgch | 6:828d29dc5562 | 58 | // websocket server should echo whatever we sent it |
mazgch | 6:828d29dc5562 | 59 | if (ws.read(str)) { |
mazgch | 6:828d29dc5562 | 60 | printf("rcv'd: %s\n", str); |
sam_grove | 3:9bd22e5386cd | 61 | } |
samux | 1:1c1802ec42a2 | 62 | } |
mazgch | 6:828d29dc5562 | 63 | ws.close(); |
mazgch | 6:828d29dc5562 | 64 | |
mazgch | 6:828d29dc5562 | 65 | mdm.disconnect(); |
mazgch | 4:b33c0bce17dc | 66 | mdm.powerOff(); |
mazgch | 4:b33c0bce17dc | 67 | c027.mdmPower(false); |
mazgch | 6:828d29dc5562 | 68 | printf("Done\n"); |
sam_grove | 3:9bd22e5386cd | 69 | |
sam_grove | 3:9bd22e5386cd | 70 | while(true); |
samux | 1:1c1802ec42a2 | 71 | } |