cJSON object with esp_event_post triggers Guru Meditation Error

sazanof
Posts: 22
Joined: Wed Sep 13, 2023 10:22 am

cJSON object with esp_event_post triggers Guru Meditation Error

Postby sazanof » Wed Sep 13, 2023 10:45 am

Hello! I work with an event model and transmit various data via esp_event_post

Firstly, I read ADC4 NTC value
  1. ESP_LOGW(NTC_TAG, "ADC Value: %d, Temp Value: %.1f", adc_raw[0][0], celsius);
What I am doing coding:

ntc.c component
  1. ESP_LOGW(NTC_TAG, "ADC Value: %d, Temp Value: %.1f", adc_raw[0][0], celsius);
  2. cJSON *json = cJSON_CreateObject();
  3. cJSON_AddNumberToObject(json, "test", 1234);
  4. esp_err_t err = esp_event_post(APP_EVENTS, S_ADC_GOT_TEMP, &json, sizeof(json), portMAX_DELAY);
rest_server.c component
  1. esp_err_t err = esp_event_handler_register(APP_EVENTS, S_ADC_GOT_TEMP, &adc_got_temp_event_handler, NULL);
handler:
  1. static void adc_got_temp_event_handler(
  2.     void *arg,
  3.     esp_event_base_t event_base,
  4.     int32_t event_id, void *adc_data)
  5.     {
  6.         const cJSON *json_data = adc_data;
  7.         ESP_LOGI(REST_TAG, "%s", cJSON_Print(json_data));
  8.         //OR
  9.         int temp = cJSON_GetObjectItem(json, "test")->valueint;
  10.         ESP_LOGW(REST_TAG, "%d", temp);
  11.     }
And when I try to pass cJSON to event_data, I got the error
  1. Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.
  2.  
  3. Core  0 register dump:
  4. PC      : 0x400014fd  PS      : 0x00060b30  A0      : 0x8015e8a2  A1      : 0x3ffbb450  
  5. 0x400014fd: strlen in ROM
  6.  
  7. A2      : 0x00000000  A3      : 0xfffffffc  A4      : 0x000000ff  A5      : 0x0000ff00  
  8. A6      : 0x00ff0000  A7      : 0xff000000  A8      : 0x00000000  A9      : 0x00000000  
  9. A10     : 0x00000000  A11     : 0x00000008  A12     : 0x3ffbb694  A13     : 0x00000000  
  10. A14     : 0x3ffbb664  A15     : 0x00000001  SAR     : 0x00000004  EXCCAUSE: 0x0000001c  
  11. EXCVADDR: 0x00000000  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xffffffff  
  12. 0x400014fd: strlen in ROM
  13.  
  14. 0x4000150d: strlen in ROM
  15.  
  16. Backtrace: 0x400014fa:0x3ffbb450 0x4015e89f:0x3ffbb460 0x4015ff49:0x3ffbb780 0x4016edc9:0x3ffbb7b0 0x40090199:0x3ffbb7e0 0x400d8571:0x3ffbb830 0x4016d9c1:0x3ffbb850 0x4016d4f7:0x3ffbb880 0x4016d5ad:0x3ffbb8c0 0x4008c715:0x3ffbb8e0
  17. 0x400014fa: strlen in ROM
This problem occurs only when reading the structure of cJSON in adc_got_temp_event_handler. If I pass number (not cJSON) it works fine.

I need to pass not a single number, but a more complex structure with key-value pairs. What was I wrong about? :oops:

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

Re: cJSON object with esp_event_post triggers Guru Meditation Error

Postby ESP_Sprite » Thu Sep 14, 2023 1:39 am

I think you're sending a **cJSON into the event, but you're treating it as a *cJSON when it comes out.

sazanof
Posts: 22
Joined: Wed Sep 13, 2023 10:22 am

Re: cJSON object with esp_event_post triggers Guru Meditation Error

Postby sazanof » Thu Sep 14, 2023 6:21 am

ESP_Sprite wrote:
Thu Sep 14, 2023 1:39 am
I think you're sending a **cJSON into the event, but you're treating it as a *cJSON when it comes out.

Thanks!

I checked, I'm sending (char *)cJSON, there are no warnings in the console. I tried passing a cJSON string to event_post: cJSON_PrintUnformatted, strlen() but also unsuccessfully.

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

Re: cJSON object with esp_event_post triggers Guru Meditation Error

Postby MicroController » Fri Sep 15, 2023 6:46 pm

Sprite is correct.
The "value" you have stored in the event is indeed the value of the cJSON*, i.e. a pointer to a cJSON, all good.
However, the event handler doesn't get the "value" from the event passed in but a pointer to the "value"; in this case it's a (void) pointer to a pointer to cJSON.
IOW and a bit oversimplified, you pass in &json (which is of type cJSON**), and the same thing, a cJSON**, comes out on the other side.
Try

Code: Select all

static void adc_got_temp_event_handler(
    void *arg,
    esp_event_base_t event_base,
    int32_t event_id, void *adc_data)
    {
        const cJSON *json_data = *((const cJSON **)adc_data);
On another note, it would be much more efficient to pass native values or structs between components of your application than taking the detour of building and parsing JSON; and it likely would be a better architecture to do so and to create JSON only where it's actually needed, so that e.g. the "NTC" won't have to care about JSON at all.

sazanof
Posts: 22
Joined: Wed Sep 13, 2023 10:22 am

Re: cJSON object with esp_event_post triggers Guru Meditation Error

Postby sazanof » Tue Sep 19, 2023 6:05 am

MicroController wrote:
Fri Sep 15, 2023 6:46 pm
IOW and a bit oversimplified, you pass in &json (which is of type cJSON**), and the same thing, a cJSON**, comes out on the other side.
nks! Yes, it woks now fine!
Tha
MicroController wrote:
Fri Sep 15, 2023 6:46 pm
On another note, it would be much more efficient to pass native values or structs between components of your application than taking the detour of building and parsing JSON; and it likely would be a better architecture to do so and to create JSON only where it's actually needed, so that e.g. the "NTC" won't have to care about JSON at all.
Very good words. I read in the documentation that events just allow you to exchange data between components (something like subscriber | listener in php frameworks) You say you can transfer data between components in some other way. How?

Look, in my project there is a components folder containing subfolders webserver, ntc, gpio, nvs etc...

in app_main I register server websockets initialization, nvs, reading of all 4 ADC ports.

Code: Select all

// EXAMPLE
init_nvs();
init_webserver();
init_wifi();
init_ntc();
and at the moment when data comes from ADC channels, I want to send a message to the websocket server with the channel name, the type of device on the channel (power meter or temperature sensor), and the corresponding values.

It's just a value to send through an event, in my case it's not quite right, since for each ADC channel I will need to write your handler. Sending a message via websockets in the ntc.c file is also not what I want from the implementation.

I would like the web server to process only the code for the web server, and the ntc to process the code for the ntc, and send events.

Since gpio management has 2 sides of the coin: changing the port-> sending a message via websockets and sending a message via websockets->changing the port, I don't quite understand how to implement this without using an event model..


I would appreciate clarification on how to do this. Thank you!

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

Re: cJSON object with esp_event_post triggers Guru Meditation Error

Postby MicroController » Fri Sep 22, 2023 2:55 pm

I want to send a message to the websocket server with the channel name, the type of device on the channel (power meter or temperature sensor), and the corresponding values.
For that, you'd use structs, like:

Code: Select all

typedef struct {
  uint32_t a;
  uint16_t b;
  int c;
} my_data_t;
Then you can send (a copy of) one of these structs:

Code: Select all

my_data_t my_data;
my_data.a = ...;
my_data.b = ...;
my_data.c = ...;
esp_event_post(APP_EVENTS, S_ADC_GOT_TEMP, &my_data, sizeof(my_data), portMAX_DELAY);
and receive it on the other end:

Code: Select all

static void my_event_handler(
    void *arg,
    esp_event_base_t event_base,
    int32_t event_id, void *event_data) {
    
      const my_data_t* const my_data_ptr = (const my_data_t*)event_data;
    
      doSomething(my_data_ptr->a, my_data_ptr->b, my_data_ptr->c);
    }

sazanof
Posts: 22
Joined: Wed Sep 13, 2023 10:22 am

Re: cJSON object with esp_event_post triggers Guru Meditation Error

Postby sazanof » Tue Sep 26, 2023 7:33 pm

Thank you very much! I figured it out :)

Who is online

Users browsing this forum: No registered users and 101 guests