320 lines
14 KiB
C++
320 lines
14 KiB
C++
/**
|
||
*
|
||
* 普通灯带 控制
|
||
*
|
||
*/
|
||
|
||
#define BLINKER_PRINT Serial
|
||
#define BLINKER_MIOT_LIGHT
|
||
#define BLINKER_WIFI
|
||
#include <ESP8266WiFi.h>
|
||
#include <IRremoteESP8266.h>
|
||
#include <IRsend.h> //红外头文件
|
||
#include <Blinker.h> //点灯科技头文件
|
||
|
||
char auth[] = "1d0a9629ea9e";//这里面填写设备的密钥key
|
||
char ssid[] = "bmy";//这里填写你的wifi名字
|
||
char pswd[] = "lb714500";//这里填写你的WiFi密码
|
||
|
||
//定义红外发射的管脚
|
||
const uint16_t kIrLed = 4; // D2
|
||
IRsend irsend(kIrLed);
|
||
|
||
bool isOpen = false; // 默认未开灯
|
||
|
||
uint8_t currentBrightness = 40;
|
||
|
||
// 开
|
||
uint16_t power_on[71] = {9300, 4510, 624, 552, 576, 576, 574, 576, 598, 554, 574, 576, 598, 554, 596, 554, 598, 554, 598, 1668, 600, 1668, 600, 1666, 602, 1642, 620, 554, 572, 1694, 600, 1668, 572, 1694, 572, 1694, 574, 1694, 598, 554, 596, 554, 574, 578, 572, 580, 598, 554, 598, 554, 572, 578, 600, 552, 598, 1670, 572, 1694, 598, 1670, 572, 1694, 572, 1694, 572, 1694, 598, 41236, 9300, 2228, 630};
|
||
|
||
// 关
|
||
uint16_t power_off[71] = {9256, 4578, 558, 592, 556, 596, 556, 594, 558, 594, 558, 594, 558, 594, 556, 594, 560, 592, 558, 1708, 558, 1710, 558, 1710, 556, 1710, 558, 594, 558, 1710, 558, 1708, 556, 1710, 558, 594, 556, 1710, 560, 592, 558, 594, 558, 594, 556, 594, 558, 594, 558, 592, 558, 1708, 560, 592, 558, 1708, 560, 1708, 558, 1710, 560, 1708, 560, 1708, 560, 1708, 558, 41290, 9232, 2294, 560};
|
||
|
||
// FLASH 闪光模式
|
||
uint16_t model_flash[71] = {9232, 4540, 580, 590, 556, 592, 556, 594, 556, 592, 556, 592, 554, 592, 556, 592, 556, 592, 556, 1702, 558, 1702, 558, 1702, 558, 1702, 558, 568, 578, 1702, 558, 1700, 558, 1702, 556, 1702, 556, 1702, 558, 590, 556, 1702, 558, 592, 558, 590, 556, 592, 556, 592, 556, 592, 558, 590, 558, 1702, 558, 592, 556, 1704, 556, 1702, 558, 1702, 558, 1702, 558, 41150, 9244, 2288, 558};
|
||
|
||
// STROBE 频闪模式 较快的多彩颜色切换
|
||
uint16_t model_strobe[71] = {9230, 4558, 578, 594, 556, 596, 554, 596, 556, 594, 556, 596, 556, 596, 556, 594, 556, 596, 556, 1712, 556, 1710, 556, 1710, 556, 1710, 556, 596, 556, 1710, 554, 1712, 556, 1710, 556, 1712, 556, 1710, 558, 1710, 556, 1712, 556, 596, 556, 594, 558, 594, 556, 596, 556, 594, 556, 596, 556, 594, 556, 596, 556, 1712, 558, 1710, 558, 1710, 558, 1710, 558, 41284, 9260, 2292, 560};
|
||
|
||
// FADE 渐变模式 较慢的呼吸多彩颜色切换
|
||
uint16_t model_fade[71] = {9258, 4556, 582, 568, 584, 574, 578, 570, 584, 594, 558, 572, 580, 570, 582, 572, 580, 572, 580, 1710, 560, 1710, 558, 1710, 558, 1712, 560, 570, 582, 1710, 560, 1708, 560, 1710, 558, 1710, 558, 1710, 560, 594, 558, 594, 558, 1710, 558, 570, 582, 594, 558, 572, 582, 572, 582, 594, 558, 1710, 558, 1710, 560, 568, 584, 1710, 560, 1710, 560, 1710, 560, 41296, 9268, 2272, 582};
|
||
|
||
// SMOOTH 平滑模式 和闪光模式区别不大 闪光模式
|
||
uint16_t model_smooth[71] = {9264, 4554, 582, 594, 558, 594, 558, 592, 560, 594, 558, 592, 558, 594, 560, 594, 558, 594, 558, 1708, 560, 1710, 558, 1708, 560, 1708, 560, 572, 580, 1708, 560, 1710, 560, 1708, 560, 1708, 560, 1710, 558, 1710, 558, 594, 558, 1710, 558, 574, 578, 594, 558, 594, 558, 594, 558, 572, 582, 572, 580, 1710, 558, 572, 582, 1708, 560, 1710, 560, 1708, 560, 41320, 9240, 2274, 580};
|
||
|
||
// 白光
|
||
uint16_t color_white[71] = {9300, 4494, 630, 544, 606, 544, 604, 546, 604, 544, 604, 548, 604, 544, 576, 574, 576, 572, 606, 1656, 604, 1658, 602, 1660, 608, 1654, 606, 542, 578, 1684, 602, 1658, 604, 1660, 602, 1658, 606, 1656, 602, 1660, 606, 542, 606, 544, 608, 542, 580, 568, 604, 546, 604, 546, 606, 542, 606, 544, 602, 1660, 608, 1654, 604, 1658, 606, 1656, 608, 1656, 576, 41164, 9308, 2214, 630};
|
||
|
||
// 红色
|
||
uint16_t color_red[71] = {9204, 4560, 558, 614, 534, 614, 536, 612, 534, 614, 534, 614, 560, 586, 534, 612, 536, 614, 534, 1722, 536, 1724, 564, 1694, 564, 1694, 568, 580, 566, 1690, 568, 1690, 570, 1686, 602, 546, 574, 572, 600, 1658, 606, 540, 604, 542, 604, 542, 576, 572, 602, 544, 604, 1654, 578, 1680, 602, 544, 574, 1684, 598, 1660, 598, 1660, 600, 1660, 600, 1658, 600, 41068, 9280, 2222, 620};
|
||
|
||
// 绿色
|
||
uint16_t color_green[71] = {9230, 4534, 582, 564, 582, 564, 582, 564, 582, 564, 582, 564, 584, 564, 582, 564, 582, 564, 582, 1674, 582, 1676, 580, 1674, 582, 1674, 580, 564, 582, 1674, 580, 1674, 582, 1674, 580, 1674, 584, 564, 582, 1672, 582, 562, 582, 564, 582, 564, 582, 564, 582, 562, 582, 564, 582, 1676, 582, 564, 582, 1674, 582, 1674, 582, 1674, 582, 1674, 580, 1676, 580, 41108, 9228, 2264, 582};
|
||
|
||
// 蓝色
|
||
uint16_t color_blue[71] = {9272, 4488, 626, 544, 604, 544, 596, 550, 602, 542, 600, 546, 598, 548, 598, 548, 602, 544, 600, 1658, 602, 1656, 598, 1658, 606, 1652, 602, 544, 602, 1656, 600, 1658, 572, 1684, 598, 548, 604, 1654, 602, 1654, 574, 572, 600, 546, 604, 542, 604, 542, 574, 572, 600, 1658, 576, 570, 572, 574, 604, 1654, 598, 1660, 604, 1654, 600, 1658, 576, 1682, 574, 41142, 9294, 2216, 630};
|
||
|
||
// 紫色
|
||
uint16_t color_purple[71] = {9088, 4534, 580, 560, 578, 586, 552, 560, 580, 558, 580, 562, 574, 562, 576, 564, 576, 562, 576, 1676, 574, 1678, 576, 1676, 576, 1678, 574, 562, 576, 1676, 580, 1674, 574, 1680, 578, 558, 576, 1676, 578, 562, 574, 564, 576, 1676, 576, 562, 574, 564, 574, 564, 574, 1676, 574, 564, 550, 1702, 550, 1706, 546, 590, 574, 1678, 548, 1704, 574, 1678, 548};
|
||
|
||
// 粉色
|
||
uint16_t color_pink[71] = {9126, 4506, 608, 554, 586, 554, 582, 556, 586, 552, 584, 556, 586, 554, 584, 554, 588, 552, 586, 1666, 586, 1668, 586, 1666, 584, 1668, 584, 554, 584, 1668, 586, 1666, 586, 1666, 586, 552, 586, 1666, 586, 1668, 586, 552, 586, 1668, 588, 552, 586, 552, 586, 554, 586, 1666, 586, 554, 586, 552, 584, 1668, 586, 554, 586, 1670, 582, 1670, 584, 1670, 582};
|
||
|
||
// 月光模式
|
||
uint16_t color_moonlight[71] = {9122, 4512, 610, 556, 586, 556, 558, 582, 556, 584, 582, 558, 558, 582, 558, 582, 558, 584, 582, 1672, 558, 1674, 606, 1650, 580, 1674, 582, 582, 556, 1676, 584, 1672, 580, 1676, 582, 580, 560, 584, 556, 1674, 606, 558, 556, 1698, 560, 582, 558, 582, 556, 584, 558, 1676, 582, 1698, 582, 560, 584, 1650, 578, 586, 558, 1672, 610, 1646, 584, 1672, 606, 41226, 9104, 2256, 582};
|
||
|
||
// 温馨 模式的黄色,较深
|
||
uint16_t color_cozy[71] = {9220, 4536, 586, 560, 586, 562, 586, 558, 588, 562, 586, 586, 560, 562, 584, 586, 562, 560, 588, 1698, 564, 1672, 586, 1672, 592, 1668, 590, 584, 564, 1670, 588, 1698, 562, 1672, 588, 586, 562, 584, 564, 582, 566, 1694, 588, 558, 564, 582, 564, 582, 564, 582, 566, 1670, 588, 1670, 592, 1670, 588, 582, 562, 1672, 590, 1670, 588, 1672, 614, 1670, 566, 41152, 9256, 2228, 614};
|
||
|
||
// 电视模式
|
||
uint16_t color_tv[71] = {9008, 4512, 566, 564, 566, 562, 568, 562, 566, 562, 568, 562, 568, 562, 568, 562, 566, 562, 566, 1670, 566, 1668, 566, 1670, 566, 1668, 566, 562, 568, 1668, 566, 1666, 568, 1670, 566, 1668, 566, 562, 568, 562, 566, 1672, 566, 562, 568, 562, 568, 562, 566, 564, 566, 564, 568, 1670, 566, 1668, 566, 564, 568, 1668, 568, 1670, 566, 1670, 568, 1670, 568, 41020, 9058, 2262, 570};
|
||
|
||
// 增加亮度
|
||
uint16_t action_add[71] = {9308, 4506, 624, 552, 600, 550, 600, 550, 602, 550, 602, 548, 602, 550, 602, 550, 602, 550, 600, 1664, 602, 1664, 600, 1666, 602, 1664, 602, 550, 600, 1666, 602, 1664, 602, 1664, 600, 550, 604, 546, 602, 550, 604, 548, 602, 550, 600, 550, 602, 550, 600, 550, 602, 1664, 604, 1664, 602, 1664, 602, 1664, 602, 1664, 602, 1666, 600, 1666, 600, 1664, 602, 41204, 9306, 2224, 626};
|
||
|
||
// 减少亮度
|
||
uint16_t action_reduce[71] = {9242, 4576, 560, 590, 588, 564, 586, 566, 562, 590, 586, 566, 586, 566, 562, 590, 586, 566, 562, 1710, 560, 1706, 586, 1684, 562, 1708, 560, 590, 586, 1684, 560, 1710, 560, 1706, 562, 1708, 560, 590, 588, 564, 588, 564, 586, 564, 586, 566, 564, 588, 564, 590, 562, 590, 590, 1678, 588, 1682, 562, 1706, 588, 1682, 586, 1710, 562, 1682, 586, 1684, 562, 41344, 9272, 2266, 588};
|
||
|
||
// 新建Blinker组件对象
|
||
BlinkerButton ButtonOn("btn-on");
|
||
// 新建Blinker组件对象
|
||
BlinkerButton ButtonOff("btn-off");
|
||
|
||
// 开灯
|
||
void on() {
|
||
isOpen = true;
|
||
send(power_on);
|
||
BLINKER_LOG("onLamp : ");
|
||
}
|
||
|
||
// 关灯
|
||
void off() {
|
||
isOpen = false;
|
||
send(power_off);
|
||
BLINKER_LOG("offLamp : ");
|
||
}
|
||
|
||
// 发送红外指令
|
||
void send(uint16_t* cmd) {
|
||
irsend.sendRaw(cmd, 199, 38);
|
||
}
|
||
|
||
// 首页按钮被点击
|
||
void switch_callback(const String & state) {
|
||
BLINKER_LOG("home button click: ", state);
|
||
if (state == BLINKER_CMD_ON) {
|
||
on();
|
||
BUILTIN_SWITCH.print("on");
|
||
} else if (state == BLINKER_CMD_OFF) {
|
||
off();
|
||
BUILTIN_SWITCH.print("off");
|
||
}
|
||
}
|
||
|
||
// 灯的颜色设置
|
||
void miotColor(int32_t color) {
|
||
BLINKER_LOG("need set color: ", color);
|
||
BlinkerMIOT.color(color);
|
||
BlinkerMIOT.print();
|
||
// 蓝色
|
||
if (color == 255) {
|
||
send(color_blue);
|
||
|
||
// 红色
|
||
} else if (color == 16711680) {
|
||
send(color_red);
|
||
|
||
// 绿色
|
||
} else if (color == 65280) {
|
||
send(color_green);
|
||
|
||
// 白色
|
||
} else if (color == 16777215) {
|
||
send(color_white);
|
||
|
||
// 紫色
|
||
} else if (color == 10494192) {
|
||
send(color_purple);
|
||
|
||
// 粉色
|
||
} else if (color == 16738740) {
|
||
send(color_pink);
|
||
}
|
||
}
|
||
|
||
// 心跳包Heartbeat
|
||
void heartbeat() {
|
||
if (isOpen) {
|
||
BUILTIN_SWITCH.print("on");
|
||
} else {
|
||
BUILTIN_SWITCH.print("off");
|
||
};
|
||
}
|
||
|
||
// 灯的模式控制
|
||
void miotMode(uint8_t mode) {
|
||
BLINKER_LOG("need set mode: ", mode);
|
||
|
||
BlinkerMIOT.mode(mode);
|
||
BlinkerMIOT.print();
|
||
|
||
if (!isOpen) {
|
||
on();
|
||
}
|
||
// 日光 模式
|
||
if (mode == BLINKER_CMD_MIOT_DAY) {
|
||
send(color_white);
|
||
}
|
||
// 月光 模式
|
||
else if (mode == BLINKER_CMD_MIOT_NIGHT) {
|
||
send(color_moonlight);
|
||
}
|
||
// 彩光 模式
|
||
else if (mode == BLINKER_CMD_MIOT_COLOR) {
|
||
send(model_fade);
|
||
}
|
||
// 温馨 模式
|
||
else if (mode == BLINKER_CMD_MIOT_WARMTH) {
|
||
send(color_cozy);
|
||
}
|
||
// 电视模式
|
||
else if (mode == BLINKER_CMD_MIOT_TV) {
|
||
send(color_tv);
|
||
}
|
||
}
|
||
|
||
// 电源控制
|
||
void miotPowerState(const String & state) {
|
||
if (state == BLINKER_CMD_ON) {
|
||
BlinkerMIOT.powerState("on");
|
||
BlinkerMIOT.print();
|
||
on();
|
||
}
|
||
else if (state == BLINKER_CMD_OFF) {
|
||
BlinkerMIOT.powerState("off");
|
||
BlinkerMIOT.print();
|
||
off();
|
||
}
|
||
}
|
||
|
||
// 小音箱控制亮度
|
||
void miotBright(const String & bright) {
|
||
BLINKER_LOG("need set brightness: ", bright);
|
||
uint8_t colorW = 0;
|
||
if (bright == BLINKER_CMD_MAX) {
|
||
colorW = 100;
|
||
} else if (bright == BLINKER_CMD_MIN) {
|
||
colorW = 0;
|
||
} else {
|
||
colorW = bright.toInt();
|
||
}
|
||
|
||
BlinkerMIOT.brightness(colorW);
|
||
BlinkerMIOT.print();
|
||
|
||
// 小爱同学的语音亮度设置范围是 0 ~ 100,所以需要转化RGB灯带的 0 ~ 40 的范围
|
||
uint8_t brightness = 40 * colorW / 100;
|
||
BLINKER_LOG("brightness ==== : ", brightness);
|
||
BLINKER_LOG("currentBrightness ==== : ", currentBrightness);
|
||
BLINKER_LOG("currentBrightness - brightness ==== : ", currentBrightness - brightness);
|
||
|
||
// 如果上次亮度 大于 当前要设置的亮度,表示你想减弱灯带亮度
|
||
// 拿到差值,循环执行 差值次数 的 减弱亮度 方法
|
||
if (currentBrightness > brightness) {
|
||
BLINKER_LOG("action_reduce 1");
|
||
for (uint8_t i = 0; i <= currentBrightness - brightness; i++)
|
||
{
|
||
BLINKER_LOG("action_reduce 2");
|
||
send(action_reduce);
|
||
delay(50);
|
||
}
|
||
|
||
// 如果上次的亮度 小于 当前要设置的亮度,表示你想增加灯带亮度
|
||
// 拿到差值,循环执行 差值次数 的 增加亮度
|
||
} else if (currentBrightness < brightness){
|
||
BLINKER_LOG("action_add 1");
|
||
for (uint8_t i = 0; i <= brightness - currentBrightness; i++)
|
||
{
|
||
BLINKER_LOG("action_add 2");
|
||
send(action_add);
|
||
delay(50);
|
||
}
|
||
} else if (currentBrightness == brightness) {
|
||
for (uint8_t i = 0; i <= 40; i++)
|
||
{
|
||
BLINKER_LOG("action_add 3");
|
||
send(action_add);
|
||
delay(50);
|
||
}
|
||
}
|
||
|
||
BLINKER_LOG("end");
|
||
currentBrightness = brightness;
|
||
}
|
||
|
||
|
||
|
||
// 初始化 Blinker,并绑定各种事件监听
|
||
void BlinkerInit() {
|
||
BLINKER_DEBUG.stream(Serial);
|
||
|
||
//配网步骤:WIFI接入
|
||
Blinker.begin(auth,ssid,pswd);
|
||
|
||
// 在列表首页显示开关按钮
|
||
BUILTIN_SWITCH.attach(switch_callback);
|
||
// 自定义心跳的监听处理
|
||
Blinker.attachHeartbeat(heartbeat);
|
||
|
||
// 注册按钮被点击的回调函数/
|
||
ButtonOn.attach(ButtonOn_callback);
|
||
ButtonOff.attach(ButtonOff_callback);
|
||
|
||
// 语音亮度调节
|
||
BlinkerMIOT.attachBrightness(miotBright);
|
||
// 语音设置模式
|
||
BlinkerMIOT.attachMode(miotMode);
|
||
// 语音颜色设置
|
||
BlinkerMIOT.attachColor(miotColor);
|
||
// 语音电源控制
|
||
BlinkerMIOT.attachPowerState(miotPowerState);
|
||
}
|
||
|
||
|
||
|
||
void ButtonOn_callback(const String &state)
|
||
{
|
||
BLINKER_LOG("ButtonOn_callback state: ", state);
|
||
//检测到普通按钮按下(btn-on)
|
||
if (state == BLINKER_CMD_BUTTON_TAP)
|
||
{
|
||
//发送红外指令
|
||
irsend.sendRaw(power_on, 199, 38); // 以38Khz发送红外数据
|
||
ButtonOn.print("打开");
|
||
}
|
||
}
|
||
|
||
void ButtonOff_callback(const String &state)
|
||
{
|
||
BLINKER_LOG("ButtonOff_callback state: ", state);
|
||
//检测到普通按钮按下(btn-on)
|
||
if (state == BLINKER_CMD_BUTTON_TAP)
|
||
{
|
||
//发送红外指令
|
||
irsend.sendRaw(power_off, 199, 38); // 以38Khz发送红外数据
|
||
ButtonOff.print("关闭");
|
||
}
|
||
}
|
||
|
||
|
||
void setup() {
|
||
//初始化串口
|
||
Serial.begin(115200);
|
||
BlinkerInit();
|
||
//红外初始化
|
||
irsend.begin();
|
||
}
|
||
|
||
void loop() {
|
||
Blinker.run();
|
||
} |