Доработка приложения

This commit is contained in:
kkira
2026-06-16 22:26:33 +03:00
parent 405a93bcae
commit b0b832055e
2 changed files with 139 additions and 18 deletions

View File

@@ -9,7 +9,7 @@
#include <Adafruit_NeoPixel.h>
// WIFI
const char* ssid = "Cathouse";
const char* ssid = "SinceCats";
const char* password = "88888888";
// CAMERA PINS

View File

@@ -6,6 +6,8 @@
#include <Wire.h>
#include <BH1750.h>
#include <ESP32Servo.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
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++;
}
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);
}