Skip to content

Commit

Permalink
Merge pull request #262 from Qrome/3.03
Browse files Browse the repository at this point in the history
Updated to include the Pi-Hole API key to fix graghing
  • Loading branch information
Qrome authored Jan 30, 2023
2 parents 20b990c + ad9c35e commit 0df78b1
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 16 deletions.
Binary file not shown.
Binary file not shown.
24 changes: 19 additions & 5 deletions marquee/PiHoleClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,19 @@ PiHoleClient::PiHoleClient() {
//Constructor
}

void PiHoleClient::getPiHoleData(String server, int port) {
void PiHoleClient::getPiHoleData(String server, int port, String apiKey) {

WiFiClient wifiClient;
errorMessage = "";
String response = "";

String apiGetData = "http://" + server + ":" + String(port) + "/admin/api.php?summary";
if (apiKey == "") {
errorMessage = "Pi-hole API Key is required to view Summary Data.";
Serial.println(errorMessage);
return;
}

String apiGetData = "http://" + server + ":" + String(port) + "/admin/api.php?summary&auth=" + apiKey;
Serial.println("Sending: " + apiGetData);
HTTPClient http; //Object of class HTTPClient
http.begin(wifiClient, apiGetData);// get the result
Expand Down Expand Up @@ -96,6 +102,7 @@ void PiHoleClient::getTopClientsBlocked(String server, int port, String apiKey)

if (apiKey == "") {
errorMessage = "Pi-hole API Key is required to view Top Clients Blocked.";
Serial.println(errorMessage);
return;
}

Expand Down Expand Up @@ -146,19 +153,26 @@ void PiHoleClient::getTopClientsBlocked(String server, int port, String apiKey)
Serial.println();
}

void PiHoleClient::getGraphData(String server, int port) {
void PiHoleClient::getGraphData(String server, int port, String apiKey) {
WiFiClient wifiClient;
HTTPClient http;

String apiGetData = "http://" + server + ":" + String(port) + "/admin/api.php?overTimeData10mins";
errorMessage = "";

if (apiKey == "") {
errorMessage = "Pi-hole API Key is required to view Graph Data.";
Serial.println(errorMessage);
return;
}

String apiGetData = "http://" + server + ":" + String(port) + "/admin/api.php?overTimeData10mins&auth=" + apiKey;
resetBlockedGraphData();
Serial.println("Getting Pi-Hole Graph Data");
Serial.println(apiGetData);
http.begin(wifiClient, apiGetData);
int httpCode = http.GET();

String result = "";
errorMessage = "";
boolean track = false;
int countBracket = 0;
blockedCount = 0;
Expand Down
5 changes: 2 additions & 3 deletions marquee/PiHoleClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ SOFTWARE.
#pragma once
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include "libs/ArduinoJson/ArduinoJson.h"

class PiHoleClient {
Expand Down Expand Up @@ -72,8 +71,8 @@ class PiHoleClient {

public:
PiHoleClient();
void getPiHoleData(String server, int port);
void getGraphData(String server, int port);
void getPiHoleData(String server, int port, String apiKey);
void getGraphData(String server, int port, String apiKey);
void getTopClientsBlocked(String server, int port, String apiKey);

String getDomainsBeingBlocked();
Expand Down
3 changes: 2 additions & 1 deletion marquee/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ String NEWS_SOURCE = "reuters"; // https://newsapi.org/sources to get full list
// DIN -> D7 (MOSI)
const int pinCS = D6; // Attach CS to this pin, DIN to MOSI and CLK to SCK (cf http://arduino.cc/en/Reference/SPI )
int displayIntensity = 1; //(This can be set from 0 - 15)
const int numberOfHorizontalDisplays = 4; // default 4 for standard 4 x 1 display Max size of 16
const int numberOfHorizontalDisplays = 8; // default 4 for standard 4 x 1 display Max size of 16
const int numberOfVerticalDisplays = 1; // default 1 for a single row height
/* set ledRotation for LED Display panels (3 is default)
0: no rotation
Expand All @@ -110,6 +110,7 @@ String OctoAuthPass = ""; // only used with haproxy or basic auth (only need
boolean USE_PIHOLE = false; // Set true to display your Pi-hole details
String PiHoleServer = ""; // IP or Address only (DO NOT include http://)
int PiHolePort = 80; // Port of your Pi-hole address (default 80)
String PiHoleApiKey = ""; // Optional -- only needed to see top blocked clients

boolean ENABLE_OTA = true; // this will allow you to load firmware to the device over WiFi (see OTA for ESP8266)
String OTA_Password = ""; // Set an OTA password here -- leave blank if you don't want to be prompted for password
Expand Down
26 changes: 19 additions & 7 deletions marquee/marquee.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

#include "Settings.h"

#define VERSION "3.02"
#define VERSION "3.03"

#define HOSTNAME "CLOCK-"
#define CONFIG "/conf.txt"
Expand Down Expand Up @@ -149,14 +149,16 @@ static const char PIHOLE_FORM[] PROGMEM = "<form class='w3-container' action='/s
"<p><input name='displaypihole' class='w3-check w3-margin-top' type='checkbox' %PIHOLECHECKED%> Show Pi-hole Statistics</p>"
"<label>Pi-hole Address (do not include http://)</label><input class='w3-input w3-border w3-margin-bottom' type='text' name='piholeAddress' id='piholeAddress' value='%PIHOLEADDRESS%' maxlength='60'>"
"<label>Pi-hole Port</label><input class='w3-input w3-border w3-margin-bottom' type='text' name='piholePort' id='piholePort' value='%PIHOLEPORT%' maxlength='5' onkeypress='return isNumberKey(event)'>"
"<label>Pi-hole API Token (from Pi-hole &rarr; Settings &rarr; API/Web interface)</label>"
"<input class='w3-input w3-border w3-margin-bottom' type='text' name='piApiToken' id='piApiToken' value='%PIAPITOKEN%' maxlength='65'>"
"<input type='button' value='Test Connection and JSON Response' onclick='testPiHole()'><p id='PiHoleTest'></p>"
"<button class='w3-button w3-block w3-green w3-section w3-padding' type='submit'>Save</button></form>"
"<script>function isNumberKey(e){var h=e.which?e.which:event.keyCode;return!(h>31&&(h<48||h>57))}</script>";

static const char PIHOLE_TEST[] PROGMEM = "<script>function testPiHole(){var e=document.getElementById(\"PiHoleTest\"),t=document.getElementById(\"piholeAddress\").value,"
"n=document.getElementById(\"piholePort\").value;"
"n=document.getElementById(\"piholePort\").value,api=document.getElementById(\"piApiToken\").value;;"
"if(e.innerHTML=\"\",\"\"==t||\"\"==n)return e.innerHTML=\"* Address and Port are required\","
"void(e.style.background=\"\");var r=\"http://\"+t+\":\"+n;r+=\"/admin/api.php?summary\",window.open(r,\"_blank\").focus()}</script>";
"void(e.style.background=\"\");var r=\"http://\"+t+\":\"+n;r+=\"/admin/api.php?summary=3&auth=\"+api,window.open(r,\"_blank\").focus()}</script>";

static const char NEWS_FORM1[] PROGMEM = "<form class='w3-container' action='/savenews' method='get'><h2>News Configuration:</h2>"
"<p><input name='displaynews' class='w3-check w3-margin-top' type='checkbox' %NEWSCHECKED%> Display News Headlines</p>"
Expand Down Expand Up @@ -427,8 +429,8 @@ void loop() {
msg += "(" + printerClient.getProgressCompletion() + "%) ";
}
if (USE_PIHOLE) {
piholeClient.getPiHoleData(PiHoleServer, PiHolePort);
piholeClient.getGraphData(PiHoleServer, PiHolePort);
piholeClient.getPiHoleData(PiHoleServer, PiHolePort, PiHoleApiKey);
piholeClient.getGraphData(PiHoleServer, PiHolePort, PiHoleApiKey);
if (piholeClient.getPiHoleStatus() != "") {
msg += " Pi-hole (" + piholeClient.getPiHoleStatus() + "): " + piholeClient.getAdsPercentageToday() + "% ";
}
Expand Down Expand Up @@ -553,10 +555,12 @@ void handleSavePihole() {
USE_PIHOLE = server.hasArg("displaypihole");
PiHoleServer = server.arg("piholeAddress");
PiHolePort = server.arg("piholePort").toInt();
PiHoleApiKey = server.arg("piApiToken");
Serial.println("PiHoleApiKey from save: " + PiHoleApiKey);
writeCityIds();
if (USE_PIHOLE) {
piholeClient.getPiHoleData(PiHoleServer, PiHolePort);
piholeClient.getGraphData(PiHoleServer, PiHolePort);
piholeClient.getPiHoleData(PiHoleServer, PiHolePort, PiHoleApiKey);
piholeClient.getGraphData(PiHoleServer, PiHolePort, PiHoleApiKey);
}
redirectHome();
}
Expand Down Expand Up @@ -746,6 +750,8 @@ void handlePiholeConfigure() {
form.replace("%PIHOLECHECKED%", isPiholeDisplayedChecked);
form.replace("%PIHOLEADDRESS%", PiHoleServer);
form.replace("%PIHOLEPORT%", String(PiHolePort));
form.replace("%PIAPITOKEN%", PiHoleApiKey);


server.sendContent(form);
form = "";
Expand Down Expand Up @@ -1348,6 +1354,7 @@ String writeCityIds() {
f.println("USE_PIHOLE=" + String(USE_PIHOLE));
f.println("PiHoleServer=" + PiHoleServer);
f.println("PiHolePort=" + String(PiHolePort));
f.println("PiHoleApiKey=" + String(PiHoleApiKey));
f.println("themeColor=" + themeColor);
}
f.close();
Expand Down Expand Up @@ -1540,6 +1547,11 @@ void readCityIds() {
PiHolePort = line.substring(line.lastIndexOf("PiHolePort=") + 11).toInt();
Serial.println("PiHolePort=" + String(PiHolePort));
}
if (line.indexOf("PiHoleApiKey=") >= 0) {
PiHoleApiKey = line.substring(line.lastIndexOf("PiHoleApiKey=") + 13);
PiHoleApiKey.trim();
Serial.println("PiHoleApiKey=" + String(PiHoleApiKey));
}
if (line.indexOf("themeColor=") >= 0) {
themeColor = line.substring(line.lastIndexOf("themeColor=") + 11);
themeColor.trim();
Expand Down

0 comments on commit 0df78b1

Please sign in to comment.