postarduinowebhooksesp8266ifttt

HTTP Response Code -1 from IFTTT via Arduino IDE


I am trying to set up an ESP8266 ESP-01 WiFi Module to post a tweet when sent some sensor data. For now, I'm just testing out the POST > Webhooks > IFTTT > Twitter workflow.

I get a response code of -1 (negative one) in the serial monitor when I run the below code. A few notes:

Here's my code. The trigger is real, so feel free to put in your own SECRET_KEY and post the app. Posts to @KreiderPlants on Twitter.

#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();
  }
}

Solution

  • Thanks to @hcheung in the comments for helping me figure this out. I was using http to send a POST request to an https site.

    I removed the 's' from the http address and it worked. If anyone else stumbles on this and needs to actually make a secure connection, check the github link in the comments.