0
点赞
收藏
分享

微信扫一扫

01-ESP8266的STA模式学习

洲行 2022-04-22 阅读 75
物联网

从今天开始记录自己学习ESP8266的过程

主要是借助Arduinop平台,使用VScode环境

主要的学习思路还是围绕ESP8266的特点功能进行

文章目录

01-ESP8266的STA模式学习:

Station(简称STA)模式

​ ESP8266处于STA模式简介状态图如下所示,主要的作用是作为一个(STA)站点去连接一个(AP)热点

img

相关头文件:#include <ESP8266WiFiSTA.h> 里面对于STA库的描述可以拆分为四个部分:

1.建立连接

 		wl_status_t begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true);
        wl_status_t begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true);
        wl_status_t begin(const String& ssid, const String& passphrase = emptyString, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true);
        wl_status_t begin();

        //The argument order for ESP is not the same as for Arduino. However, there is compatibility code under the hood
        //to detect Arduino arg order, and handle it correctly. Be aware that the Arduino default value handling doesn't
        //work here (see Arduino docs for gway/subnet defaults). In other words: at least 3 args must always be given.
        bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000);

2.管理连接

		bool reconnect();
        bool disconnect(bool wifioff = false);

        bool isConnected();

        bool setAutoConnect(bool autoConnect);
        bool getAutoConnect();

        bool setAutoReconnect(bool autoReconnect);
        bool getAutoReconnect();

        int8_t waitForConnectResult(unsigned long timeoutLength = 60000);

3.网络信息

        // STA network info
        IPAddress localIP();

        uint8_t * macAddress(uint8_t* mac);
        String macAddress();

        IPAddress subnetMask();
        IPAddress gatewayIP();
        IPAddress dnsIP(uint8_t dns_no = 0);

        IPAddress broadcastIP();
        // STA WiFi info
        wl_status_t status();
        String SSID() const;
        String psk() const;

        uint8_t * BSSID();
        String BSSIDstr();

        int8_t RSSI();

4.保护和智能配网

        bool beginWPSConfig(void);//WPS:Wi-Fi Protected Setup
        bool beginSmartConfig();
        bool stopSmartConfig();
        bool smartConfigDone();

一般的配置流程:

  • 设置为STA模式
  • 配置WiFI信息(IP地址,密码)
  • 获取连接WIFi信息

例程:

/*
内容:这个例程是学习ESP8266的STA模式
时间:2022.4.42
作者:bobo'
*/

//头文件
#include <ESP8266WiFi.h>     

IPAddress StaticIP(192,168,209,220);
IPAddress Gateway(192,168,209,10);
IPAddress SubnetMask(255,255,255,0);
//参数定义
//SSID of your network
char ssid[] = "redmi";
//password of your WPA Network
char pass[] = "88888888";

//启动函数
void setup()
{
    //设置串口调试
    Serial.begin(9600);

    delay(200);

    Serial.println("Start STA_Mode");
    //设置为STA模式
    WiFi.mode(WIFI_STA);
    //STA配置状态打印
    if( WiFi.begin(ssid,pass) == WL_CONNECT_FAILED )
    {
        Serial.println("STA_Mode config failed");
    }
    else
    {
        Serial.println("STA_Mode is config successful");
    }
    //Wifi连接状态打印
    Serial.println(String(ssid)+" is Connecting");
    //配置IP地址,网关。子掩码
    WiFi.config(StaticIP,Gateway,SubnetMask);
    while(WiFi.status() != WL_CONNECTED)
    {
        if(WiFi.status() == WL_DISCONNECTED )
        {
            Serial.print(".");
            delay(200);
        }
    }

    Serial.println("/");//换行
    Serial.println("WiFi is connect");
    //WiFi信息打印
    Serial.println(WiFi.localIP());
    Serial.println(WiFi.macAddress());
    Serial.println(WiFi.subnetMask());
    Serial.println(WiFi.gatewayIP());
    Serial.println(WiFi.SSID());
    Serial.println(WiFi.psk());
}

//死循环函数
void loop()
{
}
举报

相关推荐

0 条评论