Arduino: using WIFI (7) on ESP32

purpose

Use ESP32 general is its WIFI function, use WIFI function is probably to establish a network, connect to the network, search and other basic operations.

Networking (AP)

Basics

The following are the most basic wifi connection code. The code is burned into the module, you will be able to search for password-free WIFI called "ESP32_WIFI_AP" wifi in the list.

#include <WiFi.h>

void setup()
{
  WiFi.softAP("ESP32_WIFI_AP");
}

void loop()
{
}

The main function

  • bool WiFi.softAP(const char* ssid, const char* passphrase, int channel, int ssid_hidden, int max_connection)Use this method to open the AP mode, will return after a successful open ture
parameter Introduction
ssid AP network name, you need at least one byte, maximum not more than 32 bytes
passphrase AP established network password, may be NULL (no password) or not less than 63 and not more than 8 bytes of the byte code
channel WiFi network channel, an optional value from 1 to 13
ssid_hidden Are Foreign hidden SSID, 0- not hidden, 1 hide
max_connection The maximum number of access, the value of the optional 1 to 4
  • bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet)
    Setting AP Address
parameter Introduction
local_ip Local address, the default is 192.168.4.1
gateway Gateway address, the default is 192.168.4.1
subnet Subnet mask, default is 255.255.255.0
  • bool softAPdisconnect(bool wifioff = false)
    Close the current AP, when wifioff also true to restore the network settings

  • uint8_t softAPgetStationNum()
    Returns the number of clients connected to the AP

  • IPAddress softAPIP()
    Returns the current IP module

  • bool softAPsetHostname(const char * hostname)
    Set the host name

  • const char * softAPgetHostname()
    Returns the host name

  • uint8_t* softAPmacAddress(uint8_t* mac) String softAPmacAddress(void)
    Return mac address

Examples of Use

The code below burn test module

#include <WiFi.h>

IPAddress local_IP(192,168,4,22);
IPAddress gateway(192,168,4,22);
IPAddress subnet(255,255,255,0);

const char *ssid = "ESP32_AP_TEST";
const char *password = "12345678";

void setup()
{
  Serial.begin(115200);
  Serial.println();
  
  WiFi.mode(WIFI_AP); //设置工作在AP模式

  WiFi.softAPConfig(local_IP, gateway, subnet); //设置AP地址
  while(!WiFi.softAP(ssid, password)){}; //启动AP
  Serial.println("AP startup success");

  Serial.print("IP address: ");
  Serial.println(WiFi.softAPIP()); // 打印IP地址

  WiFi.softAPsetHostname("myHostName"); //设置主机名
  Serial.print("HostName: ");
  Serial.println(WiFi.softAPgetHostname()); //打印主机名

  Serial.print("mac Address: ");
  Serial.println(WiFi.softAPmacAddress()); //打印mac地址
}

void loop()
{
  delay(1000);
  Serial.println(WiFi.softAPgetStationNum()); //打印客户端连接数
}

Here Insert Picture Description

Network connection (STA)

Basics

The code below can be burned into the board in connection to the specified network

#include <WiFi.h>

const char *ssid = "********"; //你的网络名称
const char *password = "********"; //你的网络密码

void setup()
{
  Serial.begin(115200);
  Serial.println();

  WiFi.begin(ssid, password); //连接网络

  while (WiFi.status() != WL_CONNECTED) //等待网络连接成功
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected!");

  Serial.println("IP address: ");
  Serial.println(WiFi.localIP()); //打印模块IP
}

void loop()
{
}

The main function

  • wl_status_t begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true)
    The method used to access the network;
  • bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000)
    Set the network address;
  • bool disconnect(bool wifioff = false, bool eraseap = false)
    Disconnected from the network, if it is true wifioff also restore the network settings, if stored in flash eraseap is true of network parameters will be cleared;
  • bool isConnected()
    Returns whether the access network;
  • bool setAutoReconnect(bool autoReconnect)
    Set off network automatically reconnects;
  • bool getAutoReconnect()
    Returns whether the auto-reconnect;
  • IPAddress localIP()
    Return module address;
  • IPAddress subnetMask()
    Return subnet mask;
  • IPAddress gatewayIP()
    Returns the gateway address;
  • IPAddress dnsIP(uint8_t dns_no = 0)
    DNS return address;
  • uint8_t * macAddress(uint8_t* mac)and
    String macAddress()
    returns the MAC address;
  • const char * getHostname()
    Returns the host name;
  • bool setHostname(const char * hostname)
    Set the host name;
  • wl_status_t status()
    Return networking status, states as follows:
return value meaning
255 WL_NO_SHIELD do not care (compatible WiFi Shield design)
0 WL_IDLE_STATUS is WiFi switch between operating modes;
1 WL_NO_SSID_AVAIL can not access the network settings SSID;
2 WL_SCAN_COMPLETED scan is complete;
3 WL_CONNECTED connection is successful;
4 WL_CONNECT_FAILED connection failure;
5 WL_CONNECTION_LOST lost connection;
6 WL_DISCONNECTED disconnected;

Examples of Use

#include <WiFi.h>

const char *ssid = "*****";   //你的网络名称
const char *password = "*****";  //你的网络密码

void setup()
{
  Serial.begin(115200);
  Serial.println();

  WiFi.mode(WIFI_STA); //设置工作在STA模式

  WiFi.begin(ssid, password); //连接网络

  while (!WiFi.isConnected()) //等待网络连接成功
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected!");

  Serial.print("IP address: ");
  Serial.println(WiFi.localIP()); //打印模块IP

  Serial.print("subnetMask: ");
  Serial.println(WiFi.subnetMask()); //打印子网掩码

  Serial.print("gateway: ");
  Serial.println(WiFi.gatewayIP()); //打印网关地址

  Serial.print("dns: ");
  Serial.println(WiFi.dnsIP()); //打印DNS地址

  Serial.print("mac Address: ");
  Serial.println(WiFi.macAddress()); //打印mac地址

  WiFi.setHostname("myHostName"); //设置主机名
  Serial.print("HostName: ");
  Serial.println(WiFi.getHostname()); //打印主机名

  Serial.println(WiFi.status());
  WiFi.disconnect(); //断开当前网络
  delay(1000);
  Serial.println(WiFi.status());
}

void loop()
{
}

Here Insert Picture Description

Search Network

Sometimes in practice, we have to search for wifi then the surrounding environment to the next step. The following is asynchronous with the search, the search compared to synchronous asynchronous search does not block the program running

The main function

  • int16_t scanNetworks(bool async = false, bool show_hidden = false, bool passive = false, uint32_t max_ms_per_chan = 300)
    Start Search, the parameters are as follows:
parameter Introduction
async Asynchronous scanning When true asynchronous scanning starts, the process will not be blocked
show_hidden Whether or not the broadcast network scanning
passive Effect of the scanning speed, which is true when faster scanning (uncertain)
max_ms_per_chan Scan time per channel
  • int16_t scanComplete()
    Asynchronous mode for acquiring the number of the scanned network, if the return value of -1 indicates still scan, if the return value of -1 indicates no scanning or scanning failure
  • void scanDelete()
    Scan results Clear memory
  • String SSID(uint8_t networkItem)
    Scan return to the network name
  • wifi_auth_mode_t encryptionType(uint8_t networkItem)
    Returns the type of scan to the network encryption
  • int32_t RSSI(uint8_t networkItem)
    Returning scan to the network signal strength
  • int32_t channel(uint8_t networkItem)
    Returning scan to the network channel number

Examples of Use


#include <WiFi.h>

void setup()
{
  Serial.begin(115200);
  Serial.println();

  WiFi.mode(WIFI_STA); //设置为STA模式
  WiFi.disconnect();   //断开当前可能的连接
  delay(100);

  Serial.println("Strat scanning");
  WiFi.scanNetworks(true); //启动异步扫描
}

void loop()
{
  delay(1000);
  int n = WiFi.scanComplete(); //获取扫描状态
  if (n >= 0)
  {
    Serial.println("Scan over");
    for (int i = 0; i < n; ++i)
    {
      Serial.println();
      Serial.print(i + 1);
      Serial.print(":       ");
      Serial.print(WiFi.SSID(i)); //打印网络名称
      Serial.print("        ");
      Serial.print(WiFi.RSSI(i)); //打印信号强度
      Serial.print("        ");
      Serial.print((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? "unencrypted" : "encryption"); //打印是否加密
      delay(10);
    }
    WiFi.scanDelete(); //清除内存中的扫描结果
  }
  else if (n == -1)
  {
    Serial.println("Scanning now");
  }
  else if (n == -2)
  {
    Serial.println("Not scanned");
  }
}


Here Insert Picture Description


to sum up

Learned wifi networking function can be achieved, really started to develop network applications, and create more new innovative products.

Guess you like

Origin blog.csdn.net/weixin_43474408/article/details/90812633