当前位置: 首页 > article >正文

ESP32-CAM实验集(WebServer)

WebServer
效果图

已连接
在这里插入图片描述
在这里插入图片描述

web端
在这里插入图片描述

platformio.ini
; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:esp32]
platform = espressif32
framework = arduino
board = esp32dev

源代码
/* ESP32 HTTP IoT Server Example for Wokwi.com

  https://wokwi.com/arduino/projects/320964045035274834

  When running it on Wokwi for VSCode, you can connect to the
  simulated ESP32 server by opening http://localhost:8180
  in your browser. This is configured by wokwi.toml.
*/

#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <uri/UriBraces.h>

// #define WIFI_SSID "Wokwi-GUEST"
// #define WIFI_PASSWORD ""
#define WIFI_SSID "iQOO Neo6"
#define WIFI_PASSWORD "yyuuiioo"
// Defining the WiFi channel speeds up the connection:
#define WIFI_CHANNEL 6

WebServer server(80);

const int LED1 = 2;
const int LED2 = 4;

bool led1State = false;
bool led2State = false;

void sendHtml()
{
  String response = R"(
    <!DOCTYPE html><html>
      <head>
       <meta charset="UTF-8">
       <!-- 其他原有头部标签内容 -->
        <title>ESP32 Web Server Demo</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <style>
          html { font-family: sans-serif; text-align: center; }
          body { display: inline-flex; flex-direction: column; }
          h1 { margin-bottom: 1.2em; } 
          h2 { margin: 0; }
          div { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: auto auto; grid-auto-flow: column; grid-gap: 1em; }
          .btn { background-color: #5B5; border: none; color: #fff; padding: 0.5em 1em;
                 font-size: 2em; text-decoration: none }
          .btn.OFF { background-color: #333; }
        </style>
      </head>
            
      <body>
        <h1>ESP32 Web Server</h1>

        <div>
          <h2>蓝色LED 灯</h2>
          <a href="/toggle/1" class="btn LED1_TEXT">LED1_TEXT</a>
          <h2>绿色的LED 灯</h2>
          <a href="/toggle/2" class="btn LED2_TEXT">LED2_TEXT</a>
        </div>
      </body>
    </html>
  )";
  response.replace("LED1_TEXT", led1State ? "ON" : "OFF");
  response.replace("LED2_TEXT", led2State ? "ON" : "OFF");
  server.send(200, "text/html", response);
}

void setup(void)
{
  Serial.begin(115200);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);

  WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
  Serial.print("Connecting to WiFi ");
  Serial.print(WIFI_SSID);
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(2000);

    Serial.print("Wait for connection .");
  }
  Serial.println(" Connected!");

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

  server.on("/", sendHtml);

  server.on(UriBraces("/toggle/{}"), []()
            {
    String led = server.pathArg(0);
    Serial.print("Toggle LED #");
    Serial.println(led);

    switch (led.toInt()) {
      case 1:
        led1State = !led1State;
        if (led1State)
        {
          Serial.print("打开");
        }else{
          Serial.print("关闭");
        }
        
        Serial.println("蓝色LED");
        digitalWrite(LED1, led1State);
        break;
      case 2:
       led2State = !led2State;
       if (led2State)
        {
          Serial.print("打开");
        }else{
          Serial.print("关闭");
        }
        
        Serial.println("打开绿色LED");
       
        digitalWrite(LED2, led2State);
        break;
    }

    sendHtml(); });

  server.begin();
  Serial.println("HTTP server started (http://" + WiFi.localIP().toString() + ":80)");
}

void loop(void)
{
  server.handleClient();
  delay(2);
}


http://www.kler.cn/a/521580.html

相关文章:

  • 【Numpy核心编程攻略:Python数据处理、分析详解与科学计算】1.26 统计圣殿:从描述统计到推断检验
  • 渗透测试技法之口令安全
  • Excel 技巧21 - Excel中整理美化数据实例,Ctrl+T 超级表格(★★★)
  • 不背单词快捷键(不背单词键盘快捷键)
  • SQL注入漏洞之高阶手法 宽字节注入以及编码解释 以及堆叠注入原理说明
  • 【C语言练习题】找出不是两个数组共有的元素
  • 50. TCP报文段
  • Redis存储④Redis五大数据类型之String和Hash
  • 基于springboot+vue的流浪动物救助系统的设计与实现
  • Java 大视界 -- Java 大数据在生物信息学中的应用与挑战(67)
  • 【Postman接口测试】Postman的安装和使用
  • solidity高阶 -- 继承
  • Qt5离线安装包无法下载问题解决办法
  • postman下载、安装、注册、登陆
  • sunrays-framework配置重构
  • 图论——单源点最短路径建图方式
  • 9.3 GPT Action 设计模式:打造高效的 AI 驱动应用
  • CAN总线
  • DP专题----
  • 人工智能学习框架:深入解析与实战指南
  • 【算法】【归并排序】AcWing 算法基础 788. 逆序对的数量
  • 深度解析“Integrity”——从技术到品格的多重意义
  • AI Agent:改变的不仅是技术,更是我们的未来选择
  • Django的安装
  • 2024年度即寒假总结
  • 【Linux】磁盘