doucong3048 2019-06-06 21:03
浏览 744

需要从ESP8266发送POST请求到运行php的服务器

I want to send some date via post request from an ESP8266 to server running php. Purpose is to capture data via ESP8266 & DHT22 and store it on mariaDB via php. I know that PDO might be more secure, but I want to try it this way first. Problem is that the data seems to get send but it is not written to the db.

  • Connection to Wifi (OK)
  • connect to remote server (seems OK)
  • PHP file on server side (seems OK) - data entry via link does work

Sketch running on ESP8266 :

if (client.connect(server, 80)) {
Serial.println("connected!!");
WiFi.printDiag(Serial);


String data = "temp="
        + (String) sensorTemp
        +  "&humi="  
        +(String) sensorHum;

     client.println("POST /connect2.php HTTP/1.1"); 
     client.print("Host: xxx.xxx.xx.xxxx
");                 
     client.println("User-Agent: ESP8266/1.0");
     client.println("Connection: close");
     client.println("Content-Type: application/x-www-form-urlencoded");
     client.print("Content-Length: ");
     client.println(data.length());
     client.print("

");
     client.println (data);
     client.stop(); 

     Serial.println("
");
     Serial.println("My data string im POSTing looks like this: ");
     Serial.println(data);
     Serial.println("And it is this many bytes: ");
     Serial.println(data.length());       
     delay(2000);
     client.stop();
    } 

Server side php :

<?php
$servername = "xxx.xxx.xx.xxx";
$username = "username";
$password = "password";
$dbname = "dbname";
$temp = $_REQUEST['temp'];
$humi = $_REQUEST['humi'];
//date_default_timezone_set('America/Toronto');
//$date=date('m-d-Y H:i:s') ;


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO dbname (temp, humi)
VALUES ('$humi', '$temp')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器