Simple Wi-Fi Connection

  1. General
  2. Initialization and Settings
  3. Connecting
  4. Full Code Example

General

In this section, an example for connecting to a Wi-Fi network will be implemented. Wi-Fi is one of the three connectivity methods to connect the XDK to external devices. The other two options to connect are via Bluetooth LE and the Extension Bus. This section focuses mainly on the implementation of a simple Wi-Fi connection. For in-depth explanations of specific functions, feel free to take a view to the Wi-Fi section.

Note: The implementation will be based on an XdkApplicationTemplate project. The functions AppControllerSetup() and AppControllerEnable() are required in every project. They should not be removed, modified or have its signature altered.

To provide all necessary functions for the Wi-Fi module, such as initializing, setting up the IP configuration and of course connecting, the following interface needs to be included.

#include "XDK_WLAN.h"

Initialization and Settings

The first step to initialize the Wi-Fi module, is to create a struct of the type WLAN_Setup_T and implement it as a global variable.

static WLAN_Setup_T WiFiSetup = {
                .IsEnterprise = false,
                .IsHostPgmEnabled = false,
                .SSID = "yourNetworkSSID",
                .Password = "yourNetworkPassphrase",
                .IsStatic = false,
        };/**< WLAN setup parameters */

This struct contains all required configuration settings for the Wi-Fi connection. The first configuration in this struct declares, if the connection should be established to an Enterprise WPA2 Wi-Fi network or to a Personal WPA network. This gets clarified by the bool value true or false for the element .IsEnterprise.

The next configuration element .IsHostPgmEnabled is only necessary, if the connection to an Enterprise WPA2 Wi-Fi network should be established and the firmware of the Wi-Fi chip on the XDK requires an update.

The SSID and the password are passed in the configuration elements .SSID and .Password. The attendant strings should be changed to the local Wi-Fi network’s credentials.

To connect the XDK to a Wi-Fi access point, the IP settings have to be determined beforehand. The given options are DHCP or a static IP. These are to set at the configuration item .IsStatic. In case of using DHCP, a false statement needs to be passed in.

Otherwise, if a static IP is required, additional configuration items of the struct WLAN_Setup_T need to get implemented and filled.

static WLAN_Setup_T WiFiSetup = {
                .IsEnterprise = false,
                .IsHostPgmEnabled = false,
                .SSID = "yourNetworkSSID",
                .Password = "yourNetworkPassphrase",
                .IsStatic = true,
                .IpAddr = XDK_NETWORK_IPV4(192, 168, 0, 2),
                .GwAddr = XDK_NETWORK_IPV4(192, 168, 0, 1),
                .DnsAddr = XDK_NETWORK_IPV4(192, 168, 0, 1),
                .Mask = XDK_NETWORK_IPV4(255, 255, 255, 0),
        };/**< WLAN setup parameters */

The names of the individual settings do describe themselves, as they are mandatory for a static IP configuration.

The next step is to initialize the Wi-Fi module.

static void AppControllerSetup(void * param1, uint32_t param2) {

    // Previous AppControllerSetup() implementation here
    Retcode_T retcode = RETCODE_OK;

    vTaskDelay(UINT32_C(5000));
    retcode = WLAN_Setup(&WiFiSetup);

    if (RETCODE_OK == retcode) {
        printf("WiFi Setup succeeded \n\r");
    }

    retcode = CmdProcessor_Enqueue(AppCmdProcessor, AppControllerEnable, NULL, UINT32_C(0));

    // Further AppControllerSetup() implementation here
}

Connecting

Now, the connecting network, the Wi-Fi credentials and IP settings are configured properly, establishing a connection is straight-forward. This is done within the function AppControllerEnable().

static void AppControllerEnable(void * param1, uint32_t param2) {

    BCDS_UNUSED(param1);
    BCDS_UNUSED(param2);
    Retcode_T retcode = RETCODE_OK;

    retcode = WLAN_Enable();

    if (RETCODE_OK == retcode) {
        printf("WiFi connection established \n\r");
    }
}

To connect to a Wi-Fi network, Enterprise or Personal, a final step, a call of the function WLAN_Enable() is required. Afterwards, the connection will establish, and the function will print the XDK’s static or dynamic IP address to the XDK-Workbench console.

Finally, the first Wi-Fi connection application is ready to be built and flashed onto the XDK.

Full Code Example

/* module includes ********************************************************** */

/* own header files */
#include "XdkAppInfo.h"
#undef BCDS_MODULE_ID  /* Module ID define before including Basics package*/
#define BCDS_MODULE_ID XDK_APP_MODULE_ID_APP_CONTROLLER

/* own header files */
#include "AppController.h"

/* system header files */
#include <stdio.h>

/* additional interface header files */
#include "BCDS_CmdProcessor.h"
#include "FreeRTOS.h"
#include "task.h"

#include "XDK_WLAN.h"

/* constant definitions ***************************************************** */

/* local variables ********************************************************** */

static CmdProcessor_T * AppCmdProcessor;

/* global variables ********************************************************* */

static WLAN_Setup_T WiFiSetup = {
                .IsEnterprise = false,
                .IsHostPgmEnabled = false,
                .SSID = "yourNetworkSSID",
                .Password = "yourNetworkPassphrase",
                .IsStatic = false,
        };/**< WLAN setup parameters */

/* local functions ********************************************************** */

static void AppControllerEnable(void * param1, uint32_t param2) {

    BCDS_UNUSED(param1);
    BCDS_UNUSED(param2);
    Retcode_T retcode = RETCODE_OK;

    retcode = WLAN_Enable();

    if (RETCODE_OK == retcode) {
        printf("WiFi connection established \n\r");
    }
}

static void AppControllerSetup(void * param1, uint32_t param2)
{
    BCDS_UNUSED(param1);
    BCDS_UNUSED(param2);
    Retcode_T retcode = RETCODE_OK;

    vTaskDelay(UINT32_C(5000));
    retcode = WLAN_Setup(&WiFiSetup);

    if (RETCODE_OK == retcode) {
        printf("WiFi Setup succeeded \n\r");
    }

    retcode = CmdProcessor_Enqueue(AppCmdProcessor, AppControllerEnable, NULL, UINT32_C(0));
    if (RETCODE_OK != retcode) {
        printf("AppControllerSetup : Failed \r\n");
        Retcode_RaiseError(retcode);
        assert(0);
    }
}

void AppController_Init(void * cmdProcessorHandle, uint32_t param2)
{
    BCDS_UNUSED(param2);

    Retcode_T retcode = RETCODE_OK;

    if (cmdProcessorHandle == NULL) {
        printf("AppController_Init : Command processor handle is NULL \r\n");
        retcode = RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_NULL_POINTER);
    }
    else {
        AppCmdProcessor = (CmdProcessor_T *) cmdProcessorHandle;
        retcode = CmdProcessor_Enqueue(AppCmdProcessor, AppControllerSetup, NULL, UINT32_C(0));
    }

    if (RETCODE_OK != retcode) {
        Retcode_RaiseError(retcode);
        assert(0);
    }
}

/** ************************************************************************* */