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);
}
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;
}
?>
"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
"