注册 登录
电子工程世界-论坛 返回首页 EEWORLD首页 频道 EE大学堂 下载中心 Datasheet 专题
mingzhe123的个人空间 https://home.eeworld.com.cn/space-uid-1166487.html [收藏] [复制] [分享] [RSS]
日志

【2024 DigiKey 创意大赛】室内智能照明灯设计---进度篇2

已有 378 次阅读2024-9-1 16:23

有幸进阶得捷创意大赛,趁着周末,对当时的项目进行了一部分开发。目前进度已经完成RGB灯光控制,以及小爱音箱的控制。

下面是详细的代码:

#define BLINKER_WIFI
#define BLINKER_MIOT_LIGHT

#include <Blinker.h>
#include <Arduino.h>

char auth[] = "044be84a71f9";
char ssid[] = "C_WIFI";
char pswd[] = "C88888888";

// Download Adafruit_NeoPixel library here:
// https://github.com/adafruit/Adafruit_NeoPixel
#include <Adafruit_NeoPixel.h>

#define PIN            4
#define NUMPIXELS      1
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_RGB + NEO_KHZ800);

 

#define RGB_1 "RGBKey"


BlinkerRGB WS2812(RGB_1);
// BlinkerSlider Slider1(Slider_1);
// BlinkerSlider Slider2(Slider_2);

uint8_t colorR, colorG, colorB;

uint8_t colorW; //获取亮度
uint8_t colorT; //获取色温
uint8_t cold;//白光输出值
uint8_t warm;//黄光输出值

bool wsState;
bool COLORM;    //彩灯模式
bool READINGM;	//阅读模式
uint8_t wsMode = BLINKER_CMD_MIOT_DAY;


void pixelShow()      //RGB控制函数
{
    pixels.setBrightness(colorW);

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


void LedControl(float x,int y)  //CW控制函数(X:亮度、Y:色温)
{
    y = 100 - y ;
	cold = x/100*y;   //cold_tmp是PWM冷灯珠的占空比
    warm = x/100*(100-y);  //warm_tmp是PWM冷灯珠的占空比:
	
   


}

// void slider1_callback(int32_t value)
// {
//     //设置冷光亮度
//     ledcWrite(2, value);
//     BLINKER_LOG("C value: ", value);
// }

// void slider2_callback(int32_t value)
// {
//     //设置暖光亮度
//     ledcWrite(1, value);
//     BLINKER_LOG("W value: ", value);
// }




void ws2812_callback(uint8_t r_value, uint8_t g_value, uint8_t b_value, uint8_t bright_value)  //RGB2812回调函数
{
    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);

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

    pixelShow();
}

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, LOW);

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

        wsState = false;
        
        colorW = 0;
    }

    pixelShow();
}

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) {   //日光(10,25,41)
        // Your mode function
        COLORM = 0;
        colorR = 10;
        colorG = 255;
        colorB = 41;
    }
    else if (mode == BLINKER_CMD_MIOT_NIGHT) {   //月光 自定义
        // Your mode function
        COLORM = 0;
        COLORM = 0;
        colorR = 0;
        colorG = 0;
        colorB = 0;
    }
    else if (mode == BLINKER_CMD_MIOT_COLOR) {   //彩光
        // Your mode function
        COLORM = 1;
    }
    else if (mode == BLINKER_CMD_MIOT_WARMTH) {  //温馨
        // Your mode function
        COLORM = 0;
        colorR = 10;
        colorG = 255;
        colorB = 41;

    }
    else if (mode == BLINKER_CMD_MIOT_TV) {     //电视模式
        // Your mode function
        COLORM = 0;
    }
    else if (mode == BLINKER_CMD_MIOT_READING) { //阅读模式
        // Your mode function
        COLORM = 0;
        colorR = 0;
        colorG = 0;
        colorB = 0;

    }
    else if (mode == BLINKER_CMD_MIOT_COMPUTER) {  //电脑模式
        // Your mode function
        COLORM = 0;
        colorR = 0;
        colorG = 0;
        colorB = 0;
        LedControl(colorW,colorT);
    }

    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);;

    BLINKER_LOG("需要设置色温: ", colorTemp);
    colorT = map(colorTemp,1000,10000,0,100);
    BlinkerMIOT.colorTemp(colorT);
    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;
    colorT = 0;
    wsState = true;

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

    // Slider1.attach(slider1_callback);
    // Slider2.attach(slider2_callback);
}

void loop()
{
    Blinker.run();

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

通过该代码可以实现点灯上位机软件对RGB灯光的控制,并可以通过小爱音箱调整RGB颜色、色温、亮度以及开关等;还可以通过小爱音箱控制不同的工作模式。

 

本文来自论坛,点击查看完整帖子内容。

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

热门文章