[IDE] ESP32-S3(N4) IoT 开发板以WIFI方式控制色彩效果

[复制链接]
148|0
Lily李 发表于 2025-10-5 11:37 | 显示全部楼层 |阅读模式
本帖最后由 Lily李 于 2025-10-5 12:50 编辑

申请原创
@21小跑堂

ESP32-S3(N4) IoT 开发板具有WIFI功能,并可创建一个WIFI服务器,在客户端连接到该服务器的情况下,即可控制LED的亮灭。
以WIFI方式控制LED的步骤为:
1)在手机的蓝牙设置功能中找到“ESP32 S3”,在选取连接后,输入WIFI的密码“123”
2)在手机的网址栏输入“http://192.168.4.1/ON”,则可来打开LED灯;否则若输入“http://192.168.4.1/OFF”,则关闭LED灯。
3)此外在出现访问界面的情况下,还可通过点击上下2行的 here 来便捷地控制LED灯的亮灭,而无需要输入网址的操作。
在Arduino IDE的情况下,相应的准备条件为:
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
#define myLED 21
const char *ssid = "ESP32 S3";
const char *password = "123";
WiFiServer server(80);

实现WIFI控制LED的初始设置为:
  1. void setup() {
  2.   pinMode(myLED, OUTPUT);
  3.   Serial.begin(115200);
  4.   Serial.println();
  5.   Serial.println("Configuring access point...");
  6.   WiFi.softAP(ssid, password);
  7.   IPAddress myIP = WiFi.softAPIP();
  8.   Serial.print("AP IP address: ");
  9.   Serial.println(myIP);
  10.   server.begin();
  11.   Serial.println("Server started");
  12. }

实现功能处理的循环处理程序为:
  1. void loop() {
  2.   WiFiClient client = server.available();
  3.   if (client) {
  4.     Serial.println("New Client.");
  5.     String currentLine = "";
  6. while (client.connected())
  7. {
  8.       if (client.available())
  9.       {
  10.         char c = client.read();
  11.         if (c == '\n') {
  12.           if (currentLine.length() == 0) {
  13.             client.println("HTTP/1.1 200 OK");
  14.             client.println("Content-type:text/html");
  15.             client.println();
  16.             client.print("Click <a href="/ON">here</a> to turn ON the LED.<br>");
  17.             client.print("Click <a href="/OFF">here</a> to turn OFF the LED.<br>");
  18.             client.println();
  19.             break;
  20.           }
  21.           else
  22.           {
  23.             currentLine = "";
  24.           }
  25.         }
  26.         else if (c != '\r')
  27.         {
  28.           currentLine += c;
  29.         }
  30.         if (currentLine.endsWith("/ON")) {
  31.           digitalWrite(myLED, HIGH);
  32.         }
  33.         if (currentLine.endsWith("/OFF")) {
  34.           digitalWrite(myLED, LOW);
  35.         }
  36.       }
  37.     }
  38.     client.stop();
  39.     Serial.println("Client Disconnected.");
  40.   }
  41. }

经程序上传,其测试效果如图1至图5所示。
554068e1e689a5e79.png
1 串口输出连接信息

8147968e1e6a72ff92.png
2 串口监视器输出连接信息

2041668e1e7cf1077a.png
3 手机操作界面



4615368e1e7e55d954.png
4  点亮绿灯

6798968e1e7f67ce78.png
5 熄灭绿灯

为了产生更丰富的色彩,可外观一个RGB_LED模块,其引脚被分别连接到GPIO11GPIO12GPIO13,相应的定义为:
#define myLED 21
#define myLED1 12
#define myLED2 13
#define myLED3 11
将所用引脚设置为输出模式的语句为:
pinMode(myLED, OUTPUT);
pinMode(myLED1, OUTPUT);
pinMode(myLED2, OUTPUT);
pinMode(myLED3, OUTPUT);
为便于进行控制,更新后的页面生成代码为:
  1. client.print("Click <a href="/ON">here</a> to turn ON the LED.<br>");

  2. client.print("Click <a href="/OFF">here</a> to turn OFF the LED.<br>");

  3. client.print("Click <a href="/RED">here</a> to turn RED the LED.<br>");

  4. client.print("Click <a href="/GREEN">here</a> to turn GREEN the LED.<br>");

  5. client.print("Click <a href="/BLUE">here</a> to turn BLUE the LED.<br>");

对应的页面控制效果程序为:
  1. if (currentLine.endsWith("/ON"))
  2. {
  3.      digitalWrite(myLED, HIGH);
  4. }
  5. if (currentLine.endsWith("/OFF"))
  6. {
  7.      digitalWrite(myLED, LOW);
  8.      digitalWrite(myLED1, LOW);
  9.      digitalWrite(myLED2, LOW);
  10.      digitalWrite(myLED3, LOW);
  11. }
  12. if (currentLine.endsWith("/RED"))
  13. {
  14.      digitalWrite(myLED1, HIGH);
  15. }
  16. if (currentLine.endsWith("/GREEN"))
  17. {
  18.      digitalWrite(myLED2, HIGH);
  19. }
  20. if (currentLine.endsWith("/BLUE"))
  21. {
  22.      digitalWrite(myLED3, HIGH);
  23. }

经程序上传,其测试效果如图6至图8所示,由此可见其色彩表现力还是很不错的。
9310968e1f8fa39859.png
6 点亮红灯

3933868e1f90d60a30.png
7 产生黄色效果

4883468e1f91ef303c.png
8 产生紫色效果


您需要登录后才可以回帖 登录 | 注册

本版积分规则

13

主题

15

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部