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条回答

    报告相同问题?

    悬赏问题

    • ¥65 永磁型步进电机PID算法
    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥200 uniapp长期运行卡死问题解决
    • ¥15 latex怎么处理论文引理引用参考文献
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
    • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?