From 819725f22a41e711af847e5fe952b54b9020724b Mon Sep 17 00:00:00 2001 From: kkira Date: Sat, 16 May 2026 18:57:07 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20=D0=BF=D0=B5=D1=80=D0=B5=D0=B4=D0=B0=D1=87?= =?UTF-8?q?=D0=B8=20=D0=B2=D0=B8=D0=B4=D0=B5=D0=BE=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 | 223 ++++++++++++++++++++++++++++ 1 file changed, 223 insertions(+) create mode 100644 esp_node-red_mqtt/sketch_may14b.ino diff --git a/esp_node-red_mqtt/sketch_may14b.ino b/esp_node-red_mqtt/sketch_may14b.ino new file mode 100644 index 0000000..2cb1df2 --- /dev/null +++ b/esp_node-red_mqtt/sketch_may14b.ino @@ -0,0 +1,223 @@ +#include "esp_camera.h" +#include +#include "esp_http_server.h" +#include +#include +#include +#include +#include +#include + +// WIFI +const char* ssid = "Cathouse"; +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 + +#define XCLK_GPIO_NUM 15 +#define SIOD_GPIO_NUM 4 +#define SIOC_GPIO_NUM 5 + +#define Y9_GPIO_NUM 16 +#define Y8_GPIO_NUM 17 +#define Y7_GPIO_NUM 18 +#define Y6_GPIO_NUM 12 +#define Y5_GPIO_NUM 10 +#define Y4_GPIO_NUM 8 +#define Y3_GPIO_NUM 9 +#define Y2_GPIO_NUM 11 + +#define VSYNC_GPIO_NUM 6 +#define HREF_GPIO_NUM 7 +#define PCLK_GPIO_NUM 13 + +// HTTP SERVER +httpd_handle_t server = NULL; + + +// STREAM HANDLER +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"; + + httpd_resp_set_type(req, + "multipart/x-mixed-replace; boundary=frame"); + + uint32_t start = millis(); + + while (true) + { + if (millis() - start > 60000) + { + break; + } + + fb = esp_camera_fb_get(); + + if (!fb) + { + vTaskDelay(2); + continue; + } + + char part[64]; + int hlen = snprintf(part, sizeof(part), header, fb->len); + + if (httpd_resp_send_chunk(req, boundary, strlen(boundary)) != ESP_OK) + { + esp_camera_fb_return(fb); + break; + } + + if (httpd_resp_send_chunk(req, part, hlen) != ESP_OK) + { + esp_camera_fb_return(fb); + break; + } + + if (httpd_resp_send_chunk(req, (const char*)fb->buf, fb->len) != ESP_OK) + { + esp_camera_fb_return(fb); + break; + } + + esp_camera_fb_return(fb); + + vTaskDelay(2); + } + + return ESP_OK; +} + + +// START SERVER +void startServer() +{ + httpd_config_t config = HTTPD_DEFAULT_CONFIG(); + + config.server_port = 80; + config.stack_size = 8192; + config.max_uri_handlers = 8; + config.recv_wait_timeout = 10; + config.send_wait_timeout = 10; + + if (httpd_start(&server, &config) == ESP_OK) + { + httpd_uri_t uri = { + .uri = "/stream", + .method = HTTP_GET, + .handler = stream_handler, + .user_ctx = NULL + }; + + httpd_register_uri_handler(server, &uri); + + Serial.println("HTTP STREAM READY"); + } +} + + +bool initCamera() +{ + camera_config_t config; + + config.ledc_channel = LEDC_CHANNEL_0; + config.ledc_timer = LEDC_TIMER_0; + + config.pin_d0 = Y2_GPIO_NUM; + config.pin_d1 = Y3_GPIO_NUM; + config.pin_d2 = Y4_GPIO_NUM; + config.pin_d3 = Y5_GPIO_NUM; + config.pin_d4 = Y6_GPIO_NUM; + config.pin_d5 = Y7_GPIO_NUM; + config.pin_d6 = Y8_GPIO_NUM; + config.pin_d7 = Y9_GPIO_NUM; + + config.pin_xclk = XCLK_GPIO_NUM; + config.pin_pclk = PCLK_GPIO_NUM; + config.pin_vsync = VSYNC_GPIO_NUM; + config.pin_href = HREF_GPIO_NUM; + + config.pin_sccb_sda = SIOD_GPIO_NUM; + config.pin_sccb_scl = SIOC_GPIO_NUM; + + config.pin_pwdn = PWDN_GPIO_NUM; + config.pin_reset = RESET_GPIO_NUM; + + config.xclk_freq_hz = 20000000; + config.pixel_format = PIXFORMAT_JPEG; + + config.frame_size = FRAMESIZE_QVGA; + config.jpeg_quality = 16; + config.grab_mode = CAMERA_GRAB_LATEST; + config.fb_count = 1; + config.fb_location = CAMERA_FB_IN_DRAM; + + esp_err_t err = esp_camera_init(&config); + + if (err != ESP_OK) + { + Serial.printf("Camera init FAIL: 0x%x\n", err); + return false; + } + + sensor_t *s = esp_camera_sensor_get(); + + s->set_vflip(s, 1); + s->set_brightness(s, 1); + s->set_whitebal(s, 1); + s->set_awb_gain(s, 1); + s->set_exposure_ctrl(s, 1); + s->set_gain_ctrl(s, 1); + + Serial.println("CAMERA OK"); + return true; +} + + +void setup() +{ + Serial.begin(115200); + delay(1500); + + if (!initCamera()) + { + delay(3000); + ESP.restart(); + } + + WiFi.config(local_IP, gateway, subnet, dns); + WiFi.begin(ssid, password); + WiFi.setSleep(false); + + while (WiFi.status() != WL_CONNECTED) + { + delay(300); + Serial.print("."); + } + + Serial.println("\nWIFI OK"); + Serial.println(WiFi.localIP()); + + startServer(); + + Serial.println("STREAM READY:"); + Serial.println("http://192.168.31.235/stream"); +} + + +void loop() +{ + vTaskDelay(2); +} \ No newline at end of file