Already working codes doesn't work any more.

brigei
Posts: 5
Joined: Fri Dec 30, 2022 8:12 pm

Already working codes doesn't work any more.

Postby brigei » Sat Jan 07, 2023 8:49 pm

Hi,

below is my code which already worked. Unfortunately now I get this error.

##############################################
CORRUPT HEAP: Bad head at 0x3f85dc2c. Expected 0xabba1234 got 0x3f9ffff4
abort() was called at PC 0x4008a41d on core 1

ELF file SHA256: 0000000000000000

Backtrace: 0x4008de14:0x3ffdca10 0x4008e08d:0x3ffdca30 0x4008a41d:0x3ffdca50 0x4008a549:0x3ffdca80 0x400fd3db:0x3ffdcaa0 0x400f8eb5:0x3ffdcd60 0x400f8dbd:0x3ffdcdb0 0x400933dd:0x3ffdcde0 0x40082746:0x3ffdce00 0x4008a315:0x3ffdce20 0x4000bec7:0x3ffdce40 0x400d1152:0x3ffdce60 0x4011b9dd:0x3ffdcf80 0x4011c535:0x3ffdcfb0 0x4011c5ed:0x3ffdd040 0x4011bc48:0x3ffdd060 0x4011acc0:0x3ffdd080 0x4011ad17:0x3ffdd0c0 0x4008fd1e:0x3ffdd0e0

Rebooting...
##############################################
  1. #include "esp_camera.h"
  2. #include <WiFi.h>
  3. #include "soc/soc.h"
  4. #include "soc/rtc_cntl_reg.h"
  5. #include <LITTLEFS.h>
  6. #include <FS.h>
  7. #include "esp_timer.h"
  8. #include "Arduino.h"
  9. #include "fb_gfx.h"
  10. #include "esp_http_server.h"
  11.  
  12. #define led 33
  13. #define PART_BOUNDARY "123456789000000000000987654321"
  14.  
  15. const char* ssid = "Mi Casa";
  16. const char* password = "W1xldgmnuweadWs!";
  17.  
  18. static const char* _STREAM_CONTENT_TYPE = "multipart/x-mixed-replace;boundary=" PART_BOUNDARY;
  19. static const char* _STREAM_BOUNDARY = "\r\n--" PART_BOUNDARY "\r\n";
  20. static const char* _STREAM_PART = "Content-Type: image/jpeg\r\nContent-Length: %u\r\n\r\n";
  21.  
  22. httpd_handle_t stream_httpd = NULL;
  23.  
  24. static esp_err_t stream_handler(httpd_req_t *req){
  25.   camera_fb_t * fb = NULL;
  26.   esp_err_t res = ESP_OK;
  27.   size_t _jpg_buf_len = 0;
  28.   uint8_t * _jpg_buf = NULL;
  29.   char * part_buf[64];
  30.  
  31.   res = httpd_resp_set_type(req, _STREAM_CONTENT_TYPE);
  32.   if(res != ESP_OK){
  33.     return res;
  34.   }
  35.  
  36.   while(true){
  37.     fb = esp_camera_fb_get();
  38.     _jpg_buf_len = fb->len;
  39.     _jpg_buf = fb->buf;
  40.  
  41.     if(res == ESP_OK){
  42.       size_t hlen = snprintf((char *)part_buf, 64, _STREAM_PART, _jpg_buf_len);
  43.       res = httpd_resp_send_chunk(req, (const char *)part_buf, hlen);
  44.     }
  45.     if(res == ESP_OK){
  46.       res = httpd_resp_send_chunk(req, (const char *)_jpg_buf, _jpg_buf_len);
  47.     }
  48.     if(res == ESP_OK){
  49.       res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
  50.     }
  51.  
  52.     esp_camera_fb_return(fb);
  53.     fb = NULL;
  54.     free(_jpg_buf);
  55.     _jpg_buf = NULL;
  56.  
  57.     if(res != ESP_OK){
  58.       break;
  59.     }
  60.   }
  61.   return res;
  62. }
  63.  
  64. void startCameraServer(){
  65.   httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  66.   config.server_port = 80;
  67.  
  68.   httpd_uri_t index_uri = {
  69.     .uri       = "/",
  70.     .method    = HTTP_GET,
  71.     .handler   = stream_handler,
  72.     .user_ctx  = NULL
  73.   };
  74.  
  75.   Serial.printf("Starting web server on port: '%d'\n", config.server_port);
  76.   if (httpd_start(&stream_httpd, &config) == ESP_OK) {
  77.     httpd_register_uri_handler(stream_httpd, &index_uri);
  78.   }
  79. }
  80.  
  81. void setup()
  82. {
  83.   WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); // prevent brownouts by silencing them
  84.  
  85.   Serial.begin(115200);
  86.   Serial.setDebugOutput(true);
  87.   Serial.println();
  88.   pinMode(led, OUTPUT);
  89.  
  90.   LITTLEFS.begin(true);
  91.  
  92.   WiFi.begin(ssid, password);
  93.   Serial.print("WLAN Verbindungsaufbau...");
  94.   while (WiFi.status() != WL_CONNECTED)
  95.   {
  96.       Serial.print(".");
  97.       delay(200);
  98.   }
  99.   Serial.println();
  100.   Serial.println("Verbunden");
  101.   Serial.print("IP-Adresse: ");
  102.   Serial.println(WiFi.localIP());
  103.  
  104.   camera_config_t config;
  105.   config.ledc_channel = LEDC_CHANNEL_0;
  106.   config.ledc_timer = LEDC_TIMER_0;
  107.   config.pin_d0 = 5;
  108.   config.pin_d1 = 18;
  109.   config.pin_d2 = 19;
  110.   config.pin_d3 = 21;
  111.   config.pin_d4 = 36;
  112.   config.pin_d5 = 39;
  113.   config.pin_d6 = 34;
  114.   config.pin_d7 = 35;
  115.   config.pin_xclk = 0;
  116.   config.pin_pclk = 22;
  117.   config.pin_vsync = 25;
  118.   config.pin_href = 23;
  119.   config.pin_sscb_sda = 26;
  120.   config.pin_sscb_scl = 27;
  121.   config.pin_pwdn = 32;
  122.   config.pin_reset = -1;
  123.   config.xclk_freq_hz = 20000000;
  124.   config.pixel_format = PIXFORMAT_JPEG;
  125.   config.frame_size = FRAMESIZE_UXGA;
  126.   config.jpeg_quality = 10;
  127.   config.fb_count = 2;
  128.  
  129.   esp_err_t err = esp_camera_init(&config);
  130.   if (err != ESP_OK) {
  131.     Serial.printf("Camera init failed with error 0x%x", err);
  132.     return;
  133.   }
  134.   else {
  135.     Serial.println("Camera initialisiert.");
  136.   }
  137.  
  138.   sensor_t * s = esp_camera_sensor_get();
  139.   s->set_framesize(s, FRAMESIZE_UXGA);
  140.   s->set_vflip(s, 0);
  141.   s->set_hmirror(s, 0);
  142.  
  143.   startCameraServer();
  144.  
  145. }
  146.  
  147.  
  148. void loop()
  149. {
  150.   delay(1000);
  151. }
  152.  
Last edited by brigei on Sun Jan 08, 2023 12:55 pm, edited 1 time in total.

ESP_Sprite
Posts: 9727
Joined: Thu Nov 26, 2015 4:08 am

Re: Already working codes doesn't work any more.

Postby ESP_Sprite » Sun Jan 08, 2023 3:04 am

You're free()'ing _jpg_buf while you do not own the memory it points to (it's a pointer to a member of fb, and that memory gets freed with esp_camera_fb_return())

brigei
Posts: 5
Joined: Fri Dec 30, 2022 8:12 pm

Re: Already working codes doesn't work any more.

Postby brigei » Sun Jan 08, 2023 1:03 pm

Hi,

Thanks for your reply. That‘s it!

Greetings.

PS: Now I just have to find out how to close this post ;)

Who is online

Users browsing this forum: No registered users and 88 guests