Управление LED лентой с NODE-RED на ESP32

This commit is contained in:
kkira
2026-05-27 23:27:01 +03:00
parent bec5a96bed
commit e090f8f91e

View File

@@ -1,7 +1,7 @@
#include "esp_camera.h"
#include <WiFi.h>
#include "esp_http_server.h"
#include <Adafruit_NeoPixel.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <BH1750.h>
@@ -51,6 +51,14 @@ IPAddress dns(192,168,31,1);
// HTTP SERVER
httpd_handle_t server = NULL;
#define LED_PIN 21
#define LED_COUNT 8
Adafruit_NeoPixel strip(
LED_COUNT,
LED_PIN,
NEO_GRB + NEO_KHZ800
);
// STREAM HANDLER
static esp_err_t stream_handler(httpd_req_t *req)
@@ -268,6 +276,12 @@ void setup()
Serial.println("STREAM READY:");
Serial.println("http://192.168.31.235/stream");
strip.begin();
strip.show();
strip.setBrightness(80);
client.setCallback(callback);
}
@@ -277,6 +291,7 @@ void connectMQTT() {
if (client.connect(id.c_str())) {
Serial.println("MQTT OK");
client.subscribe("esp32/led");
} else {
Serial.println("MQTT FAIL");
delay(2000);
@@ -284,6 +299,42 @@ void connectMQTT() {
}
}
void setColor(int r, int g, int b)
{
for (int i = 0; i < LED_COUNT; i++)
{
strip.setPixelColor(i, strip.Color(r, g, b));
}
strip.show();
}
void callback(char* topic, byte* payload, unsigned int length)
{
String msg;
for (int i = 0; i < length; i++)
{
msg += (char)payload[i];
}
Serial.print("MQTT: ");
Serial.println(msg);
// OFF
if (msg == "OFF")
{
setColor(0,0,0);
return;
}
int r, g, b;
sscanf(msg.c_str(), "%d,%d,%d", &r, &g, &b);
setColor(r,g,b);
}
void loop()
{