esphome / esphome/issues

ESP32 HTTP Request get Data - Could not allocate memory for JSON document! Requested 0 bytes, free heap: 110580

Open
#4,250 5 comments 0 reactions 0 assignees View on GitHub
stale
Dominant language
No language data
Stars
313
Forks
40
PR merge metrics
No merged PRs in 30d

Description

### The problem

StaticJsonDocument is empty

```json
{
"telegram": [
"/ISk5MT174-0001",
"0.9.1(130203)",
"0.9.2(0230223)",
"0.0.0(00339188)",
"0.2.0(1.03)",
"C.1.6(FDF5)",
"1.8.1(0011404.409*kWh)",
"1.8.2(0023813.725*kWh)",
"2.8.1(0015608.962*kWh)",
"2.8.2(0000900.569*kWh)",
"F.F.0(0000000)",
"!"
]
}
```

Could not allocate memory for JSON document! Requested 0 bytes, free heap: 110580

1st log output shows the payload
2nd Error Could not allocate memory for JSON document
3rd log output shows no payload

### Which version of ESPHome has the issue?

2023.2.4

### What type of installation are you using?

Docker

### Which version of Home Assistant has the issue?

_No response_

### What platform are you using?

ESP32

### Board

az-delivery-devkit-v4

### Component causing the issue

http_request

### Example YAML snippet

```yaml
## ---------------------------------------------------
## TEST ESPLESEKOPF TELEGRAMS
## board: az-delivery-devkit-v4; framework: arduino; platform: platformio/espressif32 @ 5.2.0
## ---------------------------------------------------
## 2023.2.4|Flash: 4096kB Speed:40MHz Mode:DIO|
## Chip: ESP32 Features:WIFI_BGN,BLE,BT, Cores:2 Revision:3|
## ESP-IDF: v4.4.2|EFuse MAC: 78:21:84:9C:B2:AC|
## Reset: Software Reset CPU|Wakeup: Unknown
## ---------------------------------------------------
substitutions:
device_name_short: "esp-lesekopf2"
friendly_name: "Smartmeter"
device_description: "Smartmeter Device for Gridmeter ISKRA 2013 M13 1376, Drehstromzähler"
domain: !secret domain
appversion: "2.0.1"
service_url: !secret service_host
service: "mt174.gridmeter"

## ---------------------------------------------------
## device configuration
## ---------------------------------------------------
board: "az-delivery-devkit-v4"
uart_rx_pin: "GPIO17" # white
uart_tx_pin: "GPIO16" # orange
uart_baudrate: "300"
update_interval: "60s"

esp32:
board: az-delivery-devkit-v4
framework:
type: arduino

esphome:
name: "${device_name_short}"
comment: "${device_description}"
build_path: ./build/${device_name_short}

## ---------------------------------------------------
## WIFI Settings
## ---------------------------------------------------
wifi:
networks:
- ssid: !secret ssid3_name
password: !secret ssid3_pswd
priority : 1
- ssid: !secret ssid1_name
password: !secret ssid1_pswd
priority : 2
- ssid: !secret ssid2_name
password: !secret ssid2_pswd
priority : 3
domain: !secret domain

## ----------------------------------------------------------------------
## WEBSERVER Settings
## ----------------------------------------------------------------------
web_server:
port: 80
version: 2
local: true

## ---------------------------------------------------
## OTA Settings
## ---------------------------------------------------
ota:
password: !secret ota_pswd
on_error:
then:
- logger.log:
format: "OTA update error %d"
args: ["x"]

# ----------------------------------------------------------------
# Native API Component
# ----------------------------------------------------------------
api:
id: espapi
port: 6053
reboot_timeout: 0s

# ----------------------------------------------------------------
# Logger
# ----------------------------------------------------------------
logger:
id: mylogger
level: info
baud_rate: 0 #disable logging over uart

# ----------------------------------------------------------------
# HTTP
# ----------------------------------------------------------------
http_request:
id: http_request_data
useragent: esphome/mt174test
timeout: 10s

# ----------------------------------------------------------------
# Serial interface
# ----------------------------------------------------------------
uart:
id: my_uart
tx_pin: GPIO17 # white [TX] az-delivery-devkit-v4,
rx_pin: GPIO16 # orange [RX] az-delivery-devkit-v4,
baud_rate: 300
data_bits: 7
parity: EVEN
stop_bits: 1
rx_buffer_size: 512
debug:
direction: BOTH
after:
delimiter: "\r\n"
sequence:
- lambda: |-
UARTDebug::log_string(direction, bytes); //Still log the data
std::string str(bytes.begin(), bytes.end());
ESP_LOGI("UART", "DATA: %s",str.c_str());

## ---------------------------------------------------
## SCRIPTS
## ---------------------------------------------------
script:

- id: writeTelegram
parameters:
message: string
mode: queued
then:
- logger.log:
level: info
format: "Telegram message: %s"
args: ["message"]
- uart.write:
id: my_uart
data: !lambda |-
std::string str = message;
std::vector vec(str.begin(), str.end());
return vec;
- uart.write:
id: my_uart
data: "\r\n"
- delay: 10ms
- logger.log: "Next Telegram message"


- id: getNetInfoData
mode: queued
then:
- http_request.get:
url: http://ip-api.com/json/
headers:
Content-Type: application/json
verify_ssl: false
on_response:
- if:
condition:
lambda: |-
return status_code == 200;
then:
- logger.log:
level: info
format: "Response status: %d, Duration: %u ms"
args:
- status_code
- duration_ms
- logger.log:
level: info
format: "Start telegramdata: %s"
args: ["id(http_request_data).get_string()"]

- id: getTelegramdata
mode: queued
then:
- logger.log:
level: info
format: "HTTP Request get Data"
- http_request.get:
url: !secret data_host
headers:
Content-Type: application/json
verify_ssl: false
on_response:
- if:
condition:
lambda: |-
return status_code == 200;
then:
- logger.log:
level: info
tag: "getTelegramdata"
format: "Response status: %d, Duration: %u ms"
args:
- status_code
- duration_ms
- logger.log:
level: info
tag: "getTelegramdata"
format: "Response telegramdata: %s"
args: ["id(http_request_data).get_string()"]
# {"telegram":
# [
# "/ISk5MT174-0001",
# "0.9.1(130203)",
# "0.9.2(0230223)",
# "0.0.0(00339188)",
# "0.2.0(1.03)", "C.1.6(FDF5)",
# "1.8.1(0011404.409*kWh)",
# "1.8.2(0023813.725*kWh)",
# "2.8.1(0015608.962*kWh)",
# "2.8.2(0000900.569*kWh)",
# "F.F.0(0000000)", "!"
# ]}
- lambda: |-
ESP_LOGI("script", "Decode Response");
json::parse_json(id(http_request_data).get_string(), [](JsonObject root) {
for (uint16_t i = 0; i < 12; i++) {
ESP_LOGI("parse", "telegram %d: %s",i,root["telegram"][i] );
}
});
- logger.log:
level: info
tag: "getTelegramdata"
format: "Response telegramdata: %s"
args: ["id(http_request_data).get_string()"]
- logger.log:
level: info
tag: "getTelegramdata"
format: "End telegram decoder"

- id: snddata
then:
- lambda: |-
uint16_t size = id(seltelegram).size();
for (uint16_t i = 0; i < size; i++) {
auto option = id(seltelegram).at(i);
auto value = option.value();
ESP_LOGI("script", "Option at %d is: %s", index, value.c_str());
id(writeTelegram).execute(value);
}

select:
- platform: template
name: "Select Telegram"
id: seltelegram
optimistic: true
options:
- "/ISk5MT174-0001"
- "0.9.1(130203)"
- "0.9.2(0230223)"
- "0.0.0(00339188)"
- "0.2.0(1.03)"
- "C.1.6(FDF5)"
- "1.8.1(0016519.075*kWh)"
- "1.8.2(0029595.323*kWh)"
- "2.8.1(0020656.624*kWh)"
- "2.8.1(0020656.624*kWh)"
- "F.F.0(0000000)"
on_value:
then:
- logger.log:
format: "Telegram Data: %s"
tag: "select"
args: ["x.c_str()"]
- lambda:
id(writeTelegram).execute(x.c_str());

## ---------------------------------------------------
## switches
## ---------------------------------------------------
switch:

- platform: restart
id: restart_device
name: ${friendly_name} restart

button:
- platform: template
name: 'Test: Get Telegram Data'
id: btnTestTelegramData
on_press:
- logger.log:
level: info
format: Get Data and decoder for telegram startet"
- script.execute: getTelegramdata

- platform: template
name: 'Test: Telegram Data'
id: btnTestTelegram
on_press:
- logger.log:
level: info
format: "Simple Testcase"
- script.execute: snddata

- platform: template
name: 'Test: IP Data'
id: btnGetNetInfoData
on_press:
- script.execute: getNetInfoData

.....
```

### Anything in the logs that might be useful for us?

```txt
[13:07:15][I][main:413]: Get Data and decoder for telegram startet"
[13:07:15][I][main:267]: HTTP Request get Data
[13:07:15][I][getTelegramdata:290]: Response status: 200, Duration: 48 ms
[13:07:15][I][getTelegramdata:293]: Response telegramdata: {"telegram": ["/ISk5MT174-0001", "0.9.1(130203)", "0.9.2(0230223)", "0.0.0(00339188)", "0.2.0(1.03)", "C.1.6(FDF5)", "1.8.1(0011404.409*kWh)", "1.8.2(0023813.725*kWh)", "2.8.1(0015608.962*kWh)", "2.8.2(0000900.569*kWh)", "F.F.0(0000000)", "!"]}

[13:07:15][I][script:314]: Decode Response
[13:07:15][E][json:081]: Could not allocate memory for JSON document! Requested 0 bytes, free heap: 110580

[13:07:15][I][getTelegramdata:322]: Response telegramdata: <-- 2nd Log output empty !
[13:07:15][I][getTelegramdata:325]: End telegram decoder
```

### Additional information

_No response_

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the ESP32 Arduino http_request component and the json::parse_json lambda in the supplied YAML, then reproduce the request on the az-delivery-devkit-v4 configuration. Compare the payload log with the allocation error and the later empty response; done means the documented JSON response can be processed without that failure and the relevant behavior is covered by the project’s available checks.

Written by the indexing model from the issue text.

Assessment

Tech stack
arduino, docker, json
Domain
embedded-iot
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.