从Arduino向本地服务器发送数据时,HTTP响应代码301



我是Arduino和网络的新手,并试图将我的ESP32 CAM连接到我的本地WiFi网络,并从ESP32中的SD卡将文件发送到我PC中的文件夹(作为本地服务器)。除发送部分外,一切正常。

ESP32 CAM连接WiFi网络,可以从SD卡读取,没有任何问题。但是,它无法将数据发送到本地机器。

我在我的电脑上使用XAMPP堆栈。

这是连接到网络的配置细节:

#include <WiFi.h>
#include <HTTPClient.h>
#include "SD_MMC.h"
const char* ssid = "name";
const char* password = "pass";
const char* server = "192.168.0.106";
const int port = 8080;
const String path = "/ProjectFolder";
const char* filename = "/data.txt";//Name of Text File

这是Post Request(从SD卡读取后)

http.addHeader("Content-Type", "text/plain");
int httpCode = http.POST(data);
http.addHeader("Location", "192.168.0.106:8080/ProjectFolder/");
String response = http.getString();
http.end();
// Check HTTP response
if (httpCode > 0) {
Serial.printf("HTTP response code: %dn", httpCode);
Serial.println("HTTP response body: " + response);
} else {
Serial.println("HTTP request failed");
}
然而,我得到的错误是:
HTTP response code: 301
HTTP response body: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://192.168.0.106:8080/ProjectFolder/">here</a>.</p>
<hr>
<address>Apache/2.4.54 (Win64) OpenSSL/1.1.1p PHP/8.2.0 Server at 192.168.0.106 Port 8080</address>
</body></html>

我尝试过的事情:

  • 我已经尝试使用这个192.162.0.106:8080/ProjectFolder从我的手机访问相同的url。
  • 我也尝试用/ProjectFolder/
  • 替换路径

该目录存储在我的PC上,不包含任何文件。

如果您需要更清晰,这里是完整的代码:

#include <WiFi.h>
#include <HTTPClient.h>
#include "SD_MMC.h"
const char* ssid = "name";
const char* password = "pass";
const char* server = "192.168.0.106";
const int port = 8080;
const String path = "/ProjectFolder";
const char* filename = "/data.txt"; // replace with your text file name on SD card

void setup() {
Serial.begin(115200);
delay(1000);
// Connect to Wi-Fi network
Serial.print("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("Connected to WiFi");
// Initialize SD card
if (!SD_MMC.begin()) {
Serial.println("SD card initialization failed");
return;
}
Serial.println("SD card initialized");
}
void loop() {
// Wait for a few seconds
delay(5000);

// Check if connected to WiFi
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi connection lost");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("Reconnected to WiFi");
}

// Make HTTP request to server
HTTPClient http;
http.begin("http://" + String(server) + ":" + String(port) + path);

// Open the text file from SD card
File file = SD_MMC.open(filename);
if (!file) {
Serial.println("Failed to open file");
return;
}
// Read the text file into a String object
String data = "";
while (file.available()) {
data += char(file.read());
}
file.close();
// Send the text file contents to the server
http.addHeader("Content-Type", "text/plain");
int httpCode = http.POST(data);
http.addHeader("Location", "192.168.0.106:8080/ProjectFolder/");
String response = http.getString();
http.end();

// Check HTTP response
if (httpCode > 0) {
Serial.printf("HTTP response code: %dn", httpCode);
Serial.println("HTTP response body: " + response);
} else {
Serial.println("HTTP request failed");
}
}

看起来您使用的是旧版本的esp8266 Arduino平台。在当前版本(3+)ESP9266HTTPClient的begin方法需要作为第一个参数的WiFiClient或WiFiClientSecure类的实例。

您的服务器需要安全连接(即SSL)。提供给http.begin的WiFiClientSecure实例将处理安全连接。

关于如何初始化和使用WiFiClientSecure对象,请参阅ESP8266HTTPClient库当前版本的BasicHttpsClient示例。

下面是没有设置SSL指纹的简化代码片段:
WiFiClientSecure client;
client.setInsecure();
HTTPClient http;
http.begin(client, "http://" + String(server) + ":" + String(port) + path);

相关内容

  • 没有找到相关文章

最新更新