Skip to content

Instantly share code, notes, and snippets.

@martinschierle
Last active December 22, 2019 23:28
Show Gist options
  • Save martinschierle/99a80d9b909821cd76bdd956f0932783 to your computer and use it in GitHub Desktop.
Save martinschierle/99a80d9b909821cd76bdd956f0932783 to your computer and use it in GitHub Desktop.
Use ESP32 to get Google Lighthouse results from REST API and show via NeoPixel
#include <WiFi.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include <FastLED.h>
// Adjust these as you see need
const char* ssid = "";
const char* password = "";
const String URL = "https://www.spiegel.de";
const String targetMetric = "first-contentful-paint";
#define NUM_LEDS 16
#define DATA_PIN 2
/// adjust these end
const String PSI_URL = "https://www.googleapis.com/pagespeedonline/v5/runPagespeed?category=performance&strategy=mobile&url=";
const String targetString = "\"" + targetMetric + "\": ";
CRGB leds[NUM_LEDS];
unsigned long lastUpdate = -1;
float score;
String urlencode(String str)
{
String encodedString="";
char c;
char code0;
char code1;
char code2;
for (int i =0; i < str.length(); i++){
c=str.charAt(i);
if (c == ' '){
encodedString+= '+';
} else if (isalnum(c)){
encodedString+=c;
} else{
code1=(c & 0xf)+'0';
if ((c & 0xf) >9){
code1=(c & 0xf) - 10 + 'A';
}
c=(c>>4)&0xf;
code0=c+'0';
if (c > 9){
code0=c - 10 + 'A';
}
code2='\0';
encodedString+='%';
encodedString+=code0;
encodedString+=code1;
//encodedString+=code2;
}
yield();
}
return encodedString;
}
void setup() {
Serial.begin(115200);
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
//LEDS.setBrightness(BRIGHTNESS);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting...");
}
Serial.println("Connected");
}
void updateScore() {
lastUpdate = millis();
fill_solid(leds, NUM_LEDS, CHSV( 160, 255, 10));
FastLED.show();
if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
HTTPClient http; //Declare an object of class HTTPClient
Serial.println("Querying PageSpeed Insights API...this can take a bit! Test-URL: ");
String testUrl = PSI_URL + urlencode(URL);
Serial.println(testUrl);
http.setTimeout(60000);
http.begin(testUrl,"A1 9F 59 BC 0C 9C 8A 56 5C 3E 0F 87 E3 AA BB 96 90 CB 9A 16");
int httpCode = http.GET();//Send the request
if(httpCode == HTTP_CODE_OK){
Serial.println("Got response!");
WiFiClient * stream = http.getStreamPtr();
String result = "";
boolean startFound = false;
boolean endFound = false;
while(http.connected()) {
size_t size = stream->available();
char jsonBuffer[128]; // verify this
if(size) {
// read up to 128 byte
int c = stream->readBytes(jsonBuffer, ((size > 128) ? 128 : size));
for (int i = 0; i<c; ++i) {
char ch = (char) jsonBuffer[i];
result += ch;
if(result.endsWith(targetString)) {
Serial.println("Found start marker");
startFound = true;
result = "";
}
if(startFound && result.endsWith("}")) {
Serial.println("Found end marker");
endFound = true;
break;
}
}
}
if(result.length() > 200 && !startFound) result = result.substring(100);
if(endFound) break;
}
//Serial.println("Got result");
//Serial.println(result);
DynamicJsonDocument root(6*1024);
Serial.println("Parsing result");
deserializeJson(root, result);
score = root["score"];
Serial.println("Got score:");
Serial.println(score);
}
else {
Serial.println("Got error: " + httpCode);
}
}
}
void loop() {
if(lastUpdate == -1 || millis() - lastUpdate > 3*60*1000) {
updateScore();
}
FastLED.clear();
FastLED.show();
int color = 0;
if(score > 0.6) color = 40;
if(score > 0.8) color = 80;
int index = (millis()/150)%NUM_LEDS;
leds[(index)%NUM_LEDS] = CHSV( color, 255, 10);
leds[(index + 1)%NUM_LEDS] = CHSV( color, 255, 40);
leds[(index + 2)%NUM_LEDS] = CHSV( color, 255, 100);
leds[(index + 3)%NUM_LEDS] = CHSV( color, 255, 40);
leds[(index + 4)%NUM_LEDS] = CHSV( color, 255, 10);
FastLED.show();
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment