【PlatformIO】基于Arduino的ESP8266 网站部署

文章目录

    • 概要
    • 代码展示
    • 串口信息
    • 打开网站
    • 视频演示

概要

将制作的网站上传到ESP8266的Flash中,然后通过网址进行打开
1、创建esp8266工程,
2、添加ESPAsyncWebServer等库函数
3、打开串口监听
4、编写代码,主要包括:wifi部分、fs文件、服务器打开文件
5、上传网站
6、调试上传程序

代码展示

#include 
#include 
#include 
#include 
#include 

AsyncWebServer server(80);

const char* ssid = "xinxin";
const char* password = "xinxin1102";

void notFound(AsyncWebServerRequest *request) {
    request->send(404, "text/plain", "Not found");
}

void setup() {
  
    Serial.begin(115200);
    SPIFFS.begin();
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    if (WiFi.waitForConnectResult() != WL_CONNECTED) {
        Serial.printf("WiFi Failed!\n");
        return;
    }

    Serial.print("IP Address: ");
    Serial.println(WiFi.localIP());

    server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
    {
        // 检查SPIFFS文件系统中是否存在index.html文件
        if (SPIFFS.exists("/index.html")) {
          fs::File file = SPIFFS.open("/index.html", "r");  // 打开index.html文件
          if (file) {
              size_t fileSize = file.size();  // 获取文件大小
              String fileContent;

              // 逐字节读取文件内容
              while (file.available()) {
                  fileContent += (char)file.read();
              }
              file.close();  // 关闭文件

              // 返回HTML内容
              request->send(200, "text/html", fileContent);
              return;
          }
        }
        // 如果文件不存在,返回404错误
        request->send(404, "text/plain", "File Not Found"); });  


        server.on("/control.html", HTTP_GET, [](AsyncWebServerRequest *request)
        {
            // 检查SPIFFS文件系统中是否存在index.html文件
            if (SPIFFS.exists("/control.html")) {
              fs::File file = SPIFFS.open("/control.html", "r");  // 打开index.html文件
              if (file) {
                  size_t fileSize = file.size();  // 获取文件大小
                  String fileContent;
    
                  // 逐字节读取文件内容
                  while (file.available()) {
                      fileContent += (char)file.read();
                  }
                  file.close();  // 关闭文件
    
                  // 返回HTML内容
                  request->send(200, "text/html", fileContent);
                  return;
              }
            }
            // 如果文件不存在,返回404错误
            request->send(404, "text/plain", "File Not Found"); });



            server.on("/setting.html", HTTP_GET, [](AsyncWebServerRequest *request)
            {
                // 检查SPIFFS文件系统中是否存在index.html文件
                if (SPIFFS.exists("/setting.html")) {
                  fs::File file = SPIFFS.open("/setting.html", "r");  // 打开index.html文件
                  if (file) {
                      size_t fileSize = file.size();  // 获取文件大小
                      String fileContent;
        
                      // 逐字节读取文件内容
                      while (file.available()) {
                          fileContent += (char)file.read();
                      }
                      file.close();  // 关闭文件
        
                      // 返回HTML内容
                      request->send(200, "text/html", fileContent);
                      return;
                  }
                }
                // 如果文件不存在,返回404错误
                request->send(404, "text/plain", "File Not Found"); });
                
                

    server.begin();
}

void loop() {
}

串口信息

正在执行任务: C:\Users\zuo\.platformio\penv\Scripts\platformio.exe device monitor 

--- Terminal on COM8 | 115200 8-N-1
--- Available filters and text transformations: colorize, debug, default, direct, esp8266_exception_decoder, hexlify, log2file, nocontrol, printable, send_on_enter, time
--- More details at https://bit.ly/pio-monitor-filters
--- Terminal on COM8 | 115200 8-N-1
--- Available filters and text transformations: colorize, debug, default, direct, esp8266_exception_decoder, hexlify, log2file, nocontrol, printable, send_on_enter, time
--- More details at https://bit.ly/pio-monitor-filters
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H
rl␀l�r␂$␒�n␌␌␌�␌l�␌b|��␂␇␒�r␒b�␌b␄�nn␂lnn�b␌b␜p�$b␎lrlp�n�␂␌␌�␌l␌�␂␌␌␌b␌n�n�␌�␌␄b��nn'l�␄l`␂�␒␒nn␌l`␂␎␂nr���n␌␌bl�`␂p�n0␂␌␌r�␜␜b␌␄␂␌b␌n�n␌␄␌b��nn'␌␌l`␂�␒␒nn␌l`␂␎␂nr���n␌␌b␄�`␂␎r��n␌␌b␄�`␂�␂␌␌l$␌�␂l`␂��n�`␂�␜␂␇␒�n�␒r��n|�␌l␌�␌l`␂␜b␒␒␂␌b␒r␒␂l�nB�n␂␌l`␂�␜r␒l�l␒�␌␌␌�IP Address: 192.168.0.103

打开网站

通过串口提供的ip地址:192.168.0.103,打开网站
【PlatformIO】基于Arduino的ESP8266 网站部署_第1张图片

视频演示

【PlatformIO】基于Arduino的ESP8266 网站部署

你可能感兴趣的:(esp8266,嵌入式硬件,c语言,vscode)