乐鑫技术交流
直播中

carey123

10年用户 1524经验值
擅长:可编程逻辑 嵌入式技术
私信 关注
[问答]

ESP8266 JSON POST Https请求,响应码-1是怎么回事?

你好!我目前正在尝试使用我的 ESP8266 将 JSON 正文中的一些值发送到数据库。
当我通过 Postman 发送正文时,一切都很好。我试图按照在线教程进行操作,但是当我实现我的端点、api 密钥和正文时,我得到了响应代码“-1”。
主板:LOLIN (WEMOS) D1 mini lite
代码:
代码:全选#include
#include
#include

void setup() {

  Serial.begin(115200);                            //Serial connection
  WiFi.begin("ssid", "pass");   //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

    StaticJsonBuffer<300> JSONbuffer;   //Declaring static JSON buffer
    JsonObject& JSONencoder = JSONbuffer.createObject();

    JSONencoder["article"] = "3 mm";
    JSONencoder["amount"] = 23;

    char JSONmessageBuffer[300];
    JSONencoder.prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
    Serial.println(JSONmessageBuffer);

    HTTPClient http;    //Declare object of class HTTPClient

    http.begin("URL/endpoint");      //Specify request destination
    http.addHeader("Content-Type", "application/json");  //Specify content-type header
    http.addHeader("auth-key", "Bearer ");  //Specify authorization header
   
    int httpCode = http.POST(JSONmessageBuffer);   //Send the request
    String payload = http.getString();                                        //Get the response payload
   
    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(300);  //Send a request every 30 seconds

}


串口
代码:全选Waiting for connection
{
  "article": "3 mm",
  "amount": 23
}

-1



回帖(1)

楼斌

2024-1-17 15:59:07
响应码-1是通常意味着 ESP8266 没有能够建立与服务器的连接,这可能因为多种原因引起,例如:

1. 无法访问服务器:如果服务器不可用或离线,或者你的网络连接不稳定,可能会导致无法与服务器建立连接,从而导致响应码-1。

2. SSL 证书问题:如果服务器使用 SSL 证书,但是你的 ESP8266 没有正确验证服务器证书或没有正确配置 SSL 连接,可能会导致无法建立连接并出现响应码-1。

3. 接口错误:如果实现的端点或 API 密钥不正确或未正确配置,也可能会导致响应码-1。

您可以尝试重新检查您的代码并确保正确配置所有参数。您还可以尝试使用 Wi-Fi ClientSecure 对象建立安全连接或使用其他工具来测试您的 POST 请求。
举报

更多回帖

发帖
×
20
完善资料,
赚取积分