-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockclock.ino
executable file
·146 lines (120 loc) · 3.46 KB
/
blockclock.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
#include "config.hpp"
#include <LedController.hpp>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
int lastBlockTime = 0;
unsigned long lastMillis = millis() - UPDATE_RATE_MS;
LedController<DIGITS, 1> lc(SPI_MOSI, SPI_CLK, SPI_CS);
int connectingAnimationDigit = 0;
WiFiClient wifiClient;
WiFiClientSecure wifiClientSecure;
void setup() {
Serial.begin(115200);
setupDisplay();
setupWifi();
}
void setupDisplay() {
lc.setIntensity(BRIGHTNESS);
lc.clearMatrix();
}
void setupWifi() {
WiFi.begin(ssid, password);
Serial.print("Connecting to WLAN...");
while (WiFi.status() != WL_CONNECTED) {
displayConnectingAnimation();
Serial.print(".");
}
connectingAnimationDigit = 0;
lc.clearMatrix();
Serial.println(" connected!");
Serial.println("IP Address: " + WiFi.localIP().toString());
wifiClientSecure.setInsecure();
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - lastMillis >= UPDATE_RATE_MS) {
lastMillis = currentMillis;
if (WiFi.status() != WL_CONNECTED) {
setupWifi();
}
auto currentBlockTime = getBlockTime();
if ((currentBlockTime >= 0) && (currentBlockTime != lastBlockTime)) {
Serial.println("New BlockTime: " + currentBlockTime);
displayBlockTimeWithAnimation(currentBlockTime);
lastBlockTime = currentBlockTime;
}
}
}
int getBlockTime() {
HTTPClient http;
int blockTime = -1;
if (http.begin(*getWifiClient(), apiUrl)) {
const int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
const auto payload = http.getString();
if (payload.length() > 0) {
blockTime = getBlockTimeFromPayload(payload);
} else {
Serial.println("Received empty payload.");
}
} else {
Serial.println("HTTP error: " + String(httpCode));
}
http.end();
} else {
Serial.println("Failed to connect to API.");
}
return blockTime;
}
WiFiClient* getWifiClient() {
if (String(apiUrl).startsWith("https://")) {
return &wifiClientSecure;
} else {
return &wifiClient;
}
}
int getBlockTimeFromPayload(String payload) {
int blockTime = -1;
DynamicJsonDocument doc(1024);
DeserializationError error = deserializeJson(doc, payload);
if (!error && doc.is<JsonObject>()) // check if payload was JSON
{
if (doc.containsKey("height")) {
blockTime = doc["height"];
} else if (doc.containsKey("data") && doc["data"].containsKey("height")) {
blockTime = doc["data"]["height"];
} else {
Serial.println("Unknown JSON structure");
}
} else {
blockTime = payload.toInt();
}
return blockTime;
}
void displayBlockTimeWithAnimation(int blockTime) {
auto blockStr = String(blockTime);
auto len = blockStr.length() <= DIGITS ? blockStr.length() : DIGITS;
const auto rightIndex = (DIGITS - len) / 2;
const auto leftIndex = rightIndex + len - 1;
for (int i = rightIndex; i <= leftIndex; i++) {
lc.setChar(0, i, '-', false);
delay(animationDelay);
}
for (int i = leftIndex, digitIndex = 0; i >= rightIndex; i--, digitIndex++) {
auto value = blockStr[digitIndex];
lc.setChar(0, i, value, false);
delay(animationDelay);
}
}
void displayConnectingAnimation() {
lc.setChar(0, connectingAnimationDigit, '.', false);
connectingAnimationDigit++;
if (connectingAnimationDigit >= DIGITS) {
connectingAnimationDigit = 0;
lc.clearMatrix();
}
delay(animationDelay);
}