Failing to communicate with a mesh Light using ESP-IDF <SOLVED>

User avatar
fasani
Posts: 196
Joined: Wed Jan 30, 2019 12:00 pm
Location: Barcelona
Contact:

Failing to communicate with a mesh Light using ESP-IDF <SOLVED>

Postby fasani » Thu Jun 24, 2021 6:07 pm

Hi there dear forum experts,
I'm struggling with this since 2 days and I don't find what is wrong. I'm making an example with an epaper touch and LVGL (I'm making a PR to add parallel epaper drivers using EPDiy as a component soon)

This is the code and the lines that connect to the mesh light are here:
https://github.com/martinberlin/lv_port ... ht.cpp#L89

Note: Is the right IP, tested with Postman sending a post to the root light, and following JSON:
{
"request": "set_status",
"characteristics": [
{"cid": 1,
"value": 277
}
]}

And is all good it changes the cid:1 that is the HUE and everything works.
Now when I do the request from the ESP32 I get the following:

I (11654) epaper: EVENT_ON_CONNECTED
I (11674) epaper: HEADER_SENT
E (11684) TRANSPORT_BASE: ssl_poll_write select error 104, errno = Connection reset by peer, fd = 54
W (11684) TRANSPORT_BASE: Poll timeout or error, errno=Connection already in progress, fd=54, timeout_ms=500
E (11694) epaper:
POST failed:ESP_FAIL

It connects, sends the Headers, but get's cut somehow by the lamp without ending the request. Is like if the Mesh light does not like something in the request. I've seen it change colors only 2 times, then it hangs, could not repeat it.
When it does work I see this:
I (13644) epaper: EVENT_ON_CONNECTED
I (13654) epaper: HEADER_SENT
I (13744) epaper: ON_HEADER
I (13744) epaper: ON_HEADER
I (13744) epaper: ON_HEADER
I (13754) epaper: ON_DATA, len=39
{"status_msg":"MDF_OK","status_code":0}
I (13754) epaper: ON_FINISH

Any ideas what I'm doing wrong here?
Relevant part of the code to do the HTTP request:#

Code: Select all

esp_err_t light_request(uint8_t cid, uint16_t value) {
    char json_request[100];
    esp_http_client_config_t config = {
        .url = MESH_LAMP_REQUEST_URL,
        .method = HTTP_METHOD_POST,
        .timeout_ms = 500,
        .event_handler = _http_event_handler,
        .buffer_size = HTTP_RECEIVE_BUFFER_SIZE
        };
    esp_http_client_handle_t client = esp_http_client_init(&config);

    // Build POST   
    sprintf(json_request, "{\"request\":\"set_status\",\"characteristics\":[{\"cid\":%d,\"value\":%d}]}",
            cid, value);
    
    esp_http_client_set_header(client, "Mesh-Node-Mac", "3c71bf9d6980");
    esp_http_client_set_header(client, "Content-Type", "application/json");
    esp_http_client_set_post_field(client, json_request, strlen(json_request));
    esp_err_t err = esp_http_client_perform(client);
    if (err == ESP_OK) {
        // Disable after debugging
        ESP_LOGI(TAG, "Status=%d, content_length=%d",
                esp_http_client_get_status_code(client),
                esp_http_client_get_content_length(client));
    }else{
        ESP_LOGE(TAG, "\nPOST failed:%s", esp_err_to_name(err));
    }
    return err;
}
This is the working Arduino-esp32 code. Very simple, just the right JSON and it works.

Code: Select all

   http.begin("http://192.168.0.75/device_request");
   http.addHeader("Content-Type", "application/json"); 
   http.addHeader("Mesh-Node-Mac", "3c71bf9d6ab4,3c71bf9d6980");
   
   String json = "{\"request\": \"set_status\",\"characteristics\": [{\"cid\": 1,\"value\": "+String(signalLevel)+"}]}";
   int httpResponseCode = http.POST(json);'
Last edited by fasani on Mon Jul 12, 2021 6:32 pm, edited 1 time in total.
epdiy collaborator | http://fasani.de Fan of Espressif MCUs and electronic design

User avatar
fasani
Posts: 196
Joined: Wed Jan 30, 2019 12:00 pm
Location: Barcelona
Contact:

Re: Failing to communicate with a mesh Light using ESP-IDF

Postby fasani » Fri Jun 25, 2021 6:16 am

I just copied Postman request, that includes some additional headers (even one that is kind of a random token)
and now it's working:
https://twitter.com/martinfasani/status ... 6120491008 (Small proof-of-concept video)

Code: Select all

POST headers:
Array
(
    [Mesh-Node-Mac] => 3c71bf9d6980
    [Content-Type] => application/json
    [User-Agent] => PostmanRuntime/7.28.0
    [Accept] => */*
    [Postman-Token] => 47933e16-88fe-41ef-a830-57a549f423a9
    [Host] => 192.168.12.106
    [Connection] => keep-alive
    [Content-Length] => 131
)

Raw:
{
    "request": "set_status",
    "characteristics": [
        {
            "cid": 1,
            "value": 277
        }

    ]
}
However I would like to know why the lamp is rejecting my HTTP request. If someone sees something wrong in my requests please let me know!
epdiy collaborator | http://fasani.de Fan of Espressif MCUs and electronic design

Who is online

Users browsing this forum: No registered users and 32 guests