Skip to content

Instantly share code, notes, and snippets.

@davedarko
Last active October 6, 2018 09:47
Show Gist options
  • Save davedarko/4cf6183a682c60b31bc28f3f62298bbc to your computer and use it in GitHub Desktop.
Save davedarko/4cf6183a682c60b31bc28f3f62298bbc to your computer and use it in GitHub Desktop.
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
/*
SHA1 fingerprints found in certificate
*/
// tindie
String url = "/api/v1/order/?limit=1&shipped=false&format=json&username=davedarko&api_key=YOURAPIKEY";
const char* host = "www.tindie.com";
const char* fingerprint = "BC 73 A5 9C 6E EE 38 43 A6 37 FC 32 CF 08 16 DC CF F1 5A 66";
const char* ssid = "YOURSSID";
const char* password = "YOURPASSWORD";
const int httpsPort = 443;
WiFiClientSecure client;
char json_buf[2048];
int total_count = 0;
void setup()
{
Serial.begin(115200);
Serial.println();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(50);
}
}
void loop()
{
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}
if (client.verify(fingerprint, host))
{
Serial.println("certificate matches");
}
else
{
Serial.println("certificate doesn't match");
}
yield();
// text display tests
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n" +
"Connection: close\r\n\r\n");
Serial.println();
Serial.println("+--------------------------+");
Serial.println("| new round |");
Serial.println("+--------------------------+");
Serial.print(host);
Serial.println(url);
Serial.println("request sent");
int counter = 0;
strcpy(json_buf, "");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
while (
client.available() &&
counter < 2047
) {
json_buf[counter++] = client.read();
}
json_buf[counter++] = '\0';
Serial.print("counter: ");
Serial.println(counter);
Serial.print("buffer: ");
Serial.println(json_buf);
yield();
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json_buf);
yield();
int delay_time = 0;
if (root.success()) {
total_count = root["meta"]["total_count"];
delay_time = 5 * 60 * 1000;
} else {
// parseOb
Serial.println("error while decoding");
}
Serial.print("total_count: ");
Serial.println(total_count);
Serial.print("getFreeHeap: ");
Serial.println(ESP.getFreeHeap());
delay(delay_time);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment