ESP8266
一、简介
最近想着这做一个氛围灯,所以就在网上买了一个灯,将里面的电路进行了改装,今天就把我做的分享给大家
二、代码部分
注意修改代码的设备秘钥,char auth[] = "8dd8b63";/****设备秘钥****/
,代码支持小爱同学的控制,与智能配网,连接网络在一分钟内没有连接成功,将进入深度睡眠模式。按下复位开关后可进行重启设备再次连接
//WS2812氛围灯
#define BLINKER_PRINT Serial
#define BLINKER_MIOT_LIGHT
#define BLINKER_WIFI
#include <Blinker.h>
#include <Adafruit_NeoPixel.h>
#define BLINKER_BUTTON
#define BLINKER_BUTTON_PULLDOWN
#define BLINKER_BUTTON_LONGPRESS_POWERDOWN
//按键
#define key 13
char auth[] = "8dd8b63";/****设备秘钥****/
//RGB灯
#define PIN 15 // DIN PIN (GPIO15, D8)
#define NUMPIXELS 5 // Number of you led
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
//指示灯
#define led 12
// 新建组件对象
BlinkerRGB RGB1("RGB");
BlinkerButton Button1("BTN1"); //指示灯
BlinkerButton Button2("BTN2"); //rgb
int LED_R=0,LED_G=0,LED_B=0,LED_Bright=255;// RGB和亮度
bool WIFI_Status = true;
int colorT = 1000; //色温
char wsMode; //模式
//配网函数
void smartConfig()
{
WiFi.mode(WIFI_STA);
Serial.println("\r\nWait for Smartconfig...");
WiFi.beginSmartConfig();//等待手机端发出的用户名与密码
while (1)
{
Serial.print(".");
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
if (WiFi.smartConfigDone())//退出等待
{
Serial.println("SmartConfig Success");
Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
break;
}
}
}
//设置wifi函数
void WIFI_Set()
{
Serial.println("\r\n正在连接");
int count = 0;
while(WiFi.status()!=WL_CONNECTED)
{
if(WIFI_Status)
{
Serial.print(".");
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
count++;
if(count>=5)//5s后连接失败
{
WIFI_Status = false;
Serial.println("WiFi连接失败,请用手机进行配网");
}
}
else
{
smartConfig(); //微信智能配网
}
}
Serial.println("连接成功");
Serial.print("IP:");
Serial.println(WiFi.localIP());
}
//控制rgb灯
void SET_RGB(int R,int G,int B,int bright)
{
for (uint16_t i = 0; i < NUMPIXELS; i++) //把灯条变色
{
pixels.setPixelColor(i,R,G,B);
}
pixels.setBrightness(bright);//亮度
pixels.show(); //送出显示
}
//指示灯按键回调函数
void button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state=="on") {
digitalWrite(led, LOW);
// 反馈开关状态
Button1.print("on");
}
else if(state=="off"){
digitalWrite(led, HIGH);
// 反馈开关状态
Button1.print("off");
}
}
//RGB按键回调函数
void button2_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state=="on") {
SET_RGB(255,255,255,255);
SET_RGB(255,255,255,255);
// 反馈开关状态
Button2.print("on");
}
else if(state=="off"){
SET_RGB(0,0,0,0);
// 反馈开关状态
Button2.print("off");
}
}
//小爱电源类回调
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON)
{
SET_RGB(255,255,255,LED_Bright);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
}
else if (state == BLINKER_CMD_OFF)
{
SET_RGB(0,0,0,0);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
}
}
//APP RGB颜色设置回调
void rgb1_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);
LED_Bright = bright_value;
SET_RGB(r_value,g_value,b_value,LED_Bright);
}
//小爱设置颜色回调
void miotColor(int32_t color)
{
BLINKER_LOG("need set color: ", color);
int colorR,colorG,colorB;
colorR = color >> 16 & 0xFF;
colorG = color >> 8 & 0xFF;
colorB = color & 0xFF;
BLINKER_LOG("colorR: ", colorR, ", colorG: ", colorG, ", colorB: ", colorB);
SET_RGB(colorR,colorG,colorB,LED_Bright);
BlinkerMIOT.color(color);//反馈小爱控制状态
BlinkerMIOT.print();
}
//小爱调亮度回调
void miotBright(const String & bright)
{
BLINKER_LOG("need set brightness: ", bright);
int colorW = bright.toInt();
BLINKER_LOG("now set brightness: ", colorW);
LED_Bright = colorW;
SET_RGB(LED_R,LED_G,LED_B,LED_Bright);
Serial.printf("亮度调节中...%d",colorW);
BlinkerMIOT.brightness(colorW);//反馈小爱控制状态
BlinkerMIOT.print();
}
//色温控制 色温范围为1000-10000
void miotColoTemp(int32_t colorTemp)
{
BLINKER_LOG("need set colorTemperature: ", colorTemp);
colorT = colorTemp;
BlinkerMIOT.colorTemp(colorTemp);
BlinkerMIOT.print();
}
//小爱同学模式控制
void miotMode(uint8_t mode)
{
BLINKER_LOG("need set mode: ", mode);
//日光
if (mode == BLINKER_CMD_MIOT_DAY) {
SET_RGB(255,255,255,LED_Bright);
}
//月光
else if (mode == BLINKER_CMD_MIOT_NIGHT) {
SET_RGB(255,181,0,LED_Bright);
}
//彩光
else if (mode == BLINKER_CMD_MIOT_COLOR) {
pixels.setPixelColor(0,255,0,20);
pixels.setPixelColor(1,0,255,184);
pixels.setPixelColor(2,255,231,192);
pixels.setPixelColor(3,0,59,255);
pixels.setPixelColor(4,209,22,255);
pixels.setBrightness(LED_Bright);//亮度
pixels.show(); //送出显示
}
//温馨
else if (mode == BLINKER_CMD_MIOT_WARMTH) {
SET_RGB(255,38,0,LED_Bright);
}
//电视模式
else if (mode == BLINKER_CMD_MIOT_TV) {
// Your mode function
SET_RGB(255,0,154,LED_Bright);
}
//阅读模式
else if (mode == BLINKER_CMD_MIOT_READING) {
// Your mode function
SET_RGB(0,255,164,LED_Bright);
}
//电脑模式
else if (mode == BLINKER_CMD_MIOT_COMPUTER) {
// Your mode function
SET_RGB(255,0,0,LED_Bright);
}
wsMode = mode;
BlinkerMIOT.mode(mode);
BlinkerMIOT.print();
}
void setup() {
// 初始化串口
Serial.begin(115200);
pixels.begin();//WS2812初始化
pixels.show();
pinMode(led, OUTPUT);
pinMode(key, INPUT);
#if defined(BLINKER_PRINT)
BLINKER_DEBUG.stream(BLINKER_PRINT);
#endif
WIFI_Set();
Button1.print("on");
Button2.print("off");
//初始化blinker
Blinker.begin(auth, WiFi.SSID().c_str(), WiFi.psk().c_str());
Button1.attach(button1_callback);//Button1这个组件被触发就执行这个回调 指示灯
Button2.attach(button2_callback);//Button2这个组件被触发就执行这个回调 rgb
RGB1.attach(rgb1_callback);//注册调节颜色的回调函数
BlinkerMIOT.attachPowerState(miotPowerState);//小爱同学电源控制类
BlinkerMIOT.attachColor(miotColor);//小爱调节颜色
BlinkerMIOT.attachMode(miotMode);//小爱同学模式控制
BlinkerMIOT.attachColorTemperature(miotColoTemp);//小爱同学色温控制
BlinkerMIOT.attachBrightness(miotBright);//小爱调节RGB亮度
}
void loop() {
Blinker.run();
}
三、硬件电路
硬件部分如下所示
- 无线充电的发出板与接收模块
- ws2812 rgb灯5个
- 18650的锂电池
- 电池充电模块
- 3.7v转5v模块
- ESP8266 12F模块
- 按键和指示灯