本帖最后由 琢磨俠 于 2020-2-17 10:45 编辑
刷完程序后用安信可WiFi配网进行一键配网。
[mw_shl_code=arduino,true]#define BLINKER_WIFI
#define BLINKER_MIOT_LIGHT
#include <ESP8266WiFi.h>//配网库文件
#include <Blinker.h>
#define BUTTON_1 "k1"
char auth[] = "你的key";
char ssid[] = "";//自动配网留空
char pswd[] = "";//自动配网留空
BlinkerButton Button1(BUTTON_1);
void button1_callback(const String & state)
{
digitalWrite(2, !digitalRead(2));
BLINKER_LOG("get button state: ", state);
//Button1.icon("icon_16");
Button1.color("#F7C709");
Button1.text("台灯开关");
Button1.print("on");
}
void miotPowerState(const String & state)
{
BLINKER_LOG("电源状态: ", state);
if (state == BLINKER_CMD_ON) {
//digitalWrite(LED_BUILTIN, LOW);
digitalWrite(2, LOW);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
}
else if (state == BLINKER_CMD_OFF) {
//digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(2, HIGH);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
}
}
void setup()
{
// 初始化串口
Serial.begin(115200);
WiFi.mode(WIFI_STA);//自动配网开始
int cnt = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if (cnt++ >= 10) {
WiFi.beginSmartConfig();
while (1) {
delay(1000);
if (WiFi.smartConfigDone()) {
Serial.println();
Serial.println("SmartConfig: Success");
break;
}
}
}
}//配网结束
BLINKER_DEBUG.stream(Serial);
// 初始化有LED的IO
// pinMode(LED_BUILTIN, OUTPUT);
// digitalWrite(LED_BUILTIN, HIGH);
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
BlinkerMIOT.attachPowerState(miotPowerState);
// Button1.attach(button1_callback);
Button1.attach(button1_callback);
}
void loop() {
Blinker.run();
}[/mw_shl_code]
|