ESP32 WS2812B RGB LED Strip with Color Picker Web Server
Installing the FastLED library
- Click here to download the FastLED library. You should have a .zip folder in your Downloads folder
- Unzip the .zip folder and you should get FastLED-master folder
- Rename your folder from
FastLED-masterto FastLED - Move the FastLED folder to your Arduino IDE installation libraries folder
- Finally, re-open your Arduino IDE
/*********
Rui Santos
Complete project details at http://randomnerdtutorials.com
*********/
/*
<fastLED> 所使用的指令如下 [下載點]
#include <fastLED.h>
引入I2C 的函示庫
CRGB 物件名稱[NUM_LEDS]
建構物件,物件名稱可自訂,括弧中的參數NUM_LED為串接的LED數量
FastLED.addLeds<WS2812B, DATA_PIN, RGB>(faya_colorSticker, NUM_LEDS);
設定串列全彩LED參數,其中
WS2812B : 控制LED模組的晶片型號,其他可支援的型號請參考函式庫說明
DATA_PIN : 控制LED模組的資料腳位
RGB : 控制LED模組RGB三原色的順序,須根據所連接的訊號線路調整
FastLED.setBrightness(BRIGHTNESS);
設定串列LED的亮度 (BRIGHTNESS範圍 : 0~255)
類別名稱[i] = CRGB(R值, G值, B值);
利用RGB顏色模型概念,設定第i顆LED的顏色,參數範圍如下
R值 : 0~255
G值 : 0~255
B值 : 0~255
類別名稱[i] = CHSV(H值, S值, V值);
利用HSV顏色模型概念,設定第i顆LED的顏色,參數範圍如下,參數原理解釋在範例裡
i : 選擇第i顆LED
H值 : 色相 (Hue) 0~255
S值 : 飽和度(Saturation) 0 ~ 255
V值 : 色調 (Value) 0~255
faya_colorSticker[i] = CHSV(led_cnt*4, 255, 255-+led_cnt); //決定第i顆的顏色
FastLED.show();
點亮已設定好顏色的LED
FastLED.clear();
熄滅所有的LED
*/
#include <FastLED.h>
#define LED_PIN 12
#define NUM_LEDS 24
#define BRIGHTNESS 64
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
#define UPDATES_PER_SECOND 100
// Load Wi-Fi library
#include <WiFi.h>
// Replace with your network credentials
//const char* ssid = "REPLACE_WITH_YOUR_SSID";
//const char* password = "REPLACE_WITH_YOUR_PASSWORD";
const char* ssid = "PTS-2F";
const char* password = "PTS6662594";
// Set web server port number to 80
WiFiServer server(80);
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;
// Decode HTTP GET value
String redString = "0";
String greenString = "0";
String blueString = "0";
int pos1 = 0;
int pos2 = 0;
int pos3 = 0;
int pos4 = 0;
// Variable to store the HTTP req uest
String header;
void setup() {
Serial.begin(115200);
delay( 3000 ); // power-up safety delay
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness( BRIGHTNESS );
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop(){
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
currentTime = millis();
previousTime = currentTime;
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
currentTime = millis();
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
client.println("<link rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css\">");
client.println("<script src=\"https://cdnjs.cloudflare.com/ajax/libs/jscolor/2.0.4/jscolor.min.js\"></script>");
client.println("</head><body><div class=\"container\"><div class=\"row\"><h1>ESP Color Picker</h1></div>");
client.println("<a class=\"btn btn-primary btn-lg\" href=\"#\" id=\"change_color\" role=\"button\">Change Color</a> ");
client.println("<input class=\"jscolor {onFineChange:'update(this)'}\" id=\"rgb\"></div>");
client.println("<script>function update(picker) {document.getElementById('rgb').innerHTML = Math.round(picker.rgb[0]) + ', ' + Math.round(picker.rgb[1]) + ', ' + Math.round(picker.rgb[2]);");
client.println("document.getElementById(\"change_color\").href=\"?r\" + Math.round(picker.rgb[0]) + \"g\" + Math.round(picker.rgb[1]) + \"b\" + Math.round(picker.rgb[2]) + \"&\";}</script></body></html>");
// The HTTP response ends with another blank line
client.println();
// Request sample: /?r201g32b255&
// Red = 201 | Green = 32 | Blue = 255
if(header.indexOf("GET /?r") >= 0) {
pos1 = header.indexOf('r');
pos2 = header.indexOf('g');
pos3 = header.indexOf('b');
pos4 = header.indexOf('&');
redString = header.substring(pos1+1, pos2);
greenString = header.substring(pos2+1, pos3);
blueString = header.substring(pos3+1, pos4);
Serial.println(redString.toInt());
Serial.println(greenString.toInt());
Serial.println(blueString.toInt());
//ledcWrite(redChannel, redString.toInt());
//ledcWrite(greenChannel, greenString.toInt());
//ledcWrite(blueChannel, blueString.toInt());
for(int i=0;i<NUM_LEDS;i++) //輪流讀表設定LED顏色
{
leds[i] = CRGB(redString.toInt(),greenString.toInt(),blueString.toInt());
FastLED.show();
delay(10);
}
}
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
//===================================
// Faya-Nugget 範例程式 (Color_Sticker_3.ino)
// 單元: 模組介紹:faya串列全彩LED模組 (使用fastLED函式庫)
// 網址: https://fayalab.blogspot.com/2018/04/colorstickerfastLED.html
// 目標: (1) 使用fastLED的CRGB函式,讓跑馬燈依序顯示16種顏色
// 接線: Arduino ==> faya模組
// D2 ==> 串列全彩LED_Din
//
// <fastLED> 所使用的指令如下 [下載點]
//#include <fastLED.h>
// 引入I2C 的函示庫
//CRGB 物件名稱[NUM_LEDS]
// 建構物件,物件名稱可自訂,括弧中的參數NUM_LED為串接的LED數量
// FastLED.addLeds<WS2812B, DATA_PIN, RGB>(faya_colorSticker, NUM_LEDS);
// 設定串列全彩LED參數,其中
// WS2812B : 控制LED模組的晶片型號,其他可支援的型號請參考函式庫說明
// DATA_PIN : 控制LED模組的資料腳位
// RGB : 控制LED模組RGB三原色的順序,須根據所連接的訊號線路調整
// FastLED.setBrightness(BRIGHTNESS);
// 設定串列LED的亮度 (BRIGHTNESS範圍 : 0~255)
//類別名稱[i] = CRGB(R值, G值, B值);
// 利用RGB顏色模型概念,設定第i顆LED的顏色,參數範圍如下
// R值 : 0~255
// G值 : 0~255
// B值 : 0~255
// 類別名稱[i] = CHSV(H值, S值, V值);
// 利用HSV顏色模型概念,設定第i顆LED的顏色,參數範圍如下,參數原理解釋在範例裡
// i : 選擇第i顆LED
// H值 : 色相 (Hue) 0~255
// S值 : 飽和度(Saturation) 0 ~ 255
// V值 : 色調 (Value) 0~255
// faya_colorSticker[i] = CHSV(led_cnt*4, 255, 255-+led_cnt); //決定第i顆的顏色
// FastLED.show();
// 點亮已設定好顏色的LED
//FastLED.clear();
// 熄滅所有的LED
#include "FastLED.h" //引入FastLED標頭黨
#define NUM_LEDS 16 //定義全彩LED數量
#define DATA_PIN 12 //定義全彩LED訊號腳位
#define BRIGHTNESS 32 //定義亮度
CRGB leds[NUM_LEDS]; //定義FastLED類別
int rainbow16[16][3]={{255, 0, 0}, //紅
{255, 85, 0}, //橙
{255, 170, 0}, //
{255, 255, 0}, //黃
{127, 255, 0}, //
{ 0, 255, 0}, //綠
{ 0, 255, 85}, //
{ 0, 255, 170}, //
{ 0, 255, 255}, //青
{ 0, 127, 255}, //
{ 0, 0, 255}, //藍
{ 85, 0, 255}, //紫
{170, 0, 255}, //
{255, 0, 255}, //洋紅
{255, 0, 127}, //
{255, 255, 255}}; //白
void setup() {
delay( 3000 ); // power-up safety delay
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS); //設定串列全彩LED參數
FastLED.setBrightness(BRIGHTNESS);
}
void loop()
{
showRainbow();
delay(500);
}
void showRainbow()
{
for(int i=0;i<NUM_LEDS;i++) //輪流讀表設定LED顏色
{
leds[i] = CRGB(rainbow16[i][0],rainbow16[i][1],rainbow16[i][2]);
FastLED.show();
delay(100);
}
FastLED.clear();
}
沒有留言:
張貼留言