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

This commit is contained in:
kkira
2026-05-27 23:54:24 +03:00
parent e090f8f91e
commit 527ea7cd24

View File

@@ -5,6 +5,7 @@
#include <PubSubClient.h> #include <PubSubClient.h>
#include <Wire.h> #include <Wire.h>
#include <BH1750.h> #include <BH1750.h>
#include <ESP32Servo.h>
BH1750 lightMeter; BH1750 lightMeter;
@@ -54,6 +55,9 @@ httpd_handle_t server = NULL;
#define LED_PIN 21 #define LED_PIN 21
#define LED_COUNT 8 #define LED_COUNT 8
#define SERVO_PIN 47
Servo myServo;
Adafruit_NeoPixel strip( Adafruit_NeoPixel strip(
LED_COUNT, LED_COUNT,
LED_PIN, LED_PIN,
@@ -174,7 +178,7 @@ bool initCamera()
config.xclk_freq_hz = 20000000; config.xclk_freq_hz = 20000000;
config.pixel_format = PIXFORMAT_JPEG; config.pixel_format = PIXFORMAT_JPEG;
config.frame_size = FRAMESIZE_QQVGA; config.frame_size = FRAMESIZE_QVGA;
config.jpeg_quality = 20; config.jpeg_quality = 20;
config.grab_mode = CAMERA_GRAB_LATEST; config.grab_mode = CAMERA_GRAB_LATEST;
config.fb_count = 1; config.fb_count = 1;
@@ -282,6 +286,9 @@ void setup()
strip.setBrightness(80); strip.setBrightness(80);
client.setCallback(callback); client.setCallback(callback);
myServo.attach(SERVO_PIN);
servoStop();
} }
@@ -292,6 +299,7 @@ void connectMQTT() {
if (client.connect(id.c_str())) { if (client.connect(id.c_str())) {
Serial.println("MQTT OK"); Serial.println("MQTT OK");
client.subscribe("esp32/led"); client.subscribe("esp32/led");
client.subscribe("esp32/servo");
} else { } else {
Serial.println("MQTT FAIL"); Serial.println("MQTT FAIL");
delay(2000); delay(2000);
@@ -301,39 +309,79 @@ void connectMQTT() {
void setColor(int r, int g, int b) void setColor(int r, int g, int b)
{ {
for (int i = 0; i < LED_COUNT; i++) for (int i = 0; i < LED_COUNT; i++){
{
strip.setPixelColor(i, strip.Color(r, g, b)); strip.setPixelColor(i, strip.Color(r, g, b));
} }
strip.show(); strip.show();
} }
void servoStop()
{
myServo.write(92);
}
void servoLeft(int ms)
{
myServo.write(30);
unsigned long start = millis();
while (millis() - start < ms)
{
client.loop();
delay(10);
}
servoStop();
}
void servoRight(int ms)
{
myServo.write(150);
unsigned long start = millis();
while (millis() - start < ms)
{
client.loop();
delay(10);
}
servoStop();
}
void callback(char* topic, byte* payload, unsigned int length) void callback(char* topic, byte* payload, unsigned int length)
{ {
String msg; String msg;
for (int i = 0; i < length; i++){
for (int i = 0; i < length; i++)
{
msg += (char)payload[i]; msg += (char)payload[i];
} }
Serial.print("MQTT: "); Serial.print("MQTT: ");
Serial.println(msg); Serial.println(msg);
// OFF if (String(topic) == "esp32/led"){
if (msg == "OFF") if (msg == "OFF"){
{ setColor(0,0,0);
setColor(0,0,0); return;
return; }
int r,g,b;
sscanf(msg.c_str(), "%d,%d,%d", &r,&g,&b);
setColor(r,g,b);
} }
int r, g, b; if (String(topic) == "esp32/servo")
{
// ДЕНЬ
if (msg == "DAY"){
servoRight(10000);
}
sscanf(msg.c_str(), "%d,%d,%d", &r, &g, &b); // НОЧЬ
if (msg == "NIGHT"){
servoLeft(10000);
}
setColor(r,g,b); // ПОЛДЕНЬ
if (msg == "MID"){
servoRight(5000);
}
}
} }
void loop() void loop()