一直试图让这个网络请求工作一段时间,但没有成功。
浏览器上的相同请求效果很好(在 ESP32 生成它时从串行终端复制):caxiasmed.com/esp32/insert.php?jd=1234567&area=Area01&type=h&value=125 欢迎帮助。代码如下。谢谢保罗
#include <WiFi.h>
const char* ssid = "SkyNet";
const char* password = "ba21ca";
const char* host = "caxiasmed.com";
int jd = 1234567;
char *area = "Area01";
char type = 'h';
int value = 125;
void setup(){
Serial.begin(115200);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop(){
String url;
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
url = "/esp32/insert.php?";
url += "jd=";
url += jd;
url += "&area=";
url += area;
url += "&type=";
url += 'h';
url += "&value=";
url += value;
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
/*
Serial.println("#################################URL:");
Serial.print("GET ");
Serial.print(url);
Serial.println(" HTTP/1.1");
Serial.print("Host: ");
Serial.println(host);
Serial.println("Connection: close");
Serial.println();
Serial.println("#####################################");
*/
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
delay(15000);
}
当我使用串行控制台运行草图时,我得到了 WiFi 连接的确认。在串行控制台中,当执行以下脚本部分时,我还确认与“主机”的 http 连接成功发生:
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
出现问题,Web 服务器返回一个 html 格式的错误消息,当执行以下行时,该错误消息将打印在串行控制台上:
// Read all the lines of the reply from server and print them to Serial
while(client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
此响应包含消息 400 BAD REQUEST,就好像上面的 GET 格式不正确一样。
作为参考,我包括了之前发布的 ESP32 代码实际调用的 insert.php:
<?php
include('connection.php');
$jd = $_GET['jd'];
$area = $_GET['area'];
$type = $_GET['type'];
$value = $_GET['value'];
$sql = "INSERT INTO activity (jd,area,type,value) VALUES (:jd,:area,:type,:value)";
$stmt = $PDO->prepare($sql);
$stmt->bindParam(':jd',$jd);
$stmt->bindParam(':area',$area);
$stmt->bindParam(':type',$type);
$stmt->bindParam(':value',$value);
if($stmt->execute()) {
echo "Sucessful SQL Insertion";
} else {
echo "SQL Insert Failed.";
}
?>
正如我所说,如果我从监视正在运行的应用程序的串行终端屏幕复制 GET 请求行并将其粘贴到 Web 浏览器上,它将按照上面的 php 代码成功地将数据插入 SQL 数据库中。
欢迎协助。谢谢保罗博尔赫斯