1
我有一個ESP8266,我正在編程在Arduino中。我需要做一個POST請求。我試過這段代碼:ESP8266和POST請求
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
void setup() {
Serial.begin(115200); //Serial connection
WiFi.begin("ssid", "wifi_password"); //WiFi connection
while (WiFi.status() != WL_CONNECTED) { //Wait for the WiFI connection completion
delay(500);
Serial.println("Waiting for connection");
}
}
void loop() {
if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status
HTTPClient http; //Declare object of class HTTPClient
http.begin("server_name"); //Specify request destination
http.header("POST/HTTP/1.1");
http.header("Host: server_name");
http.header("Accept: */*");
http.header("Content-Type: application/x-www-form-urlencoded");
int httpCode = http.POST("Message from ESP8266"); //Send the request
String payload = http.getString();
Serial.println(httpCode); //Print HTTP return code
Serial.println(payload); //Print request response payload
http.end(); //Close connection
}else{
Serial.println("Error in WiFi connection");
}
delay(5000); //Send a request every 30 seconds
}
但是在網頁上我沒有收到任何迴應。
「但在網頁上我沒有收到任何迴應」 - 什麼網頁?爲什麼回覆會在**網頁上顯示**? – Quentin
'http.header(「Content-Type:application/x-www-form-urlencoded」); '和'http.POST(「來自ESP8266的消息」);' - 那些不匹配。 – Quentin
在網頁上是mysql數據庫。什麼是正確的? – Matej