来自 IFTTT 通过 Arduino 的 HTTP 响应代码 -1 IDE

HTTP Response Code -1 from IFTTT via Arduino IDE

我正在尝试设置一个 ESP8266 ESP-01 WiFi 模块,以便在发送一些传感器数据时 post 发一条推文。现在,我只是在测试 POST > Webhooks > IFTTT > Twitter 工作流程。

当我运行下面的代码时,我在串口监视器中得到了-1(负一)的响应代码。一些注意事项:

这是我的代码。触发器是真实的,所以请随意放入您自己的 SECRET_KEY 和 post 应用程序。在 Twitter 上向@KreiderPlants 发帖。

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>

const char* ssid     = "MY_SSID";
const char* password = "SSID_PW";

const char* serverName = "https://maker.ifttt.com/trigger/tweet_from_esp/with/key/SECRET_KEY";
// const char* serverName = "maker.ifttt.com";

// THE DEFAULT TIMER IS SET TO 10 SECONDS FOR TESTING PURPOSES
// For a final application, check the API call limits per hour/minute to avoid getting blocked/banned
unsigned long lastTime = 0;
// Set timer to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Timer set to 10 seconds (10000)
unsigned long timerDelay = 20000;

void setup() 
{
  Serial.begin(115200);
  delay(100);

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  // WiFi.mode(WIFI_STA);
  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());  

  Serial.println("Timer set to 10 seconds (timerDelay variable), it will take 10 seconds before publishing the first reading.");

  // Random seed is a number used to initialize a pseudorandom number generator
  randomSeed(analogRead(0));
}
int value = 0;

void loop() {
  //Send an HTTP POST request every 10 seconds
  if ((millis() - lastTime) > timerDelay) {
    //Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED){
      HTTPClient http;
      
      // Your Domain name with URL path or IP address with path
      http.begin(serverName);
      
      // Specify content-type header
      // http.addHeader("Content-Type", "application/x-www-form-urlencoded");
      // Data to send with HTTP POST
      // String httpRequestData  = "value1=" + String(random(2)) + "&value2=" + String(random(2))+ "&value3=" + String(random(2));           
      // Send HTTP POST request
      // int httpResponseCode = http.POST(httpRequestData);
      
      
      //   If you need an HTTP request with a content type: application/json, use the following:
      http.addHeader("Content-Type", "application/json");
      // JSON data to send with HTTP POST
      String httpRequestData = "{\"value1\":\"" + String(random(4)) + "\",\"value2\":\"" + String(random(4)) + "\",\"value3\":\"" + String(random(4)) + "\"}";
      // Send HTTP POST request
      int httpResponseCode = http.POST(httpRequestData);
      
     
      Serial.print("HTTP Response code: ");
      Serial.println(httpResponseCode);
        
      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}

感谢评论中的@hcheung 帮助我解决了这个问题。我正在使用 http 向 https 站点发送 POST 请求。

我从 http 地址中删除了 's',它起作用了。如果其他人偶然发现了这一点并且需要真正建立安全连接,请检查评论中的 github link。