小爱同学四路控制,大神们看看有没有问题。。。。。。...
#define BLINKER_WIFI#define BLINKER_MIOT_MULTI_OUTLET //设置为小爱多个插座的模式
#define BLINKER_PRINT Serial
#include <Blinker.h>
char auth[] = "c2b51c836ab4";
char ssid[] = "CMCC-happyking";
char pswd[] = "12349876";
//多路继电器控制////////////
int gpio[] = {LED_BUILTIN, 14, 12, 13, 15}; //1=D5 2=D6 3=D7 4=D8
//////////////////////////
// 新建组件对象
BlinkerButton Button1("k1"); //按钮1
BlinkerButton Button2("k2"); //按钮2
BlinkerButton Button3("k3"); //按钮3
BlinkerButton Button4("k4"); //按钮4
///////////////LED蓝色小灯闪烁5次开始////////////
void led()
{
for (uint8_t o_num = 0; o_num < 5; o_num++)
{
delay(80);
digitalWrite(gpio, LOW);
delay(80);
digitalWrite(gpio, HIGH);
delay(80);
}
}
///////////////LED蓝色小灯闪烁5次结束////////////
////////////////反馈按键状态开始/////////////////
void Set_Butt(int num) //on反馈
{
if (num == 1) {
Button1.color("#228B22"); //设置app按键是绿色
Button1.text(String(num)+"灯已打开");
Button1.print("on");
}
else if (num == 2) {
Button2.color("#228B22"); //设置app按键是绿色
Button2.text(String(num)+"灯已打开");
Button2.print("on");
}
else if (num == 3) {
Button3.color("#228B22"); //设置app按键是绿色
Button3.text(String(num)+"灯已打开");
Button3.print("on");
}
else if (num == 4) {
Button4.color("#228B22"); //设置app按键是绿色
Button4.text(String(num)+"灯已打开");
Button4.print("on");
}
}
void Reset_Butt(int num) //off反馈
{
if (num == 1) {
Button1.color("#778899"); //设置app按键是灰色
Button1.text(String(num)+"灯已关闭");
Button1.print("off");
}
else if (num == 2) {
Button2.color("#778899"); //设置app按键是灰色
Button2.text(String(num)+"灯已关闭");
Button2.print("off");
}
else if (num == 3) {
Button3.color("#778899"); //设置app按键是灰色
Button3.text(String(num)+"灯已关闭");
Button3.print("off");
}
else if (num == 4) {
Button4.color("#778899"); //设置app按键是灰色
Button4.text(String(num)+"灯已关闭");
Button4.print("off");
}
}
////////////////反馈按键状态结束/////////////////
/////////////控制代码开始///////////////
void control(int num, const String state)
{
BLINKER_LOG("按键"+String(num)+"状态 : ", state); //串口打印
Blinker.delay(350); //延时150ms不能太少标准按键成功率
if (state == BLINKER_CMD_ON) { //如果state是on状态
BLINKER_LOG(String(num)+"灯已打开"); //串口打印
digitalWrite(gpio, HIGH);
Set_Butt(num);
led();
}
else if (state == BLINKER_CMD_OFF) { //如果state是off状态
BLINKER_LOG(String(num)+"灯已关闭"); //串口打印
digitalWrite(gpio,LOW);
Reset_Butt(num);
led();
}
}
/////////////控制代码结束///////////////
////////////////按键控制函数开始////////////////////////
void button1_callback(const String & state)
{
control(1, state);
}
/////////////////////////////////////////////////////
void button2_callback(const String & state)
{
control(2, state);
}
void button3_callback(const String & state)
{
control(3, state);
}
void button4_callback(const String & state)
{
control(4, state);
}
////////////////按键控制函数结束//////////////////////////////
//小爱接口函数开始///////
bool oState = { false };
void miotPowerState(const String & state, uint8_t num)
{
BLINKER_LOG("need set outlet: ", num, ", power state: ", state);
if (state == BLINKER_CMD_ON) {
BlinkerMIOT.powerState("on", num);
BlinkerMIOT.print();
BLINKER_LOG(String(num)+"灯已打开"); //串口打印
digitalWrite(gpio,HIGH);
Set_Butt(num);
oState = true;
led();
}
else if (state == BLINKER_CMD_OFF) {
BlinkerMIOT.powerState("off", num);
BlinkerMIOT.print();
BLINKER_LOG(String(num)+"灯已关闭"); //串口打印
digitalWrite(gpio, LOW);
Reset_Butt(num);
oState = false;
led();
if (num == 0)
{
for (uint8_t o_num = 0; o_num < 5; o_num++)
{
oState = false;
if(o_num > 0)
{
digitalWrite(gpio, LOW);
led();
BLINKER_LOG(String(o_num)+"灯已关闭");
Reset_Butt(o_num);
}
}
BLINKER_LOG("所有灯已关闭");
}
}
}
//小爱接口函数结束///////
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("MIOT Query All");
BlinkerMIOT.powerState(oState ? "on" : "off", num);
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
BLINKER_LOG("MIOT Query Power State");
BlinkerMIOT.powerState(oState ? "on" : "off", num);
BlinkerMIOT.print();
break;
default :
BlinkerMIOT.powerState(oState ? "on" : "off", num);
BlinkerMIOT.print();
break;
}
}
//*******app定时向设备发送心跳包, 设备收到心跳包后会返回设备当前状态30s~60s一次
void heartbeat()
{
BLINKER_LOG("心跳包状态同步!");
for (uint8_t o_num = 0; o_num < 5; o_num++)
{
if (digitalRead(gpio)==HIGH)
{
Set_Butt(o_num); //返回开关状态给APPON
}
else
{
Reset_Butt(o_num);//返回开关状态给APPOFF
}
}
}
/////////心跳包函数结束//////////////////////////////////////////////
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
uint32_t BlinkerTime = millis();
Blinker.print("millis", BlinkerTime);
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
//////////////注册控制端口开始////////////////
for (uint8_t o_num = 0; o_num < 5; o_num++)
{
pinMode(gpio, OUTPUT);
digitalWrite(gpio, LOW);
}
//////////////注册控制端口结束////////////////
Blinker.begin(auth, ssid, pswd); //连网参数
Blinker.attachData(dataRead);
Button1.attach(button1_callback);
Button2.attach(button2_callback);
Button3.attach(button3_callback);
Button4.attach(button4_callback);
BlinkerMIOT.attachPowerState(miotPowerState); //小爱语音操作注册函数
BlinkerMIOT.attachQuery(miotQuery); //小爱同学 数据反馈
Blinker.attachHeartbeat(heartbeat); //app定时向设备发送心跳包
led();
}
void loop()
{
Blinker.run();
}
开关的逻辑有问题,k4应该在打开k1,k2,k3,任一个时也被打开,要不然你关闭k4时所有的都被关闭有点说不通,不像总开关该作的,因为k1通电了总开关k4当然也要通电,如果总开关k4不通电,你K1又通开了,那这个总开关有何意义?所以流程图不对。用了点灯的APP为什么点灯不能控制开关,只来当个显示用;P qq26301365 发表于 2021-1-4 13:22
开关的逻辑有问题,k4应该在打开k1,k2,k3,任一个时也被打开,要不然你关闭k4时所有的都被关闭有点说不通, ...
我现在遇到的问题是天猫精灵和小爱同学都不能控制不知道怎么办
页:
[1]