-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKilltrackerWebManager.h
92 lines (72 loc) · 2.3 KB
/
KilltrackerWebManager.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
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
// KilltrackerWebManager.h
#ifndef PORT
#define PORT (uint16_t)80 // Default to 80 if PORT is not defined
#endif
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include "config.h"
#include <ArduinoJson.h>
namespace KilltrackerWebManager {
int kills = 6969;
const String steam_id = "76561198291413327";
WiFiClientSecure client;
unsigned long lastRequestTime = 0;
const unsigned long postingIntervalMS = 5L * 1000L;
void readResponse() {
// JsonDocument doc;
while (client.available()) {
char c = client.read();
Serial.print(c);
}
// DeserializationError error = deserializeJson(doc, rawJsonResponse);
// if (error) {
// Serial.print(F("deserializeJson() failed: "));
// Serial.println(error.f_str());
// return doc;
// }
// Serial.print("PARSED JSON: ");
// serializeJson(doc, Serial);
}
void connectToWifi() {
Serial.print("CONNECTING TO WIFI: ");
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void fetchKillcount() {
client.stop();
Serial.println("Attempting to connect to: " + String(HOSTNAME) + " at port " + String(PORT));
if (client.connect(SERVER, PORT)) {
Serial.println("CONNECTION SUCCESS");
client.println("GET /steam_accounts/" + steam_id + ".json" + " HTTP/1.1");
client.println("Host: " + String(HOSTNAME));
client.println("User-Agent: ArduinoWiFi/1.1");
client.println("Connection: close");
client.println();
} else {
Serial.println("CONNECTION FAILED");
}
lastRequestTime = millis();
}
void updateKillcountTask(void *parameter) {
while (true) {
readResponse();
if (millis() - lastRequestTime > postingIntervalMS) {
fetchKillcount();
}
// Serial.print("KILLS: ");
// Serial.println(kills);
vTaskDelay(50 / portTICK_PERIOD_MS);
}
}
int getKills() {
return kills;
}
};