RGB灯成功接入小爱同学-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 3405|回复: 5

[分享] RGB灯成功接入小爱同学

[复制链接]
发表于 2020-8-3 16:55 | 显示全部楼层 |阅读模式
本帖最后由 molun 于 2020-8-3 16:59 编辑

示例代码如下:
  1. /* *****************************************************************
  2. *
  3. *  名称:RGB氛围灯
  4. *  简介:三色RGB灯共GND,R、G、B引脚分别接GPIO13、GPIO14、GPIO15,小爱同学控制
  5. *  作者:MOLUN
  6. *  抖音:tymishop
  7. *  博客:https://molun.cf/
  8. *
  9. * *****************************************************************/

  10. #define BLINKER_WIFI
  11. #define BLINKER_ESP_SMARTCONFIG
  12. #define BLINKER_MIOT_LIGHT

  13. #include <Blinker.h>

  14. char auth[] = "ce4356d6832d";

  15. const int PIN_LED_RED = 13;
  16. const int PIN_LED_GREEN = 14;
  17. const int PIN_LED_BLUE = 15;

  18. #define RGB_1 "RGBKey"

  19. BlinkerRGB RGB1(RGB_1);

  20. uint8_t colorR, colorG, colorB, colorW;
  21. bool wsState;
  22. uint8_t wsMode = BLINKER_CMD_MIOT_DAY;

  23. void rgb1_callback(uint8_t r_value, uint8_t g_value, uint8_t b_value, uint8_t bright_value)
  24. {
  25.     digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  26.     BLINKER_LOG("R value: ", r_value);
  27.     BLINKER_LOG("G value: ", g_value);
  28.     BLINKER_LOG("B value: ", b_value);
  29.     BLINKER_LOG("Rrightness value: ", bright_value);

  30.     colorR = r_value;
  31.     colorG = g_value;
  32.     colorB = b_value;
  33.     colorW = bright_value;

  34.     analogWrite(PIN_LED_RED, colorR*colorW*0.041);
  35.     analogWrite(PIN_LED_GREEN, colorG*colorW*0.041);
  36.     analogWrite(PIN_LED_BLUE, colorB*colorW*0.041);

  37. }

  38. uint32_t getColor()
  39. {
  40.     uint32_t color = colorR << 16 | colorG << 8 | colorB;

  41.     return color;
  42. }

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

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

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

  50.         wsState = true;

  51.         if (colorW == 0) colorW = 255;
  52.     }
  53.     else if (state == BLINKER_CMD_OFF) {
  54.         digitalWrite(LED_BUILTIN, LOW);

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

  57.         wsState = false;
  58.     }
  59. }

  60. void miotColor(int32_t color)
  61. {
  62.     BLINKER_LOG("need set color: ", color);

  63.     colorR = color >> 16 & 0xFF;
  64.     colorG = color >>  8 & 0xFF;
  65.     colorB = color       & 0xFF;

  66.     BLINKER_LOG("colorR: ", colorR, ", colorG: ", colorG, ", colorB: ", colorB);

  67.     analogWrite(PIN_LED_RED, colorR*colorW*0.041);
  68.     analogWrite(PIN_LED_GREEN, colorG*colorW*0.041);
  69.     analogWrite(PIN_LED_BLUE, colorB*colorW*0.041);

  70.     BlinkerMIOT.color(color);
  71.     BlinkerMIOT.print();
  72. }

  73. void miotMode(uint8_t mode)
  74. {
  75.     BLINKER_LOG("need set mode: ", mode);

  76.     if (mode == BLINKER_CMD_MIOT_DAY) {
  77.         // Your mode function
  78.     }
  79.     else if (mode == BLINKER_CMD_MIOT_NIGHT) {
  80.         // Your mode function
  81.     }
  82.     else if (mode == BLINKER_CMD_MIOT_COLOR) {
  83.         // Your mode function
  84.     }
  85.     else if (mode == BLINKER_CMD_MIOT_WARMTH) {
  86.         // Your mode function
  87.     }
  88.     else if (mode == BLINKER_CMD_MIOT_TV) {
  89.         // Your mode function
  90.     }
  91.     else if (mode == BLINKER_CMD_MIOT_READING) {
  92.         // Your mode function
  93.     }
  94.     else if (mode == BLINKER_CMD_MIOT_COMPUTER) {
  95.         // Your mode function
  96.     }

  97.     wsMode = mode;

  98.     BlinkerMIOT.mode(mode);
  99.     BlinkerMIOT.print();
  100. }

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

  104.     colorW = bright.toInt();

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

  106.     analogWrite(PIN_LED_RED, colorR*colorW*0.041);
  107.     analogWrite(PIN_LED_GREEN, colorG*colorW*0.041);
  108.     analogWrite(PIN_LED_BLUE, colorB*colorW*0.041);

  109.     BlinkerMIOT.brightness(colorW);
  110.     BlinkerMIOT.print();
  111. }

  112. void miotColoTemp(int32_t colorTemp)
  113. {
  114.     BLINKER_LOG("need set colorTemperature: ", colorTemp);

  115.     BlinkerMIOT.colorTemp(colorTemp);
  116.     BlinkerMIOT.print();
  117. }

  118. void miotQuery(int32_t queryCode)
  119. {
  120.     BLINKER_LOG("MIOT Query codes: ", queryCode);

  121.     switch (queryCode)
  122.     {
  123.         case BLINKER_CMD_QUERY_ALL_NUMBER :
  124.             BLINKER_LOG("MIOT Query All");
  125.             BlinkerMIOT.powerState(wsState ? "on" : "off");
  126.             BlinkerMIOT.color(0);
  127.             BlinkerMIOT.mode(0);
  128.             BlinkerMIOT.colorTemp(1000);
  129.             BlinkerMIOT.brightness(1);
  130.             BlinkerMIOT.print();
  131.             break;
  132.         case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
  133.             BLINKER_LOG("MIOT Query Power State");
  134.             BlinkerMIOT.powerState(wsState ? "on" : "off");
  135.             BlinkerMIOT.print();
  136.             break;
  137.         case BLINKER_CMD_QUERY_COLOR_NUMBER :
  138.             BLINKER_LOG("MIOT Query Color");
  139.             BlinkerMIOT.color(0);
  140.             BlinkerMIOT.print();
  141.             break;
  142.         case BLINKER_CMD_QUERY_MODE_NUMBER :
  143.             BLINKER_LOG("MIOT Query Mode");
  144.             BlinkerMIOT.mode(0);
  145.             BlinkerMIOT.print();
  146.             break;
  147.         case BLINKER_CMD_QUERY_COLORTEMP_NUMBER :
  148.             BLINKER_LOG("MIOT Query ColorTemperature");
  149.             BlinkerMIOT.colorTemp(1000);
  150.             BlinkerMIOT.print();
  151.             break;
  152.         case BLINKER_CMD_QUERY_BRIGHTNESS_NUMBER :
  153.             BLINKER_LOG("MIOT Query Brightness");
  154.             BlinkerMIOT.brightness(1);
  155.             BlinkerMIOT.print();
  156.             break;
  157.         default :
  158.             BlinkerMIOT.powerState(wsState ? "on" : "off");
  159.             BlinkerMIOT.color(0);
  160.             BlinkerMIOT.mode(0);
  161.             BlinkerMIOT.colorTemp(1000);
  162.             BlinkerMIOT.brightness(1);
  163.             BlinkerMIOT.print();
  164.             break;
  165.     }
  166. }

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

  170.     Blinker.vibrate();
  171.    
  172.     uint32_t BlinkerTime = millis();
  173.    
  174.     Blinker.print("millis", BlinkerTime);

  175.     RGB1.brightness(random(0, 255));
  176.     RGB1.print(random(0, 255), random(0, 255), random(0, 255));
  177. }

  178. void setup()
  179. {
  180.     Serial.begin(115200);
  181.     BLINKER_DEBUG.stream(Serial);

  182.     pinMode(LED_BUILTIN, OUTPUT);
  183.     digitalWrite(LED_BUILTIN, LOW);

  184.     Blinker.begin(auth);
  185.     Blinker.attachData(dataRead);

  186.     BlinkerMIOT.attachPowerState(miotPowerState);
  187.     BlinkerMIOT.attachColor(miotColor);
  188.     BlinkerMIOT.attachMode(miotMode);
  189.     BlinkerMIOT.attachBrightness(miotBright);
  190.     BlinkerMIOT.attachColorTemperature(miotColoTemp);
  191.     BlinkerMIOT.attachQuery(miotQuery);

  192.     pinMode(PIN_LED_RED, OUTPUT);
  193.     digitalWrite(PIN_LED_RED, HIGH);
  194.     pinMode(PIN_LED_GREEN, OUTPUT);
  195.     digitalWrite(PIN_LED_GREEN, HIGH);
  196.     pinMode(PIN_LED_BLUE, OUTPUT);
  197.     digitalWrite(PIN_LED_BLUE, HIGH);

  198.     colorR = 255;
  199.     colorG = 255;
  200.     colorB = 255;
  201.     colorW = 0;
  202.     wsState = true;

  203.     RGB1.attach(rgb1_callback);
  204. }

  205. void loop()
  206. {
  207.     Blinker.run();
  208. }
复制代码
Screenshot_2020-08-03-15-23-11-498_iot.clz.me.jpg Screenshot_2020-08-03-15-23-55-973_iot.clz.me.jpg
IMG_20200803_150920.jpg IMG_20200803_150941.jpg IMG_20200803_150949.jpg
IMG_20200803_151011.jpg IMG_20200803_151025.jpg IMG_20200803_151034.jpg
发表于 2020-8-3 20:43 | 显示全部楼层
MI6一代神机
发表于 2020-12-23 23:02 | 显示全部楼层
感谢楼主分享
发表于 2021-5-9 23:10 | 显示全部楼层
谢谢楼主分享,请问怎么在小爱上控制改变灯的颜色?
发表于 2021-5-11 08:29 来自手机 | 显示全部楼层
谢谢分享,收藏。
发表于 2021-10-15 02:21 来自手机 | 显示全部楼层
谢谢分享,收藏了,
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-11-28 11:48 , Processed in 0.232332 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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