Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
792 views
in Technique[技术] by (71.8m points)

post - 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:

  • I am using a USB programmer that the ESP-01 plugs in to to do all this (called ESP-01S USB to ESP8266 ESP-01S Wireless Wifi Adapter Module Wi-Fi CH340G 4.5-5.5V, 115200 Baud Rate, if you wanna see the specific one), so not running the ESP-01 through like an arduino uno or anything, if that somehow matters
  • I know the sketch upload is working and the script is running fine, as the ESP-01 reports its connection to my network and my Wifi network reports a connection to the device
  • I know the trigger url works because it posts the tweet when I run it from within IFTTT Webhooks service and when I "curl" it from terminal
  • I tried to use GET instead, but it had its own issues, plus I am going to want to send sensor values, so want POST to work
  • I've tried both the urlencoded and json versions below, same result. Would prefer the uncommented json version, as am familiar with that
  • I am a total POST beginner, so am probably missing something stupid
  • It says timerDelay is 10 seconds but is set to 20. That's because I thought maybe the POST was just timing out? So I gave it more time
  • I also rolled back the ESP8266 library on Arduino IDE, per some Arduino forum rec. No help.
  • I tried flashing the ESP-01 with NodeMCU one time, didn't do anything. But not sure if that has to be done before every new sketch upload?

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();
  }
}
question from:https://stackoverflow.com/questions/65929804/http-response-code-1-from-ifttt-via-arduino-ide

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...