-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfact.h
39 lines (30 loc) · 865 Bytes
/
fact.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#ifndef FACT_H
#define FACT_H
#include <ESP8266HTTPClient.h> //TODO move to config?
#include <WiFiClient.h>
const int numberOfTypes = 4;
const String types[numberOfTypes] = {"trivia", "math", "date", "year"};
int cycle = 0;
const String factLink = "http://numbersapi.com/";
String getLink() {
cycle = (cycle + 1) % numberOfTypes;
return factLink + String(random(0, 1000)) + "/" + types[cycle];
}
String fetchRandomFact() {
String payload = "";
if (WiFi.status() == WL_CONNECTED) {
WiFiClient client;
HTTPClient http;
http.begin(client, getLink());
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
payload = http.getString();
}
http.end();
}
return payload;
}
void printFact() {
displayContent({ DisplayItem(fetchRandomFact(), 1) }); //TODO add enters when word crosses screen border
}
#endif