1、带有小爱同学的设备(小爱音箱、小米手机等等)
2、ESP8266模块
搭建环境参考
1、博客 图文手把手教程--ESP8266开发环境搭建(开发平台VSCODE+PlatformIO,框架Arduino、RTOS_SDK)
2、视频VScode(PlatfromIO)嵌入式开发(ESP32)(ESP8266)(STM32)
1、下载手机端blinker程序
2、在blinker中添加设备并且获得密钥
1、在PlatformIO工程中添加blinker库
1.下载blinker库(如果这个链接无法下载则使用备用下载链接)
2.将下载好的库添加到PlatformIO工程中
2)将库复制到工程中的库下
2、程序编写
#define BLINKER_WIFI
#define BLINKER_MIOT_LIGHT //设置小爱灯类库
#define BLINKER_ALIGENIE_LIGHT //设置天猫灯类库
//#define BLINKER_APCONFIG //支持APconfig配网
#include
//#include
//#include //定时器库的 头文件
//#include //定时器库的 头文件
//Ticker flipper; //实例化定时器对象
#define LED_BUILTIN 2 //LED
const int buttonPin = 0; //按键
char auth[] = "34dd0f500434"; //将密钥复制进来
char ssid[] = "TP-LINK_0099"; //接入WIFI名称
char pswd[] = "888888"; //WIFI密码
// 新建组件对象
BlinkerButton Button1("btn-abc");//按键
BlinkerSlider SliderW("ran-ezp"); //亮度滑条
BlinkerNumber Number1("num-abc");
int counter = 0;
int32_t timerdelay = 0;
bool oState = false;
bool brightnessState =0;
int32_t brightness1 = 800; // how bright the LED is
int32_t colorW=100;
int fadeAmount =10; // 亮度变化速度
// app上按下按键即会执行该函数
void button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state); //串口打印
if (state=="on")
{ //如果state是on状态
digitalWrite(LED_BUILTIN, HIGH); //LED开
Button1.color("red"); //按键图标颜色为红色
Button1.print("on"); //状态反馈至按键,键值保存为“on”
oState = 1;
}
else if(state=="off")
{
digitalWrite(LED_BUILTIN, LOW); //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, HIGH); //LED开
Button1.color("red"); //按键图标颜色为红色
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
Button1.print("on"); //状态反馈至按键,键值保存为“on”
oState = 1;
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(LED_BUILTIN, LOW); //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, HIGH); //LED开
Button1.color("red"); //按键图标颜色为红色
BlinkerAliGenie.powerState("on");
BlinkerAliGenie.print();
Button1.print("on"); //状态反馈至按键,键值保存为“on”
oState = 1;
}
else if (state == BLINKER_CMD_OFF)
{
digitalWrite(LED_BUILTIN, LOW); //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 sliderW_callback(int32_t value) //滑条回调函数,滑条值范围0~1000
{
if(value<100) //如果滑条值小于20(0~20关灯,方便操作),执行状态为“off”的按键开关回调函数
{
button1_callback("off"); //关灯,转换图标,图标变灰,反馈为“off”
}
else //滑条值≥20,
{
button1_callback("on"); //执行状态为“on”的按键开关回调函数
//PWM输出,LED低电平有效,PWM占空比:(1000-value)/1024;
//analogWriteRange()--修改范围,默认值为1023;
//ananlogWriteFreq()--修改频率,默认值为1K;
analogWrite(LED_BUILTIN, value);
brightness1=value;
SliderW.print(brightness1);
BLINKER_LOG("get slider value: ", (1000-value)); //在APP端调试窗口输出led亮度值
}
}
void miotBright(const String & bright) //小爱亮度的控制接口
{
BLINKER_LOG("need set brightness: ", bright);
colorW = bright.toInt();
brightness1=colorW *10;
if(brightness1<100) //如果滑条值小于20(0~20关灯,方便操作),执行状态为“off”的按键开关回调函数
{
button1_callback("off"); //关灯,转换图标,图标变灰,反馈为“off”
}
else //滑条值≥20,
{
button1_callback("on"); //执行状态为“on”的按键开关回调函数
//PWM输出,LED低电平有效,PWM占空比:(1000-value)/1024;
//analogWriteRange()--修改范围,默认值为1023;
//ananlogWriteFreq()--修改频率,默认值为1K;
analogWrite(LED_BUILTIN, brightness1);
SliderW.print(brightness1);
// BLINKER_LOG("get slider value: ", (1000-value)); //在APP端调试窗口输出led亮度值
}
BLINKER_LOG("now set brightness: ", colorW);
BlinkerMIOT.brightness(colorW);
BlinkerMIOT.print();
}
//如果本地开关有动作执行下面手动模式
void KeyDetection(void)
{
if(digitalRead(buttonPin)==LOW) //如果检测到按键按下
{
Blinker.delay(3); //延时10ms
timerdelay++;
if(timerdelay>60) //长按1.5s
{
if(oState==1)
{
if(brightnessState ==1)
{
brightness1=brightness1+fadeAmount;
if(brightness1>1000){brightness1=1000;brightnessState =0; }
// brightness1=900;
}
if(brightnessState ==0)
{
brightness1=brightness1-fadeAmount;
if(brightness1<100){brightness1=100;brightnessState =1; }
// brightness1=300;
}
analogWrite(LED_BUILTIN, brightness1);
}
}
}
else //按键抬起
{
if(timerdelay>60) //长按1.5s
{ if(oState==1)
{
// brightnessState = !brightnessState;
SliderW.print(brightness1);
// BLINKER_LOG("ran-ezp:", brightness1); //在APP端调试窗口输出led亮度值
}
}
else if(timerdelay>0&&timerdelay<60) //如果按下少于1s大于10ms
{
oState = !oState;
if(oState==1)
{
button1_callback("on");
}
else
{
button1_callback("off");
}
}
timerdelay=0;
}
}
// 如果未绑定的组件被触发,则会执行其中内容
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
Blinker.begin(auth, ssid, pswd);
// Blinker.begin(auth); //AP配网函数
Button1.attach(button1_callback); //注册回调函数
SliderW.attach(sliderW_callback);
// flipper.attach(0.1, flip);//每隔0.1秒执行一次回调函数
BlinkerMIOT.attachPowerState(miotPowerState); //小爱语音操作注册函数
BlinkerMIOT.attachBrightness(miotBright);
BlinkerMIOT.attachQuery(miotQuery); //小爱语音操作回调函数
BlinkerAliGenie.attachPowerState(aligeniePowerState);//天猫语音操作注册函数
BlinkerAliGenie.attachQuery(aligenieQuery); //天猫语音操作回调函数
}
void loop() {
Blinker.run();
KeyDetection();
}
1、下载米家APP
2、添加ESP8266设备
添加了设备之后我们就可以使用绑定到米家中的小爱同学控制ESP8266的灯的熄灭了
点灯科技-点灯物联网解决方案
通过Blinker使小爱同学控制ATK-ESP8266模块_wu568760312的博客-CSDN博客_blinker小爱