基于blinker——esp8266-01与继电器组成插座接入小

2019-02-11  本文已影响0人  Walk_In_Jar

使用usb-ttl上传,上传要使用稳定的3.3v,不要usb上的。
对于小度,上传后,看到设备上线后,需要在小度音响app里,绑定binker的账号,绑定后,对小度说,发现设备。就有了我的“继电器”,在小度上可以改名,比如“灯”,对小度说,开灯/关灯即可。

#define BLINKER_WIFI
#define BLINKER_DUEROS_OUTLET

#include <Blinker.h>

char auth[] = "xxx";
char ssid[] = "xxxx";
char pswd[] = "xxxx";

bool oState = true;
const int OUT_PIN = 0;

#define BUTTON_1 "OUTButton"
BlinkerButton Button1(BUTTON_1);

void duerPowerState(const String & state)
{
  BLINKER_LOG("need set power state: ", state);

  if (state == BLINKER_CMD_ON) {
    digitalWrite(OUT_PIN, LOW);

    BlinkerDuerOS.powerState("on");
    BlinkerDuerOS.print();

    oState = true;
  }
  else if (state == BLINKER_CMD_OFF) {
    digitalWrite(OUT_PIN, HIGH);

    BlinkerDuerOS.powerState("off");
    BlinkerDuerOS.print();

    oState = false;
  }
}

void duerQuery(int32_t queryCode)
{
  BLINKER_LOG("DuerOS Query codes: ", queryCode);

  switch (queryCode)
  {
    case BLINKER_CMD_QUERY_TIME_NUMBER :
      BLINKER_LOG("DuerOS Query time");
      BlinkerDuerOS.time(millis());
      BlinkerDuerOS.print();
      break;
    default :
      BlinkerDuerOS.time(millis());
      BlinkerDuerOS.print();
      break;
  }
}

void dataRead(const String & data)
{
  BLINKER_LOG("Blinker readString: ", data);

  Blinker.vibrate();

  uint32_t BlinkerTime = millis();
  Blinker.print(BlinkerTime);
  Blinker.print("millis", BlinkerTime);
}

void setup()
{
  Serial.begin(115200);
  BLINKER_DEBUG.stream(Serial);

  pinMode(OUT_PIN, OUTPUT);
  digitalWrite(OUT_PIN, LOW);

  Blinker.begin(auth, ssid, pswd);
  Blinker.attachData(dataRead);
  Button1.attach(button1_callback);
  BlinkerDuerOS.attachPowerState(duerPowerState);
  BlinkerDuerOS.attachQuery(duerQuery);
}

void loop()
{
  Blinker.run();
}
void button1_callback(const String & state)
{
  if (state == BLINKER_CMD_BUTTON_TAP)
  {
    if (oState) {
      digitalWrite(OUT_PIN, HIGH);
      oState = false;
    }
    else {
      digitalWrite(OUT_PIN, LOW);
      oState = true;
    }
    Button1.print();
  }
}

对于小爱同学,上传代码后,
1.打开米家App。通过 我的>其他平台设备>点击添加>点灯科技>绑定账号 ,绑定blinker账号
2.绑定成功后,支持小爱控制的blinker设备会出现在 我的>其他平台设备>点灯科技 设备列表中
需要在binker app里对设备更名
4.blinker App中对设备进行修改后(如修改设备名),可在米家App或小爱音响App中,通过 我的>其他平台设备>点灯科技>同步设备 更新设备信息
然后对小爱同学说“开灯”,实测有些延迟,大多数情况能响应的

#define BLINKER_WIFI
#define BLINKER_MIOT_OUTLET

#include <Blinker.h>

char auth[] = "id";
char ssid[] = "wifi ssid";
char pswd[] = "wifi psw";

bool oState = false;
const int OUT_PIN = 0;//这个继电器低电平触发

#define BUTTON_1 "OUTButton"
BlinkerButton Button1(BUTTON_1);

void miotPowerState(const String & state)
{
    BLINKER_LOG("need set power state: ", state);

    if (state == BLINKER_CMD_ON) {
        digitalWrite(OUT_PIN, LOW);

        BlinkerMIOT.powerState("on");
        BlinkerMIOT.print();

        oState = true;
    }
    else if (state == BLINKER_CMD_OFF) {
        digitalWrite(OUT_PIN, HIGH);

        BlinkerMIOT.powerState("off");
        BlinkerMIOT.print();

        oState = false;
    }
}


void miotQuery(int32_t queryCode)
{
    BLINKER_LOG("MIOT Query codes: ", queryCode);

    switch (queryCode)
    {
        case BLINKER_CMD_QUERY_ALL_NUMBER :
            BLINKER_LOG("MIOT Query All");
            BlinkerMIOT.powerState(oState ? "on" : "off");
            BlinkerMIOT.print();
            break;
        case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
            BLINKER_LOG("MIOT Query Power State");
            BlinkerMIOT.powerState(oState ? "on" : "off");
            BlinkerMIOT.print();
            break;
        default :
            BlinkerMIOT.powerState(oState ? "on" : "off");
            BlinkerMIOT.print();
            break;
    }
}
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);

    pinMode(OUT_PIN, OUTPUT);
    digitalWrite(OUT_PIN, LOW);

    Blinker.begin(auth, ssid, pswd);
    Blinker.attachData(dataRead);
    Button1.attach(button1_callback);
    BlinkerMIOT.attachPowerState(miotPowerState);
    BlinkerMIOT.attachQuery(miotQuery);
}

void loop()
{
    Blinker.run();
}
void button1_callback(const String & state)
{
  if (state == BLINKER_CMD_BUTTON_TAP)
  {
    if (oState) {
      digitalWrite(OUT_PIN, LOW);
      oState = false;
    }
    else {
      digitalWrite(OUT_PIN, HIGH);
      oState = true;
    }
    Button1.print();
  }
}

上一篇下一篇

猜你喜欢

热点阅读