WiFi-ESP8266入门开发(四)-设置软热点

注:对于ESP8266开源技术感兴趣的可以加群,我们一起探索交流学习,群号:579932824。群名:ESP8266开源技术交流群。

将ESP8266设置为一个热点,可供其他设备连接并交换数据,主要使用的函数:WiFi.softAP(ssid, password, channel, hidden)。

参数说明:

ssid:Wifi热点名称,支持最大63个英文字符;

password:密码设置,可选参数,也就是可以没有密码,如果设定为WPA2-PSK;

channel:信道设置,可选参数,1-13,没有填写则默认为1;

hidden:是否隐藏SSID,可选参数,如果设置为true则隐藏。

 

ESP8266开启的默认IP地址为192.168.4.1,可以使用softAPConfig (local_ip, gateway, subnet) 函数进行修改。

参数说明:

local_ip:软热点的地址;

gateway:网关地址;

subnet:子网掩码。

 

程序流程:

1、连接NodeMCU板子到USB接口,打开Arduino IDE编辑器界面,选择好开发板型号和串口号。

2、在Arduino IDE的编辑器界面,开始部分添加头文件。

#include <ESP8266WiFi.h> 

3、声明各IP地址。

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

4、配置软AP参数。

WiFi.softAPConfig(local_IP, gateway, subnet);

5、启动AP模式。

WiFi.softAP("SoftAP001","123456789");

6、获取IP地址。

Serial.print("Soft-AP IP address = ");  
Serial.println(WiFi.softAPIP());

下载完成后,在手机或笔记本上Wifi可以搜到设置的AP。

整体代码如下:

#include <ESP8266WiFi.h>  
IPAddress local_IP(192, 168, 4, 4);  
IPAddress gateway(192, 168, 4, 1);  
IPAddress subnet(255, 255, 255, 0);  
void setup() {  
  // put your setup code here, to run once:  
  Serial.begin(115200);  
  WiFi.softAPConfig(local_IP, gateway, subnet);  
  WiFi.softAP("SoftAP001", "123456789");  
  Serial.print("Soft-AP IP address = ");  
  Serial.println(WiFi.softAPIP());  
  
}  
  
void loop() {  
  // put your main code here, to run repeatedly:  
  
}

 

  • 6
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr.Lanson

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值