Skip to content

ESP32-Cam http "refused to connect" after ESPHome update #6117

@ZhuDaHai

Description

@ZhuDaHai

The problem

I was running ESP32-CAM on ESPHome with an old version of ESPHome. It was working properly.
I upgraded EPSHome to 2024.7.3, which resulted in EPSHome complaining about my ESP devices needed an update.
After updating my 4 other devices, I then updated my ESP32-Cam.
The update was a success. Logs connect and show output, but I can no longer connect to it via is http URL. I get "refused to connect" when trying the URL in my web browser and TinyCam cannot connect as well. Generic Camera in HA cannot connect as well.

Which version of ESPHome has the issue?

2024..7.3

What type of installation are you using?

Home Assistant Add-on

Which version of Home Assistant has the issue?

Core: 2024.7.4, Supervisor: 2024.06.2, OS: 12.4: Frontend: 202407010.0

What platform are you using?

ESP32

Board

ESP32-CAM

Component causing the issue

esp32_camera ??? IDK

Example YAML snippet

esphome:
  name: esp32-cam
  friendly_name: ESP32-Cam

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
#  encryption:
#    key: "T0qrCJJ9HoWm9yhPwn9gzXocAvmYRWCyAkFTsmQVH/U="

ota:
#  password: "c6083ee9877b850b9b6582cabb663c9b"
  - platform: esphome

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  manual_ip:
    static_ip: 172.27.3.57
    gateway: 172.27.3.3
    subnet: 255.255.255.0

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esp32-Cam Fallback Hotspot"
    password: "JzbBlLm22Olx"

captive_portal:

esp32_camera:
  external_clock:
    pin: GPIO0
    frequency: 20MHz
  i2c_pins:
    sda: GPIO26
    scl: GPIO27
  data_pins: [GPIO5, GPIO18, GPIO19, GPIO21, GPIO36, GPIO39, GPIO34, GPIO35]

  # the order of the data_pins is significant, don't mix up the order

  vsync_pin: GPIO25
  href_pin: GPIO23
  pixel_clock_pin: GPIO22
  power_down_pin: GPIO32
  resolution: 800x600
  name: esp_cam02
  idle_framerate: 0.1fps
  aec_mode: auto
  ae_level: 1
  agc_mode: auto
  agc_gain_ceiling: 4x

output:
  - platform: ledc
    pin: GPIO4
    channel: 2 # channel 1 is used for esp32_camera
    id: led
light:
  - platform: monochromatic
    output: led
    name: espcam_02 light

Anything in the logs that might be useful for us?

/*********
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp32-cam-video-streaming-web-server-camera-home-assistant/
  
  IMPORTANT!!! 
   - Select Board "AI Thinker ESP32-CAM"
   - GPIO 0 must be connected to GND to upload a sketch
   - After connecting GPIO 0 to GND, press the ESP32-CAM on-board RESET button to put your board in flashing mode
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*********/

#include "esp_camera.h"
#include <WiFi.h>
#include "esp_timer.h"
#include "img_converters.h"
#include "Arduino.h"
#include "fb_gfx.h"
#include "soc/soc.h" //disable brownout problems
#include "soc/rtc_cntl_reg.h"  //disable brownout problems
#include "esp_http_server.h"

//Replace with your network credentials
const char* ssid = "Goober";
const char* password = "Head";

#define PART_BOUNDARY "123456789000000000000987654321"

// This project was tested with the AI Thinker Model, M5STACK PSRAM Model and M5STACK WITHOUT PSRAM
#define CAMERA_MODEL_AI_THINKER
//#define CAMERA_MODEL_M5STACK_PSRAM
//#define CAMERA_MODEL_M5STACK_WITHOUT_PSRAM

// Not tested with this model
//#define CAMERA_MODEL_WROVER_KIT

#if defined(CAMERA_MODEL_WROVER_KIT)
  #define PWDN_GPIO_NUM    -1
  #define RESET_GPIO_NUM   -1
  #define XCLK_GPIO_NUM    21
  #define SIOD_GPIO_NUM    26
  #define SIOC_GPIO_NUM    27
  
  #define Y9_GPIO_NUM      35
  #define Y8_GPIO_NUM      34
  #define Y7_GPIO_NUM      39
  #define Y6_GPIO_NUM      36
  #define Y5_GPIO_NUM      19
  #define Y4_GPIO_NUM      18
  #define Y3_GPIO_NUM       5
  #define Y2_GPIO_NUM       4
  #define VSYNC_GPIO_NUM   25
  #define HREF_GPIO_NUM    23
  #define PCLK_GPIO_NUM    22

#elif defined(CAMERA_MODEL_M5STACK_PSRAM)
  #define PWDN_GPIO_NUM     -1
  #define RESET_GPIO_NUM    15
  #define XCLK_GPIO_NUM     27
  #define SIOD_GPIO_NUM     25
  #define SIOC_GPIO_NUM     23
  
  #define Y9_GPIO_NUM       19
  #define Y8_GPIO_NUM       36
  #define Y7_GPIO_NUM       18
  #define Y6_GPIO_NUM       39
  #define Y5_GPIO_NUM        5
  #define Y4_GPIO_NUM       34
  #define Y3_GPIO_NUM       35
  #define Y2_GPIO_NUM       32
  #define VSYNC_GPIO_NUM    22
  #define HREF_GPIO_NUM     26
  #define PCLK_GPIO_NUM     21

#elif defined(CAMERA_MODEL_M5STACK_WITHOUT_PSRAM)
  #define PWDN_GPIO_NUM     -1
  #define RESET_GPIO_NUM    15
  #define XCLK_GPIO_NUM     27
  #define SIOD_GPIO_NUM     25
  #define SIOC_GPIO_NUM     23
  
  #define Y9_GPIO_NUM       19
  #define Y8_GPIO_NUM       36
  #define Y7_GPIO_NUM       18
  #define Y6_GPIO_NUM       39
  #define Y5_GPIO_NUM        5
  #define Y4_GPIO_NUM       34
  #define Y3_GPIO_NUM       35
  #define Y2_GPIO_NUM       17
  #define VSYNC_GPIO_NUM    22
  #define HREF_GPIO_NUM     26
  #define PCLK_GPIO_NUM     21

#elif defined(CAMERA_MODEL_AI_THINKER)
  #define PWDN_GPIO_NUM     32
  #define RESET_GPIO_NUM    -1
  #define XCLK_GPIO_NUM      0
  #define SIOD_GPIO_NUM     26
  #define SIOC_GPIO_NUM     27
  
  #define Y9_GPIO_NUM       35
  #define Y8_GPIO_NUM       34
  #define Y7_GPIO_NUM       39
  #define Y6_GPIO_NUM       36
  #define Y5_GPIO_NUM       21
  #define Y4_GPIO_NUM       19
  #define Y3_GPIO_NUM       18
  #define Y2_GPIO_NUM        5
  #define VSYNC_GPIO_NUM    25
  #define HREF_GPIO_NUM     23
  #define PCLK_GPIO_NUM     22
#else
  #error "Camera model not selected"
#endif

static const char* _STREAM_CONTENT_TYPE = "multipart/x-mixed-replace;boundary=" PART_BOUNDARY;
static const char* _STREAM_BOUNDARY = "\r\n--" PART_BOUNDARY "\r\n";
static const char* _STREAM_PART = "Content-Type: image/jpeg\r\nContent-Length: %u\r\n\r\n";

httpd_handle_t stream_httpd = NULL;

static esp_err_t stream_handler(httpd_req_t *req){
  camera_fb_t * fb = NULL;
  esp_err_t res = ESP_OK;
  size_t _jpg_buf_len = 0;
  uint8_t * _jpg_buf = NULL;
  char * part_buf[64];

  res = httpd_resp_set_type(req, _STREAM_CONTENT_TYPE);
  if(res != ESP_OK){
    return res;
  }

  while(true){
    fb = esp_camera_fb_get();
    if (!fb) {
      Serial.println("Camera capture failed");
      res = ESP_FAIL;
    } else {
      if(fb->width > 400){
        if(fb->format != PIXFORMAT_JPEG){
          bool jpeg_converted = frame2jpg(fb, 80, &_jpg_buf, &_jpg_buf_len);
          esp_camera_fb_return(fb);
          fb = NULL;
          if(!jpeg_converted){
            Serial.println("JPEG compression failed");
            res = ESP_FAIL;
          }
        } else {
          _jpg_buf_len = fb->len;
          _jpg_buf = fb->buf;
        }
      }
    }
    if(res == ESP_OK){
      size_t hlen = snprintf((char *)part_buf, 64, _STREAM_PART, _jpg_buf_len);
      res = httpd_resp_send_chunk(req, (const char *)part_buf, hlen);
    }
    if(res == ESP_OK){
      res = httpd_resp_send_chunk(req, (const char *)_jpg_buf, _jpg_buf_len);
    }
    if(res == ESP_OK){
      res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
    }
    if(fb){
      esp_camera_fb_return(fb);
      fb = NULL;
      _jpg_buf = NULL;
    } else if(_jpg_buf){
      free(_jpg_buf);
      _jpg_buf = NULL;
    }
    if(res != ESP_OK){
      break;
    }
    //Serial.printf("MJPG: %uB\n",(uint32_t)(_jpg_buf_len));
  }
  return res;
}

void startCameraServer(){
  httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  config.server_port = 80;

  httpd_uri_t index_uri = {
    .uri       = "/",
    .method    = HTTP_GET,
    .handler   = stream_handler,
    .user_ctx  = NULL
  };
  
  //Serial.printf("Starting web server on port: '%d'\n", config.server_port);
  if (httpd_start(&stream_httpd, &config) == ESP_OK) {
    httpd_register_uri_handler(stream_httpd, &index_uri);
  }
}

void setup() {
  WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
 
  Serial.begin(115200);
  Serial.setDebugOutput(false);
  
  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_sscb_sda = SIOD_GPIO_NUM;
  config.pin_sscb_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; 
  
  if(psramFound()){
    config.frame_size = FRAMESIZE_UXGA;
    config.jpeg_quality = 10;
    config.fb_count = 2;
  } else {
    config.frame_size = FRAMESIZE_SVGA;
    config.jpeg_quality = 12;
    config.fb_count = 1;
  }
  
  // Camera init
  esp_err_t err = esp_camera_init(&config);
  if (err != ESP_OK) {
    Serial.printf("Camera init failed with error 0x%x", err);
    return;
  }
  // Wi-Fi connection
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  
  Serial.print("Camera Stream Ready! Go to: http://");
  Serial.print(WiFi.localIP());
  
  // Start streaming web server
  startCameraServer();
}

void loop() {
  delay(1);
}

Additional information


INFO ESPHome 2024.7.3
INFO Reading configuration /config/esphome/esp32-cam.yaml...
WARNING GPIO0 is a strapping PIN and should only be used for I/O with care.
Attaching external pullup/down resistors to strapping pins can cause unexpected failures.
See https://esphome.io/guides/faq.html#why-am-i-getting-a-warning-about-strapping-pins
WARNING GPIO5 is a strapping PIN and should only be used for I/O with care.
Attaching external pullup/down resistors to strapping pins can cause unexpected failures.
See https://esphome.io/guides/faq.html#why-am-i-getting-a-warning-about-strapping-pins
INFO Generating C++ source...
INFO Compiling app...
Processing esp32-cam (board: esp32dev; framework: arduino; platform: platformio/[email protected])
--------------------------------------------------------------------------------
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
 - toolchain-xtensa-esp32 @ 8.4.0+2021r2-patch5
Dependency Graph
|-- AsyncTCP-esphome @ 2.1.3
|-- WiFi @ 2.0.0
|-- FS @ 2.0.0
|-- Update @ 2.0.0
|-- ESPAsyncWebServer-esphome @ 3.2.2
|-- DNSServer @ 2.0.0
|-- ESPmDNS @ 2.0.0
Compiling .pioenvs/esp32-cam/src/main.cpp.o
Linking .pioenvs/esp32-cam/firmware.elf
RAM:   [=         ]  13.9% (used 45444 bytes from 327680 bytes)
Flash: [=====     ]  51.8% (used 951261 bytes from 1835008 bytes)
Building .pioenvs/esp32-cam/firmware.bin
Creating esp32 image...
Successfully created esp32 image.
esp32_create_combined_bin([".pioenvs/esp32-cam/firmware.bin"], [".pioenvs/esp32-cam/firmware.elf"])
Wrote 0xf9a70 bytes to file /data/build/esp32-cam/.pioenvs/esp32-cam/firmware.factory.bin, ready to flash to offset 0x0
esp32_copy_ota_bin([".pioenvs/esp32-cam/firmware.bin"], [".pioenvs/esp32-cam/firmware.elf"])
========================= [SUCCESS] Took 15.11 seconds =========================
INFO Successfully compiled program.
INFO Connecting to 172.27.3.57
INFO Uploading /data/build/esp32-cam/.pioenvs/esp32-cam/firmware.bin (957040 bytes)
Uploading: [============================================================] 100% Done...

INFO Upload took 9.98 seconds, waiting for result...
INFO OTA successful
INFO Successfully uploaded program.
INFO Starting log output from 172.27.3.57 using esphome API
INFO Successfully connected to esp32-cam @ 172.27.3.57 in 11.261s
INFO Successful handshake with esp32-cam @ 172.27.3.57 in 0.029s
[12:23:22][I][app:100]: ESPHome version 2024.7.3 compiled on Aug  4 2024, 12:22:46
[12:23:22][C][wifi:599]: WiFi:
[12:23:22][C][wifi:427]:   Local MAC: 24:0A:C4:2A:A0:38
[12:23:22][C][wifi:432]:   SSID: [redacted]
[12:23:22][C][wifi:435]:   IP Address: 172.27.3.57
[12:23:22][C][wifi:439]:   BSSID: [redacted]
[12:23:22][C][wifi:440]:   Hostname: 'esp32-cam'
[12:23:22][C][wifi:442]:   Signal strength: -52 dB ▂▄▆█
[12:23:22][C][wifi:446]:   Channel: 1
[12:23:22][C][wifi:447]:   Subnet: 255.255.255.0
[12:23:22][C][wifi:448]:   Gateway: 172.27.3.3
[12:23:22][C][wifi:449]:   DNS1: 0.0.0.0
[12:23:22][C][wifi:450]:   DNS2: 0.0.0.0
[12:23:22][C][logger:185]: Logger:
[12:23:22][C][logger:186]:   Level: DEBUG
[12:23:22][C][logger:188]:   Log Baud Rate: 115200
[12:23:22][C][logger:189]:   Hardware UART: UART0
[12:23:22][C][ledc.output:176]: LEDC Output:
[12:23:22][C][ledc.output:177]:   Pin GPIO4
[12:23:22][C][ledc.output:178]:   LEDC Channel: 2
[12:23:22][C][ledc.output:179]:   PWM Frequency: 1000.0 Hz
[12:23:22][C][ledc.output:180]:   Phase angle: 0.0°
[12:23:22][C][ledc.output:181]:   Bit depth: 16
[12:23:22][C][light:103]: Light 'espcam_02 light'
[12:23:22][C][light:105]:   Default Transition Length: 1.0s
[12:23:22][C][light:106]:   Gamma Correct: 2.80
[12:23:22][C][esp32_camera:048]: ESP32 Camera:
[12:23:22][C][esp32_camera:049]:   Name: esp_cam02
[12:23:22][C][esp32_camera:050]:   Internal: NO
[12:23:22][C][esp32_camera:052]:   Data Pins: D0:5 D1:18 D2:19 D3:21 D4:36 D5:39 D6:34 D7:35
[12:23:22][C][esp32_camera:053]:   VSYNC Pin: 25
[12:23:22][C][esp32_camera:054]:   HREF Pin: 23
[12:23:22][C][esp32_camera:055]:   Pixel Clock Pin: 22
[12:23:22][C][esp32_camera:056]:   External Clock: Pin:0 Frequency:20000000
[12:23:22][C][esp32_camera:060]:   I2C Pins: SDA:26 SCL:27
[12:23:22][C][esp32_camera:062]:   Reset Pin: -1
[12:23:22][C][esp32_camera:083]:   Resolution: 800x600 (SVGA)
[12:23:22][C][esp32_camera:129]:   JPEG Quality: 10
[12:23:22][C][esp32_camera:131]:   Contrast: 0
[12:23:22][C][esp32_camera:132]:   Brightness: 0
[12:23:22][C][esp32_camera:133]:   Saturation: 0
[12:23:22][C][esp32_camera:134]:   Vertical Flip: ON
[12:23:22][C][esp32_camera:135]:   Horizontal Mirror: ON
[12:23:22][C][esp32_camera:136]:   Special Effect: 0
[12:23:22][C][esp32_camera:137]:   White Balance Mode: 0
[12:23:22][C][esp32_camera:140]:   Auto Exposure Control: 1
[12:23:22][C][esp32_camera:141]:   Auto Exposure Control 2: 0
[12:23:22][C][esp32_camera:142]:   Auto Exposure Level: 1
[12:23:22][C][esp32_camera:143]:   Auto Exposure Value: 300
[12:23:22][C][esp32_camera:144]:   AGC: 1
[12:23:22][C][esp32_camera:145]:   AGC Gain: 0
[12:23:22][C][esp32_camera:146]:   Gain Ceiling: 1
[12:23:22][C][esp32_camera:152]:   Test Pattern: NO
[12:23:22][C][psram:020]: PSRAM:
[12:23:22][C][psram:021]:   Available: YES
[12:23:22][C][psram:024]:   Size: 4095 KB
[12:23:22][C][captive_portal:088]: Captive Portal:
[12:23:22][C][mdns:116]: mDNS:
[12:23:22][C][mdns:117]:   Hostname: esp32-cam
[12:23:22][C][esphome.ota:073]: Over-The-Air updates:
[12:23:22][C][esphome.ota:074]:   Address: 172.27.3.57:3232
[12:23:22][C][esphome.ota:075]:   Version: 2
[12:23:22][C][safe_mode:018]: Safe Mode:
[12:23:22][C][safe_mode:020]:   Boot considered successful after 60 seconds
[12:23:22][C][safe_mode:021]:   Invoke after 10 boot attempts
[12:23:22][C][safe_mode:023]:   Remain in safe mode for 300 seconds
[12:23:22][C][api:139]: API Server:
[12:23:22][C][api:140]:   Address: 172.27.3.57:6053
[12:23:22][C][api:144]:   Using noise encryption: NO
[12:23:30][D][esp32_camera:196]: Got Image: len=18795
[12:23:40][D][esp32_camera:196]: Got Image: len=18866
[12:23:50][D][esp32_camera:196]: Got Image: len=18893

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions