ESP8266 HTTPcode -1. Error: Connection Refused

I'm using NodeMCU to use ESP8266 and I want to use ipify to get public IP address. But I get -1 on httpCode. Why is that?

If I type api.ipify.org, it gets my public IP address properly.

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
 
const char* ssid = "your ssid";
const char* password = "pass";
 
void setup () {
 
  Serial.begin(115200);
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
 
    delay(1000);
    Serial.println("Connecting..");
 
  }
 
}

void loop() {
  Serial.println(WiFi.status());
  if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
    Serial.println("az");
    HTTPClient http;  //Declare an object of class HTTPClient
    http.begin("https://api.ipify.org/?format=json");  //Specify request destination
    int httpCode = http.GET();                                                                  //Send the request
    Serial.println(httpCode);  //<<---- Here I get -1
    if (httpCode > 0) { //Check the returning code
      String payload = http.getString();   //Get the request response payload
      Serial.println(payload);                     //Print the response payload
    }
    http.end();   //Close connection
  }
  delay(10000);    //Send a request every 30 seconds
}

https is the problem

 // httpCode will be negative on error

The library either prints debug messages, or could. If it does, you need to share them. If it doesn't, you need to enable that output and then share it.

hello :slight_smile:
I made server with heroku and
host is http://myforever.herokuapp.com

as it is http, I thought it would be easy to request something and made server to receive and save parameter to database.

if user type http://myforever.herokuapp.com/turnon?id=haha&led=on ,
server get parameter haha / on and save it to database.

the problem is that I want to request above /turnon~ on esp8266.
but on httpcode part. it only comes with -1 which mean connection refused.

why is that?

I attached web info

for those who can't see image.
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
General
Request URL:http://myforever.herokuapp.com/
Request Method:GET
Status Code:304 Not Modified
Remote Address:174.129.23.39:80
Referrer Policy:no-referrer-when-downgrade
Response Headers
view source
Connection:keep-alive
Content-Length:0
Date:Wed, 24 Jan 2018 14:17:37 GMT
Etag:W/"d-NLsoItOWOA+NOkW0Qg+y8qHg3gw"
Server:Cowboy
Via:1.1 vegur
X-Powered-By:Express
Request Headers
view source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8
Accept-Encoding:gzip, deflate
Accept-Language:ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7
Connection:keep-alive
Host:myforever.herokuapp.com
If-None-Match:W/"d-NLsoItOWOA+NOkW0Qg+y8qHg3gw"
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

code is as belows.

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
 
const char* ssid = "ssid";
const char* password = "pwd";
 
void setup () {
 
  Serial.begin(115200);
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
 
    delay(1000);
    Serial.println("Connecting..");
 
  }
 
}
 
void loop() {
 Serial.println(WiFi.status());
  if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status

 Serial.println("az");
    HTTPClient http;  //Declare an object of class HTTPClient
 http.begin("myforever.herokuapp.com/turnon?id=haha&led=on");  
   
    int httpCode = http.GET();                                                                  //Send the request
 Serial.println(httpCode);
    if (httpCode > 0) { //Check the returning code
 
      String payload = http.getString();   //Get the request response payload
      Serial.println(payload);                     //Print the response payload
 
    }else {
            Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
        }
 
    http.end();   //Close connection
 
  }
 
  delay(10000);    //Send a request every 30 seconds
 
}

I made server with heroku

What is heroku?

host is http://myforever.herokuapp.com

Is that really the name of the ESP8266-as-server? Host names do NOT include the protocol.

 http.begin("myforever.herokuapp.com");

Did you look at the examples that come with the HTTPClient library? The begin() method parameter includes the protocol.

Where is the name of the script to execute on the server? Where is the data to pass to the server?

The request that the ESP is making looks very little like the one that your browser is making.

Posting ALL of your serial output will be necessary.

try:

http.begin("http://myforever.herokuapp.com/turnon?id=haha&led=on");

currently should return "both!"

Yours,
TonyWilk

Moderator: do not start (related) topics twice, ==> merged.