Example application demonstrating the WiConnect Host Library and HTTP Server WebSockets.

Dependencies:   WiConnect mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers example.cpp Source File

example.cpp

00001 /**
00002  * @example websocket/example.cpp
00003  *
00004  * This is an example of using the wiconnect API to read data
00005  * from a webpage via WebSocket
00006  *
00007  * It works as follows:
00008  * 1. Instantiate the WiConnect Library
00009  * 2. Initiate Communication with WiFi Module
00010  * 3. Set the network credentials and other settings
00011  * 4. Open:
00012  *    http://wiconnect.ack.me/_media/2.1/http_server_ws_simple/http_server_ws_simple.html
00013  *    In your web browser
00014  *    This program detects your browser is connected and displays the data
00015  * 5. That's It!
00016  *
00017  *
00018  */
00019 
00020 
00021 /******************************************************************************
00022  * Example Variables
00023  */
00024 
00025 // This is the name of your WiFi network
00026 // Look for this name in your WiFi settings
00027 // (e.g. your phone's list of WiFi networks in the WiFi settings menu)
00028 // tip: add double-quotes around SSID to add spaces to name
00029 #define NETWORK_SSID "\"<YOUR NETWORK NAME HERE>\""
00030 
00031 
00032 // This is the password of your WiFi network
00033 // Leave as empty string (e.g "") to connect to OPEN network
00034 #define NETWORK_PASSWORD "\"<YOUR NETWORK PASSWORD HERE>\""
00035 
00036 
00037 /******************************************************************************
00038  * Includes
00039  */
00040 
00041 // include C library headers
00042 #include <stdio.h> // needed for printf
00043 
00044 // include target specific defines
00045 #include "target_config.h"
00046 // include the Wiconnect Host Library API header
00047 #include "Wiconnect.h"
00048 
00049 
00050 
00051 /******************************************************************************
00052  * Global Defines
00053  */
00054 
00055 
00056 // Serial used for printfs to terminal (i.e. NOT used for WiConnect)
00057 static Serial consoleSerial(STDIO_UART_TX, STDIO_UART_RX);
00058 uint8_t rxBuffer[128], txBuffer[128];
00059 
00060 
00061 /******************************************************************************
00062  * Starting point of application
00063  */
00064 int main(int argc, char **argv)
00065 {
00066     WiconnectResult result;
00067     
00068     consoleSerial.baud(115200); // console terminal to 115200 baud
00069 
00070     //-------------------------------------------------------------------------
00071     // STEP 1: Instantiate WiConnect Library
00072     //-------------------------------------------------------------------------
00073 
00074     // Setup  wiconnect serial interface configuration
00075     // Here we only specify the rx buffer size and not rx buffer pointer, this means
00076     // The serial RX buffer will be dynamically allocated
00077     SerialConfig serialConfig(WICONNECT_RX_PIN, WICONNECT_TX_PIN, 256, NULL);
00078 
00079     // Instantiate the Wiconnect library
00080     // Here we only specify the buffer size and not buffer pointer, this means
00081     // The internal buffer will be dynamically allocated
00082     Wiconnect wiconnect(serialConfig, 256, NULL, WICONNECT_RESET_PIN);
00083 
00084     //-------------------------------------------------------------------------
00085     // STEP 2: Initiate Communication with WiFi Module
00086     //-------------------------------------------------------------------------
00087 
00088     printf("Initializing WiConnect Library...\r\n");
00089 
00090     // Initialize communication with WiFi module
00091     if(WICONNECT_FAILED(result, wiconnect.init(false)))
00092     {
00093         printf("Failed to initialize communication with WiFi module!\r\n"
00094                 "Make sure the wires are connected correctly\r\n");
00095         for(;;); // infinite loop
00096     }
00097 
00098 
00099     //-------------------------------------------------------------------------
00100     // STEP 3: Set the network credentials
00101     // Note: We manually set the network parameters to ensure SDK backward compatibility
00102     //-------------------------------------------------------------------------
00103 
00104     printf("Setting network SSID: %s\r\n", NETWORK_SSID);
00105     if(WICONNECT_FAILED(result, wiconnect.setSetting("wlan.ssid", NETWORK_SSID)))
00106     {
00107         printf("Failed to set wlan.ssid setting\r\n");
00108         for(;;); // infinite loop
00109     }
00110     
00111     printf("Setting network password\r\n");
00112     if(WICONNECT_FAILED(result, wiconnect.setSetting("wlan.passkey", NETWORK_PASSWORD)))
00113     {
00114         printf("Failed to set wlan.passkey setting\r\n");
00115         for(;;); // infinite loop
00116     }
00117 
00118     printf("Enabling mDNS\r\n");
00119     if(WICONNECT_FAILED(result, wiconnect.setSetting("mdns.enabled", 1)))
00120     {
00121         printf("Failed to enable mDNS\r\n");
00122         for(;;); // infinite loop
00123     }
00124 
00125     printf("Setting mDNS name to: mymodule.local\r\n");
00126     if(WICONNECT_FAILED(result, wiconnect.setSetting("mdns.name", "mymodule.local")))
00127     {
00128         printf("Failed to set mDNS name\r\n");
00129         for(;;); // infinite loop
00130     }
00131 
00132     printf("Enabling the HTTP Server\r\n");
00133     if(WICONNECT_FAILED(result, wiconnect.setSetting("http.server.enabled", 1)))
00134     {
00135         printf("Failed to set HTTP server enabled\r\n");
00136         for(;;); // infinite loop
00137     }
00138 
00139     printf("Saving settings to Non-volatile Memory\r\n");
00140     if(WICONNECT_FAILED(result, wiconnect.saveSettings()))
00141     {
00142         printf("Failed save settings\r\n");
00143         for(;;); // infinite loop
00144     }
00145     
00146     printf("Brining the network up\r\n");
00147     if(WICONNECT_FAILED(result, wiconnect.join()))
00148     {
00149         printf("Failed to join network: %s\r\n", NETWORK_SSID);
00150         for(;;); // infinite loop
00151     }
00152 
00153     //-------------------------------------------------------------------------
00154     // STEP 4: Wait for a websocket to connect
00155     //-------------------------------------------------------------------------
00156 
00157     printf("Using your web browser, go to:\r\n");
00158     printf("   http://wiconnect.ack.me/_media/2.1/http_server_ws_simple/http_server_ws_simple.html\r\n");
00159     printf("and click the 'Open WebSocket' button.\r\n");
00160     printf("Once you do that you'll be able to stream data between the webpage and host micro.\r\n");
00161 
00162     WiconnectSocket websocket(sizeof(rxBuffer), rxBuffer, sizeof(txBuffer), txBuffer);
00163     
00164     // wait for a client to connect
00165     while(true)
00166     {
00167         printf("\r\nWaiting for websockets to connect...\r\n");
00168         if(WICONNECT_FAILED(result, wiconnect.httpAcceptWebSocket(websocket)))
00169         {
00170             printf("Failed to accept client websocket: %d\r\n", result);
00171             continue;
00172         }
00173 
00174         printf("Websocket connected!\r\n");
00175         printf("Address: %s:%d\r\n", websocket.getHost(), websocket.getRemotePort());
00176         printf("Anything typed into the console will be set to the webpage and visa versa\r\n");
00177 
00178         while(true)
00179         {
00180             bool dataAvailable;
00181             websocket.poll(&dataAvailable, true);
00182 
00183             if(!websocket.isConnected())
00184             {
00185                 printf("Websocket disconnect!\r\n");
00186                 break;
00187             }
00188 
00189             if(dataAvailable)
00190             {
00191                 uint8_t *buffer;
00192                 uint16_t bytesRead;
00193                 websocket.read(&buffer, &bytesRead);
00194                 buffer[bytesRead] = 0;
00195                 printf("%s\r\n", buffer);
00196             }
00197 
00198             while(consoleSerial.readable())
00199             {
00200                 websocket.putc(consoleSerial.getc(), false);
00201             }
00202             // flush the buffer
00203             websocket.flushTxBuffer();
00204         }
00205     }
00206 
00207     //-------------------------------------------------------------------------
00208     // STEP 5: Done!
00209     //-------------------------------------------------------------------------
00210 
00211     while(true){} // infinite loop
00212 }
00213