Skip to content

Instantly share code, notes, and snippets.

@NiteBlock
Last active July 23, 2019 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NiteBlock/20050d35fee91c44f7e591c947ec68fd to your computer and use it in GitHub Desktop.
Save NiteBlock/20050d35fee91c44f7e591c947ec68fd to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <Adafruit_NeoPixel.h>
#include <WiFiClientSecureBearSSL.h>
// Fingerprint expires soon, needs to be updated
const uint8_t fingerprint[20] = {0x08, 0x3b, 0x71, 0x72, 0x02, 0x43, 0x6E, 0xCA, 0xED, 0x42, 0x86, 0x93, 0xBA, 0x7E, 0xDF, 0x81, 0xC4, 0xBC, 0x62, 0x30};
ESP8266WiFiMulti WiFiMulti;
Adafruit_NeoPixel tira(8, 2, NEO_GRB + NEO_KHZ800);
void setup()
{
tira.begin()
Serial.begin(115200);
// Serial.setDebugOutput(true);
Serial.println();
Serial.println();
Serial.println();
for (uint8_t t = 4; t > 0; t--)
{
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("Be_Curious", "pulpoparacomer");
}
void loop()
{
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED))
{
std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
client->setFingerprint(fingerprint);
HTTPClient https;
Serial.print("[HTTPS] begin...\n");
if (https.begin(*client, "https://platform-device.herokuapp.com/list-devices"))
{ // HTTPS
Serial.print("[HTTPS] GET...\n");
int httpCode = https.GET();
if (httpCode > 0)
{
Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)
{
StaticJsonDocument<808> doc;
String payload = https.getString();
deserializeJson(doc, payload);
String name = doc[0]["name"];
String type = doc[0]["type"];
int integerStatus = doc[0]["status"];
String value = String(integerStatus);
Serial.print("Name: " + name + "\n");
Serial.print("Type: " + type + "\n");
Serial.print("Value: " + value + "\n");
int b = integerStatus % 1000;
integerStatus = (integerStatus-b) / 1000;
int g = integerStatus % 1000;
integerStatus = (integerStatus-g) / 1000;
int r = integerStatus % 1000;
Serial.print(r);
Serial.print(g);
Serial.print(b);
tira.setPixelColor(0, tira.Color(r,g,b));
}
}
else
{
Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
}
https.end();
}
else
{
Serial.printf("[HTTPS] Unable to connect\n");
}
}
Serial.println("Wait 1s before next round...");
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment