硬件准备
1 .wemos d1 mini (ESP8266)
2.灯条应该是ws2813吧
3.小爱音箱
查看资料
- 点灯科技接入小爱流程 https://diandeng.tech/doc/xiaoai
- blinker库,下载后导入Arduino中 https://github.com/blinker-iot/blinker-library
- 灯条的库下载 https://github.com/adafruit/adafruit_NeoPixel
点灯科技app控制灯条程序
#define BLINKER_WIFI //wifi设备
#define BLINKER_MIOT_LIGHT //小爱 灯 设备,用于小爱音箱绑定识别设备类型,这个不加,同步不出来设备
#include <Blinker.h> //blinker库
#include "Adafruit_NeoPixel.h" //灯条库
//灯带IO口
#define PIN 5
//灯个数
#define NUM_LEDS 33
//亮度
#define BRIGHTNESS 255
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_KHZ800 + NEO_RGB);
char auth[] = "b*******0";//创建设备时的密钥
char ssid[] = "wifi_name"; //wifi名
char pswd[] = "pas"; //wifi密码
// 新建组件对象
BlinkerButton Button1("btn-gz2"); //app中按键的ID
BlinkerRGB RGB1("col-kxr"); //颜色选择器的ID
// 按下按键即会执行该函数
void button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
Blinker.vibrate();
if(flag == 0)
{
flag=1;
colorWipe(strip.Color(0, 0, 0, 255), 1); // 设置灯条关
}
else
{
flag = 0;
colorWipe(strip.Color(255, 255, 255), 1); //设置灯条白光
}
}
//颜色选择器,根据颜色选择器选择的颜色改变RGB灯条
void rgb1_callback(uint8_t r_value, uint8_t g_value, uint8_t b_value, uint8_t bright_value)
{
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
BLINKER_LOG("R value: ", r_value);
BLINKER_LOG("G value: ", g_value);
BLINKER_LOG("B value: ", b_value);
BLINKER_LOG("Rrightness value: ", bright_value);
uint8_t colorR = map(r_value, 0, 255, 0, bright_value);
uint8_t colorG = map(g_value, 0, 255, 0, bright_value);
uint8_t colorB = map(b_value, 0, 255, 0, bright_value);
colorWipe(strip.Color( colorG,colorR, colorB), 1); // 更改颜色
}
//初始化
void setup()
{
// 初始化串口
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.debugAll();
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
//点灯科技APP控件回调绑定
Button1.attach(button1_callback);
RGB1.attach(rgb1_callback);
strip.setBrightness(BRIGHTNESS); //设置灯条亮度
strip.begin(); //灯条初始化
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
Blinker.run();
}
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
for (uint16_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
点灯科技app添加设备
添加独立设备
网络接入
添加两个控件,按钮和颜色拾取
烧录程序就可以了.
小爱音箱控制灯条程序
在上面的程序中添加一部分用于和小爱通信的
#define BLINKER_WIFI //wifi设备
#define BLINKER_MIOT_LIGHT //小爱 灯 设备,用于小爱音箱绑定识别设备类型,这个不加,同步不出来设备
#include <Blinker.h> //blinker库
#include "Adafruit_NeoPixel.h" //灯条库
//灯带IO口
#define PIN 5
//灯个数
#define NUM_LEDS 33
//亮度
#define BRIGHTNESS 255
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_KHZ800 + NEO_RGB);
char auth[] = "b*******0";//创建设备时的密钥
char ssid[] = "wifi_name"; //wifi名
char pswd[] = "pas"; //wifi密码
// 新建组件对象
BlinkerButton Button1("btn-gz2"); //app中按键的ID
BlinkerRGB RGB1("col-kxr"); //颜色选择器的ID
// 按下按键即会执行该函数
void button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
Blinker.vibrate();
if(flag == 0)
{
flag=1;
colorWipe(strip.Color(0, 0, 0, 255), 1); // 设置灯条关
}
else
{
flag = 0;
colorWipe(strip.Color(255, 255, 255), 1); //设置灯条白光
}
}
//颜色选择器,根据颜色选择器选择的颜色改变RGB灯条
void rgb1_callback(uint8_t r_value, uint8_t g_value, uint8_t b_value, uint8_t bright_value)
{
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
BLINKER_LOG("R value: ", r_value);
BLINKER_LOG("G value: ", g_value);
BLINKER_LOG("B value: ", b_value);
BLINKER_LOG("Rrightness value: ", bright_value);
uint8_t colorR = map(r_value, 0, 255, 0, bright_value);
uint8_t colorG = map(g_value, 0, 255, 0, bright_value);
uint8_t colorB = map(b_value, 0, 255, 0, bright_value);
colorWipe(strip.Color( colorG,colorR, colorB), 1); // 更改颜色
}
//小爱电源控制回调函数,在小爱训练里面添加开关灯时,会进入到这里
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ",state);
if (state == BLINKER_CMD_OFF) { //如果语音接收到是关闭灯
colorWipe(strip.Color(0, 0, 0, 255), 1); //关闭灯
BlinkerMIOT.powerState("off"); //返回数据到小爱,否则小爱会报错
BlinkerMIOT.print();
}
else if (state == BLINKER_CMD_ON) {
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
colorWipe(strip.Color(255, 255, 255,255), 1); //白光
}
}
//小爱颜色控制回调函数,对小爱说把灯调成x色,就会到这里来执行
void miotColor(int32_t color)
{
uint8_t colorR,colorG,colorB;
BLINKER_LOG("need set color: ", color);
colorR = color >> 16 & 0xFF;
colorG = color >> 8 & 0xFF;
colorB = color & 0xFF;
BLINKER_LOG("colorR: ", colorR, ", colorG: ", colorG, ", colorB: ", colorB);
colorWipe(strip.Color(colorG,colorR, colorB,10), 1); //修改灯条颜色
strip.show();
BlinkerMIOT.color(color);
BlinkerMIOT.print();
}
//改变灯条亮度,对小爱说把灯的亮度调到百分之xx就是进到这里
void miotBright(const String & bright)
{
BLINKER_LOG("need set brightness: ", bright);
uint8_t colorW = bright.toInt();
BLINKER_LOG("now set brightness: ", colorW);
strip.setBrightness(colorW*15/10+105);//因为函数设置的亮度是0-255,小爱接口的是1-100
BlinkerMIOT.brightness(colorW);
strip.show();
BlinkerMIOT.print();
}
//小爱切换模式,对小爱说把灯调到XX模式,就会执行相应的操作
void miotMode(uint8_t mode)
{
BLINKER_LOG("need set mode: ", mode);
//日光模式
if (mode == BLINKER_CMD_MIOT_DAY) {
colorWipe(strip.Color(255, 255, 255
, 255), 1); // White
}
else if (mode == BLINKER_CMD_MIOT_NIGHT) {//夜光模式
colorWipe(strip.Color(237, 199, 204), 1); // White
}
else if (mode == BLINKER_CMD_MIOT_COLOR) {
// Your mode function
}
else if (mode == BLINKER_CMD_MIOT_WARMTH) {
// Your mode function
}
else if (mode == BLINKER_CMD_MIOT_TV) {
// Your mode function
}
else if (mode == BLINKER_CMD_MIOT_READING) {
// Your mode function
}
else if (mode == BLINKER_CMD_MIOT_COMPUTER) {
// Your mode function
}
BlinkerMIOT.mode(mode);
BlinkerMIOT.print();
}
//初始化
void setup()
{
// 初始化串口
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.debugAll();
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
//点灯科技APP控件回调绑定
Button1.attach(button1_callback);
RGB1.attach(rgb1_callback);
//小爱语音回调函数
BlinkerMIOT.attachPowerState(miotPowerState);//这段代码一定要加,不加小爱同学控制不了,务必在回调函数中反馈该
BlinkerMIOT.attachColor(miotColor); //颜色回调函数
BlinkerMIOT.attachBrightness(miotBright);//亮度
BlinkerMIOT.attachMode(miotMode); //模式
strip.setBrightness(BRIGHTNESS); //设置灯条亮度
strip.begin(); //灯条初始化
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
Blinker.run();
}
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
for (uint16_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
在米家APP中添加点灯科技的台灯设备
返回主页点开小爱音箱点进去训练就可以添加自己的语音指令
也可以使用小爱自带的指令
把灯亮度调到xxx
把灯的颜色调成xx色
结束