From b0b832055e555bebf8d23c768f9ddef8729c3bb7 Mon Sep 17 00:00:00 2001 From: kkira Date: Tue, 16 Jun 2026 22:26:33 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=BF=D1=80=D0=B8=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{ => sketch_may14a}/sketch_may14a.ino | 2 +- .../{ => sketch_may14b}/sketch_may14b.ino | 155 ++++++++++++++++-- 2 files changed, 139 insertions(+), 18 deletions(-) rename autonomy_esp/{ => sketch_may14a}/sketch_may14a.ino (99%) rename esp_node-red_mqtt/{ => sketch_may14b}/sketch_may14b.ino (72%) diff --git a/autonomy_esp/sketch_may14a.ino b/autonomy_esp/sketch_may14a/sketch_may14a.ino similarity index 99% rename from autonomy_esp/sketch_may14a.ino rename to autonomy_esp/sketch_may14a/sketch_may14a.ino index faf3039..e4ced7a 100644 --- a/autonomy_esp/sketch_may14a.ino +++ b/autonomy_esp/sketch_may14a/sketch_may14a.ino @@ -9,7 +9,7 @@ #include // WIFI -const char* ssid = "Cathouse"; +const char* ssid = "SinceCats"; const char* password = "88888888"; // CAMERA PINS diff --git a/esp_node-red_mqtt/sketch_may14b.ino b/esp_node-red_mqtt/sketch_may14b/sketch_may14b.ino similarity index 72% rename from esp_node-red_mqtt/sketch_may14b.ino rename to esp_node-red_mqtt/sketch_may14b/sketch_may14b.ino index 495940b..3124db5 100644 --- a/esp_node-red_mqtt/sketch_may14b.ino +++ b/esp_node-red_mqtt/sketch_may14b/sketch_may14b.ino @@ -6,6 +6,8 @@ #include #include #include +#include +#include BH1750 lightMeter; @@ -13,21 +15,22 @@ BH1750 lightMeter; float temperature = 0; float humidity = 0; +float lux = 0; + +String mqttStatus = "WAIT"; +bool autoMode = false; // false = MANUAL +bool isNight = false; +String currentMode = "MANUAL"; WiFiClient espClient; PubSubClient client(espClient); -const char* mqtt_server = "192.168.31.78"; +const char* mqtt_server = "172.20.10.11"; // WIFI -const char* ssid = "Cathouse"; +const char* ssid = "SinceCats"; const char* password = "88888888"; -IPAddress local_IP(192,168,31,235); -IPAddress gateway(192,168,31,1); -IPAddress subnet(255,255,255,0); -IPAddress dns(192,168,31,1); - // CAMERA PINS #define PWDN_GPIO_NUM -1 #define RESET_GPIO_NUM -1 @@ -49,6 +52,17 @@ IPAddress dns(192,168,31,1); #define HREF_GPIO_NUM 7 #define PCLK_GPIO_NUM 13 +#define SCREEN_WIDTH 128 +#define SCREEN_HEIGHT 64 + +Adafruit_SSD1306 display( + SCREEN_WIDTH, + SCREEN_HEIGHT, + &Wire, + -1 +); + + // HTTP SERVER httpd_handle_t server = NULL; @@ -249,37 +263,57 @@ void setup() { Serial.begin(115200); delay(1500); + Serial.println("\n\n=== ESP32-S3-CAM START ==="); + Serial.println("Init Camera..."); if (!initCamera()) { + Serial.println("Camera init FAIL!"); delay(3000); ESP.restart(); } + Serial.println("Camera init DONE"); - WiFi.config(local_IP, gateway, subnet, dns); + Serial.println("Connecting WiFi..."); WiFi.begin(ssid, password); WiFi.setSleep(false); - while (WiFi.status() != WL_CONNECTED) - { - delay(300); + int attempts = 0; + while (WiFi.status() != WL_CONNECTED && attempts < 40) { + delay(500); Serial.print("."); + attempts++; } - Serial.println("\nWIFI OK"); - Serial.println(WiFi.localIP()); + if (WiFi.status() == WL_CONNECTED) { + Serial.println("\nWIFI OK"); + Serial.println(WiFi.localIP()); + } else { + Serial.println("\nWIFI FAIL"); + } Wire.begin(1, 2); Wire.setClock(100000); lightMeter.begin(); + if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) + { + Serial.println("OLED FAIL"); + } + else + { + display.clearDisplay(); + display.setTextSize(1); + display.setTextColor(SSD1306_WHITE); + } + client.setServer(mqtt_server, 1883); startServer(); Serial.println("STREAM READY:"); - Serial.println("http://192.168.31.235/stream"); + Serial.println("http://172.20.10.2/stream"); strip.begin(); strip.show(); @@ -297,10 +331,13 @@ void connectMQTT() { String id = "ESP32CAM-" + String(WiFi.macAddress()); if (client.connect(id.c_str())) { + mqttStatus = "OK"; Serial.println("MQTT OK"); client.subscribe("esp32/led"); client.subscribe("esp32/servo"); + client.subscribe("esp32/mode"); } else { + mqttStatus = "FAIL"; Serial.println("MQTT FAIL"); delay(2000); } @@ -355,7 +392,26 @@ void callback(char* topic, byte* payload, unsigned int length) Serial.print("MQTT: "); Serial.println(msg); - if (String(topic) == "esp32/led"){ + if (String(topic) == "esp32/mode") + { + if (msg == "AUTO") + { + autoMode = true; + currentMode = "AUTO"; + + Serial.println("AUTO MODE"); + } + + if (msg == "MANUAL") + { + autoMode = false; + currentMode = "MANUAL"; + setColor(0, 0, 0); + Serial.println("MANUAL MODE"); + } + } + + if (String(topic) == "esp32/led" && !autoMode){ if (msg == "OFF"){ setColor(0,0,0); return; @@ -365,7 +421,7 @@ void callback(char* topic, byte* payload, unsigned int length) setColor(r,g,b); } - if (String(topic) == "esp32/servo") + if (String(topic) == "esp32/servo" && !autoMode) { // ДЕНЬ if (msg == "DAY"){ @@ -384,6 +440,35 @@ void callback(char* topic, byte* payload, unsigned int length) } } +void updateOLED(float lux) +{ + display.clearDisplay(); + + display.setCursor(0,0); + + display.print("Light: "); + display.print((int)lux); + display.println(" lx"); + + display.print("Temp: "); + display.print(temperature,1); + display.println(" C"); + + display.print("Hum : "); + display.print(humidity,1); + display.println(" %"); + + display.println(); + + display.print("MQTT: "); + display.println(mqttStatus); + + display.print("MODE: "); + display.println(currentMode); + + display.display(); +} + void loop() { if (!client.connected()) @@ -392,15 +477,44 @@ void loop() client.loop(); static unsigned long lastMQTT = 0; + static unsigned long lastOLED = 0; + // Датчики + MQTT каждые 5 секунд if (millis() - lastMQTT > 5000) { lastMQTT = millis(); - float lux = lightMeter.readLightLevel(); + lux = lightMeter.readLightLevel(); readAHT20(temperature, humidity); + if (autoMode) + { + // НОЧЬ + if (lux < 200 && !isNight) + { + isNight = true; + + servoLeft(5000); + + setColor(0,0,40); + + Serial.println("AUTO NIGHT"); + } + + // ДЕНЬ + if (lux >= 200 && isNight) + { + isNight = false; + + servoRight(5000); + + setColor(40,25,10); + + Serial.println("AUTO DAY"); + } + } + char buf[32]; snprintf(buf, sizeof(buf), "%.2f", lux); @@ -424,5 +538,12 @@ void loop() Serial.println(humidity); } + // OLED раз в секунду + if (millis() - lastOLED > 1000) + { + lastOLED = millis(); + updateOLED(lux); + } + vTaskDelay(2); } \ No newline at end of file