Course 9:RGB_Strip ===================== .. image:: _static/COURSE/26.RGB.png :align: center ---- Learning Objectives ------------------- - Learn the basic methods of using ESP32 to control RGB LED strips, understand their color mixing and data transmission principles, and implement web-based remote control functionality. ---- Required Component ------------------ - RGB Strip ---- Working Principle ----------------- - RGB strip integrate red, green, and blue LEDs with a control chip. Each LED can independently receive and parse 24-bit color data(8 bits R, 8 bits G, 8 bits B)from a single-wire serial bus(DIN pin). - The control chip distinguishes between "0" and "1" using strict timing signals(high and low level pulse width encoding)and sequentially transmits the data to the next LED, thus achieving pixel-by-pixel color control and dynamic lighting effects. ---- Wiring -------- - RGB Strip —— ESP32 IO5 .. image:: _static/COURSE/27.rgb.png :align: center ---- Example Code ------------ .. code-block:: cpp #include #include #include #include // ===== NeoPixel setup ===== const uint8_t LED_PIN = 5; // Data pin for the LED strip const uint16_t NUMPIXELS = 8; // Number of LEDs Adafruit_NeoPixel strip(NUMPIXELS, LED_PIN, NEO_GRB + NEO_KHZ800); // ===== Web server ===== WebServer server(80); // ===== Current state ===== bool stripOn = false; uint8_t colR = 255; uint8_t colG = 0; uint8_t colB = 0; // ===== WiFi Configuration ===== const char* apSSID = "RGB_Strip"; // Access Point SSID (no password) const char* apPassword = NULL; // No password String wifiSSID = ""; // Store target WiFi SSID String wifiPassword = ""; // Store target WiFi password bool isConfigMode = true; // Configuration mode flag bool wifiConnected = false; // WiFi connection status // ===== Preferences for storing WiFi credentials ===== Preferences preferences; // Forward declarations String htmlPage(); String configHTMLPage(); void handleRoot(); void handleSet(); void handleConfigure(); void applyColor(); bool connectToWiFi(); void setupAccessPoint(); void setup() { Serial.begin(115200); delay(200); strip.begin(); strip.show(); strip.setBrightness(150); // Initialize preferences preferences.begin("wifi-config", false); // Try to load saved WiFi credentials wifiSSID = preferences.getString("ssid", ""); wifiPassword = preferences.getString("password", ""); Serial.println("=== ESP32 RGB LED Controller ==="); if (wifiSSID != "" && connectToWiFi()) { // Successfully connected to WiFi isConfigMode = false; wifiConnected = true; Serial.println("Mode: Station (Connected to WiFi)"); } else { // Enter configuration mode (Access Point) isConfigMode = true; wifiConnected = false; setupAccessPoint(); Serial.println("Mode: Access Point (Configuration)"); } server.on("/", HTTP_GET, handleRoot); server.on("/set", HTTP_GET, handleSet); server.on("/configure", HTTP_POST, handleConfigure); server.begin(); Serial.println("Web server started!"); applyColor(); } void loop() { server.handleClient(); } // ===== Connect to WiFi ===== bool connectToWiFi() { if (wifiSSID == "") return false; Serial.println("Attempting to connect to WiFi: " + wifiSSID); WiFi.begin(wifiSSID.c_str(), wifiPassword.c_str()); int attempts = 0; while (WiFi.status() != WL_CONNECTED && attempts < 20) { delay(500); Serial.print("."); attempts++; } if (WiFi.status() == WL_CONNECTED) { Serial.println("\nWiFi connected successfully!"); Serial.println("IP address: " + WiFi.localIP().toString()); return true; } else { Serial.println("\nFailed to connect to WiFi"); return false; } } // ===== Setup Access Point ===== void setupAccessPoint() { Serial.println("Setting up Access Point..."); WiFi.softAP(apSSID, apPassword); Serial.println("Access Point started"); Serial.println("SSID: " + String(apSSID)); Serial.println("Password: None (Open Network)"); Serial.println("IP address: " + WiFi.softAPIP().toString()); } void handleRoot() { if (isConfigMode) { server.send(200, "text/html", configHTMLPage()); } else { server.send(200, "text/html", htmlPage()); } } void handleSet() { if (server.hasArg("on")) { String s = server.arg("on"); stripOn = (s == "1" || s == "true"); } if (server.hasArg("r")) colR = (uint8_t)constrain(server.arg("r").toInt(), 0, 255); if (server.hasArg("g")) colG = (uint8_t)constrain(server.arg("g").toInt(), 0, 255); if (server.hasArg("b")) colB = (uint8_t)constrain(server.arg("b").toInt(), 0, 255); applyColor(); server.send(200, "text/plain", "OK"); } void handleConfigure() { wifiSSID = server.arg("ssid"); wifiPassword = server.arg("password"); // Save credentials to preferences preferences.putString("ssid", wifiSSID); preferences.putString("password", wifiPassword); server.send(200, "text/html", "

Connecting to WiFi...

" "

SSID: " + wifiSSID + "

" "

Device will restart and attempt connection.

" "" ""); delay(2000); ESP.restart(); } void applyColor() { if (stripOn) { uint32_t c = strip.Color(colR, colG, colB); for (uint16_t i = 0; i < NUMPIXELS; i++) strip.setPixelColor(i, c); } else { for (uint16_t i = 0; i < NUMPIXELS; i++) strip.setPixelColor(i, 0); } strip.show(); } // ===== Configuration Web Page HTML ===== String configHTMLPage() { String html = R"rawliteral( ESP32 WiFi Configuration

WiFi Configuration

)rawliteral"; return html; } // ===== Control Web Page HTML ===== String htmlPage() { String html = R"rawliteral( ESP32 RGB Controller

ESP32 RGB LED Controller

Current State: OFF
)rawliteral"; return html; } ---- **Code burning options** 1. You can directly copy the code provided above into the Arduino IDE for burning. 2. Find the **9.RGB_Strip.ino** file in the provided folder, download it, open it with the **Arduino IDE**, and burn the program to the ESP32 development board. 3. Find the **9.RGB_Strip.bin** file in the provided folder, download it and use **Flash Download Tool** to flash the program to the ESP32 development board. ---- Effects Demonstration --------------------- 1. The web control page includes: a power button(to turn the RGB lights on and off), an RGB slider(to adjust the color), a color preview area, and a status display(ON / OFF). .. image:: _static/COURSE/28.rgb.png :width: 800 :align: center ----