Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
&引用;发送有效负载错误“;在HTTP POST-Arduino ESP8266中_Http_Arduino_Http Post_Esp8266_Payload - Fatal编程技术网

&引用;发送有效负载错误“;在HTTP POST-Arduino ESP8266中

&引用;发送有效负载错误“;在HTTP POST-Arduino ESP8266中,http,arduino,http-post,esp8266,payload,Http,Arduino,Http Post,Esp8266,Payload,我正在尝试使用Arduino IDE通过ESP8266发出POST HTTP请求,但由于任何原因,我收到以下错误: [HTTP]POST。。。失败,错误:发送有效负载失败 Wifi连接的正确性和标题在图书馆中似乎得到了正确处理,邮递员也在处理同样的请求 通过在库中进行一些研究,特别是在,这段代码是触发错误的地方,这是因为调用\u tcp->write(&payload[0],size)返回0,而size是128(我发送的有效负载的正确长度) 在可变有效载荷中,我发送了以下内容(我在POSTMAN

我正在尝试使用Arduino IDE通过ESP8266发出POST HTTP请求,但由于任何原因,我收到以下错误:

[HTTP]POST。。。失败,错误:发送有效负载失败

Wifi连接的正确性和标题在图书馆中似乎得到了正确处理,邮递员也在处理同样的请求

通过在库中进行一些研究,特别是在,这段代码是触发错误的地方,这是因为调用
\u tcp->write(&payload[0],size)
返回
0
,而
size
128
(我发送的有效负载的正确长度)

在可变有效载荷中,我发送了以下内容(我在POSTMAN中也检查了这一点):

以下是我在Arduino IDE中的代码:

#include <Arduino.h>
#define DEBUG_ESP_HTTP_CLIENT
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

//#include <ESP8266HTTPClient.h>

#define USE_SERIAL Serial

#include "ESP8266HTTPClient2.h"

ESP8266WiFiMulti WiFiMulti;

void setup() {
    USE_SERIAL.begin(115200);

    USE_SERIAL.println();

    for(uint8_t t = 4; t > 0; t--) {
        USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
        USE_SERIAL.flush();
        delay(1000);
    }

    WiFiMulti.addAP("xxxxxxxxxxxx", "yyyyyyyyyyyyyy");
}

void loop() {
 // wait for WiFi connection
    if((WiFiMulti.run() == WL_CONNECTED)) {

        HTTPClient http;

        USE_SERIAL.print("\n---------------------\n[HTTP] begin...\n");
        // configure traged server and url

        http.setReuse(true);
        http.begin("iotmmsp1941648609trial.hanatrial.ondemand.com",
                    443,
                    "/com.sap.iotservices.mms/v1/api/http/data/ab5b0ae0-9e2c-47e2-ac93-0deea935a588");

        http.setAuthorization("Bearer 77eb2493769bb566f5bc346f6d598e7");
        //http.addHeader("Authorization", "Bearer 77eb2493769bb566f5bc346f6d598e7");
        http.addHeader("Content-Type", "application/json;charset=utf-8");
        http.addHeader("cache-control", "no-cache");

        USE_SERIAL.print("[HTTP] POST...\n");
        // start connection and send HTTP header and body
        int httpCode = http.POST("{\"mode\":\"sync\",\"messageType\":\"4a0f64bf6fc780e39a51\",\"messages\":[{\"Humidity\":90,\"Temperature\":50,\"Brightness\":60,\"timestamp\":1}]}");

        // httpCode will be negative on error
        if(httpCode > 0) {
            // HTTP header has been send and Server response header has been handled
            USE_SERIAL.printf("[HTTP] POST... code: %d\n", httpCode);

            // file found at server
            if(httpCode == HTTP_CODE_OK) {
                String payload = http.getString();
                USE_SERIAL.println(payload);
            }
        } else {
            USE_SERIAL.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
        }

        http.end();
    }

    delay(3000);
}
#包括
#定义调试\u ESP\u HTTP\u客户端
#包括
#包括
//#包括
#定义使用\u串行
#包括“ESP8266HTTPClient2.h”
ESP8266WiFiMulti WiFiMulti;
无效设置(){
使用_SERIAL.begin(115200);
使用_SERIAL.println();
对于(uint8_t=4;t>0;t--){
使用_SERIAL.printf(“[设置]等待%d..\n”,t);
使用_SERIAL.flush();
延迟(1000);
}
WiFiMulti.addAP(“xxxxxxxxxxx”、“yyyyyyyyyyyyyyyy”);
}
void循环(){
//等待WiFi连接
如果((WiFiMulti.run()==WL_已连接)){
HTTPClient-http;
使用_SERIAL.print(“\n------------------\n[HTTP]begin…\n”);
//配置悲剧服务器和url
http.setReuse(true);
http.begin(“iotmmp1941648609trial.hanatrial.ondemand.com”,
443,
“/com.sap.iotservices.mms/v1/api/http/data/ab5b0ae0-9e2c-47e2-ac93-0deea935a588”);
http.setAuthorization(“承载77eb2493769bb566f5bc346f6d598e7”);
//http.addHeader(“授权”、“承载77eb2493769bb566f5bc346f6d598e7”);
addHeader(“内容类型”,“应用程序/json;字符集=utf-8”);
addHeader(“缓存控制”、“无缓存”);
使用_SERIAL.print(“[HTTP]POST…\n”);
//启动连接并发送HTTP头和正文
int-httpCode=http.POST(“{”模式\“:”同步\“,”消息类型\“:”4a0f64bf6fc780e39a51 \“,”消息\“:[{”湿度\“:90,\”温度\“:50,\”亮度\“:60,\”时间戳\“:1}”);
//httpCode在出现错误时为负值
如果(httpCode>0){
//HTTP标头已发送,服务器响应标头已处理
使用_SERIAL.printf(“[HTTP]POST…代码:%d\n”,httpCode);
//在服务器上找到文件
如果(httpCode==HTTP\u代码\u确定){
字符串有效负载=http.getString();
使用_SERIAL.println(有效载荷);
}
}否则{
使用_SERIAL.printf(“[HTTP]POST…失败,错误:%s\n”,HTTP.errorToString(httpCode.c_str());
}
http.end();
}
延迟(3000);
}
问题:


为什么ESP8266HTTPClient中的那部分代码失败了?为什么它没有正确发送有效负载

您正在使用一长串常量字符串(大约600个字符),而没有使用
F()
宏将它们存储到代码中。你有足够的内存来存储它们吗?我刚刚测试过。这似乎不是问题。你确定
\u tcp->write(&payload[0],size)
返回
0
(如果条件是返回值大小)?是的,我修改了库(必须将文件复制到本地pc)并打印了值。类型
std::unique\u ptr\u tcp
表示调用
size\u t WiFiClient::write(const uint8\u t*buf,size\u t size)
,返回的
0
可以是
(!\u client | | size)
!!!
{\"mode\":\"sync\",\"messageType\":\"4a0f64bf6fc780e39a51\",\"messages\":[{\"Humidity\":90,\"Temperature\":50,\"Brightness\":60,\"timestamp\":1}]}
#include <Arduino.h>
#define DEBUG_ESP_HTTP_CLIENT
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

//#include <ESP8266HTTPClient.h>

#define USE_SERIAL Serial

#include "ESP8266HTTPClient2.h"

ESP8266WiFiMulti WiFiMulti;

void setup() {
    USE_SERIAL.begin(115200);

    USE_SERIAL.println();

    for(uint8_t t = 4; t > 0; t--) {
        USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
        USE_SERIAL.flush();
        delay(1000);
    }

    WiFiMulti.addAP("xxxxxxxxxxxx", "yyyyyyyyyyyyyy");
}

void loop() {
 // wait for WiFi connection
    if((WiFiMulti.run() == WL_CONNECTED)) {

        HTTPClient http;

        USE_SERIAL.print("\n---------------------\n[HTTP] begin...\n");
        // configure traged server and url

        http.setReuse(true);
        http.begin("iotmmsp1941648609trial.hanatrial.ondemand.com",
                    443,
                    "/com.sap.iotservices.mms/v1/api/http/data/ab5b0ae0-9e2c-47e2-ac93-0deea935a588");

        http.setAuthorization("Bearer 77eb2493769bb566f5bc346f6d598e7");
        //http.addHeader("Authorization", "Bearer 77eb2493769bb566f5bc346f6d598e7");
        http.addHeader("Content-Type", "application/json;charset=utf-8");
        http.addHeader("cache-control", "no-cache");

        USE_SERIAL.print("[HTTP] POST...\n");
        // start connection and send HTTP header and body
        int httpCode = http.POST("{\"mode\":\"sync\",\"messageType\":\"4a0f64bf6fc780e39a51\",\"messages\":[{\"Humidity\":90,\"Temperature\":50,\"Brightness\":60,\"timestamp\":1}]}");

        // httpCode will be negative on error
        if(httpCode > 0) {
            // HTTP header has been send and Server response header has been handled
            USE_SERIAL.printf("[HTTP] POST... code: %d\n", httpCode);

            // file found at server
            if(httpCode == HTTP_CODE_OK) {
                String payload = http.getString();
                USE_SERIAL.println(payload);
            }
        } else {
            USE_SERIAL.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
        }

        http.end();
    }

    delay(3000);
}