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

Dependencies:   WiConnect mbed

Files at this revision

API Documentation at this revision

Comitter:
dan_ackme
Date:
Tue Feb 24 06:26:05 2015 +0000
Commit message:
Initial commit

Changed in this revision

WiConnect.lib Show annotated file Show diff for this revision Revisions of this file
example.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
target_config.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 0cfba8d43d52 WiConnect.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WiConnect.lib	Tue Feb 24 06:26:05 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/teams/ACKme/code/WiConnect/#66beaca0fd1a
diff -r 000000000000 -r 0cfba8d43d52 example.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example.cpp	Tue Feb 24 06:26:05 2015 +0000
@@ -0,0 +1,213 @@
+/**
+ * @example websocket/example.cpp
+ *
+ * This is an example of using the wiconnect API to read data
+ * from a webpage via WebSocket
+ *
+ * It works as follows:
+ * 1. Instantiate the WiConnect Library
+ * 2. Initiate Communication with WiFi Module
+ * 3. Set the network credentials and other settings
+ * 4. Open:
+ *    http://wiconnect.ack.me/_media/2.1/http_server_ws_simple/http_server_ws_simple.html
+ *    In your web browser
+ *    This program detects your browser is connected and displays the data
+ * 5. That's It!
+ *
+ *
+ */
+
+
+/******************************************************************************
+ * Example Variables
+ */
+
+// This is the name of your WiFi network
+// Look for this name in your WiFi settings
+// (e.g. your phone's list of WiFi networks in the WiFi settings menu)
+// tip: add double-quotes around SSID to add spaces to name
+#define NETWORK_SSID "\"<YOUR NETWORK NAME HERE>\""
+
+
+// This is the password of your WiFi network
+// Leave as empty string (e.g "") to connect to OPEN network
+#define NETWORK_PASSWORD "\"<YOUR NETWORK PASSWORD HERE>\""
+
+
+/******************************************************************************
+ * Includes
+ */
+
+// include C library headers
+#include <stdio.h> // needed for printf
+
+// include target specific defines
+#include "target_config.h"
+// include the Wiconnect Host Library API header
+#include "Wiconnect.h"
+
+
+
+/******************************************************************************
+ * Global Defines
+ */
+
+
+// Serial used for printfs to terminal (i.e. NOT used for WiConnect)
+static Serial consoleSerial(STDIO_UART_TX, STDIO_UART_RX);
+uint8_t rxBuffer[128], txBuffer[128];
+
+
+/******************************************************************************
+ * Starting point of application
+ */
+int main(int argc, char **argv)
+{
+    WiconnectResult result;
+    
+    consoleSerial.baud(115200); // console terminal to 115200 baud
+
+    //-------------------------------------------------------------------------
+    // STEP 1: Instantiate WiConnect Library
+    //-------------------------------------------------------------------------
+
+    // Setup  wiconnect serial interface configuration
+    // Here we only specify the rx buffer size and not rx buffer pointer, this means
+    // The serial RX buffer will be dynamically allocated
+    SerialConfig serialConfig(WICONNECT_RX_PIN, WICONNECT_TX_PIN, 256, NULL);
+
+    // Instantiate the Wiconnect library
+    // Here we only specify the buffer size and not buffer pointer, this means
+    // The internal buffer will be dynamically allocated
+    Wiconnect wiconnect(serialConfig, 256, NULL, WICONNECT_RESET_PIN);
+
+    //-------------------------------------------------------------------------
+    // STEP 2: Initiate Communication with WiFi Module
+    //-------------------------------------------------------------------------
+
+    printf("Initializing WiConnect Library...\r\n");
+
+    // Initialize communication with WiFi module
+    if(WICONNECT_FAILED(result, wiconnect.init(false)))
+    {
+        printf("Failed to initialize communication with WiFi module!\r\n"
+                "Make sure the wires are connected correctly\r\n");
+        for(;;); // infinite loop
+    }
+
+
+    //-------------------------------------------------------------------------
+    // STEP 3: Set the network credentials
+    // Note: We manually set the network parameters to ensure SDK backward compatibility
+    //-------------------------------------------------------------------------
+
+    printf("Setting network SSID: %s\r\n", NETWORK_SSID);
+    if(WICONNECT_FAILED(result, wiconnect.setSetting("wlan.ssid", NETWORK_SSID)))
+    {
+        printf("Failed to set wlan.ssid setting\r\n");
+        for(;;); // infinite loop
+    }
+    
+    printf("Setting network password\r\n");
+    if(WICONNECT_FAILED(result, wiconnect.setSetting("wlan.passkey", NETWORK_PASSWORD)))
+    {
+        printf("Failed to set wlan.passkey setting\r\n");
+        for(;;); // infinite loop
+    }
+
+    printf("Enabling mDNS\r\n");
+    if(WICONNECT_FAILED(result, wiconnect.setSetting("mdns.enabled", 1)))
+    {
+        printf("Failed to enable mDNS\r\n");
+        for(;;); // infinite loop
+    }
+
+    printf("Setting mDNS name to: mymodule.local\r\n");
+    if(WICONNECT_FAILED(result, wiconnect.setSetting("mdns.name", "mymodule.local")))
+    {
+        printf("Failed to set mDNS name\r\n");
+        for(;;); // infinite loop
+    }
+
+    printf("Enabling the HTTP Server\r\n");
+    if(WICONNECT_FAILED(result, wiconnect.setSetting("http.server.enabled", 1)))
+    {
+        printf("Failed to set HTTP server enabled\r\n");
+        for(;;); // infinite loop
+    }
+
+    printf("Saving settings to Non-volatile Memory\r\n");
+    if(WICONNECT_FAILED(result, wiconnect.saveSettings()))
+    {
+        printf("Failed save settings\r\n");
+        for(;;); // infinite loop
+    }
+    
+    printf("Brining the network up\r\n");
+    if(WICONNECT_FAILED(result, wiconnect.join()))
+    {
+        printf("Failed to join network: %s\r\n", NETWORK_SSID);
+        for(;;); // infinite loop
+    }
+
+    //-------------------------------------------------------------------------
+    // STEP 4: Wait for a websocket to connect
+    //-------------------------------------------------------------------------
+
+    printf("Using your web browser, go to:\r\n");
+    printf("   http://wiconnect.ack.me/_media/2.1/http_server_ws_simple/http_server_ws_simple.html\r\n");
+    printf("and click the 'Open WebSocket' button.\r\n");
+    printf("Once you do that you'll be able to stream data between the webpage and host micro.\r\n");
+
+    WiconnectSocket websocket(sizeof(rxBuffer), rxBuffer, sizeof(txBuffer), txBuffer);
+    
+    // wait for a client to connect
+    while(true)
+    {
+        printf("\r\nWaiting for websockets to connect...\r\n");
+        if(WICONNECT_FAILED(result, wiconnect.httpAcceptWebSocket(websocket)))
+        {
+            printf("Failed to accept client websocket: %d\r\n", result);
+            continue;
+        }
+
+        printf("Websocket connected!\r\n");
+        printf("Address: %s:%d\r\n", websocket.getHost(), websocket.getRemotePort());
+        printf("Anything typed into the console will be set to the webpage and visa versa\r\n");
+
+        while(true)
+        {
+            bool dataAvailable;
+            websocket.poll(&dataAvailable, true);
+
+            if(!websocket.isConnected())
+            {
+                printf("Websocket disconnect!\r\n");
+                break;
+            }
+
+            if(dataAvailable)
+            {
+                uint8_t *buffer;
+                uint16_t bytesRead;
+                websocket.read(&buffer, &bytesRead);
+                buffer[bytesRead] = 0;
+                printf("%s\r\n", buffer);
+            }
+
+            while(consoleSerial.readable())
+            {
+                websocket.putc(consoleSerial.getc(), false);
+            }
+            // flush the buffer
+            websocket.flushTxBuffer();
+        }
+    }
+
+    //-------------------------------------------------------------------------
+    // STEP 5: Done!
+    //-------------------------------------------------------------------------
+
+    while(true){} // infinite loop
+}
+
diff -r 000000000000 -r 0cfba8d43d52 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Feb 24 06:26:05 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/9ad691361fac
\ No newline at end of file
diff -r 000000000000 -r 0cfba8d43d52 target_config.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/target_config.h	Tue Feb 24 06:26:05 2015 +0000
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2014, ACKme Networks
+ * All Rights Reserved.
+ *
+ * This is UNPUBLISHED PROPRIETARY SOURCE CODE of ACKme Networks;
+ * the contents of this file may not be disclosed to third parties, copied
+ * or duplicated in any form, in whole or in part, without the prior
+ * written permission of ACKme Networks.
+ */
+
+#pragma once
+
+
+// The BAUD rate your PC/MAC/Linux terminal uses with the eval board
+#define CONSOLE_BAUD 115200
+
+
+// Uncomment this to enable WiConnect serial interface hardware flow control
+// NOTE: your platform must support the serial flow control api functions
+//#define ENABLE_FLOW_CONTROL
+
+
+#define WICONNECT_INTERNAL_BUFFER_SIZE (4*1024)
+#define WICONNECT_SERIAL_RX_BUFFER_SIZE (4*1024)
+
+#define DEFAULT_CMD_GETCHAR_TIMEOUT 250
+#define DEFAULT_COMMAND_LINE_LENGTH_MAX 128
+#define DEFAULT_COMMAND_MAX_HISTORY 16
+#define DEFAULT_CMD_PROMPT_STR "> "
+#define DEFAULT_COMMAND_MAX_ARGV 16
+
+#define TEST_NONBLOCKING_API false
+#define TEST_BUFFER_LENGTH 4*1024
+
+
+//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+// Seabass Target Configuration
+#ifdef TARGET_SEABASS
+
+#define WICONNECT_TX_PIN PA_9
+#define WICONNECT_RX_PIN PA_10
+#define WICONNECT_RESET_PIN PB_0
+#define WICONNECT_WAKE_PIN NC
+
+#ifdef ENABLE_FLOW_CONTROL
+#define WICONNECT_CTS_PIN PA_11
+#define WICONNECT_RTS_PIN PA_12
+#else
+#define WICONNECT_CTS_PIN NC
+#define WICONNECT_RTS_PIN NC
+#endif
+
+#endif
+
+
+//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+// Nucleo F401RE Target Configuration
+#ifdef TARGET_NUCLEO_F401RE
+
+#define WICONNECT_TX_PIN PA_9
+#define WICONNECT_RX_PIN PA_10
+#define WICONNECT_RESET_PIN PC_7
+#define WICONNECT_WAKE_PIN NC
+
+#ifdef ENABLE_FLOW_CONTROL
+#define WICONNECT_CTS_PIN PA_11
+#define WICONNECT_RTS_PIN PA_12
+#else
+#define WICONNECT_CTS_PIN NC
+#define WICONNECT_RTS_PIN NC
+#endif
+
+#endif