C

The next code is based on language C. This code would help us to connect and get the ip retrieved.

First we need to import the libraries of wifi module.

/* system header files */
#include "stdio.h"
/* additional interface header files */
#include "FreeRTOS.h"
#include "timers.h"

#include "BCDS_NetworkConfig.h"
#include "BCDS_WlanConnect.h"

#include "BCDS_CmdProcessor.h"
#include "BCDS_Assert.h"

We need to set up the SSID and the password with your respective credentials.

/* macros ******************************************************************* */

#warning Set these Macros accordingly, before running this application
#define USE_DHCP    1    // 1: DHCP; 0: Static IP
#define USE_WPA        1    // 1: WPA; 0: Open Access point
#define SSID        "yourWifiNetworkSSID"
#define PW            "yourWifiNetworkPW"

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

We define a function to initialized the wifi, and use the method of WlanConnect_Init()

* If the Wi-Fi is initialized, it is possible to scan for surrounding Wi-Fi networks before connecting to them. In order to do that, three variable types are of particular interest.

Retcode_T - Return state for programable request logics

WlanConnect_ScanInterval_T - Defined time period in which intervals the scan occurs

WlanConnect_ScanList_T - A struct which contains the scanned network informations.

/* local functions ********************************************************** */
static void initWifi(void)
{
  /* initialize the Wi-Fi module */
  WlanConnect_Init();
}

The function NetworkConfig_Ipv4Value() converts the committed decimal IP parameters to hexadecimal IP parameters.

The NetworkConfig_SetIpStatic() function sets the committed IP parameters in the interface.

static void setIPSettings(void) {
#if USE_DHCP
    NetworkConfig_SetIpDhcp(NULL);
#else
    NetworkConfig_IpSettings_T myIpSet;
    myIpSet.isDHCP = (uint8_t) NETWORKCONFIG_DHCP_DISABLED;
    myIpSet.ipV4 = NetworkConfig_Ipv4Value(192, 168, 0, 2);
    myIpSet.ipV4DnsServer = NetworkConfig_Ipv4Value(192, 168, 0, 1);
    myIpSet.ipV4Gateway = NetworkConfig_Ipv4Value(192, 168, 0, 1);
    myIpSet.ipV4Mask = NetworkConfig_Ipv4Value(255, 255, 255, 0);
    NetworkConfig_SetIpStatic(myIpSet);
#endif
}
static void connectWiFi(void){
    WlanConnect_SSID_T connectSSID = (WlanConnect_SSID_T) SSID;
#if USE_WPA
    WlanConnect_PassPhrase_T connectPassPhrase = (WlanConnect_PassPhrase_T) PW;
    WlanConnect_WPA(connectSSID, connectPassPhrase, NULL);
#else
    WlanConnect_Open(connectSSID, NULL);
#endif // USE_WPA
}

static void printIP(void){
    NetworkConfig_IpSettings_T myIp;
    NetworkConfig_GetIpSettings(&myIp);

    // insert a delay here, if the IP is not properly printed
    printf("The IP was retrieved: %u.%u.%u.%u \n\r",
    (unsigned int) (NetworkConfig_Ipv4Byte(myIp.ipV4, 3)),
    (unsigned int) (NetworkConfig_Ipv4Byte(myIp.ipV4, 2)),
    (unsigned int) (NetworkConfig_Ipv4Byte(myIp.ipV4, 1)),
    (unsigned int) (NetworkConfig_Ipv4Byte(myIp.ipV4, 0)));
}

/* global functions ********************************************************* */

/**
 * @brief This is a template function where the user can write his custom application.
 *
 */
CmdProcessor_T *appCmdProcessor;

void appInitSystem(void *CmdProcessorHandle, uint32_t param2) {
    if(CmdProcessorHandle == NULL){
        printf("Command processor handle is null \n\r");
        assert(false);
    }
    BCDS_UNUSED(param2);
    appCmdProcessor = CmdProcessorHandle;

    initWifi();
    setIPSettings();
    connectWiFi();
    printIP();
}

Last updated

Was this helpful?