Fork me on GitHub

WiFi support

This library provides common WiFi API.

Configuration

A library adds a wifi configuration entry with two sub-entries: wifi.ap which contains configuration settings for the Access Point mode, and wifi.sta for the Station mode.

"wifi": {
  "sta": {
    "enable": true,         // Enable Station mode
    "ssid": "",             // WiFi network name
    "pass": "",             // Password
    "user": "",             // Username for WPA-PEAP mode
    "anon_identity": "",    // Anonymous identity for WPA mode
    "cert": "",             // Client certificate for WPA-TTLS mode
    "key": "",              // Client key for WPA-TTLS mode
    "ca_cert": "",          // CA certificate for WPA-enterprise mode
    "ip": "",               // Static IP Address
    "netmask": "",          // Static Netmask
    "gw": "",               // Static Default Gateway
    "nameserver": "",       // DNS Server
    "dhcp_hostname": ""     // Host name to include in DHCP requests
  },
  "ap": {
    "enable": true,               // Enable Access Point mode
    "ssid": "Mongoose_??????",    // SSID to use. ?? symbols are substituted by MAC address
    "pass": "Mongoose",           // Password
    "hidden": false,              // Hide WiFi network
    "channel": 6,                 // WiFi channel
    "max_connections": 10,        // Maximum number of connections
    "ip": "192.168.4.1",          // Static IP Address
    "netmask": "255.255.255.0",   // Static Netmask
    "gw": "192.168.4.1",          // Static Default Gateway
    "dhcp_start": "192.168.4.2",  // DHCP Start Address
    "dhcp_end": "192.168.4.100",  // DHCP End Address
    "trigger_on_gpio": -1         // Trigger AP on low GPIO
  },
}

C API Reference mgos_wifi.h

#include "mgos_wifi.h"


bool mgos_wifi_setup_sta(const struct sys_config_wifi_sta *cfg);

bool mgos_wifi_setup_ap(const struct sys_config_wifi_ap *cfg);

bool mgos_wifi_connect(void);

bool mgos_wifi_disconnect(void);

To the previously _setup network.

enum mgos_wifi_status {
  MGOS_WIFI_DISCONNECTED = 0,
  MGOS_WIFI_CONNECTING = 1,
  MGOS_WIFI_CONNECTED = 2,
  MGOS_WIFI_IP_ACQUIRED = 3,
};

DEPRECATED: API below is deprecated in favor of mgosnet* API. Check for events with if_type == MGOS_NET_IF_TYPE_WIFI and if_instance == MGOS_NET_IF_WIFI_STA or MGOS_NET_IF_WIFI_AP

typedef void (*mgos_wifi_changed_t)(enum mgos_wifi_status event, void *arg);

void mgos_wifi_add_on_change_cb(mgos_wifi_changed_t fn, void *arg);

Add a callback to be invoked when WiFi state changes.

void mgos_wifi_remove_on_change_cb(mgos_wifi_changed_t fn, void *arg);

Remove a previously added callback, fn and arg have to match exactly.

bool mgos_wifi_validate_ap_cfg(const struct sys_config_wifi_ap *cfg,
                               char **msg);

bool mgos_wifi_validate_sta_cfg(const struct sys_config_wifi_sta *cfg,
                                char **msg);

enum mgos_wifi_status mgos_wifi_get_status(void);

char *mgos_wifi_get_status_str(void);

These return allocated strings which will be free'd.

char *mgos_wifi_get_connected_ssid(void);

char *mgos_wifi_get_sta_default_dns(void);

enum mgos_wifi_auth_mode {
  MGOS_WIFI_AUTH_MODE_OPEN = 0,
  MGOS_WIFI_AUTH_MODE_WEP = 1,
  MGOS_WIFI_AUTH_MODE_WPA_PSK = 2,
  MGOS_WIFI_AUTH_MODE_WPA2_PSK = 3,
  MGOS_WIFI_AUTH_MODE_WPA_WPA2_PSK = 4,
  MGOS_WIFI_AUTH_MODE_WPA2_ENTERPRISE = 5,
};

Callback must be invoked, with list of SSIDs or NULL on error. Caller owns SSIDS, they are not freed by the callee. Invoking inline is ok.

struct mgos_wifi_scan_result {
  char ssid[33];
  uint8_t bssid[6];
  enum mgos_wifi_auth_mode auth_mode;
  int channel;
  int rssi;
};

typedef void (*mgos_wifi_scan_cb_t)(int num_res,
                                    struct mgos_wifi_scan_result *res,
                                    void *arg);

void mgos_wifi_scan(mgos_wifi_scan_cb_t cb, void *arg);

bool mgos_wifi_init(void);

void mgos_wifi_scan_js(struct mjs *mjs);