点灯科技 + 小爱同学语音控制开关灯(WiFi版通信 、四路继电器、ESP8266)
单个继电器效果图:哔哩哔哩
四路继电器效果图:
### ESP8266 模块和 单个继电器接线图 ![image](https://img2024.cnblogs.com/blog/1543371/202401/1543371-20240121124127193-1448865458.png) #### 可以参考csdn https://blog.csdn.net/weixin_51069271/article/details/120509597单路继电器模块 点击查看代码
#define BLINKER_PRINT Serial
#define BLINKER_WIFI
#define BLINKER_MIOT_LIGHT
#include <Blinker.h>
int LIGHT = D4; //esp8266 D4引脚
char auth[] = "************"; //点灯科技 key
char ssid[] = "************"; //wifi名称
char pswd[] = "*****"; //wifi密码
// 新建组件对象
BlinkerButton Button1("bin");//你的按钮名称
BlinkerNumber Number1("num-abc");
int counter = 0;
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(LIGHT, LOW);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(LIGHT, HIGH);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
}
}
// 按下按键即会执行该函数
void button1_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
digitalWrite(LIGHT, !digitalRead(LIGHT));
}
// 如果未绑定的组件被触发,则会执行其中内容
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
counter++;
Number1.print(counter);
}
void setup() {
// 初始化串口
Serial.begin(115200);
#if defined(BLINKER_PRINT)
BLINKER_DEBUG.stream(BLINKER_PRINT);
#endif
// 初始化有LED的IO
pinMode(LIGHT, OUTPUT);
digitalWrite(LIGHT, HIGH);
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Button1.attach(button1_callback);
BlinkerMIOT.attachPowerState(miotPowerState);
}
void loop() {
Blinker.run();
}
四路继电器效果:这个就不放了 原理是一样的
ESP8266 模块和 继电器接线图
Arduino 四路继电器 WiFi接入 源代码 点击查看代码
#define BLINKER_PRINT Serial
#define BLINKER_WIFI
#define BLINKER_MIOT_MULTI_OUTLET //设置为小爱多个插座的模式
#include <Blinker.h>
#define LIGHT D3
#define LIGHT0 D0
#define LIGHT1 D1
#define LIGHT2 D2
char auth[] = "********"; //点灯科技申请设备的 key 注意这里是WiFi接入的
char ssid[] = "********"; //
char pswd[] = "********";
// 新建组件对象
BlinkerButton Button4("bin4");//你的按钮名称
BlinkerButton Button3("bin3");//你的按钮名称
BlinkerButton Button0("bin0");//你的按钮名称
BlinkerButton Button1("bin1");//你的按钮名称
BlinkerButton Button2("bin2");//你的按钮名称
BlinkerNumber Number1("num-abc");
int counter = 0;
// flag标识符 按键状态 0关闭off,1打开on
bool switch0_onoff = 0;
bool switch1_onoff = 0;
bool switch2_onoff = 0;
bool switch3_onoff = 0;
bool switch4_onoff = 0;
bool oState[4] = { false };
void miotPowerState(const String & state, uint8_t num)
{
BLINKER_LOG("need set outlet: ", num, ", power state: ", state);
if (state == BLINKER_CMD_ON) {
if (num == 0)
{
switch0_onoff = 1;
digitalWrite(LED_BUILTIN, LOW);
// 反馈开关状态
//Button0.icon("fas fa-lightbulb-on");
Button4.color("#fddb00");
Button4.print("on");
}
else if (num == 1) //打开客厅的灯
{
switch1_onoff = 1;
digitalWrite(LIGHT, HIGH);
// 反馈开关状态
//Button1.icon("fas fa-lightbulb-on");
//Button0.color("#FFFF00");
Button0.print("on");
}
else if (num == 2) //打开餐厅的灯
{
switch2_onoff = 1;
digitalWrite(LIGHT0, HIGH);
// 反馈开关状态
//Button2.icon("fas fa-lightbulb-on");
Button1.color("#FFFF00");
Button1.print("on");
}
else if (num == 3) //打开厨房的灯
{
switch3_onoff = 1;
digitalWrite(LIGHT1, HIGH);
// 反馈开关状态
//Button3.icon("fas fa-lightbulb-on");
Button2.color("#FFFF00");
Button2.print("on");
}
else if (num == 4) //打开浴室的热水器
{
switch4_onoff = 1;
digitalWrite(LIGHT2, HIGH);
// 反馈开关状态
//Button4.icon("fas fa-lightbulb-on");
Button3.color("#FFFF00");
Button3.print("on");
}
BlinkerMIOT.powerState("on", num);
BlinkerMIOT.print();
oState[num] = true;
}
else if (state == BLINKER_CMD_OFF) {
if (num == 0)
{
switch0_onoff = 0;
digitalWrite(LED_BUILTIN, HIGH);
// 反馈开关状态
//Button0.icon("fal fa-lightbulb");
Button4.color("#808080");
Button4.print("off");
}
else if (num == 1) //关闭客厅的灯
{
switch1_onoff = 0;
digitalWrite(LIGHT, LOW);
//Button1.icon("fal fa-lightbulb");
Button0.color("#808080");
Button0.print("off");
}
else if (num == 2) //关闭餐厅的灯
{
switch2_onoff = 0;
digitalWrite(LIGHT0, LOW);
//Button2.icon("fal fa-lightbulb");
Button1.color("#808080");
Button1.print("off");
}
else if (num == 3) //关闭厨房的灯
{
switch3_onoff = 0;
digitalWrite(LIGHT1, LOW);
//Button3.icon("fal fa-lightbulb");
Button2.color("#808080");
Button2.print("off");
}
else if (num == 4) //关闭浴室的热水器
{
switch4_onoff = 0;
digitalWrite(LIGHT2, LOW);
//Button4.icon("fal fa-lightbulb");
Button3.color("#808080");
Button3.print("off");
}
BlinkerMIOT.powerState("off", num);
BlinkerMIOT.print();
oState[num] = true;
if (num == 0)
{
for (uint8_t o_num = 0; o_num < 5; o_num++)
{
oState[o_num] = false;
}
}
}
}
void miotQuery(int32_t queryCode, uint8_t num)
{
BLINKER_LOG("AliGenie Query outlet: ", num, ", codes: ", queryCode);
switch (queryCode)
{
case BLINKER_CMD_QUERY_ALL_NUMBER :
BLINKER_LOG("AliGenie Query All");
BlinkerMIOT.powerState(oState[num] ? "on" : "off", num);
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
BLINKER_LOG("AliGenie Query Power State");
BlinkerMIOT.powerState(oState[num] ? "on" : "off", num);
BlinkerMIOT.print();
break;
default :
BlinkerMIOT.powerState(oState[num] ? "on" : "off", num);
BlinkerMIOT.print();
break;
}
}
// bin3 按下按键即会执行该函数
void button3_callback(const String & state) {
switch0_onoff = ! switch0_onoff; //记录开关状态
if (switch0_onoff == 1)
{
digitalWrite(LIGHT, HIGH);
// 反馈开关状态
//Button2.icon("fas fa-lightbulb-on");
Button3.color("#fddb00");
Button3.print("on");
}
else
{
digitalWrite(LIGHT, LOW);
// 反馈开关状态
//Button2.icon("fal fa-lightbulb");
Button3.color("#565656");
Button3.print("off");
}
BLINKER_LOG("get button state: ", state);
}
// bin0 按下按键即会执行该函数
void button0_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
switch1_onoff = ! switch1_onoff; //记录开关状态
if (switch1_onoff == 1)
{
digitalWrite(LIGHT0, HIGH);
// 反馈开关状态
//Button2.icon("fas fa-lightbulb-on");
Button0.color("#fddb00");
Button0.print("on");
}
else
{
digitalWrite(LIGHT0, LOW);
// 反馈开关状态
//Button2.icon("fal fa-lightbulb");
Button0.color("#565656");
Button0.print("off");
}
BLINKER_LOG("get state state: ", !digitalRead(LIGHT0));
}
// bin1 按下按键即会执行该函数
void button1_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
switch3_onoff = ! switch3_onoff; //记录开关状态
if (switch3_onoff == 1)
{
digitalWrite(LIGHT1, HIGH);
// 反馈开关状态
//Button2.icon("fas fa-lightbulb-on");
Button1.color("#fddb00");
Button1.print("on");
}
else
{
digitalWrite(LIGHT1, LOW);
// 反馈开关状态
//Button2.icon("fal fa-lightbulb");
Button1.color("#565656");
Button1.print("off");
}
BLINKER_LOG("get state state: ", !digitalRead(LIGHT1));
}
// bin2 按下按键即会执行该函数
void button2_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
switch4_onoff = ! switch4_onoff; //记录开关状态
if (switch4_onoff == 1)
{
digitalWrite(LIGHT2, HIGH);
// 反馈开关状态
//Button2.icon("fas fa-lightbulb-on");
Button2.color("#fddb00");
Button2.print("on");
}
else
{
digitalWrite(LIGHT2, LOW);
// 反馈开关状态
//Button2.icon("fal fa-lightbulb");
Button2.color("#565656");
Button2.print("off");
}
BLINKER_LOG("get state state: ", !digitalRead(LIGHT2));
}
// 如果未绑定的组件被触发,则会执行其中内容
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
counter++;
Number1.print(counter);
}
void setup() {
// 初始化串口
Serial.begin(115200);
#if defined(BLINKER_PRINT)
BLINKER_DEBUG.stream(BLINKER_PRINT);
#endif
// 初始化有LED的IO
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(D4, HIGH);
pinMode(LIGHT, OUTPUT);
pinMode(LIGHT0, OUTPUT);
pinMode(LIGHT1, OUTPUT);
pinMode(LIGHT2, OUTPUT);
digitalWrite(LIGHT, LOW);
digitalWrite(LIGHT0, LOW);
digitalWrite(LIGHT1, LOW);
digitalWrite(LIGHT2, LOW);
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Button3.attach(button3_callback);
Button0.attach(button0_callback);
Button1.attach(button1_callback);
Button2.attach(button2_callback);
BlinkerMIOT.attachQuery(miotQuery);
BlinkerMIOT.attachPowerState(miotPowerState);
}
void loop() {
Blinker.run();
}