Nick Desaulniers / Mbed 2 deprecated websocket_server

Dependencies:   EthernetInterface WebSocketClient WiflyInterface mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 //#include "EthernetInterface.h"
00003 #include "WiflyInterface.h"
00004 #include "Websocket.h"
00005 
00006 #define WS_SERVER_IP_ADDR "ws://192.168.42.61:8080/"
00007 #define SSID "mbed"
00008 #define PASSPHRASE "6033937142"
00009 
00010 static DigitalOut led1(LED1);
00011 static DigitalOut led2(LED2);
00012 static DigitalOut led3(LED3);
00013 static DigitalOut led4(LED4);
00014 
00015 static int count = 0;
00016 static char buffer[2];
00017 
00018 /* wifly object where:
00019 *     - p13 and p14 are for the serial communication
00020 *     - p22 is for the reset pin
00021 *     - p21 is for the connection status
00022 *     - SSID is the ssid of the network
00023 *     - PASSPHRASE is the password
00024 *     - WPA is the security
00025 */
00026 WiflyInterface wifly(p13, p14, p22, p21, SSID, PASSPHRASE, WPA);
00027 
00028 void static clear_leds () {
00029   led1 = led2 = led3 = led4 = 0;
00030 }
00031 
00032 void static counter () {
00033   if (count == 99) {
00034     count = 0;
00035   }
00036   count++;
00037   sprintf(buffer, "%d", count);
00038 }
00039 
00040 /*static int connect_eth () {
00041   EthernetInterface eth;
00042   // Use DHCP
00043   eth.init();
00044   eth.connect();
00045   // screen /dev/tty.usbmodem*
00046   //printf("IP Address is %s\n\r", eth.getIPAddress());
00047   return 0;
00048 }*/
00049 
00050 static int connect_wifi () {
00051   wifly.init(); // use DHCP
00052   while (!wifly.connect()); // join the network
00053   return 0;
00054 }
00055 
00056 static Websocket connect_ws (char* ip_addr) {
00057   // Viewable at: http://sockets.mbed.org/demo/viewer
00058   //Websocket ws("ws://sockets.mbed.org:443/ws/demo/wo");
00059 
00060   Websocket ws(ip_addr);
00061   //printf("WS Constructed\n\r");
00062   while(!ws.connect());
00063   //printf("WS Connected\n\r");
00064   return ws;
00065 }
00066 
00067 static Websocket connect (char* ip_addr) {
00068   clear_leds();
00069   //led1 = !connect_eth();
00070   led1 = !connect_wifi();
00071   Websocket ws = connect_ws(ip_addr);
00072   led2 = 1;
00073   return ws;
00074 }
00075 
00076 int main () {
00077   Websocket ws = connect(WS_SERVER_IP_ADDR);
00078   
00079   while (1) {
00080     counter();
00081     if (ws.is_connected()) {
00082       //printf("Sending...%s\n\r", buffer);
00083       ws.send(buffer);
00084     } else {
00085       clear_leds();
00086     }
00087     //wait(0.1);
00088   }
00089 }