Websocket client cellular hello world example
Dependencies: C027_Support WebSocketClient mbed
Fork of Websocket_Ethernet_HelloWorld by
main.cpp@4:b33c0bce17dc, 2014-05-09 (annotated)
- Committer:
- mazgch
- Date:
- Fri May 09 17:50:56 2014 +0000
- Revision:
- 4:b33c0bce17dc
- Parent:
- 3:9bd22e5386cd
- Child:
- 6:828d29dc5562
first working version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 1:1c1802ec42a2 | 1 | #include "mbed.h" |
mazgch | 4:b33c0bce17dc | 2 | #include "C027.h" |
mazgch | 4:b33c0bce17dc | 3 | #include "MDM.h" |
samux | 1:1c1802ec42a2 | 4 | #include "Websocket.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 | 4:b33c0bce17dc | 36 | c027.gpsPower(true); |
mazgch | 4:b33c0bce17dc | 37 | wait(2); |
mazgch | 4:b33c0bce17dc | 38 | // Create the modem object |
mazgch | 4:b33c0bce17dc | 39 | MDMSerial mdm; |
sam_grove | 3:9bd22e5386cd | 40 | |
mazgch | 4:b33c0bce17dc | 41 | // initialize the modem |
mazgch | 4:b33c0bce17dc | 42 | printf("Device Init\r\n"); |
mazgch | 4:b33c0bce17dc | 43 | MDMParser::DevStatus devStatus; |
mazgch | 4:b33c0bce17dc | 44 | MDMParser::NetStatus netStatus; |
mazgch | 4:b33c0bce17dc | 45 | bool mdmOk = mdm.init(SIMPIN, &devStatus); |
mazgch | 4:b33c0bce17dc | 46 | if (mdmOk) |
mazgch | 4:b33c0bce17dc | 47 | { |
mazgch | 4:b33c0bce17dc | 48 | // wait until we are connected |
mazgch | 4:b33c0bce17dc | 49 | printf("Network Check\r\n"); |
mazgch | 4:b33c0bce17dc | 50 | while (!mdm.checkNetStatus(&netStatus)) |
mazgch | 4:b33c0bce17dc | 51 | wait_ms(1000); |
sam_grove | 3:9bd22e5386cd | 52 | |
mazgch | 4:b33c0bce17dc | 53 | printf("Network Join\r\n"); |
mazgch | 4:b33c0bce17dc | 54 | // join the internet connection |
mazgch | 4:b33c0bce17dc | 55 | MDMParser::IP ip = mdm.join(APN,USERNAME,PASSWORD); |
mazgch | 4:b33c0bce17dc | 56 | if (ip != NOIP) |
mazgch | 4:b33c0bce17dc | 57 | { |
mazgch | 4:b33c0bce17dc | 58 | printf(" IP Address: " IPSTR "\r\n", IPNUM(ip)); |
mazgch | 4:b33c0bce17dc | 59 | // view @ http://sockets.mbed.org/demo/viewer |
mazgch | 4:b33c0bce17dc | 60 | Websocket ws("ws://sockets.mbed.org:443/ws/demo/rw"); |
mazgch | 4:b33c0bce17dc | 61 | |
mazgch | 4:b33c0bce17dc | 62 | printf("Websocket\n"); |
mazgch | 4:b33c0bce17dc | 63 | ws.connect(); |
mazgch | 4:b33c0bce17dc | 64 | char str[100]; |
mazgch | 4:b33c0bce17dc | 65 | printf("Websocket connected\n"); |
mazgch | 4:b33c0bce17dc | 66 | |
mazgch | 4:b33c0bce17dc | 67 | for(int i=0; i<0x7fffffff; ++i) { |
mazgch | 4:b33c0bce17dc | 68 | // string with a message |
mazgch | 4:b33c0bce17dc | 69 | sprintf(str, "%d WebSocket Hello World over Cellular", i); |
mazgch | 4:b33c0bce17dc | 70 | ws.send(str); |
mazgch | 4:b33c0bce17dc | 71 | |
mazgch | 4:b33c0bce17dc | 72 | // clear the buffer and wait a sec... |
mazgch | 4:b33c0bce17dc | 73 | memset(str, 0, 100); |
mazgch | 4:b33c0bce17dc | 74 | wait(0.5f); |
mazgch | 4:b33c0bce17dc | 75 | |
mazgch | 4:b33c0bce17dc | 76 | // websocket server should echo whatever we sent it |
mazgch | 4:b33c0bce17dc | 77 | if (ws.read(str)) { |
mazgch | 4:b33c0bce17dc | 78 | printf("rcv'd: %s\n", str); |
mazgch | 4:b33c0bce17dc | 79 | } |
mazgch | 4:b33c0bce17dc | 80 | } |
mazgch | 4:b33c0bce17dc | 81 | ws.close(); |
mazgch | 4:b33c0bce17dc | 82 | mdm.disconnect(); |
sam_grove | 3:9bd22e5386cd | 83 | } |
samux | 1:1c1802ec42a2 | 84 | } |
mazgch | 4:b33c0bce17dc | 85 | mdm.powerOff(); |
mazgch | 4:b33c0bce17dc | 86 | c027.mdmPower(false); |
sam_grove | 3:9bd22e5386cd | 87 | |
sam_grove | 3:9bd22e5386cd | 88 | while(true); |
samux | 1:1c1802ec42a2 | 89 | } |