每次断电就要重新配网,请大神帮忙看看
#define BLINKER_WIFI#define BLINKER_PRINT Serial
#define BLINKER_ALIGENIE_LIGHT
#define BLINKER_MIOT_OUTLET
#include <Blinker.h>
#include <ESP8266WiFi.h>
char auth[] = "6ef9***";//app中获取到的Secret Key(密钥)
//char ssid[] = "***";//WIFI名称
//char pswd[] = "***";//WIFI密码
BlinkerButton Button1("k1");//绑定按键,APP上按键的数据键名
BlinkerText Text1("k1");//绑定文字组件,APP上按键的数据键名
#define int10
bool oState = false;
int count = 0; //时间计数
bool WIFI_Status = true; //WIFI状态标志位
/* 微信智能配网 */
void smartConfig()
{
WiFi.mode(WIFI_STA);//设置STA模式
Serial.println("\r\nWait for Smartconfig...");//打印log信息
WiFi.beginSmartConfig();//开始SmartConfig,等待手机端发出用户名和密码
while(1)
{
Serial.println(".");
digitalWrite(LED_BUILTIN,HIGH);//指示灯闪烁
delay(1000);
digitalWrite(LED_BUILTIN,LOW);//指示灯闪烁
delay(1000);
if(WiFi.smartConfigDone())//配网成功,接收到SSID和密码
{
Serial.println("SmartConfig Success");
Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
break;
}
}
}
/*连接网络*/
void blinkerConnect()
{
Serial.println("\r\n正在连接WIFI...");
while(WiFi.status()!=WL_CONNECTED)//判断是否连接WIFI成功
{
if(WIFI_Status)
{
Serial.print(".");
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
count++;
if(count>=5)//5s
{
WIFI_Status = false;
Serial.println("WiFi连接失败,请用手机进行配网");
}
}
else
{
smartConfig();//微信智能配网
}
}
Serial.println("连接成功");
Serial.print("IP:");
Serial.println(WiFi.localIP());
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.debugAll();
pinMode(int1, OUTPUT);
digitalWrite(int1, LOW);
//网络连接
blinkerConnect();
// 初始化blinker
Blinker.begin(auth,WiFi.SSID().c_str(),WiFi.psk().c_str()); //根据配网得到的WIFI信息和设备秘钥,连接到Blinker云
Button1.attach(button1_callback);
BlinkerAliGenie.attachPowerState(aligeniePowerState);
BlinkerAliGenie.attachQuery(aligenieQuery);
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachQuery(miotQuery);
}
void loop()
{
Blinker.run();
}
void button1_callback(const String & state) //向上
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_ON)
{
digitalWrite(int1,LOW);
Text1.print("已开");
Button1.color("#00FF00");
Button1.text("ON");
Button1.print("on");
oState = true;
}
else if (state == BLINKER_CMD_OFF)
{
digitalWrite(int1,HIGH);
Text1.print("已关");
Button1.color("#FF0000");
Button1.text("OFF");
Button1.print("off");
oState = false;
}
}
void aligeniePowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON)
{
digitalWrite(int1, LOW);
Text1.print("已开");
Button1.color("#00FF00");
Button1.text("ON");
Button1.print("on");
BlinkerAliGenie.powerState("on");
BlinkerAliGenie.print();
oState = true;
}
else if (state == BLINKER_CMD_OFF)
{
digitalWrite(int1, HIGH);
Text1.print("已关");
Button1.color("#FF0000");
Button1.text("OFF");
Button1.print("off");
BlinkerAliGenie.powerState("off");
BlinkerAliGenie.print();
oState = false;
}
}
void aligenieQuery(int32_t queryCode)
{
BLINKER_LOG("AliGenie Query codes: ", queryCode);
switch (queryCode)
{
case BLINKER_CMD_QUERY_ALL_NUMBER :
BLINKER_LOG("AliGenie Query All");
BlinkerAliGenie.powerState(oState ? "on" : "off");
BlinkerAliGenie.print();
break;
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
BLINKER_LOG("AliGenie Query Power State");
BlinkerAliGenie.powerState(oState ? "on" : "off");
BlinkerAliGenie.print();
break;
default :
BlinkerAliGenie.powerState(oState ? "on" : "off");
BlinkerAliGenie.print();
break;
}
}
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON)
{
digitalWrite(int1, LOW);
Text1.print("已开");
Button1.color("#00FF00");
Button1.text("ON");
Button1.print("on");
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
oState = true;
}
else if (state == BLINKER_CMD_OFF)
{
digitalWrite(int1, HIGH);
Text1.print("已关");
Button1.color("#FF0000");
Button1.text("OFF");
Button1.print("off");
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
oState = false;
}
}
void miotQuery(int32_t queryCode)
{
BLINKER_LOG("AliGenie Query codes: ", queryCode);
switch (queryCode)
{
case BLINKER_CMD_QUERY_ALL_NUMBER :
BLINKER_LOG("AliGenie Query All");
BlinkerMIOT.powerState(oState ? "on" : "off");
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
BLINKER_LOG("AliGenie Query Power State");
BlinkerMIOT.powerState(oState ? "on" : "off");
BlinkerMIOT.print();
break;
default :
BlinkerMIOT.powerState(oState ? "on" : "off");
BlinkerMIOT.print();
break;
}
}
请大神帮忙看一下,是哪里出错了吗
换了wifimanager解决了 package和lib版本不匹配导致的,按照点灯官方文档安装即可。 楼主,wifimanager你是用在哪里的,可以请教一下么
页:
[1]