Websocket Hello World over a wifi network
Dependencies: EthernetNetIf mbed DNSResolver
main.cpp@8:02df37981408, 2012-02-01 (annotated)
- Committer:
- samux
- Date:
- Wed Feb 01 17:24:14 2012 +0000
- Revision:
- 8:02df37981408
- Parent:
- 7:1c874f214a98
- Child:
- 9:7b9912c6d812
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 8:02df37981408 | 1 | #include "mbed.h" |
samux | 8:02df37981408 | 2 | #include "Wifly.h" |
samux | 8:02df37981408 | 3 | #include "Websocket.h" |
samux | 8:02df37981408 | 4 | |
samux | 8:02df37981408 | 5 | DigitalOut l1(LED1); |
samux | 8:02df37981408 | 6 | |
samux | 8:02df37981408 | 7 | //Here, we create an instance, with pins 9 and 10 connecting to the |
samux | 8:02df37981408 | 8 | //WiFly's TX and RX pins, and pin 21 to RESET. We are connecting to the |
samux | 8:02df37981408 | 9 | //"mbed" network, password "password", and we are using WPA. |
samux | 8:02df37981408 | 10 | Wifly wifly(p9, p10, p21, "mbed", "mbedapm2011", true); |
samux | 8:02df37981408 | 11 | |
samux | 8:02df37981408 | 12 | //Here, we create a Websocket instance in 'rw' (read-write) mode |
samux | 8:02df37981408 | 13 | //on the 'samux' channel |
samux | 8:02df37981408 | 14 | Websocket ws("ws://sockets.mbed.org/ws/samux/rw", &wifly); |
samux | 8:02df37981408 | 15 | |
samux | 8:02df37981408 | 16 | |
samux | 8:02df37981408 | 17 | int main() { |
samux | 8:02df37981408 | 18 | char recv[40]; |
samux | 8:02df37981408 | 19 | |
samux | 8:02df37981408 | 20 | while (1) { |
samux | 8:02df37981408 | 21 | |
samux | 8:02df37981408 | 22 | //we connect the network |
samux | 8:02df37981408 | 23 | while (!wifly.join()) { |
samux | 8:02df37981408 | 24 | wifly.reset(); |
samux | 8:02df37981408 | 25 | } |
samux | 8:02df37981408 | 26 | |
samux | 8:02df37981408 | 27 | //we connect to the websocket server |
samux | 8:02df37981408 | 28 | while (!ws.connect()); |
samux | 8:02df37981408 | 29 | |
samux | 8:02df37981408 | 30 | while (1) { |
samux | 8:02df37981408 | 31 | wait(0.5); |
samux | 8:02df37981408 | 32 | |
samux | 8:02df37981408 | 33 | //Send Hello world |
samux | 8:02df37981408 | 34 | ws.send("Hello World! over Wifi"); |
samux | 8:02df37981408 | 35 | |
samux | 8:02df37981408 | 36 | // if a message is available, print it |
samux | 8:02df37981408 | 37 | if (ws.read(recv)) { |
samux | 8:02df37981408 | 38 | // show that we receive messages |
samux | 8:02df37981408 | 39 | l1 = !l1; |
samux | 8:02df37981408 | 40 | } |
samux | 8:02df37981408 | 41 | } |
samux | 8:02df37981408 | 42 | } |
samux | 0:e10d21debdaa | 43 | } |