接入小爱控制引脚4

  • 通过wifi连接点灯科技
  • 将点灯科技设备接入米家
  • 小爱同学通过命令发送不同的操作指令控制灯

APP软件有

- 电灯-blinker
- 米家
- 小爱音响

[ESP8266引脚图](ESP8266 GPIO 的指南:引脚图 - 哔哩哔哩 (bilibili.com))

[点灯科技引入](点灯科技 (diandeng.tech))

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#define BLINKER_WIFI  //用于指定设备接入方式 wifi 接入
#define BLINKER_MIOT_LIGHT
#define BLINKER_MIOT_OUTLET
#define BLINKER_MIOT_SENSOR
#define BLINKER_MIOT_FAN
#define BLINKER_MIOT_AIR_CONDITION

#include <Blinker.h>

//设备密钥
char auth[] = "58347aa10ecb";
//wifi名称
char ssid[] = "ChinaNet-aemM";
//wifi密码
char pswd[] = "wp907678446";

int colorW = 0;

//app的按钮名称(若不添加按钮可以不设置)
BlinkerButton Button1("myLamp");

//app 按钮回调
void button1_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
if (state=="on") {
digitalWrite(LED_BUILTIN, LOW);
// 反馈开关状态
Button1.print("on");
} else if(state=="off"){
digitalWrite(LED_BUILTIN, HIGH);
// 反馈开关状态
Button1.print("off");
}
}

//电源类回调
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == "on") {
digitalWrite(LED_BUILTIN, LOW);
// 反馈开关状态
Button1.print("on");
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
}
else if (state == "off") {
digitalWrite(LED_BUILTIN, HIGH);
// 反馈开关状态
Button1.print("off");
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
}
}

//亮度回调
void miotBright(const String & bright)
{
BLINKER_LOG("need set brightness: ", bright);
colorW = bright.toInt();
BLINKER_LOG("now set brightness: ", colorW);
analogWrite(2,100-colorW); //给指定引脚写入数据
BlinkerMIOT.brightness(colorW);
BlinkerMIOT.print();
}

void setup() {
//初始化串口,并开启调试信息
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
//初始化blinker
Blinker.begin(auth, ssid, pswd);
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachBrightness(miotBright);
Button1.attach(button1_callback); //绑定按键执行回调函数
//设置LED灯为输出模式
pinMode(LED_BUILTIN, OUTPUT);
pinMode(2,OUTPUT);
analogWriteRange(100);
}

void loop() {
Blinker.run();
}