Blinker双色温吸顶灯小爱同学控制-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2174|回复: 2

[分享] Blinker双色温吸顶灯小爱同学控制

[复制链接]
发表于 2021-3-25 18:03 | 显示全部楼层 |阅读模式
基于奈何col大神的帖子,自己增加一些功能实现小爱同学语音控制双色温吸顶灯原贴连接:https://www.arduino.cn/forum.php ... mp;page=1#pid607355


功能:1、blinker手机APP端可选模式,可自己手动调整亮度和色温,一键总开关控制;
          2、可以小爱同学控制:①开关灯;②调亮度;③调色温;④模式设置包括 日光模式、夜光模式、温馨模式、电视模式、阅读模式、电脑模式
注意事项:添加设备选择“专属设备”


简单粗暴的上代码:
  1. #define BLINKER_PRO_ESP
  2. #define BLINKER_WITHOUT_SSL
  3. #define BLINKER_BUTTON
  4. #define BLINKER_BUTTON_LONGPRESS_POWERDOWN
  5. #if defined(ESP32)
  6. #define BLINKER_BUTTON_PIN 4
  7. #else
  8. #define BLINKER_BUTTON_PIN D7
  9. #endif

  10. #define BLINKER_OTA_VERSION_CODE "0.1.1"
  11. #define BLINKER_MIOT_LIGHT

  12. #include <Blinker.h>

  13. char type[] = "EjtbFjnSLpL6";     //不需要修改
  14. char auth[] = "AU1ldFdhHy7z";     //不需要修改


  15. //定义灯接口
  16. #define ColdLED D2
  17. #define WarmLED D3

  18. //定义冷暖色温输出值
  19. int CVal,WVal;

  20. // 设备状态
  21. String switchState = "on";
  22. int modeState = 0;
  23. int brightvalue = 0;    //brightness定义和小爱回调及blinker库有冲突,故采用brightvalue
  24. int colortemp = 0;
  25. bool oState = false;

  26. //定义小爱电源类回调函数
  27. void miotPowerState(const String & state)
  28. {
  29.     BLINKER_LOG("need set power state: ", state);

  30.     if (state == BLINKER_CMD_ON)
  31.     {
  32.       digitalWrite(LED_BUILTIN, HIGH);
  33.       analogWrite(ColdLED,CVal);
  34.       analogWrite(WarmLED,WVal);
  35.       BlinkerMIOT.powerState("on");
  36.       BlinkerMIOT.print();
  37.       switchState = "on";               //给APP反馈按钮状态
  38.       oState = true;                    //给小爱同学反馈按钮状态
  39.     }
  40.    else if (state == BLINKER_CMD_OFF)
  41.     {
  42.       digitalWrite(LED_BUILTIN, LOW);
  43.       analogWrite(ColdLED,LOW);
  44.       analogWrite(WarmLED,LOW);
  45.       BlinkerMIOT.powerState("off");
  46.       BlinkerMIOT.print();
  47.       switchState = "off";
  48.       oState = false;
  49.     }
  50.     BUILTIN_SWITCH.print(switchState);
  51. }

  52. //定义小爱亮度回调函数
  53. void miotBright(const String & bright)
  54. {
  55.   //BLINKER_LOG("need set Brightness:",bright);           //调试过程串口监视器观察用
  56.   brightvalue = bright.toInt();
  57.   //BLINKER_LOG("now set brightness:", brightvalue);      //调试过程串口监视器观察用
  58.   
  59.   BlinkerMIOT.brightness(brightvalue);
  60.   BlinkerMIOT.print();
  61. }

  62. //定义小爱色温回调函数
  63. void miotColoTemp(int32_t colorTemperature)
  64. {
  65.   //BLINKER_LOG("need set colorTemperature:",colorTemperature);
  66.   
  67.   colortemp = map(colorTemperature,1000,10000,0,255);           //小爱同学默认色温范围是1000~10000,APP中为0~255
  68.   
  69.   BlinkerMIOT.colorTemp(colorTemperature);
  70.   BlinkerMIOT.print();
  71. }

  72. //定义小爱状态回调函数
  73. void miotQuery(int32_t queryCode)
  74. {
  75.     //BLINKER_LOG("MIOT Query codes: ", queryCode);

  76.     switch (queryCode)
  77.     {
  78.         case BLINKER_CMD_QUERY_ALL_NUMBER :
  79.             //BLINKER_LOG("MIOT Query All");
  80.             BlinkerMIOT.powerState(oState ? "on" : "off");
  81.             BlinkerMIOT.print();
  82.             break;
  83.         default :
  84.             break;
  85.     }
  86. }


  87. //用户自定义模式设置的回调函数
  88. void miotMode(uint8_t mode)
  89. {
  90.     //BLINKER_LOG("need set mode: ", mode);

  91.     switch(mode)
  92.     {
  93.       case BLINKER_CMD_MIOT_DAY:
  94.            sunMode();
  95.            break;
  96.       case BLINKER_CMD_MIOT_NIGHT:
  97.            moonMode();
  98.            break;
  99.       case BLINKER_CMD_MIOT_WARMTH:
  100.            warmMode();
  101.            break;
  102.       case BLINKER_CMD_MIOT_TV:
  103.            TVMode();
  104.            break;
  105.       case BLINKER_CMD_MIOT_READING:
  106.            readMode();
  107.            break;
  108.       case BLINKER_CMD_MIOT_COMPUTER:
  109.            compuerMode();
  110.            break;
  111.       default :
  112.            break;
  113.   }
  114.    
  115.     changeLight();
  116.     feedbackData();

  117.     BlinkerMIOT.mode(mode);
  118.     BlinkerMIOT.print();
  119. }

  120. bool dataParse(const JsonObject & data)
  121. {
  122.   String getData;

  123.   serializeJson(data, getData);
  124.   //BLINKER_LOG("Get user command: ", getData);

  125.   modeState = data["set"]["mode"];
  126.   brightvalue = data["set"]["brt"];
  127.   colortemp = data["set"]["crt"];
  128.   changeMode();

  129.   // if you parsed this data, return TRUE.
  130.   // return true;
  131.   return true;
  132. }

  133. //APP端模式选择
  134. void changeMode() {
  135.   switch(modeState)
  136.   {
  137.     case 0:
  138.            sunMode();
  139.            break;
  140.     case 1:
  141.            moonMode();
  142.            break;
  143.     case 2:
  144.          starMode();
  145.          break;
  146.   }
  147.   changeLight();
  148.   feedbackData();
  149. }

  150. void sunMode() {
  151.   brightvalue = 255;
  152.   colortemp = 128;
  153. }

  154. void moonMode() {
  155.   brightvalue = 255;
  156.   colortemp = 0;
  157. }

  158. void starMode() {
  159.   brightvalue = 128;
  160.   colortemp = 0;
  161. }

  162. void warmMode(){
  163.   brightvalue = 200;
  164.   colortemp = 0;
  165. }

  166. void TVMode(){
  167.   brightvalue = 128;
  168.   colortemp = 128;
  169. }

  170. void readMode(){
  171.   brightvalue = 200;
  172.   colortemp = 150;
  173. }

  174. void compuerMode(){
  175.   brightvalue = 200;
  176.   colortemp = 200;
  177. }

  178. void changeLight(){
  179.   //根据brightvalue,colortemp两参数调整灯
  180.   if(switchState == "on"){
  181.     CVal =map(brightvalue*colortemp/255,0,(colortemp+1),0,1023);
  182.     WVal =map(brightvalue*(255-colortemp)/255,0,(256-colortemp),0,1023);
  183.     analogWrite(ColdLED,CVal);
  184.     analogWrite(WarmLED,WVal);
  185.    
  186.     switchState = "on";
  187.     oState = true;
  188.   }
  189.   else if(switchState == "off"){
  190.     analogWrite(ColdLED,LOW);
  191.     analogWrite(WarmLED,LOW);

  192.     switchState = "off";
  193.     oState = false;
  194.     }
  195.     BUILTIN_SWITCH.print(switchState);
  196. }

  197. void feedbackData(){
  198.   Blinker.print("mode", modeState);
  199.   Blinker.print("brt", brightvalue);
  200.   Blinker.print("crt", colortemp);
  201. }

  202. void heartbeat()
  203. {
  204.   BUILTIN_SWITCH.print(switchState);
  205.   feedbackData();
  206. }

  207. #if defined(BLINKER_BUTTON)
  208. ICACHE_RAM_ATTR void buttonTick()
  209. {
  210.   Blinker.tick();
  211. }

  212. void singleClick()
  213. {
  214.   BLINKER_LOG("Button clicked!");
  215. }

  216. void doubleClick()
  217. {
  218.   BLINKER_LOG("Button double clicked!");
  219. }

  220. void longPressStart()
  221. {
  222.   BLINKER_LOG("Button long press start!");
  223. }

  224. void duringLongPress()
  225. {
  226.   // BLINKER_LOG("During button long press!");

  227.   uint16_t pressed_time = Blinker.pressedTime();

  228.   if (pressed_time >= 5000 && Blinker.configType() != BLINKER_AP_CONFIG)
  229.   {
  230.     Blinker.apConfigInit();
  231.   }
  232. }
  233. #endif

  234. void dataRead(const String & data)
  235. {
  236.   BLINKER_LOG("Blinker readString: ", data);
  237. }

  238. void switch_callback(const String & state)
  239. {
  240.   if (state == BLINKER_CMD_ON) {
  241.     digitalWrite(LED_BUILTIN, HIGH);
  242.     analogWrite(ColdLED,CVal);
  243.     analogWrite(WarmLED,WVal);
  244.    
  245.     switchState = "on";
  246.     oState = true;
  247.   }
  248.   else {
  249.     digitalWrite(LED_BUILTIN, LOW);
  250.     analogWrite(ColdLED,LOW);
  251.     analogWrite(WarmLED,LOW);
  252.     switchState = "off";
  253.     oState = false;
  254.   }
  255.   BUILTIN_SWITCH.print(switchState);
  256. }

  257. void setup()
  258. {
  259.   Serial.begin(115200);
  260.   BLINKER_DEBUG.stream(Serial);
  261.   BLINKER_DEBUG.debugAll();

  262.   pinMode(LED_BUILTIN, OUTPUT);
  263.   digitalWrite(LED_BUILTIN, LOW);
  264.   
  265.   pinMode(ColdLED, OUTPUT);
  266.   digitalWrite(ColdLED, LOW);
  267.   pinMode(WarmLED, OUTPUT);
  268.   digitalWrite(WarmLED, LOW);  

  269.   Blinker.begin(auth,type);
  270.   Blinker.apConfigInit();

  271.   Blinker.attachData(dataRead);
  272.   Blinker.attachParse(dataParse);
  273.   Blinker.attachHeartbeat(heartbeat);
  274.   BUILTIN_SWITCH.attach(switch_callback);
  275.   
  276.   //注册小爱回调函数
  277.   BlinkerMIOT.attachBrightness(miotBright);
  278.   BlinkerMIOT.attachColorTemperature(miotColoTemp);
  279.   BlinkerMIOT.attachPowerState(miotPowerState);
  280.   BlinkerMIOT.attachQuery(miotQuery);
  281.   BlinkerMIOT.attachMode(miotMode);

  282. #if defined(BLINKER_BUTTON)
  283.   Blinker.attachClick(singleClick);
  284.   Blinker.attachDoubleClick(doubleClick);
  285.   Blinker.attachLongPressStart(longPressStart);
  286.   Blinker.attachDuringLongPress(duringLongPress);
  287.   attachInterrupt(BLINKER_BUTTON_PIN, buttonTick, CHANGE);
  288. #endif
  289. }

  290. void loop()
  291. {
  292.   Blinker.run();
  293. }
复制代码


发表于 2021-11-22 14:55 | 显示全部楼层
楼主,请问一下为什么配不了网blinker app提示没有找到设备
发表于 2022-8-21 18:53 | 显示全部楼层
请问这个物理上怎么实现?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-12-1 04:04 , Processed in 0.090759 second(s), 16 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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