ESP32-WiFiAP(接入点模式)

AP 模式介绍

接入点(AP)是一种提供 Wi-Fi 网络访问的设备,并将其连接到有线网络的装置。ESP32除了不具有与有线网络的接口外,还可以提供类似的功能。这种操作模式称为软接入点(soft-AP)。可以同时连接到soft-AP的最大站数可以设置4,默认为4。
在这里插入图片描述
当ESP32单独处于AP模式下时,可以被认为是一个无法访问外网的局域网WiFi路由器节点,它可以接受各类设备的连接请求。并可以和连接设备进行TCP、UDP连接,实现数据流。在局域物联网的设计中可以承担数据收发节点的作用。

AP模式的配置

AP模式的配置思路可以对比与我们日常中配置家里的WiFi路由器。

API 分析

1.1、配置AP模式IP

/**
 * Configure access point
 * @param local_ip      access point IP
 * @param gateway       gateway IP
 * @param subnet        subnet mask
 */
bool WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet)

首先配置了AP模式下的本地IP、网关IP、子网掩码。当然这一步并不是必须的,我们可以直接进行下一步的配置,这三个配置项将会以默认的形式自动配置。

1.2、开启、关闭AP模式

/**
 * Set up an access point
 * @param ssid              Pointer to the SSID (max 63 char).
 * @param passphrase        (for WPA2 min 8 char, for open use NULL)
 * @param channel           WiFi channel number, 1 - 13.
 * @param ssid_hidden       Network cloaking (0 = broadcast SSID, 1 = hide SSID)
 * @param max_connection    Max simultaneous connected clients, 1 - 4.
*/
bool WiFiAPClass::softAP(const char* ssid, const char* passphrase, int channel, int ssid_hidden, int max_connection)

开启AP模式,需要配置其网络名称、连接密码(可忽略)、WiFi信道、是否隐藏信号、最大连接数(1-4)

  • 网络名称是必须配置项。(最大字符数为63)
  • 连接密码如果不配置则默认为开放性网络,谁都可以连接。
  • WiFi信道的配置,简单理解就是最好与环境中的其他信号不相同,避免信号干扰。(1-13)
  • 是否隐藏信号,可以选择广播信号(broadcast )所有人直接看到,也可以隐藏则需要指定查找。
  • 最大连接数在默认下是4,可以配置范围1-4。
/**
 * Disconnect from the network (close AP)
 * @param wifioff disable mode?
 * @return one value of wl_status_t enum
 */
bool WiFiAPClass::softAPdisconnect(bool wifioff)

关闭AP模式,直接调用即可~

1.3、获取连接的设备数

/**
 * Get the count of the Station / client that are connected to the softAP interface
 * @return Stations count
 */
uint8_t WiFiAPClass::softAPgetStationNum()

1.5、设置AP的主机名

/**
 * Set the softAP    interface Host name.
 * @param  hostname  pointer to const string
 * @return true on   success
 */
bool WiFiAPClass::softAPsetHostname(const char * hostname)

1.4、各类IP参数获取

    IPAddress softAPIP();  // 查看本地IP
    IPAddress softAPBroadcastIP(); // 查看广播IP
    IPAddress softAPNetworkID();	
    uint8_t softAPSubnetCIDR();
    String softAPmacAddress(void);
    const char * softAPgetHostname();

例子

#include <Arduino.h>
#include <WiFi.h>
#include <WiFiAP.h>

WiFiAPClass WiFiAP;

// Set these to your desired credentials.
const char *ssid = "tastAP";
const char *password = "123456";

uint8_t x=1;
IPAddress local_IP(192, 168, x, 1);  
IPAddress gateway(192, 168, x, 1);
IPAddress subnet(255, 255, 255, 0);


void setup() {
  Serial.begin(9600);
  Serial.println();
  Serial.println("Configuring access point...");

  // You can remove the password parameter if you want the AP to be open.
  WiFiAP.softAPConfig(local_IP,gateway,subnet);
  WiFiAP.softAP(ssid, password);

  Serial.println("Server started");
}

void loop() {
  delay(2000);
  Serial.print("AP IP address: ");
  Serial.println(WiFiAP.softAPIP());

  Serial.print("softAP Broadcast IP: ");
  Serial.println(WiFiAP.softAPBroadcastIP());

  Serial.print("softAP NetworkID: ");
  Serial.println(WiFiAP.softAPNetworkID());

  Serial.print("softAP SubnetCIDR: ");
  Serial.println(WiFiAP.softAPSubnetCIDR());

  Serial.print("softAP Hostname: ");
  Serial.println(WiFiAP.softAPgetHostname());

  Serial.print("softAP macAddress: ");
  Serial.println(WiFiAP.softAPmacAddress());

  Serial.print("softAP StationNum: ");
  Serial.println(WiFiAP.softAPgetStationNum());
}

  • 8
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值