|
8266 4路继电器,小爱同学代码应该怎么加,点灯的按钮可以正常使用,小爱同学不能用,继电器为高电平触发,求大神看看
- #define BLINKER_WIFI
- #define BLINKER_MIOT_MULTI_OUTLET //定义小爱同学为4孔插座
- #include <Blinker.h>
- char auth[] = "0b64e478e6c9"; //上一步中在app中获取到的Secret Key
- char ssid[] = "SM"; //您的WiFi热点名称
- char pswd[] = "a88888888"; //您的WiFi密码
- //定义继电器信号
- #define bt1 5 //定义继电器1信号接入GPIO5,D1
- #define bt2 4 //定义继电器2信号接入GPIO4,D2
- #define bt3 0 //定义继电器3信号接入GPIO0,D3
- #define bt4 2 //定义继电器4信号接入GPIO2,D4
- // 新建Blinker软件组件对象
- BlinkerButton Button1("Switch1"); //组件对象,要和APP组件中的“数据键名”一致,开关1
- BlinkerButton Button2("Switch2"); //组件对象,要和APP组件中的“数据键名”一致,开关2
- BlinkerButton Button3("Switch3"); //组件对象,要和APP组件中的“数据键名”一致,开关3
- BlinkerButton Button4("Switch4"); //组件对象,要和APP组件中的“数据键名”一致,开关4
- // 按下按键1即会执行该函数 浇水
- void button1_callback(const String & state) {
- BLINKER_LOG("get button state: ", state);
- if (state == "on") {
- digitalWrite(bt1, HIGH);
- // 反馈开关状态
- Button1.print("on");
- } else if (state == "off") {
- digitalWrite(bt1, LOW);
- // 反馈开关状态
- Button1.print("off");
- }
- }
- // 按下按键2即会执行该函数 水循环
- void button2_callback(const String & state) {
- BLINKER_LOG("get button state: ", state);
- if (state == "on") {
- digitalWrite(bt2, HIGH);
- // 反馈开关状态
- Button2.print("on");
- } else if (state == "off") {
- digitalWrite(bt2, LOW);
- // 反馈开关状态
- Button2.print("off");
- }
- }
- // 按下按键3即会执行该函数 鱼食
- void button3_callback(const String & state) {
- BLINKER_LOG("get button state: ", state);
- if (state == "on") {
- digitalWrite(bt3, HIGH);
- // 反馈开关状态
- Button3.print("on");
- Blinker.delay(10000);//打开后延时10S关闭设置为喂鱼时间,预留网络延迟时间1-2S
- digitalWrite(bt3, LOW);
- // 反馈开关状态
- Button3.print("off");
- } else if (state == "off") {
- digitalWrite(bt3, LOW);
- // 反馈开关状态
- Button3.print("off");
- }
- }
- // 按下按键4即会执行该函数 水池加水
- void button4_callback(const String & state) {
- BLINKER_LOG("get button state: ", state);
- if (state == "on") {
- digitalWrite(bt4, HIGH);
- // 反馈开关状态
- Button4.print("on");
- } else if (state == "off") {
- digitalWrite(bt4, LOW);
- // 反馈开关状态
- Button4.print("off");
- }
- }
- void setup() {
- // 初始化串口
- Serial.begin(115200);
- // 初始化有LED的IO
- pinMode(bt1, OUTPUT);
- digitalWrite(bt1, LOW);//高电平触发,默认低电平
- pinMode(bt2, OUTPUT);
- digitalWrite(bt2, LOW);
- pinMode(bt3, OUTPUT);
- digitalWrite(bt3, LOW);
- pinMode(bt4, OUTPUT);
- digitalWrite(bt4, LOW);
- // 初始化blinker
- Blinker.begin(auth, ssid, pswd);
- Button1.attach(button1_callback);
- Button2.attach(button2_callback);
- Button3.attach(button3_callback);
- Button4.attach(button4_callback);
- }
- void loop() {
- Blinker.run();
- }
复制代码
|
|