esp-idf http post

anhchill123
Posts: 2
Joined: Sat Jan 18, 2025 6:51 pm

esp-idf http post

Postby anhchill123 » Sat Jan 18, 2025 7:26 pm

hi to all.
I'm not good at English
i'm using esp8266 kit + php +mysql +xampp ( localhost), try to using POST request to my php. I'm able to connect to wifi rourter and using this code:

Code: Select all

static void post_rest_function()
{
  
    esp_http_client_config_t config = {
        .url = "http://192.168.56.1/final/updateHX711data_and_recordtable.php", 
        .method = HTTP_METHOD_POST, 
        .cert_pem = NULL,            
        .event_handler=_http_event_handler,

    esp_http_client_handle_t client = esp_http_client_init(&config);

  
    const char *post_data="id=esp8266_01&name=DaoDuyAnh&weight=10.5&status_read_sensor_hx711=SUCCESS&led_01=OFF&led_02=OFF");

    esp_http_client_set_header(client, "Content-Type", "application/x-www-form-urlencoded");
    esp_http_client_set_post_field(client, post_data, strlen(post_data));

    
   

   
    esp_http_client_perform(client);

    
    esp_http_client_cleanup(client);
}
This is my php code :

Code: Select all

<?php
  require 'database.php';
  

  if (!empty($_POST)) {
  
    $id = $_POST['id'];
    $weight = $_POST['weight'];
    $name = $_POST['name'];
    $status_read_sensor_hx711 = $_POST['status_read_sensor_hx711'];
    $led_01 = $_POST['led_01'];
    $led_02 = $_POST['led_02'];

    date_default_timezone_set("Etc/GMT+7"); // Look here for your timezone : https://www.php.net/manual/en/timezones.php
    $tm = date("H:i:s");
    $dt = date("Y-m-d");

    $pdo = Database::connect();
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "UPDATE 	esp8266_table_hx711_leds_update	 SET  weight = ?, status_read_sensor_hx711 = ?, time = ?, date = ? WHERE id = ?";
    $q = $pdo->prepare($sql);
    $q->execute(array($weight,$status_read_sensor_hx711,$tm,$dt,$id));
    Database::disconnect();
    }
    function generate_string_id($strength = 16) {
    $permitted_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $input_length = strlen($permitted_chars);
    $random_string = '';
    for($i = 0; $i < $strength; $i++) {
      $random_character = $permitted_chars[mt_rand(0, $input_length - 1)];
      $random_string .= $random_character;
    }
    return $random_string;
  }

?>
    
i test postman to send data to "http://192.168.56.1/final/updateHX711da ... dtable.php" and success update data in mysql database but when i run the esp-idf code, i have a trouble:

"E (20789) HTTP_CLIENT: Connection failed, sock < 0
E (20790) HTTP: HTTP POST request failed: ESP_ERR_HTTP_CONNECT "
How to fix this or I use wrong way.
Thank you
Regard
"

nopnop2002
Posts: 139
Joined: Thu Oct 03, 2019 10:52 pm
Contact:

Re: esp-idf http post

Postby nopnop2002 » Sun Jan 19, 2025 7:22 am

I am using this code.

I am using this as an HTTP server.
https://github.com/typicode/json-server

Code: Select all

    esp_http_client_config_t config = {
        .url = "http://192.168.10.43:3000/posts",
        .event_handler = _http_event_handler,
    };
    esp_http_client_handle_t client = esp_http_client_init(&config);

    esp_err_t err;

    // POST
    const char *post_data = "{\"id\":\"3\",\"title\":\"hohe\",\"views\":123}";
    esp_http_client_set_method(client, HTTP_METHOD_POST);
    esp_http_client_set_header(client, "Content-Type", "application/json");
    esp_http_client_set_post_field(client, post_data, strlen(post_data));
    err = esp_http_client_perform(client);
    if (err == ESP_OK) {
        ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %"PRId64,
                esp_http_client_get_status_code(client),
                esp_http_client_get_content_length(client));
    } else {
        ESP_LOGE(TAG, "HTTP POST request failed: %s", esp_err_to_name(err));
    }



POST can be used for a wide range of purposes, such as creating new resources, starting batch operations, deleting data, etc., whereas PUT is more specific and is primarily used to "replace" an existing resource.

Code: Select all

$sql = "UPDATE 	esp8266_table_hx711_leds_update	 SET  weight = ?, status_read_sensor_hx711 = ?, time = ?, date = ? WHERE id = ?";
POST is widely used to add data, while PUT is widely used to update a database.

anhchill123
Posts: 2
Joined: Sat Jan 18, 2025 6:51 pm

Re: esp-idf http post

Postby anhchill123 » Sun Jan 19, 2025 8:00 am

nopnop2002 wrote:
Sun Jan 19, 2025 7:22 am

Code: Select all

    esp_http_client_config_t config = {
        .url = "http://192.168.10.43:3000/todos",
        .event_handler = _http_event_handler,
    };
    esp_http_client_handle_t client = esp_http_client_init(&config);

    // POST
    const char *post_data = "{\"title\":\"hoge\",\"time\":123,\"date\":456}";
    esp_http_client_set_method(client, HTTP_METHOD_POST);
    esp_http_client_set_header(client, "Content-Type", "application/json");
    esp_http_client_set_post_field(client, post_data, strlen(post_data));
    esp_err_t err = esp_http_client_perform(client);
    if (err == ESP_OK) {
        ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %"PRId64,
                esp_http_client_get_status_code(client),
                esp_http_client_get_content_length(client));
    } else {
        ESP_LOGE(TAG, "HTTP POST request failed: %s", esp_err_to_name(err));
    }

Hi , i don't understand. Can u explain more :(((

nopnop2002
Posts: 139
Joined: Thu Oct 03, 2019 10:52 pm
Contact:

Re: esp-idf http post

Postby nopnop2002 » Mon Jan 20, 2025 2:47 am

The only thing you need in the config is the url and event_handler.

Code: Select all

    esp_http_client_config_t config = {
        .url = "http://192.168.10.43:3000/posts",
        .event_handler = _http_event_handler,
    };
    esp_http_client_handle_t client = esp_http_client_init(&config);
you need esp_http_client_set_method befor post.

Code: Select all

   esp_http_client_set_method(client, HTTP_METHOD_POST);

Who is online

Users browsing this forum: oz1cmdk and 72 guests