Skip to content

Commit ad9c35e

Browse files
committed
Updated to include the Pi-Hole API key to fix graghing
1 parent 20b990c commit ad9c35e

File tree

6 files changed

+42
-16
lines changed

6 files changed

+42
-16
lines changed
Binary file not shown.
Binary file not shown.

marquee/PiHoleClient.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,19 @@ PiHoleClient::PiHoleClient() {
2727
//Constructor
2828
}
2929

30-
void PiHoleClient::getPiHoleData(String server, int port) {
30+
void PiHoleClient::getPiHoleData(String server, int port, String apiKey) {
3131

3232
WiFiClient wifiClient;
3333
errorMessage = "";
3434
String response = "";
3535

36-
String apiGetData = "http://" + server + ":" + String(port) + "/admin/api.php?summary";
36+
if (apiKey == "") {
37+
errorMessage = "Pi-hole API Key is required to view Summary Data.";
38+
Serial.println(errorMessage);
39+
return;
40+
}
41+
42+
String apiGetData = "http://" + server + ":" + String(port) + "/admin/api.php?summary&auth=" + apiKey;
3743
Serial.println("Sending: " + apiGetData);
3844
HTTPClient http; //Object of class HTTPClient
3945
http.begin(wifiClient, apiGetData);// get the result
@@ -96,6 +102,7 @@ void PiHoleClient::getTopClientsBlocked(String server, int port, String apiKey)
96102

97103
if (apiKey == "") {
98104
errorMessage = "Pi-hole API Key is required to view Top Clients Blocked.";
105+
Serial.println(errorMessage);
99106
return;
100107
}
101108

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

149-
void PiHoleClient::getGraphData(String server, int port) {
156+
void PiHoleClient::getGraphData(String server, int port, String apiKey) {
150157
WiFiClient wifiClient;
151158
HTTPClient http;
152159

153-
String apiGetData = "http://" + server + ":" + String(port) + "/admin/api.php?overTimeData10mins";
160+
errorMessage = "";
161+
162+
if (apiKey == "") {
163+
errorMessage = "Pi-hole API Key is required to view Graph Data.";
164+
Serial.println(errorMessage);
165+
return;
166+
}
167+
168+
String apiGetData = "http://" + server + ":" + String(port) + "/admin/api.php?overTimeData10mins&auth=" + apiKey;
154169
resetBlockedGraphData();
155170
Serial.println("Getting Pi-Hole Graph Data");
156171
Serial.println(apiGetData);
157172
http.begin(wifiClient, apiGetData);
158173
int httpCode = http.GET();
159174

160175
String result = "";
161-
errorMessage = "";
162176
boolean track = false;
163177
int countBracket = 0;
164178
blockedCount = 0;

marquee/PiHoleClient.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ SOFTWARE.
2424
#pragma once
2525
#include <ESP8266WiFi.h>
2626
#include <ESP8266HTTPClient.h>
27-
#include <WiFiClient.h>
2827
#include "libs/ArduinoJson/ArduinoJson.h"
2928

3029
class PiHoleClient {
@@ -72,8 +71,8 @@ class PiHoleClient {
7271

7372
public:
7473
PiHoleClient();
75-
void getPiHoleData(String server, int port);
76-
void getGraphData(String server, int port);
74+
void getPiHoleData(String server, int port, String apiKey);
75+
void getGraphData(String server, int port, String apiKey);
7776
void getTopClientsBlocked(String server, int port, String apiKey);
7877

7978
String getDomainsBeingBlocked();

marquee/Settings.h

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

114115
boolean ENABLE_OTA = true; // this will allow you to load firmware to the device over WiFi (see OTA for ESP8266)
115116
String OTA_Password = ""; // Set an OTA password here -- leave blank if you don't want to be prompted for password

marquee/marquee.ino

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
#include "Settings.h"
2929

30-
#define VERSION "3.02"
30+
#define VERSION "3.03"
3131

3232
#define HOSTNAME "CLOCK-"
3333
#define CONFIG "/conf.txt"
@@ -149,14 +149,16 @@ static const char PIHOLE_FORM[] PROGMEM = "<form class='w3-container' action='/s
149149
"<p><input name='displaypihole' class='w3-check w3-margin-top' type='checkbox' %PIHOLECHECKED%> Show Pi-hole Statistics</p>"
150150
"<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'>"
151151
"<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)'>"
152+
"<label>Pi-hole API Token (from Pi-hole &rarr; Settings &rarr; API/Web interface)</label>"
153+
"<input class='w3-input w3-border w3-margin-bottom' type='text' name='piApiToken' id='piApiToken' value='%PIAPITOKEN%' maxlength='65'>"
152154
"<input type='button' value='Test Connection and JSON Response' onclick='testPiHole()'><p id='PiHoleTest'></p>"
153155
"<button class='w3-button w3-block w3-green w3-section w3-padding' type='submit'>Save</button></form>"
154156
"<script>function isNumberKey(e){var h=e.which?e.which:event.keyCode;return!(h>31&&(h<48||h>57))}</script>";
155157

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

161163
static const char NEWS_FORM1[] PROGMEM = "<form class='w3-container' action='/savenews' method='get'><h2>News Configuration:</h2>"
162164
"<p><input name='displaynews' class='w3-check w3-margin-top' type='checkbox' %NEWSCHECKED%> Display News Headlines</p>"
@@ -427,8 +429,8 @@ void loop() {
427429
msg += "(" + printerClient.getProgressCompletion() + "%) ";
428430
}
429431
if (USE_PIHOLE) {
430-
piholeClient.getPiHoleData(PiHoleServer, PiHolePort);
431-
piholeClient.getGraphData(PiHoleServer, PiHolePort);
432+
piholeClient.getPiHoleData(PiHoleServer, PiHolePort, PiHoleApiKey);
433+
piholeClient.getGraphData(PiHoleServer, PiHolePort, PiHoleApiKey);
432434
if (piholeClient.getPiHoleStatus() != "") {
433435
msg += " Pi-hole (" + piholeClient.getPiHoleStatus() + "): " + piholeClient.getAdsPercentageToday() + "% ";
434436
}
@@ -553,10 +555,12 @@ void handleSavePihole() {
553555
USE_PIHOLE = server.hasArg("displaypihole");
554556
PiHoleServer = server.arg("piholeAddress");
555557
PiHolePort = server.arg("piholePort").toInt();
558+
PiHoleApiKey = server.arg("piApiToken");
559+
Serial.println("PiHoleApiKey from save: " + PiHoleApiKey);
556560
writeCityIds();
557561
if (USE_PIHOLE) {
558-
piholeClient.getPiHoleData(PiHoleServer, PiHolePort);
559-
piholeClient.getGraphData(PiHoleServer, PiHolePort);
562+
piholeClient.getPiHoleData(PiHoleServer, PiHolePort, PiHoleApiKey);
563+
piholeClient.getGraphData(PiHoleServer, PiHolePort, PiHoleApiKey);
560564
}
561565
redirectHome();
562566
}
@@ -746,6 +750,8 @@ void handlePiholeConfigure() {
746750
form.replace("%PIHOLECHECKED%", isPiholeDisplayedChecked);
747751
form.replace("%PIHOLEADDRESS%", PiHoleServer);
748752
form.replace("%PIHOLEPORT%", String(PiHolePort));
753+
form.replace("%PIAPITOKEN%", PiHoleApiKey);
754+
749755

750756
server.sendContent(form);
751757
form = "";
@@ -1348,6 +1354,7 @@ String writeCityIds() {
13481354
f.println("USE_PIHOLE=" + String(USE_PIHOLE));
13491355
f.println("PiHoleServer=" + PiHoleServer);
13501356
f.println("PiHolePort=" + String(PiHolePort));
1357+
f.println("PiHoleApiKey=" + String(PiHoleApiKey));
13511358
f.println("themeColor=" + themeColor);
13521359
}
13531360
f.close();
@@ -1540,6 +1547,11 @@ void readCityIds() {
15401547
PiHolePort = line.substring(line.lastIndexOf("PiHolePort=") + 11).toInt();
15411548
Serial.println("PiHolePort=" + String(PiHolePort));
15421549
}
1550+
if (line.indexOf("PiHoleApiKey=") >= 0) {
1551+
PiHoleApiKey = line.substring(line.lastIndexOf("PiHoleApiKey=") + 13);
1552+
PiHoleApiKey.trim();
1553+
Serial.println("PiHoleApiKey=" + String(PiHoleApiKey));
1554+
}
15431555
if (line.indexOf("themeColor=") >= 0) {
15441556
themeColor = line.substring(line.lastIndexOf("themeColor=") + 11);
15451557
themeColor.trim();

0 commit comments

Comments
 (0)