Доработка приложения
This commit is contained in:
@@ -9,7 +9,7 @@
|
|||||||
#include <Adafruit_NeoPixel.h>
|
#include <Adafruit_NeoPixel.h>
|
||||||
|
|
||||||
// WIFI
|
// WIFI
|
||||||
const char* ssid = "Cathouse";
|
const char* ssid = "SinceCats";
|
||||||
const char* password = "88888888";
|
const char* password = "88888888";
|
||||||
|
|
||||||
// CAMERA PINS
|
// CAMERA PINS
|
||||||
@@ -6,6 +6,8 @@
|
|||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
#include <BH1750.h>
|
#include <BH1750.h>
|
||||||
#include <ESP32Servo.h>
|
#include <ESP32Servo.h>
|
||||||
|
#include <Adafruit_GFX.h>
|
||||||
|
#include <Adafruit_SSD1306.h>
|
||||||
|
|
||||||
BH1750 lightMeter;
|
BH1750 lightMeter;
|
||||||
|
|
||||||
@@ -13,21 +15,22 @@ BH1750 lightMeter;
|
|||||||
|
|
||||||
float temperature = 0;
|
float temperature = 0;
|
||||||
float humidity = 0;
|
float humidity = 0;
|
||||||
|
float lux = 0;
|
||||||
|
|
||||||
|
String mqttStatus = "WAIT";
|
||||||
|
bool autoMode = false; // false = MANUAL
|
||||||
|
bool isNight = false;
|
||||||
|
String currentMode = "MANUAL";
|
||||||
|
|
||||||
WiFiClient espClient;
|
WiFiClient espClient;
|
||||||
PubSubClient client(espClient);
|
PubSubClient client(espClient);
|
||||||
|
|
||||||
const char* mqtt_server = "192.168.31.78";
|
const char* mqtt_server = "172.20.10.11";
|
||||||
|
|
||||||
// WIFI
|
// WIFI
|
||||||
const char* ssid = "Cathouse";
|
const char* ssid = "SinceCats";
|
||||||
const char* password = "88888888";
|
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
|
// CAMERA PINS
|
||||||
#define PWDN_GPIO_NUM -1
|
#define PWDN_GPIO_NUM -1
|
||||||
#define RESET_GPIO_NUM -1
|
#define RESET_GPIO_NUM -1
|
||||||
@@ -49,6 +52,17 @@ IPAddress dns(192,168,31,1);
|
|||||||
#define HREF_GPIO_NUM 7
|
#define HREF_GPIO_NUM 7
|
||||||
#define PCLK_GPIO_NUM 13
|
#define PCLK_GPIO_NUM 13
|
||||||
|
|
||||||
|
#define SCREEN_WIDTH 128
|
||||||
|
#define SCREEN_HEIGHT 64
|
||||||
|
|
||||||
|
Adafruit_SSD1306 display(
|
||||||
|
SCREEN_WIDTH,
|
||||||
|
SCREEN_HEIGHT,
|
||||||
|
&Wire,
|
||||||
|
-1
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
// HTTP SERVER
|
// HTTP SERVER
|
||||||
httpd_handle_t server = NULL;
|
httpd_handle_t server = NULL;
|
||||||
|
|
||||||
@@ -249,37 +263,57 @@ void setup()
|
|||||||
{
|
{
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
delay(1500);
|
delay(1500);
|
||||||
|
Serial.println("\n\n=== ESP32-S3-CAM START ===");
|
||||||
|
|
||||||
|
Serial.println("Init Camera...");
|
||||||
if (!initCamera())
|
if (!initCamera())
|
||||||
{
|
{
|
||||||
|
Serial.println("Camera init FAIL!");
|
||||||
delay(3000);
|
delay(3000);
|
||||||
ESP.restart();
|
ESP.restart();
|
||||||
}
|
}
|
||||||
|
Serial.println("Camera init DONE");
|
||||||
|
|
||||||
WiFi.config(local_IP, gateway, subnet, dns);
|
Serial.println("Connecting WiFi...");
|
||||||
WiFi.begin(ssid, password);
|
WiFi.begin(ssid, password);
|
||||||
WiFi.setSleep(false);
|
WiFi.setSleep(false);
|
||||||
|
|
||||||
while (WiFi.status() != WL_CONNECTED)
|
int attempts = 0;
|
||||||
{
|
while (WiFi.status() != WL_CONNECTED && attempts < 40) {
|
||||||
delay(300);
|
delay(500);
|
||||||
Serial.print(".");
|
Serial.print(".");
|
||||||
|
attempts++;
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.println("\nWIFI OK");
|
if (WiFi.status() == WL_CONNECTED) {
|
||||||
Serial.println(WiFi.localIP());
|
Serial.println("\nWIFI OK");
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
} else {
|
||||||
|
Serial.println("\nWIFI FAIL");
|
||||||
|
}
|
||||||
|
|
||||||
Wire.begin(1, 2);
|
Wire.begin(1, 2);
|
||||||
Wire.setClock(100000);
|
Wire.setClock(100000);
|
||||||
|
|
||||||
lightMeter.begin();
|
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);
|
client.setServer(mqtt_server, 1883);
|
||||||
|
|
||||||
startServer();
|
startServer();
|
||||||
|
|
||||||
Serial.println("STREAM READY:");
|
Serial.println("STREAM READY:");
|
||||||
Serial.println("http://192.168.31.235/stream");
|
Serial.println("http://172.20.10.2/stream");
|
||||||
|
|
||||||
strip.begin();
|
strip.begin();
|
||||||
strip.show();
|
strip.show();
|
||||||
@@ -297,10 +331,13 @@ void connectMQTT() {
|
|||||||
String id = "ESP32CAM-" + String(WiFi.macAddress());
|
String id = "ESP32CAM-" + String(WiFi.macAddress());
|
||||||
|
|
||||||
if (client.connect(id.c_str())) {
|
if (client.connect(id.c_str())) {
|
||||||
|
mqttStatus = "OK";
|
||||||
Serial.println("MQTT OK");
|
Serial.println("MQTT OK");
|
||||||
client.subscribe("esp32/led");
|
client.subscribe("esp32/led");
|
||||||
client.subscribe("esp32/servo");
|
client.subscribe("esp32/servo");
|
||||||
|
client.subscribe("esp32/mode");
|
||||||
} else {
|
} else {
|
||||||
|
mqttStatus = "FAIL";
|
||||||
Serial.println("MQTT FAIL");
|
Serial.println("MQTT FAIL");
|
||||||
delay(2000);
|
delay(2000);
|
||||||
}
|
}
|
||||||
@@ -355,7 +392,26 @@ void callback(char* topic, byte* payload, unsigned int length)
|
|||||||
Serial.print("MQTT: ");
|
Serial.print("MQTT: ");
|
||||||
Serial.println(msg);
|
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"){
|
if (msg == "OFF"){
|
||||||
setColor(0,0,0);
|
setColor(0,0,0);
|
||||||
return;
|
return;
|
||||||
@@ -365,7 +421,7 @@ void callback(char* topic, byte* payload, unsigned int length)
|
|||||||
setColor(r,g,b);
|
setColor(r,g,b);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (String(topic) == "esp32/servo")
|
if (String(topic) == "esp32/servo" && !autoMode)
|
||||||
{
|
{
|
||||||
// ДЕНЬ
|
// ДЕНЬ
|
||||||
if (msg == "DAY"){
|
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()
|
void loop()
|
||||||
{
|
{
|
||||||
if (!client.connected())
|
if (!client.connected())
|
||||||
@@ -392,15 +477,44 @@ void loop()
|
|||||||
client.loop();
|
client.loop();
|
||||||
|
|
||||||
static unsigned long lastMQTT = 0;
|
static unsigned long lastMQTT = 0;
|
||||||
|
static unsigned long lastOLED = 0;
|
||||||
|
|
||||||
|
// Датчики + MQTT каждые 5 секунд
|
||||||
if (millis() - lastMQTT > 5000)
|
if (millis() - lastMQTT > 5000)
|
||||||
{
|
{
|
||||||
lastMQTT = millis();
|
lastMQTT = millis();
|
||||||
|
|
||||||
float lux = lightMeter.readLightLevel();
|
lux = lightMeter.readLightLevel();
|
||||||
|
|
||||||
readAHT20(temperature, humidity);
|
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];
|
char buf[32];
|
||||||
|
|
||||||
snprintf(buf, sizeof(buf), "%.2f", lux);
|
snprintf(buf, sizeof(buf), "%.2f", lux);
|
||||||
@@ -424,5 +538,12 @@ void loop()
|
|||||||
Serial.println(humidity);
|
Serial.println(humidity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OLED раз в секунду
|
||||||
|
if (millis() - lastOLED > 1000)
|
||||||
|
{
|
||||||
|
lastOLED = millis();
|
||||||
|
updateOLED(lux);
|
||||||
|
}
|
||||||
|
|
||||||
vTaskDelay(2);
|
vTaskDelay(2);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user