|
基于第三方平台----点灯科技,官方代码有点不适应此芯片,代码有所改动,并且加入了自定义模式。
#define BLINKER_WIFI
#define BLINKER_MIOT_LIGHT
#include <Blinker.h>
char auth[] = "**********"; //设备码
char ssid [] = "**********"; //wifi名字
char pswd[] = "**********"; //WiFi密码
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#define PIN 2 //定义引脚
#define NUMPIXELS 30 //灯珠数
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
#define RGB_1 "RGBKey"
BlinkerRGB WS2812(RGB_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));
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();
wsMode = BLINKER_CMD_MIOT_DAY;
wsState = true;
if (colorW == 0) colorW = 255;
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(LED_BUILTIN, LOW);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
wsMode = BLINKER_CMD_MIOT_DAY;
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) //日光模式,默认日光模式
{
// Your mode function
for(int i = 0; i < NUMPIXELS; i++){
pixels.setPixelColor(i, colorR, colorG, colorB);
};
}
else if (mode == BLINKER_CMD_MIOT_NIGHT) //夜光模式
{
// Your mode function
colorR = 135;
colorG = 206;
colorB = 235; //自定义颜色,修改RGB三位数值
breath(); //呼吸灯库
}
else if (mode == BLINKER_CMD_MIOT_COLOR) //彩光模式
{
// Your mode function
colorScan(); //跑马灯库
}
else if (mode == BLINKER_CMD_MIOT_WARMTH) // 温馨模式 {
// Your mode function
colorWarmth(); colorW=255;
}
else if (mode == BLINKER_CMD_MIOT_TV) //电视模式
{
// Your mode function
colorTV();
colorW=255;
}
else if (mode == BLINKER_CMD_MIOT_READING) //阅读模式
{
// Your mode function
colorR = 255;
colorG = 215;
colorB = 0;
colorW=128;
}
else if (mode == BLINKER_CMD_MIOT_COMPUTER) //电脑模式
{
// Your mode function
colorComputer();
}
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(2, OUTPUT);
digitalWrite(2, HIGH);
colorR = 255;
colorG = 255;
colorB = 255;
colorW = 0;
wsState = true;
pixels.begin();
pixels.setBrightness(colorW);
WS2812.attach(ws2812_callback);
pixelShow();
}
void loop()
{
Blinker.run();
pixels.show();
if (wsMode == BLINKER_CMD_MIOT_DAY) {
for(int i = 0; i < NUMPIXELS; i++){
pixels.setPixelColor(i, colorR, colorG, colorB);
}
};
if (wsMode == BLINKER_CMD_MIOT_COMPUTER) {
colorComputer();
};
if (wsMode == BLINKER_CMD_MIOT_COLOR) {
colorScan();
};
if (wsMode == BLINKER_CMD_MIOT_NIGHT) {
breath();
};
if (wsMode == BLINKER_CMD_MIOT_WARMTH) {
colorWarmth();
};
if (wsMode == BLINKER_CMD_MIOT_TV) {
colorTV();
};
}
void breath()//呼吸灯
{
for (int brt = 5; brt < 20 + 1; brt++) {
colorW = brt;
pixelShow();
delay(50);
}
for (int brt = 20; brt > 0 ; brt--) {
colorW = brt;
pixelShow();
delay(50);
}
}
void colorWipe(uint32_t c, uint8_t wait) {
for (uint16_t i = 0; i < pixels.numPixels(); i++) {
pixels.setPixelColor(i, c);
pixels.show();
delay(wait);
}
}
void colorScan()//跑马灯
{
colorW=255;
colorWipe(pixels.Color(255, 0, 0), 25); // Red
colorWipe(pixels.Color(0, 255, 0), 25); // Green
colorWipe(pixels.Color(0, 0, 255), 25);
colorWipe(pixels.Color(0, 255, 255), 25);
colorWipe(pixels.Color(255, 0, 255), 25);
colorWipe(pixels.Color(255, 0, 0), 25);
}
void colorWarmth()//温馨
{
pixels.Color(86, 77, 18);
delay (100);
pixels.Color(86, 37, 18);
delay (100);
}
void colorTV()
{ pixels.Color(18, 19, 86);
delay (100);
pixels.Color(18, 50, 86);
delay (100);
pixels.Color(18, 85, 86);
delay (100);
pixels.Color(18, 86, 47);
delay (100);
}
void colorComputer()//电脑
{
colorW=255;
colorWipe(pixels.Color(86,18, 61), 25);
colorWipe(pixels.Color(67, 18, 86), 25);
colorWipe(pixels.Color(28,18, 86), 25);
colorWipe(pixels.Color(18, 48, 86), 25);
colorWipe(pixels.Color(18,86, 82), 25);
colorWipe(pixels.Color(18, 86, 37), 25);
}
|
-
-
|