|
100金币
我想用天猫和小爱控制WS2812灯带,大部分功能都实现。就是天猫(影院模式)和小爱(彩光模式)调用liushuideng();或者huancaideng();灯的效果是实现,就是再发其他指令都没反应,串口打印就停在调用liushuideng();或者huancaideng();上出不来。
//WS2812RGB+ESP8266-01s或NodeMCU
//点灯APP和小爱控制
//完善亮度控制
#define BLINKER_PRINT Serial
#define BLINKER_MIOT_LIGHT
#define BLINKER_ALIGENIE_LIGHT
#define BLINKER_WIFI
#include <Blinker.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
char auth[] = "3659a4788dc3";/****秘钥****/
#define PIN D7 // 设置引脚
#define NUMPIXELS 12 // 设置LED灯珠数
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
byte neopix_gamma[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114,
115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142,
144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175,
177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213,
215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 };
// 新建组件对象
BlinkerRGB RGB1("RGB");
BlinkerButton Button1("key");
BlinkerNumber Number1("num-abc");
int counter = 0;
int LED_R=0,LED_G=0,LED_B=0,LED_Bright=180;// RGB和亮度
bool LED_Flag = false;
bool WIFI_Status = true;
void smartConfig()//配网函数
{
WiFi.mode(WIFI_STA);
Serial.println("\r\nWait for Smartconfig...");
WiFi.beginSmartConfig();//等待手机端发出的用户名与密码
while (1)
{
Serial.print(".");
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
if (WiFi.smartConfigDone())//退出等待
{
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 WIFI_Set()//
{
//Serial.println("\r\n正在连接");
int count = 0;
while(WiFi.status()!=WL_CONNECTED)
{
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 button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
Blinker.print(state);
//digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
LED_Flag = !LED_Flag;
if(LED_Flag) SET_RGB(255,255,255,LED_Bright);
else SET_RGB(0,0,0,0);
}
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
counter=counter;
Number1.print(counter);
}
void SET_RGB(int R,int G,int B,int bright)
{
for (uint16_t i = 0; i < NUMPIXELS; i++) //把灯条变色
{
pixels.setPixelColor(i,R,G,B);
}
pixels.setBrightness(bright);//亮度
pixels.show(); //送出显示
}
//APP 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);
LED_Bright = bright_value;
SET_RGB(r_value,g_value,b_value,LED_Bright);
}
/********************************************************************************/
//天猫电源回调
void aligeniePowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
//digitalWrite(LED_BUILTIN, HIGH);
SET_RGB(255,255,255,255);
BlinkerAliGenie.powerState("on");
BlinkerAliGenie.print();
}
else if (state == BLINKER_CMD_OFF) {
//digitalWrite(LED_BUILTIN, LOW);
SET_RGB(255,255,255,0);
BlinkerAliGenie.powerState("off");
BlinkerAliGenie.print();
}
}
//小爱电源类回调
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
//digitalWrite(LED_BUILTIN, HIGH);
SET_RGB(255,255,255,255);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
}
else if (state == BLINKER_CMD_OFF) {
//digitalWrite(LED_BUILTIN, LOW);
SET_RGB(255,255,255,0);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
}
}
//天猫设置颜色回调
void aligenieColor(const String & color)
{
BLINKER_LOG("need set color: ", color);
int colorR,colorG,colorB;
if (color == "Red") {
SET_RGB(255,0,0,200);
}
else if (color == "Yellow") {
SET_RGB(255,255,0,200);
}
else if (color == "Blue") {
SET_RGB(0,0,255,200);
}
else if (color == "Green") {
SET_RGB(0,255,0,200);
}
else if (color == "White") {
SET_RGB(255,255,255,200);
}
else if (color == "Black") {
SET_RGB(0,0,0,255);
}
else if (color == "Cyan") {
SET_RGB(0,255,255,200);
}
else if (color == "Purple") {
SET_RGB(128,0,128,200);
}
else if (color == "Orange") {
SET_RGB(255,165,0,200);
}
BlinkerAliGenie.color(color);
BlinkerAliGenie.print();
}
//小爱设置颜色回调
void miotColor(int32_t color)
{
BLINKER_LOG("need set color: ", color);
int colorR,colorG,colorB;
colorR = color >> 16 & 0xFF;
colorG = color >> 8 & 0xFF;
colorB = color & 0xFF;
BLINKER_LOG("colorR: ", colorR, ", colorG: ", colorG, ", colorB: ", colorB);
SET_RGB(colorR,colorG,colorB,LED_Bright);
//pixelShow();
BlinkerMIOT.color(color);//反馈小爱控制状态
BlinkerMIOT.print();
}
//天猫亮度回调
void aligenieBright(const String & bright)
{ int colorW;
BLINKER_LOG("need set brightness: ", bright);
if (bright == BLINKER_CMD_MAX) {
colorW = 255;
}
else if (bright == BLINKER_CMD_MIN) {
colorW = 0;
}
else {
colorW = bright.toInt();
}
BLINKER_LOG("now set brightness: ", colorW);
BlinkerAliGenie.brightness(colorW);
BlinkerAliGenie.print();
}
//天猫亮度步长回调
void aligenieRelativeBright(int32_t bright)
{
BLINKER_LOG("need set relative brightness: ", bright);
int colorW = bright;
BlinkerAliGenie.brightness(bright);
BlinkerAliGenie.print();
}
void aligenieRelativeColoTemp(int32_t colorTemp)
{
BLINKER_LOG("need set relative colorTemperature: ", colorTemp);
//int colorT +=
colorTemp;
BlinkerAliGenie.colorTemp(colorTemp);
BlinkerAliGenie.print();
}
//小爱调亮度回调
void miotBright(const String & bright)
{
BLINKER_LOG("need set brightness: ", bright);
int colorW = bright.toInt();
BLINKER_LOG("now set brightness: ", colorW);
LED_Bright = colorW;
if(LED_Bright>=80&&LED_Bright<=100){
SET_RGB(255,255,255,255);
}else if(LED_Bright>=40&&LED_Bright<=79){
SET_RGB(125,125,125,125);
}else if(LED_Bright>=1&&LED_Bright<=39){
SET_RGB(50,50,50,50);
}
Serial.printf("亮度调节中...%d",colorW);
BlinkerMIOT.brightness(colorW);//反馈小爱控制状态
BlinkerMIOT.print();
}
//天猫模式的设置接口
void aligenieMode(const String & mode)
{
BLINKER_LOG("need set mode: ", mode);
if (mode == BLINKER_CMD_READING) {
SET_RGB(255,245,247,185);
}
else if (mode == BLINKER_CMD_MOVIE) {
whlie(mode == BLINKER_CMD_MOVIE){ 问题在这里一旦调用,天猫发其他指令都没反应 串口打印就停在 need set mode: movie
huancaideng();
}
}
else if (mode == BLINKER_CMD_SLEEP) {
SET_RGB(35,10,10,20);
}
else if (mode == BLINKER_CMD_HOLIDAY) {
SET_RGB(111,110,102,109);
}
else if (mode == BLINKER_CMD_MUSIC) {
//可以添加
}
else if (mode == BLINKER_CMD_COMMON) {
//没用
}
// wsMode = mode;
BlinkerAliGenie.mode(mode);
BlinkerAliGenie.print();
}
//小爱模式的设置接口
void miotMode(uint8_t mode)
{
BLINKER_LOG("need set mode: ", mode);
if (mode == BLINKER_CMD_MIOT_DAY) {
SET_RGB(111,110,102,109);
}
else if (mode == BLINKER_CMD_MIOT_NIGHT) {
SET_RGB(51,197,255,47);
}
else if (mode == BLINKER_CMD_MIOT_COLOR) {
while (mode>0){ 问题在这里一旦调用,小爱发指令都反应串口就停在 need set mode:COLOR
liushuideng();
}
}
else if (mode == BLINKER_CMD_MIOT_WARMTH) {
SET_RGB(249,205,173,109.8285);
}
else if (mode == BLINKER_CMD_MIOT_TV) {
SET_RGB(35,235,185,50);
}
else if (mode == BLINKER_CMD_MIOT_READING) {
SET_RGB(255,245,247,185);
}
else if (mode == BLINKER_CMD_MIOT_COMPUTER) {
SET_RGB(255,96,255,100);
}
int wsMode = mode;
BlinkerMIOT.mode(mode);
BlinkerMIOT.print();
}
//流水灯回调函数
void liushuideng(){
for (int i = 1; i <= 12; i = i + (1)) {
pixels.setBrightness(50);
pixels.setPixelColor((i)-1, ((50& 0xffffff) << 16) | ((197 & 0xffffff) << 8) | 255);
delay(200);
pixels.show();
if(i==12){
for(int l=12;l>=1;l = l - (1)){
pixels.setPixelColor((l)-1, ((0 & 0xffffff) << 16) | ((0 & 0xffffff) << 8) | 0);
delay(200);
pixels.show();
}
}
}
}
//天猫色温的控制接口
void aligenieColoTemp(int32_t colorTemp)
{
BLINKER_LOG("need set colorTemperature: ", colorTemp);
int colorT = colorTemp;
SET_RGB(colorT*40,colorT*58,colorT*66,colorT*88);
BlinkerAliGenie.colorTemp(colorTemp);
BlinkerAliGenie.print();
}
//小爱色温的控制接口
void miotColoTemp(int32_t colorTemp)
{
BLINKER_LOG("need set colorTemperature: ", colorTemp);
int colorT = colorTemp;
SET_RGB(colorT*40,colorT*58,colorT*66,colorT*88);
BlinkerMIOT.colorTemp(colorTemp);
BlinkerMIOT.print();
}
//小爱设备查询接口
void miotQuery(int32_t queryCode)
{
BLINKER_LOG("MIOT Query codes: ", queryCode);
switch (queryCode)
{
case BLINKER_CMD_QUERY_PM25_NUMBER :
BLINKER_LOG("MIOT Query PM25");
BlinkerMIOT.pm25(20);
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_HUMI_NUMBER :
BLINKER_LOG("MIOT Query HUMI");
BlinkerMIOT.humi(20);
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_TEMP_NUMBER :
BLINKER_LOG("MIOT Query TEMP");
BlinkerMIOT.temp(20);
BlinkerMIOT.print();
break;
default :
BlinkerMIOT.temp(20);
BlinkerMIOT.humi(20);
BlinkerMIOT.pm25(20);
BlinkerMIOT.co2(20);
BlinkerMIOT.print();
break;
}
}
//天猫设备查询接口
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(wsState ? "on" : "off");
BlinkerAliGenie.color(getColor());
BlinkerAliGenie.colorTemp(colorT);
BlinkerAliGenie.brightness(colorW);
BlinkerAliGenie.print();
break;
case BLINKER_CMD_POWERSTATE_NUMBER :
BLINKER_LOG("AliGenie Query Power State");
BlinkerAliGenie.powerState(wsState ? "on" : "off");
BlinkerAliGenie.print();
break;
case BLINKER_CMD_COLOR_NUMBER :
BLINKER_LOG("AliGenie Query Color");
BlinkerAliGenie.color(getColor());
BlinkerAliGenie.print();
break;
case BLINKER_CMD_QUERY_MODE_NUMBER :
BLINKER_LOG("AliGenie Query Mode");
BlinkerAliGenie.mode(wsMode);
BlinkerAliGenie.print();
break;*/
/*case BLINKER_CMD_COLORTEMP_NUMBER :
BLINKER_LOG("AliGenie Query ColorTemperature");
BlinkerAliGenie.colorTemp(colorT);
BlinkerAliGenie.print();
break;*/
/*case BLINKER_CMD_BRIGHTNESS_NUMBER :
BLINKER_LOG("AliGenie Query Brightness");
BlinkerAliGenie.brightness(colorW);
BlinkerAliGenie.print();
break;*/
default :
break;
}
}
/***********/
void huancaideng() {
// Some example procedures showing how to display to the pixels:
colorWipe(pixels.Color(255, 0, 0), 50); // Red
colorWipe(pixels.Color(0, 255, 0), 50); // Green
colorWipe(pixels.Color(0, 0, 255), 50); // Blue
colorWipe(pixels.Color(0, 0, 0, 255), 50); // White
whiteOverRainbow(20,75,5);
pulseWhite(5);
// fullWhite();
// delay(2000);
rainbowFade2White(3,3,1);
}
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<pixels.numPixels(); i++) {
pixels.setPixelColor(i, c);
pixels.show();
delay(wait);
}
}
void pulseWhite(uint8_t wait) {
for(int j = 0; j < 256 ; j++){
for(uint16_t i=0; i<pixels.numPixels(); i++) {
pixels.setPixelColor(i, pixels.Color(0,0,0, neopix_gamma[j] ) );
}
delay(wait);
pixels.show();
}
for(int j = 255; j >= 0 ; j--){
for(uint16_t i=0; i<pixels.numPixels(); i++) {
pixels.setPixelColor(i, pixels.Color(0,0,0, neopix_gamma[j] ) );
}
delay(wait);
pixels.show();
}
}
void rainbowFade2White(uint8_t wait, int rainbowLoops, int whiteLoops) {
float fadeMax = 100.0;
int fadeVal = 0;
uint32_t wheelVal;
int redVal, greenVal, blueVal;
for(int k = 0 ; k < rainbowLoops ; k ++){
for(int j=0; j<256; j++) { // 5 cycles of all colors on wheel
for(int i=0; i< pixels.numPixels(); i++) {
wheelVal = Wheel(((i * 256 / pixels.numPixels()) + j) & 255);
redVal = red(wheelVal) * float(fadeVal/fadeMax);
greenVal = green(wheelVal) * float(fadeVal/fadeMax);
blueVal = blue(wheelVal) * float(fadeVal/fadeMax);
pixels.setPixelColor( i, pixels.Color( redVal, greenVal, blueVal ) );
}
//First loop, fade in!
if(k == 0 && fadeVal < fadeMax-1) {
fadeVal++;
}
//Last loop, fade out!
else if(k == rainbowLoops - 1 && j > 255 - fadeMax ){
fadeVal--;
}
pixels.show();
delay(wait);
}
}
delay(500);
for(int k = 0 ; k < whiteLoops ; k ++){
for(int j = 0; j < 256 ; j++){
for(uint16_t i=0; i < pixels.numPixels(); i++) {
pixels.setPixelColor(i, pixels.Color(0,0,0, neopix_gamma[j] ) );
}
pixels.show();
}
delay(1000);
for(int j = 255; j >= 0 ; j--){
for(uint16_t i=0; i < pixels.numPixels(); i++) {
pixels.setPixelColor(i, pixels.Color(0,0,0, neopix_gamma[j] ) );
}
pixels.show();
}
}
delay(250);
}
void whiteOverRainbow(uint8_t wait, uint8_t whiteSpeed, uint8_t whiteLength ) {
if(whiteLength >= pixels.numPixels()) whiteLength = pixels.numPixels() - 1;
int head = whiteLength - 1;
int tail = 0;
int loops = 3;
int loopNum = 0;
static unsigned long lastTime = 0;
while(true){
for(int j=0; j<256; j++) {
for(uint16_t i=0; i<pixels.numPixels(); i++) {
if((i >= tail && i <= head) || (tail > head && i >= tail) || (tail > head && i <= head) ){
pixels.setPixelColor(i, pixels.Color(0,0,0, 255 ) );
}
else{
pixels.setPixelColor(i, Wheel(((i * 256 / pixels.numPixels()) + j) & 255));
}
}
if(millis() - lastTime > whiteSpeed) {
head++;
tail++;
if(head == pixels.numPixels()){
loopNum++;
}
lastTime = millis();
}
if(loopNum == loops) return;
head%=pixels.numPixels();
tail%=pixels.numPixels();
pixels.show();
delay(wait);
}
}
}
void fullWhite() {
for(uint16_t i=0; i<pixels.numPixels(); i++) {
pixels.setPixelColor(i, pixels.Color(0,0,0, 255 ) );
}
pixels.show();
}
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256 * 5; j++) { // 5 cycles of all colors on wheel
for(i=0; i< pixels.numPixels(); i++) {
pixels.setPixelColor(i, Wheel(((i * 256 / pixels.numPixels()) + j) & 255));
}
pixels.show();
delay(wait);
}
}
void rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<pixels.numPixels(); i++) {
pixels.setPixelColor(i, Wheel((i+j) & 255));
}
pixels.show();
delay(wait);
}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3,0);
}
if(WheelPos < 170) {
WheelPos -= 85;
return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3,0);
}
WheelPos -= 170;
return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0,0);
}
uint8_t red(uint32_t c) {
return (c >> 16);
}
uint8_t green(uint32_t c) {
return (c >> 8);
}
uint8_t blue(uint32_t c) {
return (c);
}
/*********************/
void setup() {
// 初始化串口
Serial.begin(115200);
pixels.begin();//WS2812初始化
pixels.show();
pinMode(LED_BUILTIN, OUTPUT);
#if defined(BLINKER_PRINT)
BLINKER_DEBUG.stream(BLINKER_PRINT);
#endif
WIFI_Set();
// 初始化blinker
Blinker.begin(auth, WiFi.SSID().c_str(), WiFi.psk().c_str());
Blinker.attachData(dataRead);//如果没有被绑定的组件被触发就执行这个回调
Button1.attach(button1_callback);//Button1这个组件被触发就执行这个回调
RGB1.attach(rgb1_callback);//注册调节颜色的回调函数
BlinkerMIOT.attachPowerState(miotPowerState);//小爱电源回调函数
BlinkerAliGenie.attachPowerState(aligeniePowerState);//天猫电源回调函数
BlinkerMIOT.attachColor(miotColor);//小爱调节颜色
BlinkerAliGenie.attachColor(aligenieColor);//天猫颜色回调
BlinkerMIOT.attachBrightness(miotBright);//小爱调节RGB亮度
BlinkerAliGenie.attachBrightness(aligenieBright);//天猫亮度回调
BlinkerAliGenie.attachRelativeBrightness(aligenieRelativeBright);//天猫亮度步长回调
BlinkerMIOT.attachMode(miotMode);//小爱灯光模式切换
BlinkerAliGenie.attachMode(aligenieMode);//天猫灯光模式切换
BlinkerMIOT.attachColorTemperature(miotColoTemp);//小爱色温回调
BlinkerAliGenie.attachColorTemperature(aligenieColoTemp);//天猫色温回调
BlinkerMIOT.attachQuery(miotQuery);//小爱设备状态查询回调
BlinkerAliGenie.attachRelativeColorTemperature(aligenieRelativeColoTemp);//天猫亮度步长回调
BlinkerAliGenie.attachQuery(aligenieQuery);//天猫设备状态查询回调
}
void loop() {
Blinker.run();
}
|
|