Arduino for BPI:bit 7: Set ESP32 Wifi

ESP32 typically USES its WIFI capabilities, which are basic operations like setting up, connecting, and searching networks.

Networking (AP)

Here’s the basic code for a wifi connection. Burn the code into the module to search for password-less wifi named “ESP32_WIFI_AP” in the wifi list.

#include <WiFi.h>

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

void loop()
{
}

Burn the code below into the module to test

#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()); //打印客户端连接数
}

wifi%20seting

Connection network (STA)

Burn the following code into the board to connect 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()
{
}

example code:

#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()
{
}

wifi%20set%201

Scan network

Sometimes, in practice, we need to search the wifi in the surrounding environment before we proceed to the next step.Asynchronous search does not block the program compared to synchronous search

int16_t scanNetworks(bool async = false, bool show_hidden = false, bool passive = false, uint32_t max_ms_per_chan = 300)

#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");
  }
}

wifi%20set%202