mbedTLS heap error

Ahmetsafi
Posts: 10
Joined: Wed Aug 07, 2024 12:01 pm

mbedTLS heap error

Postby Ahmetsafi » Mon Aug 26, 2024 11:30 am

Hi i have problems with mbedtls. i am not using mbedTLS APIs directly but i am using certificated MQTT connection. I use AWS IoT Core certificates. i added them manually. Here is output of my code:
I (30) boot: ESP-IDF v5.1.4-dirty 2nd stage bootloader
I (30) boot: compile time Aug 26 2024 11:28:06
I (30) boot: chip revision: v0.3
I (34) boot.esp32c3: SPI Speed : 80MHz
I (39) boot.esp32c3: SPI Mode : DIO
I (43) boot.esp32c3: SPI Flash Size : 4MB
I (48) boot: Enabling RNG early entropy source...
I (53) boot: Partition Table:
I (57) boot: ## Label Usage Type ST Offset Length
I (64) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (72) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (79) boot: 2 factory factory app 00 00 00010000 00200000
I (87) boot: End of partition table
I (91) esp_image: segment 0: paddr=00010020 vaddr=3c120020 size=40588h (263560) map
I (142) esp_image: segment 1: paddr=000505b0 vaddr=3fc97600 size=034dch ( 13532) load
I (145) esp_image: segment 2: paddr=00053a94 vaddr=40380000 size=0c584h ( 50564) load
I (157) esp_image: segment 3: paddr=00060020 vaddr=42000020 size=11f858h (1177688) map
I (349) esp_image: segment 4: paddr=0017f880 vaddr=4038c584 size=0ae7ch ( 44668) load
I (358) esp_image: segment 5: paddr=0018a704 vaddr=50000010 size=00004h ( 4) load
I (358) esp_image: segment 6: paddr=0018a710 vaddr=50000018 size=00010h ( 16) load
I (371) boot: Loaded app from partition at offset 0x10000
I (371) boot: Disabling RNG early entropy source...
I (386) cpu_start: Unicore app
I (387) cpu_start: Pro cpu up.
I (395) cpu_start: Pro cpu start user code
I (395) cpu_start: cpu freq: 160000000 Hz
I (395) cpu_start: Application information:
I (398) cpu_start: Project name: i2c_sensor_read
I (404) cpu_start: App version: 1
I (408) cpu_start: Compile time: Aug 26 2024 11:28:03
I (414) cpu_start: ELF file SHA256: f938281dec373981...
I (420) cpu_start: ESP-IDF: v5.1.4-dirty
I (426) cpu_start: Min chip rev: v0.3
I (430) cpu_start: Max chip rev: v1.99
I (435) cpu_start: Chip rev: v0.3
I (440) heap_init: Initializing. RAM available for dynamic allocation:
I (447) heap_init: At 3FCA2B90 len 0001D470 (117 KiB): DRAM
I (454) heap_init: At 3FCC0000 len 0001C710 (113 KiB): DRAM/RETENTION
I (461) heap_init: At 3FCDC710 len 00002950 (10 KiB): DRAM/RETENTION/STACK
I (468) heap_init: At 50000028 len 00001FC0 (7 KiB): RTCRAM
I (475) spi_flash: detected chip: generic
I (479) spi_flash: flash io: dio
I (483) sleep: Configure to isolate all GPIO pins in sleep state
I (490) sleep: Enable automatic switching of GPIO sleep configuration
I (497) coexist: coex firmware version: d96c1e51f
I (502) coexist: coexist rom version 9387209
I (508) app_start: Starting scheduler on CPU0
I (512) main_task: Started on CPU0
I (512) main_task: Calling app_main()
I (512) SENSOR: Wake up from timer. Time spent in deep sleep: 125 ms
I (532) SENSOR: I2C initialized successfully
I (532) pp: pp rom version: 9387209
I (532) net80211: net80211 rom version: 9387209
I (552) wifi:wifi driver task: 3fcaca20, prio:23, stack:6656, core=0
I (552) wifi:wifi firmware version: 3ce09e5
I (552) wifi:wifi certification version: v7.0
I (552) wifi:config NVS flash: enabled
I (552) wifi:config nano formating: disabled
I (562) wifi:Init data frame dynamic rx buffer num: 32
I (562) wifi:Init static rx mgmt buffer num: 5
I (572) wifi:Init management short buffer num: 32
I (572) wifi:Init dynamic tx buffer num: 32
I (572) wifi:Init static tx FG buffer num: 2
I (582) wifi:Init static rx buffer size: 1600
I (582) wifi:Init static rx buffer num: 10
I (592) wifi:Init dynamic rx buffer num: 32
I (592) wifi_init: rx ba win: 6
I (592) wifi_init: tcpip mbox: 32
I (602) wifi_init: udp mbox: 6
I (602) wifi_init: tcp mbox: 6
I (602) wifi_init: tcp tx win: 5760
I (612) wifi_init: tcp rx win: 5760
I (612) wifi_init: tcp mss: 1440
I (622) wifi_init: WiFi IRAM OP enabled
I (622) wifi_init: WiFi RX IRAM OP enabled
I (632) phy_init: phy_version 1170,f4aea9b,Apr 30 2024,10:49:24
I (672) wifi:mode : sta (58:cf:79:14:a7:ac)
I (672) wifi:enable tsf
W (672) SCD4X: PERIYODIK ÖLÇÜM DURDURULDU.
I (682) wifi:new:<12,0>, old:<1,0>, ap:<255,255>, sta:<12,0>, prof:1
I (682) wifi:state: init -> auth (b0)
I (682) wifi:state: auth -> assoc (0)
I (692) wifi:state: assoc -> run (10)
I (732) wifi:connected with OPPO Reno4 Lite, aid = 1, channel 12, BW20, bssid = fa:d4:63:3b:e3:ef
I (742) wifi:security: WPA2-PSK, phy: bgn, rssi: -43
I (742) wifi:pm start, type: 1

I (742) wifi:dp: 1, bi: 102400, li: 3, scale listen interval from 307200 us to 307200 us
I (752) wifi:set rx beacon pti, rx_bcn_pti: 14, bcn_timeout: 25000, mt_pti: 14, mt_time: 10000
I (762) INTERNET-CHECK: Connected to the Wi-Fi network.
I (762) INTERNET-CHECK: IP Address for config.target_addr: 8.8.8.8
E (772) ping_sock: send error=0
I (782) wifi:dp: 2, bi: 102400, li: 4, scale listen interval from 307200 us to 409600 us
I (782) wifi:AP's beacon interval = 102400 us, DTIM period = 2
I (1172) SCD4X: Serial Number: 4cb7948b072f
I (1172) SENSOR: timer wakeup source is ready
I (1172) BLE_INIT: BT controller compile version [30b57c4]
I (1172) BLE_INIT: Bluetooth MAC: 58:cf:79:14:a7:ae

I (1202) BLE_GENERAL: create attribute table successfully, the number handle = 5

I (1202) BLE_GENERAL: SERVICE_START_EVT, status 0, service_handle 40
I (1202) BLE_GENERAL: (0) ***** advertising start successfully *****

I (1762) esp_netif_handlers: sta ip: 192.168.46.7, mask: 255.255.255.0, gw: 192.168.46.2
I (1772) INTERNET-CHECK: Ping attempt 1 failed.
I (2772) INTERNET-CHECK: Ping attempt 2 failed.
I (2892) INTERNET-CHECK: 64 bytes from 8.8.8.8 icmp_seq=3 ttl=54 time=121 ms
I (3642) wifi:<ba-add>idx:0 (ifx:0, fa:d4:63:3b:e3:ef), tid:0, ssn:16, winSize:64
I (4822) MQTT_CLIENT: MQTT_EVENT_CONNECTED
I (6212) main_task: Returned from app_main()
I (6262) SCD4X: Raw data: 09 C7 92 6B A4 F6 6A 64 45
I (6262) SENSOR: CO2: 2503 ppm, Temperature: 28.58 C, Humidity: 41.56%
I (6522) MQTT_CLIENT: MQTT_EVENT_PUBLISHED, msg_id=23024
Active Time: 320295
I (6532) MQTT_CLIENT: Data sent, entering light sleep mode
I (6632) wifi:bcn_timeout,ap_probe_send_start
I (6682) SCD4X: Raw data: 0A 41 63 6A 3B 15 71 9A AC
I (6682) SENSOR: CO2: 2625 ppm, Temperature: 27.62 C, Humidity: 44.38%
E (7642) mqtt_client: No PING_RESP, disconnected
W (7642) MQTT_CLIENT: MQTT_EVENT_DISCONNECTED, attempting to reconnect...
I (7642) mqtt_client: Client force reconnect requested
I (14302) wifi:state: run -> init (7c0)
I (14312) wifi:pm stop, total sleep time: 10867327 us / 73614418 us

I (14312) wifi:<ba-del>idx:0, tid:0
I (14312) wifi:new:<12,0>, old:<12,0>, ap:<255,255>, sta:<12,0>, prof:1
I (14312) INTERNET-CHECK: Disconnected from Wi-Fi. Attempting to reconnect (1/5)...
E (14322) esp-tls: [sock=54] delayed connect error: Software caused connection abort
E (14332) esp-tls: Failed to open new connection
E (14342) transport_base: Failed to open a new connection
E (14342) mqtt_client: Error transport connect
E (14352) MQTT_CLIENT: MQTT_EVENT_ERROR
E (14352) MQTT_CLIENT: Last error reported from esp-tls: 0x8004
E (14362) MQTT_CLIENT: Last error captured as transport's socket errno: 0x71
I (14372) MQTT_CLIENT: Last errno string (Software caused connection abort)
W (14372) MQTT_CLIENT: MQTT_EVENT_DISCONNECTED, attempting to reconnect...
I (14382) mqtt_client: Client force reconnect requested
I (14372) wifi:new:<12,0>, old:<12,0>, ap:<255,255>, sta:<12,0>, prof:1
I (14392) wifi:state: init -> auth (b0)
I (14402) wifi:state: auth -> assoc (0)
I (14412) wifi:state: assoc -> run (10)
I (14442) wifi:connected with OPPO Reno4 Lite, aid = 1, channel 12, BW20, bssid = fa:d4:63:3b:e3:ef
I (14442) wifi:security: WPA2-PSK, phy: bgn, rssi: -37
I (14452) wifi:pm start, type: 1

I (14452) wifi:dp: 1, bi: 102400, li: 3, scale listen interval from 307200 us to 307200 us
I (14462) wifi:set rx beacon pti, rx_bcn_pti: 14, bcn_timeout: 25000, mt_pti: 14, mt_time: 10000
I (14472) wifi:dp: 2, bi: 102400, li: 4, scale listen interval from 307200 us to 409600 us
I (14472) wifi:AP's beacon interval = 102400 us, DTIM period = 2
I (14822) INTERNET-CHECK: Connected to the Wi-Fi network.
I (14822) INTERNET-CHECK: IP Address for config.target_addr: 8.8.8.8
E (14822) ping_sock: send error=0
E (14892) esp-tls: [sock=55] connect() error: Host is unreachable
E (14892) esp-tls: Failed to open new connection
E (14892) transport_base: Failed to open a new connection
E (14902) mqtt_client: Error transport connect
E (14902) MQTT_CLIENT: MQTT_EVENT_ERROR
E (14912) MQTT_CLIENT: Last error reported from esp-tls: 0x8004
I (14912) MQTT_CLIENT: Last errno string (Success)
W (14922) MQTT_CLIENT: MQTT_EVENT_DISCONNECTED, attempting to reconnect...
I (14922) mqtt_client: Client force reconnect requested
E (15432) esp-tls: [sock=55] connect() error: Host is unreachable
E (15432) esp-tls: Failed to open new connection
E (15432) transport_base: Failed to open a new connection
E (15432) mqtt_client: Error transport connect
E (15442) MQTT_CLIENT: MQTT_EVENT_ERROR
E (15442) MQTT_CLIENT: Last error reported from esp-tls: 0x8004
I (15452) MQTT_CLIENT: Last errno string (Success)
W (15462) MQTT_CLIENT: MQTT_EVENT_DISCONNECTED, attempting to reconnect...
I (15462) mqtt_client: Client force reconnect requested
I (15822) esp_netif_handlers: sta ip: 192.168.46.7, mask: 255.255.255.0, gw: 192.168.46.2
I (15822) INTERNET-CHECK: Ping attempt 1 failed.
I (15912) INTERNET-CHECK: 64 bytes from 8.8.8.8 icmp_seq=2 ttl=54 time=93 ms
I (16522) wifi:<ba-add>idx:0 (ifx:0, fa:d4:63:3b:e3:ef), tid:0, ssn:16, winSize:64
I (17642) MQTT_CLIENT: MQTT_EVENT_CONNECTED
E (17652) mqtt_client: Client has started
I (18792) MQTT_CLIENT: MQTT_EVENT_PUBLISHED, msg_id=21881
Active Time: 12163113
I (18792) MQTT_CLIENT: Data sent, entering light sleep mode
I (18902) wifi:bcn_timeout,ap_probe_send_start
I (18952) SCD4X: Raw data: 0A 8E 0A 68 AC 62 78 EF 62
I (18952) SENSOR: CO2: 2702 ppm, Temperature: 26.55 C, Humidity: 47.24%
I (18982) wifi:state: run -> init (7c0)
I (18982) wifi:pm stop, total sleep time: 2416528 us / 64575667 us

I (18982) wifi:<ba-del>idx:0, tid:0
I (18982) wifi:new:<12,0>, old:<12,0>, ap:<255,255>, sta:<12,0>, prof:1
I (18992) INTERNET-CHECK: Disconnected from Wi-Fi. Attempting to reconnect (1/5)...
E (18992) transport_base: poll_read select error 113, errno = Software caused connection abort, fd = 55
E (19002) mqtt_client: Poll read error: 119, aborting connection
I (19012) wifi:new:<12,0>, old:<12,0>, ap:<255,255>, sta:<12,0>, prof:1
I (19022) wifi:state: init -> auth (b0)

assert failed: heap_caps_free heap_caps.c:387 (heap != NULL && "free() target pointer is outside heap areas")
Core 0 register dump:
MEPC : 0x40380700 RA : 0x4038bafc SP : 0x3fcc4190 GP : 0x3fc97e00
0x40380700: panic_abort at /home/ahmet/esp/v5.1.4/esp-idf/components/esp_system/panic.c:466
0x4038bafc: __ubsan_include at /home/ahmet/esp/v5.1.4/esp-idf/components/esp_system/ubsan.c:313

TP : 0x3fc8471c T0 : 0x37363534 T1 : 0x7271706f T2 : 0x33323130
S0/FP : 0x0000005a S1 : 0x00000001 A0 : 0x3fcc41cc A1 : 0x3fc99c1d
A2 : 0x00000001 A3 : 0x00000029 A4 : 0x00000001 A5 : 0x3fc9f000
A6 : 0x7a797877 A7 : 0x76757473 S2 : 0x00000009 S3 : 0x3fcc430d
S4 : 0x3fc99c1c S5 : 0x00000000 S6 : 0x00000000 S7 : 0x00000000
S8 : 0x00000000 S9 : 0x00000000 S10 : 0x00000000 S11 : 0x00000000
T3 : 0x6e6d6c6b T4 : 0x6a696867 T5 : 0x66656463 T6 : 0x62613938
MSTATUS : 0x00001881 MTVEC : 0x40380001 MCAUSE : 0x00000007 MTVAL : 0x00000000
0x40380001: _vector_table at ??:?

MHARTID : 0x00000000

Stack memory:
3fcc4190: 0x3fcc4258 0x00000000 0x3c1211b8 0x4039329a 0x3fc99ff4 0x3c1211b8 0x3fc9a3b4 0x3c1210ae
0x4039329a: newlib_include_assert_impl at /home/ahmet/esp/v5.1.4/esp-idf/components/newlib/assert.c:93

3fcc41b0: 0x3fc9a004 0x3fcc41c4 0x3fc9a008 0x3c121104 0x3fc99c1c 0x00373833 0x3fcc4240 0x65737361
3fcc41d0: 0x66207472 0x656c6961 0x68203a64 0x5f706165 0x73706163 0x6572665f 0x65682065 0x635f7061
3fcc41f0: 0x2e737061 0x38333a63 0x68282037 0x20706165 0x4e203d21 0x204c4c55 0x22202626 0x65657266
3fcc4210: 0x74202928 0x65677261 0x6f702074 0x65746e69 0x73692072 0x74756f20 0x65646973 0x61656820
3fcc4230: 0x72612070 0x22736165 0x3fca0029 0x00000000 0x00000000 0x00000000 0x00000001 0x40390b34
0x40390b34: multi_heap_internal_unlock at /home/ahmet/esp/v5.1.4/esp-idf/components/heap/multi_heap.c:168 (discriminator 1)

3fcc4250: 0x3fca5b0c 0x3fcb44d0 0x3fca2b90 0x40390afa 0x3c14b154 0x00021efa 0x3fcb44d0 0x40380d76
0x40390afa: multi_heap_free_impl at /home/ahmet/esp/v5.1.4/esp-idf/components/heap/multi_heap.c:233
0x40380d76: heap_caps_free at /home/ahmet/esp/v5.1.4/esp-idf/components/heap/heap_caps.c:391

3fcc4270: 0x00000000 0x00000000 0x00000001 0x40390b34 0x3fca5b0c 0x3fcb4454 0x3fca2b90 0x40390afa
0x40390b34: multi_heap_internal_unlock at /home/ahmet/esp/v5.1.4/esp-idf/components/heap/multi_heap.c:168 (discriminator 1)
0x40390afa: multi_heap_free_impl at /home/ahmet/esp/v5.1.4/esp-idf/components/heap/multi_heap.c:233

3fcc4290: 0x3fca596c 0x00000000 0x00000000 0x40000000 0x3fcc4308 0x00021efa 0x00000001 0x00004a3a
0x40000000: _start in ROM

3fcc42b0: 0x3fca5b0c 0x00021efa 0x41d46edc 0x40380d9e 0x3c12973c 0x3c128b1c 0x3fca5b0c 0x40387e1c
0x40380d9e: heap_caps_free at /home/ahmet/esp/v5.1.4/esp-idf/components/heap/heap_caps.c:387 (discriminator 1)
0x40387e1c: esp_mbedtls_mem_free at /home/ahmet/esp/v5.1.4/esp-idf/components/mbedtls/port/esp_mem.c:39

3fcc42d0: 0x00000000 0x00000000 0x3fca5bd8 0x420b9126 0x00000000 0x00000000 0x3fca5b0c 0x420c5b3c
0x420b9126: mbedtls_free at /home/ahmet/esp/v5.1.4/esp-idf/components/mbedtls/mbedtls/library/platform.c:55
0x420c5b3c: mbedtls_x509_crt_free at /home/ahmet/esp/v5.1.4/esp-idf/components/mbedtls/mbedtls/library/x509_crt.c:3238

3fcc42f0: 0x00000000 0x00021efa 0x3fca55f0 0x420a41ce 0x00000000 0x00000000 0x3fca55f0 0x420a4210
0x420a41ce: esp_mbedtls_cleanup at /home/ahmet/esp/v5.1.4/esp-idf/components/esp-tls/esp_tls_mbedtls.c:328
0x420a4210: esp_mbedtls_conn_delete at /home/ahmet/esp/v5.1.4/esp-idf/components/esp-tls/esp_tls_mbedtls.c:276

3fcc4310: 0x00000000 0x00021efa 0x3fca55f0 0x420a37dc 0x00000001 0x00021efa 0x3fcb3828 0x420a506a
0x420a37dc: esp_tls_conn_destroy at /home/ahmet/esp/v5.1.4/esp-idf/components/esp-tls/esp_tls.c:147
0x420a506a: base_close at /home/ahmet/esp/v5.1.4/esp-idf/components/tcp_transport/transport_ssl.c:318

3fcc4330: 0x00000000 0xffffffff 0x3fca7258 0x420a4b08 0x00000000 0x00021efa 0x3fca7258 0x4200b6f6
0x420a4b08: esp_transport_close at /home/ahmet/esp/v5.1.4/esp-idf/components/tcp_transport/transport.c:175
0x4200b6f6: esp_mqtt_abort_connection at /home/ahmet/esp/v5.1.4/esp-idf/components/mqtt/esp-mqtt/mqtt_client.c:792

3fcc4350: 0x3c128b1c 0x00000077 0x3fca7258 0x4200c950 0x00000000 0x00000000 0x000104b3 0x00000000
0x4200c950: esp_mqtt_task at /home/ahmet/esp/v5.1.4/esp-idf/components/mqtt/esp-mqtt/mqtt_client.c:1689 (discriminator 3)

3fcc4370: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x4038e544
0x4038e544: vPortTaskWrapper at /home/ahmet/esp/v5.1.4/esp-idf/components/freertos/FreeRTOS-Kernel/portable/riscv/port.c:205

3fcc4390: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fcc43b0: 0x00000000 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
3fcc43d0: 0x00004150 0x00000000 0x03000000 0x00030317 0x00000004 0x00000000 0x00000002 0x72567d00
3fcc43f0: 0x5ce5d5ee 0xddfe19b9 0x9b628b84 0x5d9a46cc 0xaf217d30 0x754d63fc 0x01010433 0xe785a500
3fcc4410: 0xdcdb6306 0x51eb9d79 0x7821eacf 0x37adab7f 0xf097d95f 0x7c3a981a 0x20dd8afb 0x5033d0e2
3fcc4430: 0xfe3d1ee2 0x4628dda3 0x86fc7368 0x48c43291 0x6938f490 0xac3d1ff9 0xd79c67c9 0x0a9c01e7
3fcc4450: 0x3327b94b 0x2d8dee62 0x32ffebec 0xda4ec264 0xcc8c552e 0x8536bbf2 0x610928ec 0x5570a382
3fcc4470: 0x67de6074 0x49e06c4f 0xcd6a3363 0xb5498f2f 0x3454a980 0xf43aa47f 0xec48bb29 0xa7f984bb
3fcc4490: 0xc675e65d 0x6251710d 0x9385780a 0x5c58fde0 0x55e757e6 0x80360149 0x38c5ce68 0x68c6f9bf
3fcc44b0: 0x9a555367 0xf049a255 0xcd46fa91 0xfee0dbdc 0xce47159c 0x97afcce3 0xfd51ab4b 0x5c6fc5dd
3fcc44d0: 0x6ac5d7d2 0x1d546fe9 0x13fb41d8 0x269dce65 0x0ae649de 0x5f33a218 0x1a26ee86 0x51f945e2
3fcc44f0: 0x0c29e0e9 0xfd69289e 0xfa18e546 0xf94444f3 0xf26ae6b2 0x93f05794 0xa7129170 0xddbdcea9
3fcc4510: 0x7b4d0397 0x1a3be4e7 0x72e66abc 0x83e4150b 0x021a58ab 0x6a41c8b5 0x222bfefc 0x9419e772
3fcc4530: 0x2e00c9ad 0x40c5788c 0x03136ac0 0xbff30056 0xe78156af 0x713d9c3e 0xf941111c 0xd9474918
3fcc4550: 0x4489c2e9 0x2682e1ef 0xda57e8d2 0x29636bf3 0x4247eb37 0x9a0f7c3a 0x463b5555 0x1f353718
3fcc4570: 0x41b84057 0xf773393a 0xe9f5b852 0xd1c47805 0x9519dc16 0x7db4cdd7 0x467eebcb 0x9cfed0de



ELF file SHA256: f938281dec373981

Rebooting...
ESP-ROM:esp32c3-api1-20210207
Build:Feb 7 2021
rst:0x3 (RTC_SW_SYS_RST),boot:0xd (SPI_FAST_FLASH_BOOT)
Saved PC:0x40048b82
0x40048b82: ets_secure_boot_verify_bootloader_with_keys in ROM

SPIWP:0xee
mode:DIO, clock div:1
load:0x3fcd5820,len:0x170c
load:0x403cc710,len:0x968
load:0x403ce710,len:0x2f9c
entry 0x403cc710
and here is my main.c code:
#include "main.h"
#include "esp_vfs_semihost.h"
#include "esp_vfs_fat.h"
#include "esp_spiffs.h"
#include "sdmmc_cmd.h"
#include "soc/soc_caps.h"
#include "freertos/semphr.h"
#include "driver/uart.h"
#include "esp_timer.h"
#include "esp_check.h"
#include "ble_init.h"

#define TIMER_WAKEUP_TIME_US 60050000

static RTC_DATA_ATTR struct timeval sleep_enter_time;
static const char *TAG = "SENSOR";
TaskHandle_t i2cTaskHandle = NULL;
int64_t active_time_start = 0;
int64_t active_time_stop = 0;

void esp_dp_sleep();

typedef struct {
uint16_t co2;
float temperature;
float humidity;
} sensor_data_t;

void scd4x_read_measurements_task(void *pvParameters)
{
sensor_data_t *sensor_data = (sensor_data_t *)pvParameters;
esp_err_t ret;

mainTaskHandle = xTaskGetCurrentTaskHandle();

while (1) {
active_time_start = esp_timer_get_time();
vTaskDelay(50 / portTICK_PERIOD_MS);

ret = scd4x_read_measurement(&(sensor_data->co2), &(sensor_data->temperature), &(sensor_data->humidity));
if (ret == ESP_OK) {
ESP_LOGI(TAG, "CO2: %u ppm, Temperature: %.2f C, Humidity: %.2f%%",
sensor_data->co2, sensor_data->temperature, sensor_data->humidity);

// Create JSON object
cJSON *root = cJSON_CreateObject();
cJSON_AddNumberToObject(root, "temperature", sensor_data->temperature);
cJSON_AddNumberToObject(root, "humidity", sensor_data->humidity);
cJSON_AddNumberToObject(root, "CO2", sensor_data->co2);
char *jsonString = cJSON_Print(root);

esp_mqtt_client_publish(mqtt_client, "sensor/scd4x", jsonString, strlen(jsonString), 1, 0);

cJSON_Delete(root);
if (jsonString) {
free(jsonString);
}
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
//esp_dp_sleep();
} else {
ESP_LOGE(TAG, "Failed to read measurement");
}
}
}

void esp_dp_sleep(){
nvs_handle_t nvs_handle;
esp_err_t err;

gettimeofday(&sleep_enter_time, NULL); // Record the time before going to sleep

err = nvs_open("storage", NVS_READWRITE, &nvs_handle);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Error (%s) opening NVS handle!", esp_err_to_name(err));
} else {
ESP_LOGI(TAG, "NVS opened successfully");
ESP_ERROR_CHECK(nvs_set_i32(nvs_handle, "slp_enter_sec", sleep_enter_time.tv_sec));
ESP_ERROR_CHECK(nvs_set_i32(nvs_handle, "slp_enter_usec", sleep_enter_time.tv_usec));
ESP_ERROR_CHECK(nvs_commit(nvs_handle));
nvs_close(nvs_handle);
}

esp_deep_sleep_start();
}

static void example_deep_sleep_register_rtc_timer_wakeup(void)
{
const int wakeup_time_sec = 120;
ESP_LOGI(TAG, "Enabling timer wakeup, %ds", wakeup_time_sec);
ESP_ERROR_CHECK(esp_sleep_enable_timer_wakeup(wakeup_time_sec * 1000000));
}

esp_err_t example_register_timer_wakeup(void)
{
ESP_RETURN_ON_ERROR(esp_sleep_enable_timer_wakeup(TIMER_WAKEUP_TIME_US), TAG, "Configure timer as wakeup source failed");
ESP_LOGI(TAG, "timer wakeup source is ready");
return ESP_OK;
}

void app_main(void)
{
struct timeval now;
gettimeofday(&now, NULL);

int sleep_time_ms = (now.tv_sec - sleep_enter_time.tv_sec) * 1000 + (now.tv_usec - sleep_enter_time.tv_usec) / 1000;
ESP_LOGI(TAG, "Wake up from timer. Time spent in deep sleep: %d ms", sleep_time_ms);

uint8_t serial_number_scd4x[6] = {0};
sensor_data_t sensor_data = {0};

esp_err_t ret;
/* Initialize NVS. */
ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);

ESP_ERROR_CHECK(i2c_master_init());
ESP_LOGI(TAG, "I2C initialized successfully");
mqtt_app_start();
wifi_connection();

scd4x_stop_periodic_measurement();
vTaskDelay(500 / portTICK_PERIOD_MS);
esp_err_t scd_ser = scd4x_read(SCD41_SN_ADDR, serial_number_scd4x, sizeof(serial_number_scd4x));
if (scd_ser == ESP_OK) {
ESP_LOGI(SCD4X_TAG, "Serial Number: %02x%02x%02x%02x%02x%02x",
serial_number_scd4x[0], serial_number_scd4x[1], serial_number_scd4x[2],
serial_number_scd4x[3], serial_number_scd4x[4], serial_number_scd4x[5]);
} else {
ESP_LOGE(TAG, "Failed to read serial number");
}

scd4x_mode_low_power_periodic_measurement();
example_register_timer_wakeup();
ble_app();
vTaskDelay(5000 / portTICK_PERIOD_MS);
xTaskCreate(scd4x_read_measurements_task, "SCD4X_TASK", 8192, &sensor_data, 5, &i2cTaskHandle);
}
As i understand from output mbedtls. There is a problem with deallocation of certificates. When i use lower sleep time than 30 second i have no problem because MQTT client does not disconnect.

MicroController
Posts: 1552
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: mbedTLS heap error

Postby MicroController » Mon Aug 26, 2024 12:55 pm

Code: Select all

xTaskCreate(scd4x_read_measurements_task, ... , &sensor_data, ... );
sensor_data is local to app_main(), i.e. &sensor_data becomes invalid as soon as app_main() returns.
Do not pass a pointer to a local variable to the new task!
The new task subsequently writing to this variable will cause stack/memory corruption, which may result in any kind of unexpected and seemingly completely unrelated errors immediately or later.

Ahmetsafi
Posts: 10
Joined: Wed Aug 07, 2024 12:01 pm

Re: mbedTLS heap error

Postby Ahmetsafi » Wed Aug 28, 2024 9:48 pm

MicroController wrote:
Mon Aug 26, 2024 12:55 pm

Code: Select all

xTaskCreate(scd4x_read_measurements_task, ... , &sensor_data, ... );
sensor_data is local to app_main(), i.e. &sensor_data becomes invalid as soon as app_main() returns.
Do not pass a pointer to a local variable to the new task!
The new task subsequently writing to this variable will cause stack/memory corruption, which may result in any kind of unexpected and seemingly completely unrelated errors immediately or later.
thank you, problem solved i did not even realized.

Who is online

Users browsing this forum: Basalt, Bing [Bot] and 317 guests