Files
esp8266_study/code/hdmi_change/send/send.ino
编码猿 e62f697592 www
2025-08-29 22:01:03 +08:00

198 lines
4.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
*
* 三进一出 HDMI 切换
*
*/
#define BLINKER_PRINT Serial
#define BLINKER_MIOT_LIGHT
#define BLINKER_WIFI
#include <Arduino.h>
// 红外模块的 控制库
#include <IRremoteESP8266.h>
#include <IRsend.h>
// wifi 和 WebServer 库
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
// json处理库
#include <ArduinoJson.h>
#include <Blinker.h>
// 开发板的 GPIO 4
const uint16_t kIrLed = 4;
// 点灯app中新创建的设备密钥key
char auth[] = "c55b251e4fd2";
// 2.4g wifi名字
char ssid[] = "bmy";
//2.4g wifi密码
char pswd[] = "lb714500";
// 遥控器的 NEC 红外控制码,通过红外接收模块从遥控器上获取到
uint32_t Button1 = 0xFFE01F; // 笔记本
uint32_t Button2 = 0xFF9867; // N100服务器
uint32_t Button3 = 0xFF906F; // Switch 主机
uint32_t ButtonMode = 0xFF02FD; // 1 2 3 顺序切换
// 和点灯app上的按键进行绑定
BlinkerButton But1("btn1");
BlinkerButton But2("btn2");
BlinkerButton But3("btn3");
BlinkerButton ButMode("btnmode");
// 初始化 红外发射类,传入 GPIO
IRsend irsend(kIrLed);
// 创建Web服务器实例监听8080端口
ESP8266WebServer server(8080);
// 接口的响应数据json格式
JsonDocument response;
// 按键回调函数 - 开始
void But1_callback(const String& state) {
BLINKER_LOG("笔记本");
send(Button1);
}
void But2_callback(const String& state) {
BLINKER_LOG("N100服务器");
send(Button2);
}
void But3_callback(const String& state) {
BLINKER_LOG("Switch 主机");
send(Button3);
}
void ButMode_callback(const String& state) {
BLINKER_LOG("1 2 3 顺序切换");
send(ButtonMode);
}
// 按键回调函数 - 结束
// 语音 模式切换
void miotMode(uint8_t mode) {
BLINKER_LOG("need set mode: ", mode);
// 电脑模式 == 笔记本
if (mode == BLINKER_CMD_MIOT_COMPUTER) {
send(Button1);
// 电视模式 == Switch 主机
} else if (mode == BLINKER_CMD_MIOT_TV) {
send(Button3);
// 阅读模式 == N100服务器
} else if (mode == BLINKER_CMD_MIOT_READING) {
send(Button2);
}
BlinkerMIOT.mode(mode);
BlinkerMIOT.print();
}
// 发送NEC红外 方法的封装
void send(uint32_t cmd) {
irsend.sendNEC(cmd);
}
// 初始化 Blinker并绑定各种事件监听
void BlinkerInit() {
// 开启调试信息
BLINKER_DEBUG.stream(Serial);
//配网步骤WIFI接入
Blinker.begin(auth, ssid, pswd);
// 按钮事件的绑定
But1.attach(But1_callback);
But2.attach(But2_callback);
But3.attach(But3_callback);
ButMode.attach(ButMode_callback);
// 语音设置模式
BlinkerMIOT.attachMode(miotMode);
}
// 当访问的接口地址不存在的时候
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
void changeDisplayEvent() {
// 获取前端传递过来的参数
String code = server.arg("code");
response["code"] = 200;
if (code == "1") {
Serial.print("笔记本");
send(Button1);
response["mesage"] = "请求参数为1已显示为 笔记本";
} else if (code == "2") {
Serial.print("n100");
send(Button2);
response["mesage"] = "请求参数为2已显示为 N100服务器";
} else if (code == "3") {
Serial.print("switch");
send(Button3);
response["mesage"] = "请求参数为3已显示为 Switch";
} else {
response["code"] = 404;
response["mesage"] = "code 参数无效";
}
String jsonCode;
serializeJson(response, jsonCode);
server.send(200, "application/json", jsonCode);
}
// WebServer 的 事件监听
void serverOnEvent() {
// 定义切换显示器的接口
server.on("/changeDisplay", changeDisplayEvent);
// 处理404的情况
server.onNotFound(handleNotFound);
}
void setup() {
// 初始化串口
Serial.begin(9600);
// 调用方法
serverOnEvent();
// 点灯 SDK 初始化
BlinkerInit();
// 启动 WebServer 启动服务器
server.begin();
// 初始化红外
irsend.begin();
}
void loop() {
// 处理客户端请求
server.handleClient();
Blinker.run();
}