C:\Program Files (x86)\Arduino\libraries\blinker-library-master\examples\Blinker_MIOT\MIOT_OUTLET
char auth[] = "*********";//设备秘钥
char ssid[] = "123456";//wifi名称
char pswd[] = "17388753767";//wifi密码
#define pin2 D2 //实际测试D0-D8均可使用
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
pinMode(LED_BUILTIN, OUTPUT); //ESP12F指示灯端口,也是D0端口
digitalWrite(LED_BUILTIN, LOW);
pinMode(pin2, OUTPUT); //开关端口
digitalWrite(pin2, LOW);
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachQuery(miotQuery);
}
开关动作需要添加在小爱同学回调函数中。
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(pin2, HIGH);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
oState = true;
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(pin2, LOW);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
oState = false;
}
}
/* *****************************************************************
*
* Download latest Blinker library here:
* https://github.com/blinker-iot/blinker-library/archive/master.zip
*
*
* Blinker is a cross-hardware, cross-platform solution for the IoT.
* It provides APP, device and server support,
* and uses public cloud services for data transmission and storage.
* It can be used in smart home, data monitoring and other fields
* to help users build Internet of Things projects better and faster.
*
* Make sure installed 2.5.0 or later ESP8266/Arduino package,
* if use ESP8266 with Blinker.
* https://github.com/esp8266/Arduino/releases
*
* Make sure installed 1.0.2 or later ESP32/Arduino package,
* if use ESP32 with Blinker.
* https://github.com/espressif/arduino-esp32/releases
*
* Docs: https://doc.blinker.app/
* https://github.com/blinker-iot/blinker-doc/wiki
*
* *****************************************************************
*
* Blinker 库下载地址:
* https://github.com/blinker-iot/blinker-library/archive/master.zip
*
* Blinker 是一套跨硬件、跨平台的物联网解决方案,提供APP端、设备端、
* 服务器端支持,使用公有云服务进行数据传输存储。可用于智能家居、
* 数据监测等领域,可以帮助用户更好更快地搭建物联网项目。
*
* 如果使用 ESP8266 接入 Blinker,
* 请确保安装了 2.5.0 或更新的 ESP8266/Arduino 支持包。
* https://github.com/esp8266/Arduino/releases
*
* 如果使用 ESP32 接入 Blinker,
* 请确保安装了 1.0.2 或更新的 ESP32/Arduino 支持包。
* https://github.com/espressif/arduino-esp32/releases
*
* 文档: https://doc.blinker.app/
* https://github.com/blinker-iot/blinker-doc/wiki
*
* *****************************************************************/
#define BLINKER_WIFI
#define BLINKER_MIOT_OUTLET
#include
char auth[] = "xxxxxxx";
char ssid[] = "123456";
char pswd[] = "17388753767";
#define pin2 D2
bool oState = false;
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(pin2, HIGH);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
oState = true;
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(pin2, LOW);
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(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
pinMode(pin2, OUTPUT);
digitalWrite(pin2, LOW);
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachQuery(miotQuery);
}
void loop()
{
Blinker.run();
}
编辑完成后,上传代码到ESP8266中即可。
打开Blinker软件,点击右上角+号,选择设备为Arduino,选择WIFI接入,复制保存key即设备秘钥。将秘钥填入代码中。
在所有设备中选择新加入的设备,点击右上角的…按钮,编辑设备名称即可。暂时未加入Blinker控制设备按钮。
打开米家APP,选择我的,选择其他平台设备,添加点灯科技,点击进入点灯科技,同步设备即可看到你新建的设备。
完成上述步骤后,即可使用小爱同学进行控制你的设备。