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 js调用html页面需要隐藏某个按钮
    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥15 一道python难题3
    • ¥15 牛顿斯科特系数表表示
    • ¥15 arduino 步进电机
    • ¥20 程序进入HardFault_Handler
    • ¥15 oracle集群安装出bug
    • ¥15 关于#python#的问题:自动化测试
    • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题