Skip to content

Instantly share code, notes, and snippets.

@sticilface
Created November 13, 2015 15:25
Show Gist options
  • Save sticilface/3795d4e2b8047c729bbe to your computer and use it in GitHub Desktop.
Save sticilface/3795d4e2b8047c729bbe to your computer and use it in GitHub Desktop.
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <FS.h>
const char * ssid = "...";
const char * pass = "...";
const char * remotehost_git = "raw.githubusercontent.com";
const char * path_git = "/sticilface/ESPmanager/Strings/examples/Settingsmanager-example/data";
const char * raw_github_fingerprint = "B0 74 BB EF 10 C2 DD 70 89 C8 EA 58 A2 F9 E1 41 00 D3 38 82";
void setup() {
Serial.begin(115200);
Serial.setDebugOutput(true);
delay(500);
SPIFFS.begin();
WiFi.begin(ssid,pass);
uint8_t i = 0;
while (WiFi.waitForConnectResult() != WL_CONNECTED)
{
delay(500);
i++;
Serial.print(".");
}
Serial.println();
Serial.print(F("Free Heap: "));
Serial.println(ESP.getFreeHeap());
delay(5000);
HTTPSDownloadtoSPIFFS(remotehost_git, raw_github_fingerprint, path_git, "/config.htm");
}
void loop() {
}
void HTTPSDownloadtoSPIFFS(const char * remotehost, const char * fingerprint, const char * path, const char * file) {
const size_t buf_size = 1024;
uint8_t buf[buf_size];
const int httpsPort = 443;
WiFiClientSecure SecClient;
size_t totalbytes = 0;
SPIFFS.remove(file);
File f = SPIFFS.open(file, "w");
if (!f) {
Serial.println("file open failed");
return;
} else {
Serial.println("File Created");
delay(100);
Serial.printf("HOST: %s:%u\n", remotehost, httpsPort);
if (!SecClient.connect(remotehost, httpsPort)) {
Serial.println("Connection failed");
return;
} else {
Serial.printf("Connected to %s\n", remotehost);
if (SecClient.verify(fingerprint, remotehost)) {
Serial.println("certificate matches");
} else {
Serial.println("certificate doesn't match");
}
// send GET request
String request = "GET " + String(path) + String(file) + " HTTP/1.1\r\n";
request += "Host: " + String(remotehost) + "\r\n";
request += "User-Agent: BuildFailureDetectorESP8266\r\n";
request += "Accept: */*\r\n";
request += "Connection: close\r\n\r\n";
SecClient.print(request);
Serial.println("---- HEADER ---");
Serial.println(request);
Serial.println("---------------");
// wait up to 5 seconds for server response
Serial.println("Waiting for server response: ");
int i = 0;
while ((!SecClient.connected()) && (i < 500)) {
delay(100);
i++;
yield();
if ( i % 10 == 0) Serial.print(".");
}
yield();
Serial.println("---- BODY ---");
while (SecClient.available()) {
memset(buf, 0, buf_size);;
size_t length = SecClient.available();
length = (length > buf_size)? buf_size: length;
totalbytes += length;
SecClient.read(buf, length);
f.write(buf, length);
delay(1);
Serial.print(buf[0],length);
}
Serial.println("---------------");
Serial.printf("File %s %u Bytes\n", file, totalbytes);
SecClient.stop();
f.close();
} // is connected to remote host
} // managed to open file
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment