天天看点

ESP8266与NodeMCU开发(二)基础实验三则实验一   连接指定Wifi网络实验二   网页客户端实验三   设置ESP8266为软热点

这一章将通过编程实验的方式来认识ESP8266的功能,并体验快速而便捷的开发流程

实验一   连接指定Wifi网络

这一段程序将使得ESP8266连接指定名称和密码的Wifi热点,主要流程如下

程序流程:

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

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
           

之后设置两个宏定义用于填写Wifi名称和密码

#define WIFINAME "******"
#define WIFIPW   "****"
           

点亮一个LED灯用于指示电源,并设定串口波特率用以通信

pinMode(BUILTIN_LED, OUTPUT);
  Serial.begin(115200);
           

设定并连接Wifi,如果没有连接上就会一直重试

WiFi.begin(WIFINAME, WIFIPW);
  Serial.print("Connecting..");
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
           

整体的代码如下

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>

#define WIFINAME "******"
#define WIFIPW   "****"

void setup() {
  // put your setup code here, to run once:
  pinMode(BUILTIN_LED, OUTPUT);
  Serial.begin(115200);
  Serial.println("");
  WiFi.begin(WIFINAME, WIFIPW);
  Serial.print("Connecting..");
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.print("Connected,IP Address:");
  Serial.println(WiFi.localIP());


  
}

void loop() {
  // put your main code here, to run repeatedly:

}
           

如果成功连接,串口将输出连接成功提示,并且可以通过PC端PING通ESP8266(在同一网段)

ESP8266与NodeMCU开发(二)基础实验三则实验一   连接指定Wifi网络实验二   网页客户端实验三   设置ESP8266为软热点

实验二   网页客户端

ESP8266可以直接作为一个网页客户端来使用,封装的库中提供函数以快速配置ESP8266为一个网页客户端,能够直接获取网页服务端的代码。例如打开百度首页,通过串口返回所有html代码。这里暂未设置DNS服务因此需要通过IP直接访问。

程序流程:

添加头文件

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
           

以宏定义的形式设定地址,端口和wifi名称,密码

#define HTTPIP      "14.215.177.37"   //baidu的ip
#define HTTPPORT    80
#define WIFINAME    "你的wifi名称"
#define WIFIPW      "wifi密码"
           

配置串口并尝试连接Wifi

Serial.begin(115200);
  Serial.println();
  Serial.println("Connecting");
  WiFiMulti.addAP(WIFINAME,WIFIPW);
  while(WiFiMulti.run()!=WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected!");
           

连接指定的网页服务器IP,这里的"/"是指页面文件名称,例如"index.jsp""main.php"等

http.begin(HTTPIP,HTTPPORT,"/");
           

获取代码,如果返回值为200则表示请求网页成功,打印页面

int Code = http.GET();
if(Code)
{
    Serial.printf("HTTP Code:%d\n",Code);
    if(Code == 200)
    {
      String payload = http.getString();
      Serial.println(payload);
    }
    else
{
  Serial.println("Couldn't link to server");
}
           

实验二整体代码如下

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
ESP8266WiFiMulti WiFiMulti;

#define HTTPIP      "14.215.177.37"//baidu
#define HTTPPORT    80
#define WIFINAME    "*******"
#define WIFIPW      "*****"


void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println();
  Serial.println("Connecting");
  WiFiMulti.addAP(WIFINAME,WIFIPW);
  while(WiFiMulti.run()!=WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected!");
  
}

void loop() {
  // put your main code here, to run repeatedly:
HTTPClient http;
Serial.println("Try link to http.");
http.begin(HTTPIP,HTTPPORT,"/");
int Code = http.GET();
if(Code)
{
    Serial.printf("HTTP Code:%d\n",Code);
    if(Code == 200)
    {
      String payload = http.getString();
      Serial.println(payload);
    }
    else
{
  Serial.println("Couldn't link to server");
}
}

 delay(5000);
}
           
ESP8266与NodeMCU开发(二)基础实验三则实验一   连接指定Wifi网络实验二   网页客户端实验三   设置ESP8266为软热点

实验三   设置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:子网掩码

程序流程:

头文件

#include <ESP8266WiFi.h>
           

声明各个IP地址

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

配置软AP参数

WiFi.softAPConfig(local_IP, gateway, subnet);
           

启动AP

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

获取IP地址

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

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

ESP8266与NodeMCU开发(二)基础实验三则实验一   连接指定Wifi网络实验二   网页客户端实验三   设置ESP8266为软热点

实验三整体代码如下

#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", "999999999");
  Serial.print("Soft-AP IP address = ");
  Serial.println(WiFi.softAPIP());

}

void loop() {
  // put your main code here, to run repeatedly:

}
           

参考:

https://github.com/esp8266/Arduino/blob/master/doc/esp8266wifi/soft-access-point-class.md#softap

继续阅读