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.
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.
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.