ESP8266使用指南(三)



  • 摘要:

    本节重点介绍ESP8266的Arduino开发教程,ESP8266接线图与AT指令集可参考上篇博客。

    环境搭建

    具体搭建过程可参考: https://www.jianshu.com/p/cb0274d612b5

    0_1614827630039_01.png
    搭建完成后,重启IDE,选择: 开发板:"Generic ESP8266 Module" → ESP8266 Boards(2.7.4) → Generic ESP8266 Module即可。

    环境测试:

    搭建好开发环境后,可以写一个点灯小程序进行测试:

    void setup() {
      pinMode(2,OUTPUT);
      Serial.begin(115200);
    }
     
    void loop() {
      digitalWrite(2,HIGH);
      delay(500);
      digitalWrite(2,LOW);
      delay(500);
      Serial.print("hellow world\n\r");
    }
    


  • 连接WiFi

    // 测试esp8266连接WiFi
    #include <ESP8266WiFi.h>
    void setup() {
      // put your setup code here, to run once:
      Serial.begin(115200);
      WiFi.begin("vivo X21", "123456789");
      Serial.println();
      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() {
    }
    


  • 作为AP,释放热点

    //将ESP8266作为AP,打印出连接用户数
    #include <ESP8266WiFi.h>
    
    IPAddress local_IP(192,168,4,22);
    IPAddress gateway(192,168,4,9);
    IPAddress subnet(255,255,255,0);
    
    void setup()
    {
      Serial.begin(115200);
      Serial.println();
    
      Serial.print("Setting soft-AP configuration ... ");
      Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
    
      Serial.print("Setting soft-AP ... ");
      Serial.println(WiFi.softAP("ESPsoftAP_01") ? "Ready" : "Failed!"); //用户名是ESPsoftAP_01,无密码
    
      Serial.print("Soft-AP IP address = ");
      Serial.println(WiFi.softAPIP());
    }
    
    void loop() {
      Serial.printf("Stations connected = %d\n", WiFi.softAPgetStationNum());
      delay(3000);
      }
    


  • TCP收发测试:

    //进行TCP收发测试,ESP8266作为TCP Server
    #include <ESP8266WiFi.h>
    
    IPAddress local_IP(192,168,4,22);
    IPAddress gateway(192,168,4,9);
    IPAddress subnet(255,255,255,0);
    
    WiFiServer server(80);
    
    void setup()
    {
      Serial.begin(115200);
      Serial.println();
    
      Serial.print("Setting soft-AP configuration ... ");
      Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
    
      Serial.print("Setting soft-AP ... ");
      Serial.println(WiFi.softAP("ESPsoftAP") ? "Ready" : "Failed!"); //用户名是ESPsoftAP_01,无密码
    
      Serial.print("Soft-AP IP address = ");
      Serial.println(WiFi.softAPIP());
      server.begin(); //Don't forget!!
    
    }
    void loop()
    {
      WiFiClient client = server.available();
      // wait for a client (web browser) to connect
      if (client)
      {
        Serial.println("\n[Client connected]");
        while (client.connected())
        {
          // read line by line what the client (web browser) is requesting
          if (client.available())
          {
            String line = client.readStringUntil('\r');
            Serial.println(line);
            client.println(line); //return the same
          }
        }
        delay(1);
        client.stop();
        Serial.println("[Client disonnected]");
      }
    }
    



 

Copyright © 2018 bbs.dian.org.cn All rights reserved.

Looks like your connection to Dian was lost, please wait while we try to reconnect.