From bec5a96bed2e4a9cba5756c09c7a16f1739525c5 Mon Sep 17 00:00:00 2001 From: kkira Date: Sat, 16 May 2026 20:23:46 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=B4=D0=B0=D1=87?= =?UTF-8?q?=D0=B0=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85=20=D1=81=20=D0=B4?= =?UTF-8?q?=D0=B0=D1=82=D1=87=D0=B8=D0=BA=D0=BE=D0=B2=20=D1=81=20ESP32=20?= =?UTF-8?q?=D0=B2=20NODE-RED=20(mqtt-broker:Mosquitto)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- esp_node-red_mqtt/sketch_may14b.ino | 126 +++++++++++++++++++++++++--- 1 file changed, 116 insertions(+), 10 deletions(-) diff --git a/esp_node-red_mqtt/sketch_may14b.ino b/esp_node-red_mqtt/sketch_may14b.ino index 2cb1df2..0b649f5 100644 --- a/esp_node-red_mqtt/sketch_may14b.ino +++ b/esp_node-red_mqtt/sketch_may14b.ino @@ -1,12 +1,22 @@ #include "esp_camera.h" #include #include "esp_http_server.h" -#include + +#include #include #include -#include -#include -#include + +BH1750 lightMeter; + +#define AHT20_ADDR 0x38 + +float temperature = 0; +float humidity = 0; + +WiFiClient espClient; +PubSubClient client(espClient); + +const char* mqtt_server = "192.168.31.78"; // WIFI const char* ssid = "Cathouse"; @@ -48,11 +58,9 @@ static esp_err_t stream_handler(httpd_req_t *req) camera_fb_t *fb = NULL; static const char* boundary = "--frame\r\n"; - static const char* header = - "Content-Type: image/jpeg\r\nContent-Length: %u\r\n\r\n"; + static const char* header = "Content-Type: image/jpeg\r\nContent-Length: %u\r\n\r\n"; - httpd_resp_set_type(req, - "multipart/x-mixed-replace; boundary=frame"); + httpd_resp_set_type(req, "multipart/x-mixed-replace; boundary=frame"); uint32_t start = millis(); @@ -158,8 +166,8 @@ bool initCamera() config.xclk_freq_hz = 20000000; config.pixel_format = PIXFORMAT_JPEG; - config.frame_size = FRAMESIZE_QVGA; - config.jpeg_quality = 16; + config.frame_size = FRAMESIZE_QQVGA; + config.jpeg_quality = 20; config.grab_mode = CAMERA_GRAB_LATEST; config.fb_count = 1; config.fb_location = CAMERA_FB_IN_DRAM; @@ -186,6 +194,45 @@ bool initCamera() } +bool readAHT20(float &temp, float &hum) +{ + uint8_t cmd[3] = {0xAC, 0x33, 0x00}; + + Wire.beginTransmission(AHT20_ADDR); + Wire.write(cmd, 3); + + if (Wire.endTransmission() != 0) + return false; + + delay(80); + + Wire.requestFrom(AHT20_ADDR, 6); + + if (Wire.available() < 6) + return false; + + uint8_t data[6]; + + for (int i = 0; i < 6; i++) + data[i] = Wire.read(); + + uint32_t h = + ((data[1] << 12) | + (data[2] << 4) | + (data[3] >> 4)); + + uint32_t t = + ((data[3] & 0x0F) << 16) | + (data[4] << 8) | + data[5]; + + hum = (h * 100.0) / 1048576.0; + temp = (t * 200.0 / 1048576.0) - 50; + + return true; +} + + void setup() { Serial.begin(115200); @@ -210,6 +257,13 @@ void setup() Serial.println("\nWIFI OK"); Serial.println(WiFi.localIP()); + Wire.begin(1, 2); + Wire.setClock(100000); + + lightMeter.begin(); + + client.setServer(mqtt_server, 1883); + startServer(); Serial.println("STREAM READY:"); @@ -217,7 +271,59 @@ void setup() } +void connectMQTT() { + while (!client.connected()) { + String id = "ESP32CAM-" + String(WiFi.macAddress()); + + if (client.connect(id.c_str())) { + Serial.println("MQTT OK"); + } else { + Serial.println("MQTT FAIL"); + delay(2000); + } + } +} + + void loop() { + if (!client.connected()) + connectMQTT(); + + client.loop(); + + static unsigned long lastMQTT = 0; + + if (millis() - lastMQTT > 5000) + { + lastMQTT = millis(); + + float lux = lightMeter.readLightLevel(); + + readAHT20(temperature, humidity); + + char buf[32]; + + snprintf(buf, sizeof(buf), "%.2f", lux); + client.publish("esp32/sensors/light", buf); + + snprintf(buf, sizeof(buf), "%.2f", temperature); + client.publish("esp32/sensors/temp", buf); + + snprintf(buf, sizeof(buf), "%.2f", humidity); + client.publish("esp32/sensors/humidity", buf); + + Serial.println("MQTT SEND"); + + Serial.print("Lux: "); + Serial.println(lux); + + Serial.print("Temp: "); + Serial.println(temperature); + + Serial.print("Hum: "); + Serial.println(humidity); + } + vTaskDelay(2); } \ No newline at end of file