-
Notifications
You must be signed in to change notification settings - Fork 0
/
modern-warzone-counter.ino
192 lines (157 loc) · 4.74 KB
/
modern-warzone-counter.ino
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <Adafruit_GFX.h>
#include "Adafruit_LEDBackpack.h"
#include <Arduino.h>
#include <ArduinoJson.h>
#include <stdint.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <WiFiClient.h>
#include <Wire.h>
#include <HTTPClient.h>
#include "ascii.h"
#include "credentials.h"
#define SERIAL_BAUDRATE 115200
#define FREQUENCY 300000
int warzone_wins = 0;
unsigned long lastMillis;
WiFiMulti WiFiMulti;
Adafruit_7segment matrix;
void setup() {
Serial.begin(115200);
// put your setup code here, to run once:
Serial.println();
Serial.println();
Serial.println();
//For some reason this can't be called in the constructor, so I created this begin function
matrix = Adafruit_7segment();
matrix.begin(0x70);
// // matrix.drawColon(true);
matrix.setBrightness(5);
printName(WARZONE_USERNAME);
WiFi.mode(WIFI_STA);
WiFiMulti.addAP(WIFI_SSID, WIFI_PASS);
// wait for WiFi connection
Serial.print("Waiting for WiFi to connect...");
while ((WiFiMulti.run() != WL_CONNECTED)) {
Serial.print(".");
}
Serial.println(" connected");
setClock();
Serial.println(makeGetURL());
updateFromServer();
}
void setClock() {
configTime(0, 0, "pool.ntp.org", "time.nist.gov");
Serial.print(F("Waiting for NTP time sync: "));
time_t nowSecs = time(nullptr);
while (nowSecs < 8 * 3600 * 2) {
delay(500);
Serial.print(F("."));
yield();
nowSecs = time(nullptr);
}
Serial.println();
struct tm timeinfo;
gmtime_r(&nowSecs, &timeinfo);
Serial.print(F("Current time: "));
Serial.print(asctime(&timeinfo));
}
String makeGetURL() {
return WARZONE_GET_URL + WARZONE_USERNAME + "/" + WARZONE_PLATFORM;
}
void updateFromServer() {
WiFiClientSecure *client = new WiFiClientSecure;
if(client) {
// client -> setCACert(rootCACertificate);
client->setInsecure();
{
// Add a scoping block for HTTPClient https to make sure it is destroyed before WiFiClientSecure *client is
HTTPClient https;
Serial.print("[HTTPS] begin...\n");
if (https.begin(*client, makeGetURL())) { // HTTPS
Serial.print("[HTTPS] GET...\n");
// start connection and send HTTP header
https.addHeader("x-rapidapi-key", RAPID_API_KEY);
https.addHeader("x-rapidapi-host", "call-of-duty-modern-warfare.p.rapidapi.com");
https.addHeader("useQueryString", "true");
int httpCode = https.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_FOUND) {
parseResponse(https.getString());
}
} else {
Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
}
https.end();
} else {
Serial.printf("[HTTPS] Unable to connect\n");
}
// End extra scoping block
}
delete client;
} else {
Serial.println("Unable to create client");
}
}
void parseResponse(String httpsResponse){
StaticJsonDocument<2048> doc;
DeserializationError error = deserializeJson(doc, httpsResponse);
// Test if parsing succeeds.
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
JsonObject br = doc["br"];
warzone_wins = br["wins"]; // 0
Serial.print("Warzone Wins: ");
Serial.println(warzone_wins);
}
void loop() {
if (millis() - lastMillis >= FREQUENCY)
{
lastMillis = millis(); //get ready for the next iteration
updateFromServer();
}
updateDisplay();
}
void updateDisplay(){
matrix.writeDigitNum(0, (warzone_wins / 1000));
matrix.writeDigitNum(1, (warzone_wins / 100) % 10);
matrix.drawColon(false);
matrix.writeDigitNum(3, (warzone_wins / 10) % 10);
matrix.writeDigitNum(4, warzone_wins % 10);
matrix.writeDisplay();
delay(1000);
}
void printName(String name){
for(int i = 0; i <= name.length() + 3; i++){
if(i > 14){
matrix.writeDigitRaw(4, 0b00000000);
}else{
matrix.writeDigitRaw(4, get_ascii(name.charAt(i) - ' '));
}
if(i-1 >= 0 && i-1 < name.length()){
matrix.writeDigitRaw(3, get_ascii(name.charAt(i-1) - ' '));
}else{
matrix.writeDigitRaw(3, 0b00000000);
}
if(i-2 >= 0 && i-2 < name.length()){
matrix.writeDigitRaw(1, get_ascii(name.charAt(i-2) - ' '));
}else{
matrix.writeDigitRaw(1, 0b00000000);
}
if(i-3 >= 0 && i-3 < name.length()){
matrix.writeDigitRaw(0, get_ascii(name.charAt(i-3) - ' '));
}else{
matrix.writeDigitRaw(0, 0b00000000);
}
matrix.writeDisplay();
delay(250);
}
delay(1000);
}