想做一个接入小爱的RGB氛围灯-求代码-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 5825|回复: 11

[过期] 想做一个接入小爱的RGB氛围灯-求代码

[复制链接]
发表于 2020-3-26 15:51 | 显示全部楼层 |阅读模式
WS2812灯带和控制模块都准备好了,找到的代码可能是ESP32的,好像代码不适用ESP8266-01S 求大神赐代码,跪求!跪求!跪求!!!

已准备的控制模块

已准备的控制模块
发表于 2020-3-26 17:02 | 显示全部楼层
#define BLINKER_WIFI/////////////////// wifi
#define BLINKER_MIOT_LIGHT ////////////语音助手

#include <Blinker.h>

char auth[] = "b6482886a3d0";//////////手机下载 blinker app 安卓应用商店 苹果在https://www.diandeng.tech/home   app-右上角+ DIY设备:Arduino 添加后得到该授权码
char ssid[] = "智能家居专用";/////wifi ssid
char pswd[] = "fk123456..";///wifi 密码
int openState = 0; ///默认为单色模式
// Download Adafruit_NeoPixel library here:
// https://github.com/adafruit/Adafruit_NeoPixel
#include <Adafruit_NeoPixel.h>

#define PIN            2   ////开发板的引脚编号 接WS2812 data  VCC--VCC  GND-GND DATA/DI(DATA INPUT)--17 具体看产品说明
#define NUMPIXELS      22  ////开发板灯灯珠数量 位
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
#define BUTTON_1 "ButtonKey"   ///////////////【APP内用来控制呼吸灯开关】app中添加 开关按键 数据键名ButtonKey  一致即可 注意大小写
#define RGB_1 "RGBKey"              ///////////////【APP内用来控制颜色及亮度】app中添加调色盘 数据键名RGBKey  一致即可

BlinkerRGB WS2812(RGB_1);
BlinkerButton Button1(BUTTON_1);
uint8_t colorR, colorG, colorB, colorW;
bool wsState;
uint8_t wsMode = BLINKER_CMD_MIOT_DAY;

void pixelShow()
{
  pixels.setBrightness(colorW);

  for (int i = 0; i < NUMPIXELS; i++) {
    pixels.setPixelColor(i, colorR, colorG, colorB);
  }
  pixels.show();
}

void ws2812_callback(uint8_t r_value, uint8_t g_value, uint8_t b_value, uint8_t bright_value)
{
  digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  openState = 0;
  Button1.print("off");
  BLINKER_LOG("R value: ", r_value);
  BLINKER_LOG("G value: ", g_value);
  BLINKER_LOG("B value: ", b_value);
  BLINKER_LOG("Rrightness value: ", bright_value);

  colorR = r_value;
  colorG = g_value;
  colorB = b_value;
  colorW = bright_value;

  pixelShow();
}
void button1_callback(const String & state)
{
  digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  BLINKER_LOG("get button state: ", state);

  if (state == BLINKER_CMD_ON) {
    BLINKER_LOG("Toggle on!");
    Button1.print("on");
    openState = 1;
  }
  else if (state == BLINKER_CMD_OFF) {
    BLINKER_LOG("Toggle off!");
    Button1.print("off");
    openState = 0;
  }

}

uint32_t getColor()
{
  uint32_t color = colorR << 16 | colorG << 8 | colorB;

  return color;
}

void miotPowerState(const String & state)
{
  BLINKER_LOG("need set power state: ", state);

  if (state == BLINKER_CMD_ON) {
    digitalWrite(LED_BUILTIN, HIGH);

    BlinkerMIOT.powerState("on");
    BlinkerMIOT.print();

    wsState =true;

    if (colorW==0) colorW =255;
  }
  else if (state == BLINKER_CMD_OFF) {
    digitalWrite(LED_BUILTIN, HIGH);

    BlinkerMIOT.powerState("off");
    BlinkerMIOT.print();

    wsState = false;
    if (colorW >0) colorW = 0;
  
  }

  pixelShow();
}
///////////////////////////////////////////////////////呼吸灯
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if (WheelPos < 85) {
    return pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if (WheelPos < 170) {
    WheelPos -= 85;
    return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for (j = 0; j < 256; j++) {
    for (i = 0; i < pixels.numPixels(); i++) {
      //time_show();
      // dhtShow();
      pixels.setPixelColor(i, Wheel((i + j) & 255));
    }
    pixels.show();
    delay(wait);
  }
}


void miotColor(int32_t color)
{
  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);

  pixelShow();

  BlinkerMIOT.color(color);
  BlinkerMIOT.print();
}

void miotMode(uint8_t mode)
{
  BLINKER_LOG("need set mode: ", mode);

  if (mode == BLINKER_CMD_MIOT_DAY) {
    //日光模式   小爱同学的语音命令 模式中文名称参考 https://www.diandeng.tech/doc/xiaoai
  }
  else if (mode == BLINKER_CMD_MIOT_NIGHT) {
    // Your mode function
  }
  else if (mode == BLINKER_CMD_MIOT_COLOR) {
    // Your mode function
    openState = 0;
  }
  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) {
    // 电脑模式
    openState = 1;
  }

  wsMode = mode;

  BlinkerMIOT.mode(mode);
  BlinkerMIOT.print();
}

void miotBright(const String & bright)
{
  BLINKER_LOG("need set brightness: ", bright);

  colorW = bright.toInt();

  BLINKER_LOG("now set brightness: ", colorW);

  pixelShow();

  BlinkerMIOT.brightness(colorW);
  BlinkerMIOT.print();
}

void miotColoTemp(int32_t colorTemp)
{
  BLINKER_LOG("need set colorTemperature: ", colorTemp);

  BlinkerMIOT.colorTemp(colorTemp);
  BlinkerMIOT.print();
}

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(wsState ? "on" : "off");
      BlinkerMIOT.color(0);
      BlinkerMIOT.mode(0);
      BlinkerMIOT.colorTemp(1000);
      BlinkerMIOT.brightness(1);
      BlinkerMIOT.print();
      break;
    case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
      BLINKER_LOG("MIOT Query Power State");
      BlinkerMIOT.powerState(wsState ? "on" : "off");
      BlinkerMIOT.print();
      break;
    case BLINKER_CMD_QUERY_COLOR_NUMBER :
      BLINKER_LOG("MIOT Query Color");
      BlinkerMIOT.color(0);
      BlinkerMIOT.print();
      break;
    case BLINKER_CMD_QUERY_MODE_NUMBER :
      BLINKER_LOG("MIOT Query Mode");
      BlinkerMIOT.mode(0);
      BlinkerMIOT.print();
      break;
    case BLINKER_CMD_QUERY_COLORTEMP_NUMBER :
      BLINKER_LOG("MIOT Query ColorTemperature");
      BlinkerMIOT.colorTemp(1000);
      BlinkerMIOT.print();
      break;
    case BLINKER_CMD_QUERY_BRIGHTNESS_NUMBER :
      BLINKER_LOG("MIOT Query Brightness");
      BlinkerMIOT.brightness(1);
      BlinkerMIOT.print();
      break;
    default :
      BlinkerMIOT.powerState(wsState ? "on" : "off");
      BlinkerMIOT.color(0);
      BlinkerMIOT.mode(0);
      BlinkerMIOT.colorTemp(1000);
      BlinkerMIOT.brightness(1);
      BlinkerMIOT.print();
      break;
  }
}

void dataRead(const String & data)
{
  BLINKER_LOG("Blinker readString: ", data);

  Blinker.vibrate();

  uint32_t BlinkerTime = millis();

  Blinker.print("millis", BlinkerTime);
}

void setup()
{
  Serial.begin(115200);
  BLINKER_DEBUG.stream(Serial);
  BLINKER_DEBUG.debugAll();

  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

  Blinker.begin(auth, ssid, pswd);
  Blinker.attachData(dataRead);

  BlinkerMIOT.attachPowerState(miotPowerState);
  BlinkerMIOT.attachColor(miotColor);
  BlinkerMIOT.attachMode(miotMode);
  BlinkerMIOT.attachBrightness(miotBright);
  BlinkerMIOT.attachColorTemperature(miotColoTemp);
  BlinkerMIOT.attachQuery(miotQuery);

  pinMode(14, OUTPUT);
  digitalWrite(14, HIGH);
  pinMode(15, OUTPUT);
  digitalWrite(15, HIGH);

  colorR = 255;
  colorG = 255;
  colorB = 255;
  colorW = 0;
  wsState = true;
  

  pixels.begin();
  pixels.setBrightness(colorW);
  WS2812.attach(ws2812_callback);
  Button1.attach(button1_callback);
  pixelShow();
}

void loop()
{
  Blinker.run();
  if (openState == 0)
  {
    pixelShow();
    //Button1.print("off");

  }
  else if (openState == 1)
  {
    rainbow(15);/////////15呼吸灯的频率
    //Button1.print("on");
  }
}
发表于 2020-3-26 16:04 | 显示全部楼层
发表于 2020-3-26 16:04 | 显示全部楼层
照着示例改就行,ESP32和ESP8266的库很多部分是相同的
 楼主| 发表于 2020-4-23 10:39 | 显示全部楼层
搞笑的山贼 发表于 2020-3-26 17:02
#define BLINKER_WIFI/////////////////// wifi
#define BLINKER_MIOT_LIGHT ////////////语音助手

感谢感谢,
发表于 2020-4-26 13:21 | 显示全部楼层
搞笑的山贼 发表于 2020-3-26 17:02
#define BLINKER_WIFI/////////////////// wifi
#define BLINKER_MIOT_LIGHT ////////////语音助手

你那个按键没有用啊
发表于 2020-4-26 13:22 | 显示全部楼层

他那个按键是不是没用,只有调色盘有用,并且,跑马灯只有四个颜色,只有一个跑马灯模式
 楼主| 发表于 2020-4-27 11:17 | 显示全部楼层
932045152 发表于 2020-4-26 13:22
他那个按键是不是没用,只有调色盘有用,并且,跑马灯只有四个颜色,只有一个跑马灯模式 ...

调色盘控制颜色亮度和开关,按键点击一下就是跑马灯
发表于 2020-4-29 22:11 | 显示全部楼层
qq260091118 发表于 2020-4-27 11:17
调色盘控制颜色亮度和开关,按键点击一下就是跑马灯

为什么我添加那个你说的跑马灯按键,点击了没有用
发表于 2020-4-30 12:46 | 显示全部楼层
看这个视频,附网盘地址,代码已经更新!https://www.bilibili.com/video/BV1oZ4y1W7Ni
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-28 17:41 , Processed in 0.102469 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表