Station API

Station API is used to work with ESP acting in station mode. It allows to join other access point, scan for available access points or simply disconnect from it.

An example below is showing how all examples (coming with this library) scan for access point and then try to connect to AP from list of preferred one.

Station manager used with all examples
  1#include "station_manager.h"
  2#include "utils.h"
  3#include "lwesp/lwesp.h"
  4
  5/*
  6 * List of preferred access points for ESP device
  7 * SSID and password
  8 *
  9 * ESP will try to scan for access points
 10 * and then compare them with the one on the list below
 11 */
 12ap_entry_t
 13ap_list[] = {
 14    //{ "SSID name", "SSID password" },
 15    { "TilenM_ST", "its private" },
 16    { "Majerle WIFI", "majerle_internet_private" },
 17    { "Majerle AMIS", "majerle_internet_private" },
 18};
 19
 20/**
 21 * \brief           List of access points found by ESP device
 22 */
 23static
 24lwesp_ap_t aps[100];
 25
 26/**
 27 * \brief           Number of valid access points in \ref aps array
 28 */
 29static
 30size_t apf;
 31
 32/**
 33 * \brief           Connect to preferred access point
 34 *
 35 * \note            List of access points should be set by user in \ref ap_list structure
 36 * \param[in]       unlimited: When set to 1, function will block until SSID is found and connected
 37 * \return          \ref lwespOK on success, member of \ref lwespr_t enumeration otherwise
 38 */
 39lwespr_t
 40connect_to_preferred_access_point(uint8_t unlimited) {
 41    lwespr_t eres;
 42    uint8_t tried;
 43
 44    /*
 45     * Scan for network access points
 46     * In case we have access point,
 47     * try to connect to known AP
 48     */
 49    do {
 50        if (lwesp_sta_has_ip()) {
 51            return lwespOK;
 52        }
 53
 54        /* Scan for access points visible to ESP device */
 55        printf("Scanning access points...\r\n");
 56        if ((eres = lwesp_sta_list_ap(NULL, aps, LWESP_ARRAYSIZE(aps), &apf, NULL, NULL, 1)) == lwespOK) {
 57            tried = 0;
 58            /* Print all access points found by ESP */
 59            for (size_t i = 0; i < apf; i++) {
 60                printf("AP found: %s, CH: %d, RSSI: %d\r\n", aps[i].ssid, aps[i].ch, aps[i].rssi);
 61            }
 62
 63            /* Process array of preferred access points with array of found points */
 64            for (size_t j = 0; j < LWESP_ARRAYSIZE(ap_list); j++) {
 65                for (size_t i = 0; i < apf; i++) {
 66                    if (!strcmp(aps[i].ssid, ap_list[j].ssid)) {
 67                        tried = 1;
 68                        printf("Connecting to \"%s\" network...\r\n", ap_list[j].ssid);
 69                        /* Try to join to access point */
 70                        if ((eres = lwesp_sta_join(ap_list[j].ssid, ap_list[j].pass, NULL, NULL, NULL, 1)) == lwespOK) {
 71                            lwesp_ip_t ip;
 72                            uint8_t is_dhcp;
 73
 74                            printf("Connected to %s network!\r\n", ap_list[j].ssid);
 75
 76                            lwesp_sta_copy_ip(&ip, NULL, NULL, &is_dhcp);
 77                            utils_print_ip("Station IP address: ", &ip, "\r\n");
 78                            printf("; Is DHCP: %d\r\n", (int)is_dhcp);
 79                            return lwespOK;
 80                        } else {
 81                            printf("Connection error: %d\r\n", (int)eres);
 82                        }
 83                    }
 84                }
 85            }
 86            if (!tried) {
 87                printf("No access points available with preferred SSID!\r\nPlease check station_manager.c file and edit preferred SSID access points!\r\n");
 88            }
 89        } else if (eres == lwespERRNODEVICE) {
 90            printf("Device is not present!\r\n");
 91            break;
 92        } else {
 93            printf("Error on WIFI scan procedure!\r\n");
 94        }
 95        if (!unlimited) {
 96            break;
 97        }
 98    } while (1);
 99    return lwespERR;
100}
group LWESP_STA

Station API.

Functions

lwespr_t lwesp_sta_join(const char *name, const char *pass, const lwesp_mac_t *mac, const lwesp_api_cmd_evt_fn evt_fn, void *const evt_arg, const uint32_t blocking)

Join as station to access point.

Configuration changes will be saved in the NVS area of ESP device.

Parameters
  • name[in] SSID of access point to connect to

  • pass[in] Password of access point. Use NULL if AP does not have password

  • mac[in] Pointer to MAC address of AP. If multiple APs with same name exist, MAC may help to select proper one. Set to NULL if not needed

  • evt_fn[in] Callback function called when command has finished. Set to NULL when not used

  • evt_arg[in] Custom argument for event callback function

  • blocking[in] Status whether command should be blocking or not

Returns

lwespOK on success, member of lwespr_t enumeration otherwise

lwespr_t lwesp_sta_quit(const lwesp_api_cmd_evt_fn evt_fn, void *const evt_arg, const uint32_t blocking)

Quit (disconnect) from access point.

Parameters
  • evt_fn[in] Callback function called when command has finished. Set to NULL when not used

  • evt_arg[in] Custom argument for event callback function

  • blocking[in] Status whether command should be blocking or not

Returns

lwespOK on success, member of lwespr_t enumeration otherwise

lwespr_t lwesp_sta_autojoin(uint8_t en, const lwesp_api_cmd_evt_fn evt_fn, void *const evt_arg, const uint32_t blocking)

Configure auto join to access point on startup.

Note

For auto join feature, you need to do a join to access point with default mode. Check lwesp_sta_join for more information

Parameters
  • en[in] Set to 1 to enable or 0 to disable

  • evt_fn[in] Callback function called when command has finished. Set to NULL when not used

  • evt_arg[in] Custom argument for event callback function

  • blocking[in] Status whether command should be blocking or not

Returns

lwespOK on success, member of lwespr_t enumeration otherwise

lwespr_t lwesp_sta_reconnect_set_config(uint16_t interval, uint16_t rep_cnt, const lwesp_api_cmd_evt_fn evt_fn, void *const evt_arg, const uint32_t blocking)

Set reconnect interval and maximum tries when connection drops.

Parameters
  • interval[in] Interval in units of seconds. Valid numbers are 1-7200 or 0 to disable reconnect feature

  • rep_cnt[in] Repeat counter. Number of maximum tries for reconnect. Valid entries are 1-1000 or 0 to always try. This parameter is only valid if interval is not 0

  • evt_fn[in] Callback function called when command has finished. Set to NULL when not used

  • evt_arg[in] Custom argument for event callback function

  • blocking[in] Status whether command should be blocking or not

Returns

lwespOK on success, member of lwespr_t enumeration otherwise

lwespr_t lwesp_sta_getip(lwesp_ip_t *ip, lwesp_ip_t *gw, lwesp_ip_t *nm, const lwesp_api_cmd_evt_fn evt_fn, void *const evt_arg, const uint32_t blocking)

Get station IP address.

Parameters
  • ip[out] Pointer to variable to save IP address

  • gw[out] Pointer to output variable to save gateway address

  • nm[out] Pointer to output variable to save netmask address

  • evt_fn[in] Callback function called when command has finished. Set to NULL when not used

  • evt_arg[in] Custom argument for event callback function

  • blocking[in] Status whether command should be blocking or not

Returns

lwespOK on success, member of lwespr_t enumeration otherwise

lwespr_t lwesp_sta_setip(const lwesp_ip_t *ip, const lwesp_ip_t *gw, const lwesp_ip_t *nm, const lwesp_api_cmd_evt_fn evt_fn, void *const evt_arg, const uint32_t blocking)

Set station IP address.

Application may manually set IP address. When this happens, stack will check for DHCP settings and will read actual IP address from device. Once procedure is finished, LWESP_EVT_WIFI_IP_ACQUIRED event will be sent to application where user may read the actual new IP and DHCP settings.

Configuration changes will be saved in the NVS area of ESP device.

Note

DHCP is automatically disabled when using static IP address

Parameters
  • ip[in] Pointer to IP address

  • gw[in] Pointer to gateway address. Set to NULL to use default gateway

  • nm[in] Pointer to netmask address. Set to NULL to use default netmask

  • evt_fn[in] Callback function called when command has finished. Set to NULL when not used

  • evt_arg[in] Custom argument for event callback function

  • blocking[in] Status whether command should be blocking or not

Returns

lwespOK on success, member of lwespr_t enumeration otherwise

lwespr_t lwesp_sta_getmac(lwesp_mac_t *mac, const lwesp_api_cmd_evt_fn evt_fn, void *const evt_arg, const uint32_t blocking)

Get station MAC address.

Parameters
  • mac[out] Pointer to output variable to save MAC address

  • evt_fn[in] Callback function called when command has finished. Set to NULL when not used

  • evt_arg[in] Custom argument for event callback function

  • blocking[in] Status whether command should be blocking or not

Returns

lwespOK on success, member of lwespr_t enumeration otherwise

lwespr_t lwesp_sta_setmac(const lwesp_mac_t *mac, const lwesp_api_cmd_evt_fn evt_fn, void *const evt_arg, const uint32_t blocking)

Set station MAC address.

Configuration changes will be saved in the NVS area of ESP device.

Parameters
  • mac[in] Pointer to variable with MAC address

  • evt_fn[in] Callback function called when command has finished. Set to NULL when not used

  • evt_arg[in] Custom argument for event callback function

  • blocking[in] Status whether command should be blocking or not

Returns

lwespOK on success, member of lwespr_t enumeration otherwise

uint8_t lwesp_sta_has_ip(void)

Check if ESP got IP from access point.

Returns

1 on success, 0 otherwise

uint8_t lwesp_sta_is_joined(void)

Check if station is connected to WiFi network.

Returns

1 on success, 0 otherwise

lwespr_t lwesp_sta_copy_ip(lwesp_ip_t *ip, lwesp_ip_t *gw, lwesp_ip_t *nm, uint8_t *is_dhcp)

Copy IP address from internal value to user variable.

Note

Use lwesp_sta_getip to refresh actual IP value from device

Parameters
  • ip[out] Pointer to output IP variable. Set to NULL if not interested in IP address

  • gw[out] Pointer to output gateway variable. Set to NULL if not interested in gateway address

  • nm[out] Pointer to output netmask variable. Set to NULL if not interested in netmask address

  • is_dhcp[out] Pointer to output DHCP status variable. Set to NULL if not interested

Returns

lwespOK on success, member of lwespr_t enumeration otherwise

lwespr_t lwesp_sta_list_ap(const char *ssid, lwesp_ap_t *aps, size_t apsl, size_t *apf, const lwesp_api_cmd_evt_fn evt_fn, void *const evt_arg, const uint32_t blocking)

List for available access points ESP can connect to.

Parameters
  • ssid[in] Optional SSID name to search for. Set to NULL to disable filter

  • aps[in] Pointer to array of available access point parameters

  • apsl[in] Length of aps array

  • apf[out] Pointer to output variable to save number of access points found

  • evt_fn[in] Callback function called when command has finished. Set to NULL when not used

  • evt_arg[in] Custom argument for event callback function

  • blocking[in] Status whether command should be blocking or not

Returns

lwespOK on success, member of lwespr_t enumeration otherwise

lwespr_t lwesp_sta_get_ap_info(lwesp_sta_info_ap_t *info, const lwesp_api_cmd_evt_fn evt_fn, void *const evt_arg, const uint32_t blocking)

Get current access point information (name, mac, channel, rssi)

Note

Access point station is currently connected to

Parameters
  • info[in] Pointer to connected access point information

  • evt_fn[in] Callback function called when command has finished. Set to NULL when not used

  • evt_arg[in] Custom argument for event callback function

  • blocking[in] Status whether command should be blocking or not

Returns

lwespOK on success, member of lwespr_t enumeration otherwise

uint8_t lwesp_sta_is_ap_802_11b(lwesp_ap_t *ap)

Check if access point is 802.11b compatible.

Parameters

ap[in] Access point detailes acquired by lwesp_sta_list_ap

Returns

1 on success, 0 otherwise

uint8_t lwesp_sta_is_ap_802_11g(lwesp_ap_t *ap)

Check if access point is 802.11g compatible.

Parameters

ap[in] Access point detailes acquired by lwesp_sta_list_ap

Returns

1 on success, 0 otherwise

uint8_t lwesp_sta_is_ap_802_11n(lwesp_ap_t *ap)

Check if access point is 802.11n compatible.

Parameters

ap[in] Access point detailes acquired by lwesp_sta_list_ap

Returns

1 on success, 0 otherwise

uint8_t lwesp_sta_has_ipv6_local(void)

Check if station has local IPV6 IP Local IP is used between station and router.

Note

Defined as macro with 0 constant if LWESP_CFG_IPV6 is disabled

Returns

1 if local IPv6 is available, 0 otherwise

uint8_t lwesp_sta_has_ipv6_global(void)

Check if station has global IPV6 IP Global IP is used router and outside network.

Note

Defined as macro with 0 constant if LWESP_CFG_IPV6 is disabled

Returns

1 if global IPv6 is available, 0 otherwise

struct lwesp_sta_t
#include <lwesp_typedefs.h>

Station data structure.

Public Members

lwesp_ip_t ip

IP address of connected station

lwesp_mac_t mac

MAC address of connected station