代码拉取完成,页面将自动刷新
/* *****************************************************************
测试环境:
ESP8266nodemcu开发板 --工具-WIFIDUINO
ESP8266 sdk 2.7.4
Arduino 1.8.16
Blinker v0.3.80210527
更新日志:
2021112 HAPPY
20211117断电恢复时亮度问题、设备上线时显示模式、颜色亮度条指示
20211205 BACK
* *****************************************************************/
#include <EEPROM.h>
#define BLINKER_WIFI
#define BLINKER_APCONFIG
#define BLINKER_MIOT_LIGHT
#include <Blinker.h>
#include <WS2812FX.h>
#include <ArduinoOTA.h>
char auth[] = "2680effb9dfc";
#include "OneButton.h"
#define BLINKER_BUTTON_PIN 4
//char ssid[] = "do_connect()";
//char pswd[] = "#4com2net";
#define readHist //上电后会读取上次存储的模式-亮度-颜色-速度,如果不需要请注释掉!
#define LED_PIN 5 //ESP8266-Node MCU开发板 D1引脚
#define LED_COUNT 30 //灯珠数量
int Mode = 0; //初始模式 温馨提示:固定颜色为模式0
int Speed = 100; //初始速度 由于存储设置限制,实际时间为10倍数 毫秒
uint8_t colorW = 130; //初始亮度
uint8_t colorR = 139;//初始颜色RGB
uint8_t colorG = 0;
uint8_t colorB = 250;
bool wsState;
uint8_t wsMode = BLINKER_CMD_MIOT_DAY;//语音助手模式定义
OneButton button(BLINKER_BUTTON_PIN, true);
WS2812FX ws2812fx = WS2812FX(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
BlinkerRGB WS2812("RGB_Pad");
BlinkerSlider Slider_Speed("Speed");
BlinkerSlider Slider_Mode("Mode");
BlinkerText modeName("ModeName");
BlinkerText modeSaved("savedMode");
BlinkerText IPaddr("IPaddress");
BlinkerButton Button_Save("saveSetting");
BlinkerButton Button_Read("readSetting");
void deviceReset()
{
// Reset device ,erase WiFi config.
Blinker.reset();
}
void Speed_callback(int32_t value)
{
BLINKER_LOG("速度调整: ", value);
Speed = int(value);
ws2812fx.setSpeed(Speed * 10);
Slider_Speed.print(Speed);
}
void Mode_callback(int32_t value)
{
BLINKER_LOG("模式切换: ", value);
Mode = int(value);
ws2812fx.setMode(Mode);
Serial.println(ws2812fx.getModeName(Mode));
modeName.print("当前模式", String(ws2812fx.getModeName(Mode)));
String IPADDRESS = WiFi.localIP().toString();
IPaddr.print(IPADDRESS);
}
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;
ws2812fx.setBrightness(colorW);
ws2812fx.setColor(colorR, colorG, colorB);
String IPADDRESS = WiFi.localIP().toString();
IPaddr.print(IPADDRESS);
}
void save_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_BUTTON_TAP) {
EEPROM.begin(4096);
BLINKER_LOG("存储!");
EEPROM.write(2500, int(colorR));
delay(50);
EEPROM.write(2501, int(colorG));
delay(50);
EEPROM.write(2502, int(colorB));
delay(50);
EEPROM.write(2503, int(colorW));
delay(50);
EEPROM.write(2504, int(Speed));
delay(50);
EEPROM.write(2505, int(Mode));
delay(50);
EEPROM.commit();
if (EEPROM.commit()) {
Serial.println("模式存储成功!");
modeSaved.print("已存储模式", String(ws2812fx.getModeName(Mode)));
} else {
Serial.println("模式存储失败!");
}
}
}
void read_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_BUTTON_TAP) {
EEPROM.begin(4096);
BLINKER_LOG("读取!");
colorR = EEPROM.read(2500);
colorG = EEPROM.read(2501);
colorB = EEPROM.read(2502);
colorW = EEPROM.read(2503);
Speed = EEPROM.read(2504);
Mode = EEPROM.read(2505);
Serial.println(colorR);
Serial.println(colorG);
Serial.println(colorB);
Serial.println(colorW);
Serial.println(Speed);
Serial.println(Mode);
}
}
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) ws2812fx.setBrightness(150);
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(LED_BUILTIN, LOW);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
wsState = false;
ws2812fx.setBrightness(0);
}
}
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);
BlinkerMIOT.color(color);
BlinkerMIOT.print();
ws2812fx.setColor(colorR, colorG, colorB);
ws2812fx.setMode(0);//语音调整颜色固定色 0
WS2812.print(colorR, colorG, colorB);
}
/*
#define FX_MODE_STATIC 0
#define FX_MODE_BLINK 1
#define FX_MODE_BREATH 2
#define FX_MODE_COLOR_WIPE 3
#define FX_MODE_COLOR_WIPE_INV 4
#define FX_MODE_COLOR_WIPE_REV 5
#define FX_MODE_COLOR_WIPE_REV_INV 6
#define FX_MODE_COLOR_WIPE_RANDOM 7
#define FX_MODE_RANDOM_COLOR 8
#define FX_MODE_SINGLE_DYNAMIC 9
#define FX_MODE_MULTI_DYNAMIC 10
#define FX_MODE_RAINBOW 11
#define FX_MODE_RAINBOW_CYCLE 12
#define FX_MODE_SCAN 13
#define FX_MODE_DUAL_SCAN 14
#define FX_MODE_FADE 15
#define FX_MODE_THEATER_CHASE 16
#define FX_MODE_THEATER_CHASE_RAINBOW 17
#define FX_MODE_RUNNING_LIGHTS 18
#define FX_MODE_TWINKLE 19
#define FX_MODE_TWINKLE_RANDOM 20
#define FX_MODE_TWINKLE_FADE 21
#define FX_MODE_TWINKLE_FADE_RANDOM 22
#define FX_MODE_SPARKLE 23
#define FX_MODE_FLASH_SPARKLE 24
#define FX_MODE_HYPER_SPARKLE 25
#define FX_MODE_STROBE 26
#define FX_MODE_STROBE_RAINBOW 27
#define FX_MODE_MULTI_STROBE 28
#define FX_MODE_BLINK_RAINBOW 29
#define FX_MODE_CHASE_WHITE 30
#define FX_MODE_CHASE_COLOR 31
#define FX_MODE_CHASE_RANDOM 32
#define FX_MODE_CHASE_RAINBOW 33
#define FX_MODE_CHASE_FLASH 34
#define FX_MODE_CHASE_FLASH_RANDOM 35
#define FX_MODE_CHASE_RAINBOW_WHITE 36
#define FX_MODE_CHASE_BLACKOUT 37
#define FX_MODE_CHASE_BLACKOUT_RAINBOW 38
#define FX_MODE_COLOR_SWEEP_RANDOM 39
#define FX_MODE_RUNNING_COLOR 40
#define FX_MODE_RUNNING_RED_BLUE 41
#define FX_MODE_RUNNING_RANDOM 42
#define FX_MODE_LARSON_SCANNER 43
#define FX_MODE_COMET 44
#define FX_MODE_FIREWORKS 45
#define FX_MODE_FIREWORKS_RANDOM 46
#define FX_MODE_MERRY_CHRISTMAS 47
#define FX_MODE_FIRE_FLICKER 48
#define FX_MODE_FIRE_FLICKER_SOFT 49
#define FX_MODE_FIRE_FLICKER_INTENSE 50
#define FX_MODE_CIRCUS_COMBUSTUS 51
#define FX_MODE_HALLOWEEN 52
#define FX_MODE_BICOLOR_CHASE 53
#define FX_MODE_TRICOLOR_CHASE 54
#define FX_MODE_TWINKLEFOX 55
*/
void miotMode(uint8_t mode)//在下方根据上面注释修改自己喜欢的模式的编号
{
BLINKER_LOG("need set mode: ", mode);
if (mode == BLINKER_CMD_MIOT_DAY) {
Mode = 10;
}
else if (mode == BLINKER_CMD_MIOT_NIGHT) {
Mode = 20;
}
else if (mode == BLINKER_CMD_MIOT_COLOR) {
Mode = 0;
}
else if (mode == BLINKER_CMD_MIOT_WARMTH) {
Mode = 2;
}
else if (mode == BLINKER_CMD_MIOT_TV) {
Mode = 11;
}
else if (mode == BLINKER_CMD_MIOT_READING) {
Mode = 43;
}
else if (mode == BLINKER_CMD_MIOT_COMPUTER) {
Mode = 12;
}
ws2812fx.setMode(Mode);
wsMode = mode;
BlinkerMIOT.mode(mode);
BlinkerMIOT.print();
Slider_Mode.print(Mode);
modeName.print(ws2812fx.getModeName(Mode));
}
void miotBright(const String & bright)
{
BLINKER_LOG("need set brightness: ", bright);
colorW = bright.toInt();
BLINKER_LOG("now set brightness: ", colorW);
BlinkerMIOT.brightness(colorW);
BlinkerMIOT.print();
WS2812.brightness(colorW);
}
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)
{
String IPADDRESS = WiFi.localIP().toString();
IPaddr.print(IPADDRESS);
}
void setup()
{
delay(100);
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
//Blinker.begin(auth, ssid, pswd);
Blinker.begin(auth);
Blinker.attachData(dataRead);
#ifdef readHist
EEPROM.begin(4096);
Serial.println("已开启历史模式功能,读取历史设置数据中...");
if (EEPROM.read(2505) != 255)
{
colorR = EEPROM.read(2500);
delay(10);
colorG = EEPROM.read(2501);
delay(10);
colorB = EEPROM.read(2502);
delay(10);
colorW = EEPROM.read(2503);
delay(10);
Speed = EEPROM.read(2504);
delay(10);
Mode = EEPROM.read(2505);
delay(10);
for (int i = 0; i < 6; i++)
{
Serial.println(EEPROM.read(i + 2500));
}
}
if (EEPROM.read(2505) == 255)
{
Serial.println("读取存储模式失败...");
}
#endif
ws2812fx.init();
ws2812fx.setBrightness(colorW);
ws2812fx.setSpeed(Speed * 10);
ws2812fx.setColor(colorR, colorG, colorB);
ws2812fx.setMode(Mode);
ws2812fx.start();
Serial.println("模式:");
Serial.println(ws2812fx.getModeName(Mode));
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachColor(miotColor);
BlinkerMIOT.attachMode(miotMode);
BlinkerMIOT.attachBrightness(miotBright);
BlinkerMIOT.attachColorTemperature(miotColoTemp);
BlinkerMIOT.attachQuery(miotQuery);
Slider_Speed.attach(Speed_callback);
Slider_Mode.attach(Mode_callback);
WS2812.attach(ws2812_callback);
Button_Save.attach(save_callback);
Button_Read.attach(read_callback);
Slider_Mode.print(Mode);
modeName.print(ws2812fx.getModeName(Mode));
modeSaved.print(Mode);
Slider_Speed.print(Speed);
String IPADDRESS = WiFi.localIP().toString();
Serial.println(IPADDRESS);
IPaddr.print(IPADDRESS);
WS2812.brightness(colorW);
WS2812.print(colorR, colorG, colorB);
ArduinoOTA.begin();
button.attachLongPressStop(deviceReset);
}
void loop()
{
button.tick();
ArduinoOTA.handle();
Blinker.run();
ws2812fx.service();
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。