RGB为三色灯珠,颜色为红,蓝,绿;
接线 所有灯阴极
接GED,蓝接GPIO15
绿接GPIO13
红接GPIO12
支持语音(变色、亮度调节,开关灯)
色温还有模式代码暂时不加,用不上,
如果装灯带可以使用MOS管作为驱动;
后期慢慢更多骚操作;
可以关注抖音 雨天实验室
[mw_shl_code=arduino,true]#define BLINKER_WIFI
#define BLINKER_MIOT_LIGHT
#define BLINKER_PRINT Serial
#include <Blinker.h>
uint8_t colorR, colorG, colorB, colorW=50;//默认亮度为50,防止亮瞎眼睛
char auth[] = "";//你的密钥
char ssid[] = "你的wifi名";
char pswd[] = "你的wifi密码";
// 新建组件对象
BlinkerButton Button1("yu");//一路继电器的键值
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);
analogWrite(15, colorB*colorW*0.041);//colorw为亮度值,colorB为蓝灯数值(0-255)esp8266的pwm为0-1024,换算一下公式
analogWrite(13, colorG*colorW*0.041);
analogWrite(12, colorR*colorW*0.041);
BlinkerMIOT.color(color);
BlinkerMIOT.print();
}
void miotBright(const String & bright)
{
BLINKER_LOG("need set brightness: ", bright);
colorW = bright.toInt();
BLINKER_LOG("now set brightness: ", colorW);
analogWrite(15,colorB*colorW*0.041);//在之前的变色调节亮度
analogWrite(13, colorG*colorW*0.041);
analogWrite(12, colorR*colorW*0.041);
BlinkerMIOT.brightness(colorW);
BlinkerMIOT.print();
}
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
analogWrite(15,512);//开机亮度为三色50
analogWrite(13, 512);
analogWrite(12,512);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
}
else if (state == BLINKER_CMD_OFF) {
analogWrite(15,0);
analogWrite(13, 0);
analogWrite(12,0);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
}
}
// 按下按键即会执行该函数
void button1_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
digitalWrite(2, !digitalRead(2));
}//继电器接口为2,要不要无所谓
// 如果未绑定的组件被触发,则会执行其中内容
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
}
void setup() {
// 初始化串口
Serial.begin(115200);
#if defined(BLINKER_PRINT)
BLINKER_DEBUG.stream(BLINKER_PRINT);
#endif
// 初始化有LED的IO
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Button1.attach(button1_callback);
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachColor(miotColor);
BlinkerMIOT.attachBrightness(miotBright);
}
void loop() {
Blinker.run();
}[/mw_shl_code]
|