|
基于奈何col大神的帖子,自己增加一些功能实现小爱同学语音控制双色温吸顶灯原贴连接:https://www.arduino.cn/forum.php ... mp;page=1#pid607355
功能:1、blinker手机APP端可选模式,可自己手动调整亮度和色温,一键总开关控制;
2、可以小爱同学控制:①开关灯;②调亮度;③调色温;④模式设置包括 日光模式、夜光模式、温馨模式、电视模式、阅读模式、电脑模式
注意事项:添加设备选择“专属设备”
简单粗暴的上代码:
- #define BLINKER_PRO_ESP
- #define BLINKER_WITHOUT_SSL
- #define BLINKER_BUTTON
- #define BLINKER_BUTTON_LONGPRESS_POWERDOWN
- #if defined(ESP32)
- #define BLINKER_BUTTON_PIN 4
- #else
- #define BLINKER_BUTTON_PIN D7
- #endif
- #define BLINKER_OTA_VERSION_CODE "0.1.1"
- #define BLINKER_MIOT_LIGHT
- #include <Blinker.h>
- char type[] = "EjtbFjnSLpL6"; //不需要修改
- char auth[] = "AU1ldFdhHy7z"; //不需要修改
- //定义灯接口
- #define ColdLED D2
- #define WarmLED D3
- //定义冷暖色温输出值
- int CVal,WVal;
- // 设备状态
- String switchState = "on";
- int modeState = 0;
- int brightvalue = 0; //brightness定义和小爱回调及blinker库有冲突,故采用brightvalue
- int colortemp = 0;
- bool oState = false;
- //定义小爱电源类回调函数
- void miotPowerState(const String & state)
- {
- BLINKER_LOG("need set power state: ", state);
-
- if (state == BLINKER_CMD_ON)
- {
- digitalWrite(LED_BUILTIN, HIGH);
- analogWrite(ColdLED,CVal);
- analogWrite(WarmLED,WVal);
- BlinkerMIOT.powerState("on");
- BlinkerMIOT.print();
- switchState = "on"; //给APP反馈按钮状态
- oState = true; //给小爱同学反馈按钮状态
- }
- else if (state == BLINKER_CMD_OFF)
- {
- digitalWrite(LED_BUILTIN, LOW);
- analogWrite(ColdLED,LOW);
- analogWrite(WarmLED,LOW);
- BlinkerMIOT.powerState("off");
- BlinkerMIOT.print();
- switchState = "off";
- oState = false;
- }
- BUILTIN_SWITCH.print(switchState);
- }
- //定义小爱亮度回调函数
- void miotBright(const String & bright)
- {
- //BLINKER_LOG("need set Brightness:",bright); //调试过程串口监视器观察用
- brightvalue = bright.toInt();
- //BLINKER_LOG("now set brightness:", brightvalue); //调试过程串口监视器观察用
-
- BlinkerMIOT.brightness(brightvalue);
- BlinkerMIOT.print();
- }
- //定义小爱色温回调函数
- void miotColoTemp(int32_t colorTemperature)
- {
- //BLINKER_LOG("need set colorTemperature:",colorTemperature);
-
- colortemp = map(colorTemperature,1000,10000,0,255); //小爱同学默认色温范围是1000~10000,APP中为0~255
-
- BlinkerMIOT.colorTemp(colorTemperature);
- 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(oState ? "on" : "off");
- BlinkerMIOT.print();
- break;
- default :
- break;
- }
- }
- //用户自定义模式设置的回调函数
- void miotMode(uint8_t mode)
- {
- //BLINKER_LOG("need set mode: ", mode);
- switch(mode)
- {
- case BLINKER_CMD_MIOT_DAY:
- sunMode();
- break;
- case BLINKER_CMD_MIOT_NIGHT:
- moonMode();
- break;
- case BLINKER_CMD_MIOT_WARMTH:
- warmMode();
- break;
- case BLINKER_CMD_MIOT_TV:
- TVMode();
- break;
- case BLINKER_CMD_MIOT_READING:
- readMode();
- break;
- case BLINKER_CMD_MIOT_COMPUTER:
- compuerMode();
- break;
- default :
- break;
- }
-
- changeLight();
- feedbackData();
- BlinkerMIOT.mode(mode);
- BlinkerMIOT.print();
- }
- bool dataParse(const JsonObject & data)
- {
- String getData;
- serializeJson(data, getData);
- //BLINKER_LOG("Get user command: ", getData);
- modeState = data["set"]["mode"];
- brightvalue = data["set"]["brt"];
- colortemp = data["set"]["crt"];
- changeMode();
- // if you parsed this data, return TRUE.
- // return true;
- return true;
- }
- //APP端模式选择
- void changeMode() {
- switch(modeState)
- {
- case 0:
- sunMode();
- break;
- case 1:
- moonMode();
- break;
- case 2:
- starMode();
- break;
- }
- changeLight();
- feedbackData();
- }
- void sunMode() {
- brightvalue = 255;
- colortemp = 128;
- }
- void moonMode() {
- brightvalue = 255;
- colortemp = 0;
- }
- void starMode() {
- brightvalue = 128;
- colortemp = 0;
- }
- void warmMode(){
- brightvalue = 200;
- colortemp = 0;
- }
- void TVMode(){
- brightvalue = 128;
- colortemp = 128;
- }
- void readMode(){
- brightvalue = 200;
- colortemp = 150;
- }
- void compuerMode(){
- brightvalue = 200;
- colortemp = 200;
- }
- void changeLight(){
- //根据brightvalue,colortemp两参数调整灯
- if(switchState == "on"){
- CVal =map(brightvalue*colortemp/255,0,(colortemp+1),0,1023);
- WVal =map(brightvalue*(255-colortemp)/255,0,(256-colortemp),0,1023);
- analogWrite(ColdLED,CVal);
- analogWrite(WarmLED,WVal);
-
- switchState = "on";
- oState = true;
- }
- else if(switchState == "off"){
- analogWrite(ColdLED,LOW);
- analogWrite(WarmLED,LOW);
- switchState = "off";
- oState = false;
- }
- BUILTIN_SWITCH.print(switchState);
- }
- void feedbackData(){
- Blinker.print("mode", modeState);
- Blinker.print("brt", brightvalue);
- Blinker.print("crt", colortemp);
- }
- void heartbeat()
- {
- BUILTIN_SWITCH.print(switchState);
- feedbackData();
- }
- #if defined(BLINKER_BUTTON)
- ICACHE_RAM_ATTR void buttonTick()
- {
- Blinker.tick();
- }
- void singleClick()
- {
- BLINKER_LOG("Button clicked!");
- }
- void doubleClick()
- {
- BLINKER_LOG("Button double clicked!");
- }
- void longPressStart()
- {
- BLINKER_LOG("Button long press start!");
- }
- void duringLongPress()
- {
- // BLINKER_LOG("During button long press!");
- uint16_t pressed_time = Blinker.pressedTime();
- if (pressed_time >= 5000 && Blinker.configType() != BLINKER_AP_CONFIG)
- {
- Blinker.apConfigInit();
- }
- }
- #endif
- void dataRead(const String & data)
- {
- BLINKER_LOG("Blinker readString: ", data);
- }
- void switch_callback(const String & state)
- {
- if (state == BLINKER_CMD_ON) {
- digitalWrite(LED_BUILTIN, HIGH);
- analogWrite(ColdLED,CVal);
- analogWrite(WarmLED,WVal);
-
- switchState = "on";
- oState = true;
- }
- else {
- digitalWrite(LED_BUILTIN, LOW);
- analogWrite(ColdLED,LOW);
- analogWrite(WarmLED,LOW);
- switchState = "off";
- oState = false;
- }
- BUILTIN_SWITCH.print(switchState);
- }
- void setup()
- {
- Serial.begin(115200);
- BLINKER_DEBUG.stream(Serial);
- BLINKER_DEBUG.debugAll();
- pinMode(LED_BUILTIN, OUTPUT);
- digitalWrite(LED_BUILTIN, LOW);
-
- pinMode(ColdLED, OUTPUT);
- digitalWrite(ColdLED, LOW);
- pinMode(WarmLED, OUTPUT);
- digitalWrite(WarmLED, LOW);
- Blinker.begin(auth,type);
- Blinker.apConfigInit();
- Blinker.attachData(dataRead);
- Blinker.attachParse(dataParse);
- Blinker.attachHeartbeat(heartbeat);
- BUILTIN_SWITCH.attach(switch_callback);
-
- //注册小爱回调函数
- BlinkerMIOT.attachBrightness(miotBright);
- BlinkerMIOT.attachColorTemperature(miotColoTemp);
- BlinkerMIOT.attachPowerState(miotPowerState);
- BlinkerMIOT.attachQuery(miotQuery);
- BlinkerMIOT.attachMode(miotMode);
- #if defined(BLINKER_BUTTON)
- Blinker.attachClick(singleClick);
- Blinker.attachDoubleClick(doubleClick);
- Blinker.attachLongPressStart(longPressStart);
- Blinker.attachDuringLongPress(duringLongPress);
- attachInterrupt(BLINKER_BUTTON_PIN, buttonTick, CHANGE);
- #endif
- }
- void loop()
- {
- Blinker.run();
- }
复制代码
|
|