|
代码如下:
#define BLINKER_WIFI
#define BLINKER_MIOT_OUTLET //设置小爱灯类库
#define BLINKER_ALIGENIE_OUTLET //设置天猫灯类库
#include <WiFiManager.h>
#include <Blinker.h>
#define LED_BUILTIN 2 //LED
const int buttonPin = 0; //按键
char auth[] = "*******";//密匙
// 新建组件对象
BlinkerButton Button1("btn-abc");//按键
BlinkerNumber Number1("num-abc");
int counter = 0;
int32_t timerdelay = 0;
bool oState = false;
//使用web配置网络
WiFiManager wifiManager;
char devname[] = "ESP8266-01S-1"; //定义WIFI名称和主机名
void WebConfig() {
Serial.println("Entered config mode...");
// 自动连接WiFi,参数是连接ESP8266时的WiFi名称
wifiManager.autoConnect(devname);
WiFi.hostname(devname); //设置主机名
// WiFi连接成功后将通过串口监视器输出连接成功信息
Serial.println("");
Serial.println("======== WifiManager Network Info ========");
Serial.print("### ESP8266 Connected to: ");
Serial.println("### SSID: " + (String)WiFi.SSID()); // WiFi名称
Serial.println("### PSWD: " + (String)WiFi.psk()); // WiFi名称
Serial.print("### IP address: ");
Serial.println(WiFi.localIP()); // IP
Serial.println("### HostName: " + WiFi.hostname()); // 主机名
Serial.println("==========================================");
Serial.println("");
}
//本地开关手动模式
void KeyDetection(void)
{
if(digitalRead(buttonPin)==HIGH) //如果检测到按键按下
{
Blinker.delay(3); //延时10ms
timerdelay++;
}
else //按键抬起
{
if(timerdelay>0&&timerdelay<60) //如果按下少于1s大于10ms
{
oState = !oState;
if(oState==1)
{
button1_callback("on");
}
else
{
button1_callback("off");
}
}
timerdelay=0;
}
if(timerdelay>600) //如果按下大于6s
{
Serial.println("Reset Config...");
wifiManager.resetSettings(); //擦除WIFI信息
//wifiManager.reboot(); //重启设备
Blinker.reset(); //重置设备
}
timerdelay=0;
}
// app上按下按键即会执行该函数
void button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state); //串口打印
if (state=="on")
{ //如果state是on状态
digitalWrite(LED_BUILTIN, LOW); //LED开
Button1.color("red"); //按键图标颜色为红色
Button1.print("on"); //状态反馈至按键,键值保存为“on”
oState =1;
}
else if(state=="off")
{
digitalWrite(LED_BUILTIN, HIGH); //LED关
Button1.color("gray"); //按键图标颜色变灰
Button1.print("off"); //状态反馈至按键,键值保存为“off”
oState =0;
}
Blinker.vibrate(); //振动,默认值为200ms
}
//*******如果小爱有对设备进行操作就执行下面
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
//如果state是on状态
digitalWrite(LED_BUILTIN, LOW); //LED开
Button1.color("red"); //按键图标颜色为红色
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
Button1.print("on"); //状态反馈至按键,键值保存为“on”
oState =1;
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(LED_BUILTIN, HIGH); //LED关
Button1.color("gray"); //按键图标颜色变灰
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
Button1.print("off"); //状态反馈至按键,键值保存为“off”
oState =0;
}
}
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 aligeniePowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(LED_BUILTIN, LOW); //LED开
Button1.color("red"); //按键图标颜色为红色
BlinkerAliGenie.powerState("on");
BlinkerAliGenie.print();
Button1.print("on"); //状态反馈至按键,键值保存为“on”
oState =1;
}
else if (state == BLINKER_CMD_OFF)
{
digitalWrite(LED_BUILTIN, HIGH); //LED关
Button1.color("gray"); //按键图标颜色变灰
BlinkerAliGenie.powerState("off");
BlinkerAliGenie.print();
Button1.print("off"); //状态反馈至按键,键值保存为“off”
oState =0;
}
}
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 dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
counter++;
Number1.print(counter);
}
void setup()
{
// 初始化串口
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.debugAll();
// 初始化有LED的IO
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
// 初始化blinker
WebConfig(); //配置网络
Blinker.begin(auth, WiFi.SSID().c_str(), WiFi.psk().c_str()); //初始化blinker
Blinker.attachData(dataRead);
Button1.attach(button1_callback); //注册回调函数
BlinkerMIOT.attachPowerState(miotPowerState); //小爱语音操作注册函数
BlinkerMIOT.attachQuery(miotQuery); //小爱语音操作回调函数
BlinkerAliGenie.attachPowerState(aligeniePowerState);//天猫语音操作注册函数
BlinkerAliGenie.attachQuery(aligenieQuery); //天猫语音操作回调函数
}
void loop() {
Blinker.run();
KeyDetection();
}
现在网络开关正常,本地开关无效,请大神看下开关代码是否存在问题,如有问题该怎么改,谢谢您不辞辛苦的回复
|
|