I am using a NodeMCU and I'm trying to get the following code to work using the HTTPClient library:
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
const char* ssid = "........";
const char* password = "........";
String deviceID = "testing123abcxyz";
void setup() {
Serial.begin(115200);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
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());
// Post data to dweet.io
HTTPClient http;
String URL = "http://dweet.io/dweet/for/" + deviceID + "?foo=bar&test=123"; // Works with HTTP
http.begin(URL); // Works with HTTP
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println(payload); // Print response
}
http.end();
}
void loop() {
// Nothing here
}
If I change line 25 to "https://" instead of "http://" then it doesn't work. I know that this library can work with HTTPS with a fingerprint, but what if I don't have one? I also tried "http.begin(URL, "");" to pass an empty fingerprint parameter in the function but that obviously didn't work either.
It's way to late but this post is one one of the ones that gets returned by google so it's a good way to help others like me who are just starting out with this.
Here's a working example using the default HTTPClient. It does require setting the certificate's sha1 fingerprint in the begin statement.
void loop() {
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED)) {
HTTPClient http;
Serial.print("[HTTPS] begin...\n");
http.begin("https://some.secure_server.com/auth/authorise", "2F 2A BB 23 6B 03 89 76 E6 4C B8 36 E4 A6 BF 84 3D DA D3 9F");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpCode = http.POST("user_id=mylogin&user_password=this%20is%20my%20%24ecret%20pa%24%24word");
if (httpCode > 0) {
http.writeToStream(&Serial);
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] ... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTP] ... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
delay(10000);
}
After trying a number of approaches, the solution came (for my specific situation) after combining the question by @androidfanboy, and the answers by @isoboy and @m12lrpv.
My code ended up like this:
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
const char* ssid = "xxx";
const char* password = "xxxxxxx";
const char* url = "https://api.angelcam.com/v1/events/";
const char* fingerprint = "51 87 10 92 52 38 41 A9 SD 23 V3 23 72 58 SE EF 06 8D 35 4C";
void setup() {
unsigned short count = 0;
Serial.begin(115200);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
count++;
if (count >= 30)
wifiRestart();
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void wifiRestart(){
Serial.println("Turning WiFi off...");
WiFi.mode(WIFI_OFF);
Serial.println("Sleepping for 10 seconds...");
delay(10000);
Serial.println("Trying to connect to WiFi...");
WiFi.mode(WIFI_STA);
}
void loop() {
Serial.println("test");
short response_code = 0;
// wait for WiFi connection
if ((WiFi.status() == WL_CONNECTED)) {
HTTPClient http;
Serial.print("[HTTPS] begin...\n");
http.begin(url, fingerprint);
http.addHeader("Content-Type", "application/json");
int httpCode = http.POST("{\"hash\": \"34jh34uh3v\"}");
if (httpCode > 0) {
http.writeToStream(&Serial);
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] ... code: %d\n", httpCode);
// file found at server
if (httpCode >= 200 and httpCode <= 299) {
response_code = 1;
String payload = http.getString();
Serial.println(payload);
}
}
else {
Serial.printf("[HTTP] ... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
else {
wifiRestart();
}
if (response_code){
Serial.println("All fine. Going to sleep...");
ESP.deepSleep(0);
}
}