esp8266post请求html,ESP8266和POST请求

Matej..

5

我找到了此代码,它可以正常工作!

#include

const char* ssid = "ssid";

const char* password = "password";

const char* host = "server_name";

void setup() {

Serial.begin(115200);

delay(100);

// We start by connecting to a WiFi network

Serial.println();

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() {

WiFiClient client;

const int httpPort = 80;

if (!client.connect(host, httpPort)) {

Serial.println("connection failed");

return;

}

String data = "pst=temperature>" + String(random(0,100)) +"||humidity>" + String(random(0,100)) + "||data>text";

Serial.print("Requesting POST: ");

// Send request to the server:

client.println("POST / HTTP/1.1");

client.println("Host: server_name");

client.println("Accept: */*");

client.println("Content-Type: application/x-www-form-urlencoded");

client.print("Content-Length: ");

client.println(data.length());

client.println();

client.print(data);

delay(500); // Can be changed

if (client.connected()) {

client.stop(); // DISCONNECT FROM THE SERVER

}

Serial.println();

Serial.println("closing connection");

delay(5000);

}

非常感谢您的帮助。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要让 ESP8266 接收 POST 请求,你需要执行以下步骤: 1. 通过 WiFi 连接到网络,并获取 IP 地址。 2. 创建一个 TCP 服务器并监听来自客户端的连接。 3. 在服务器上接收来自客户端的请求。 4. 解析请求中的数据。 5. 响应客户端的请求。 下面是一个示例代码,可以帮助你了解如何实现这些步骤: ```c #include <ESP8266WiFi.h> const char* ssid = "YOUR_SSID"; const char* password = "YOUR_PASSWORD"; WiFiServer server(80); void setup() { Serial.begin(115200); delay(10); // Connect to Wi-Fi network with SSID and password Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); // Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); // Start the server server.begin(); Serial.println("Server started"); } void loop() { // Check if a client has connected WiFiClient client = server.available(); if (!client) { return; } // Wait for data from the client while (!client.available()) { delay(1); } // Read the first line of the request String request = client.readStringUntil('\r'); Serial.println(request); // Parse the request to get the method, URL, and HTTP version String method = request.substring(0, request.indexOf(' ')); String url = request.substring(request.indexOf(' ') + 1, request.lastIndexOf(' ')); String httpVersion = request.substring(request.lastIndexOf(' ') + 1); // Check if the method is POST and the URL is correct if (method == "POST" && url == "/your-url") { // Read the headers while (client.available()) { String header = client.readStringUntil('\r'); if (header == "\n") { break; } } // Read the POST data String postBody = ""; while (client.available()) { postBody += client.readStringUntil('\r'); } // Print the POST data Serial.println(postBody); // Send the response client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connection: close"); client.println(); client.println("<!DOCTYPE HTML>"); client.println("<html>"); client.println("<head>"); client.println("<title>POST Request Received</title>"); client.println("</head>"); client.println("<body>"); client.println("<h1>POST Request Received</h1>"); client.println("<p>" + postBody + "</p>"); client.println("</body>"); client.println("</html>"); delay(100); client.stop(); } } ``` 在上面的代码中,将 `YOUR_SSID` 和 `YOUR_PASSWORD` 替换为你的 WiFi SSID 和密码。在 `loop()` 函数中,我们使用 `server.available()` 检查是否有客户端连接到服务器,并使用 `client.readStringUntil('\r')` 读取请求中的数据。然后,我们解析请求中的数据并检查是否为 POST 请求和正确的 URL。如果是 POST 请求,则读取 POST 数据,并向客户端发送响应。 请注意,此代码只是一个示例,你需要根据自己的需求进行修改。你可以将代码上传到 ESP8266 板子上并运行,然后使用 POST 请求测试它。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值