Going mad, simple http get wont work on Nodemcu

Hi,

i am going insane trying to get a simple http get to work with a Nodemcu
The below code should get the ETH price from coingecko but won't connect.
I can see it connect to my router so i know the wifi connection is ok.

Putting the url 'https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd' into a browser returns the price.

Please help, i dont know what i am doing wrong....


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

const char* ssid = ".....";
const char* password = "....";

void setup () {

  Serial.begin(115200);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {

    delay(1000);
    Serial.println("Connecting..");

  }

}

void loop() {

  if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status

    HTTPClient http;  //Declare an object of class HTTPClient

    http.begin("https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd");  //Specify request destination
    int httpCode = http.GET();     //Send the request                                                           

    if (httpCode > 0) { //Check the returning code

      Serial.println("CONNECTED");
      String payload = http.getString();   //Get the request response payload
      Serial.println(payload);                     //Print the response payload

    }

    http.end();   //Close connection

  }

  delay(30000);    //Send a request every 30 seconds

}

The issue is that you are attempting a HTTP connection using a HTTPS URL.
Read this article

OK, thanks for that bit of info, I was going insane!

Now, if I wanted to read the returned data from an api url like:
http://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd
How would I do this?

Simply using this url does not seem to work.

Yeah sorry the link i provided was quite incomplete.
By combining it with the information i found here and looking up the https port (433) and doing a bit of fiddling, with the BasicHttpsClient example, i came to this working version.

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h> 
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>

const char* ssid = "....";
const char* password = "Password";
const int httpsPort = 443; 

void setup () {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting..");
  }
}

void loop() {
  String host = "https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd"; 
  if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
    WiFiClientSecure client;
    HTTPClient http;
    client.setInsecure(); //the magic line, use with caution
    client.connect(host, httpsPort);
    http.begin(client, host);     
    int httpCode = http.GET();     //Send the request                                      
    if (httpCode > 0) { //Check the returning code
      Serial.println("CONNECTED");
      String payload = http.getString();   //Get the request response payload
      Serial.println(payload);                     //Print the response payload
    }
    http.end();   //Close connection
  }
  delay(3000);    //Send a request every 30 seconds
}

Compiled using core version 2.5.2 and run on an ESP-01s. Newer core versions should work, i had to upgrade from 2.4.2, since that does not support BearSSL.
If you want to do the whole fingerprint thing that is a different matter.

This works!
Thank you for your help, this is awesome !!!

The only thing to I have to do from here is extract the actual price as a variable, is that what you meant by fingerprint ?

No fingerprint is a method to create an actual secure connection instead of the setInsecure() . It is described in both links. You will have to parse the actual price. The easiest would be to find in the payload "usd": and read everything until the '}' marker and then convert it. But it is a completely different matter as well and you should be able to find a lot of examples on how to do this if you search the forum and other sites.

Thanks for your help, you have been amazing.

1 Like