WiFi Scan

From openPicus Wiki
Jump to: navigation, search

Introduction

This tutorial show how to perform a Wi-Fi network scan using Flyport Wi-Fi.

How to use it

Copy and paste the following code example in a Flyport Wi-Fi project. Compile and download.
Scanning is started using the function WFScan(). When the WiFi Scan process is completed, the variable BOOL ScanCompleted is set to TRUE by the event function void OnScanResult(UINT8 ev, UINT16 evinfo) (inside WF_Events.c file).
The number of discovered networks is available in the system variable WFNetworkFound.
All the data related to the discovered networks is located in Flyport RAM. Data is read using the WFScanList(int n) command. The range of n varies from 1 to the number of networks found.

When scan is completed, results are reported via Serial Monitor of openPicus IDE.
Network scan can be performed only if Flyport is not connected to any network, so in the following example Scan is performed BEFORE Flyport starts Wi-Fi connection.

Code Example

#include "taskFlyport.h"
 
extern BOOL ScanCompleted;     // This var is declared in WF_Events.c file
 
void FlyportTask()
{
    vTaskDelay(200);        // Delay to wait the TCP stack and hardware initializing properly
    tWFNetwork NetData;        // tWFNetwork is a struct that contains all network parameters
 
    _dbgwrite("Starting Network scan function...");
    // Before to connect to default network, use the Scan function:
    WFScan();
 
    while(ScanCompleted != TRUE)
        vTaskDelay(5);
 
    _dbgwrite("done!\r\n");
    char netf[2];
    _dbgwrite("** *Networks discovered: ");
    sprintf(netf,"%d",WFNetworkFound);    // WFNetworkFound is a system variable, contains the number of discovered networks
    _dbgwrite(netf);
    _dbgwrite(" ***\r\n");
    int pos = 1;
    char datatemp[2];
 
    // Reads all discovered networks stored in the memory of Flyport and writes the results
    while (pos < (WFNetworkFound+1))
    {
        sprintf(datatemp,"%d",pos);
        _dbgwrite(datatemp);
        _dbgwrite(" - ");
        NetData = WFScanList(pos);        // Command to read the stored network parameters
        _dbgwrite("SSID: ");
        _dbgwrite(NetData.ssid);
        _dbgwrite("\r\nNetwork type: ");
        if (NetData.type == WF_INFRASTRUCTURE)
             _dbgwrite(" infrastructure");
        else
             _dbgwrite(" ad hoc");
        _dbgwrite("\r\nSignal strength:");
        sprintf (datatemp,"%d",NetData.signal);
        _dbgwrite(datatemp);
        _dbgwrite("\r\nChannel: ");
        sprintf (datatemp,"%d",NetData.channel);
        _dbgwrite(datatemp);
        _dbgwrite("\r\nBeacon: ");
        sprintf (datatemp,"%d",NetData.beacon);
        _dbgwrite(datatemp);                    
        if (NetData.security == WF_SECURITY_OPEN)
        _dbgwrite("\r\nSecurity OPEN\r\n");
        if (NetData.security == WF_SECURITY_GENERIC_WPA)
        _dbgwrite("\r\nSecurity WPA\r\n");
        if (NetData.security == WF_SECURITY_GENERIC_WPA2)
        _dbgwrite("\r\nSecurity WPA2\r\n");
        if (NetData.security == WF_SECURITY_GENERIC_WEP)
        _dbgwrite("\r\nSecurity WEP\r\n");
        _dbgwrite("***\r\n");    
 
        pos++;
    }
 
    _dbgwrite("Scan Completed, Flyport is starting connection to DEFAULT network!\r\n");
 
    // Flyport connects to default network
    WFConnect(WF_DEFAULT);
    while(WFGetStat() != CONNECTED);
    vTaskDelay(25);
    _dbgwrite("Flyport Wi-fi connected...hello world!\r\n");
 
    while(1)
    {
        //    Main user's firmware loop
    }
}
Personal tools
Namespaces

Variants
Actions
START HERE
DEVELOPMENT
HARDWARE INFO
RESOURCES
PHASED OUT
Toolbox