您需要将年、月、日、时、分、秒的数字转换为字符串后再进行字符串拼接。以下是修改后的代码:

void loop() { // wait for WiFi connection time_t now = time(nullptr); // Print formatted time to serial monitor if ((WiFi.status() == WL_CONNECTED)) {

WiFiClient client;
HTTPClient http;

Serial.print("[HTTP] begin...\n");
// configure traged server and url
http.begin(client, "http://" SERVER_IP "/upload");  // HTTP
http.addHeader("Content-Type", "application/json");

Serial.print("[HTTP] POST...\n");
// start connection and send HTTP header and body
String yearStr = String(year(now));
String monthStr = (month(now) < 10 ? "0" : "") + String(month(now));
String dayStr = (day(now) < 10 ? "0" : "") + String(day(now));
String hourStr = (hour(now) < 10 ? "0" : "") + String(hour(now));
String minuteStr = (minute(now) < 10 ? "0" : "") + String(minute(now));
String secondStr = (second(now) < 10 ? "0" : "") + String(second(now));
String timeString = yearStr + "-" + monthStr + "-" + dayStr + " " + hourStr + ":" + minuteStr + ":" + secondStr;
String postData = "{\"data\":\"world\",\"id\":\"world\",\"name\":\"world\",\"time\":\"" + timeString + "\"}";
int httpCode = http.POST(postData);
Serial.print(postData);
// httpCode will be negative on error
if (httpCode > 0) {
  // HTTP header has been send and Server response header has been handled
  Serial.printf("[HTTP] POST... code: %d\n", httpCode);

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

http.end();

}

delay(10000); }

PostHTTPClientino Created on 21112016#include WiFiUdph#include TimeLibh#define NTP_OFFSET 60 60 In seconds#define NTP_INTERVAL 60 1000 In miliseconds#define NTP_ADDRESS europepoolntporgWi

原文地址: https://www.cveoy.top/t/topic/bfzl 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录