-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitcoin Ticker v3 Backup 2.txt
More file actions
794 lines (687 loc) · 29.6 KB
/
Copy pathBitcoin Ticker v3 Backup 2.txt
File metadata and controls
794 lines (687 loc) · 29.6 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
// Libraries
#include "TFT_eSPI.h"
#include "time.h"
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
#include <HTTPClient.h>
#include "ArduinoJson.h"
#include "WiFi.h"
#include "Free_Fonts.h"
// Images
#include "Screen1a_Success_2.h"
#include "Screen1b_Fail_2.h"
#include "Screen1_WiFi_2.h"
#include "Screen2_Price_2.h"
// Fonts
#include "LemonMilkFont.h"
#include "AlarmClockFont.h"
// LEDs
#define CYD_LED_BLUE 17
#define CYD_LED_RED 4
#define CYD_LED_GREEN 16
// TFT Display
TFT_eSPI tft = TFT_eSPI();
TFT_eSprite sprite = TFT_eSprite(&tft);
// Time
const char* ntpServer = "pool.ntp.org";
#define DEFAULT_TIMEZONE 0 // UTC +/-
int gmtOffset_sec{ DEFAULT_TIMEZONE };
// Preferred Currency
#define DEFAULT_CURRENCY "USD" // Default
char prefCurrency[5]{DEFAULT_CURRENCY};
// Function prototypes
void getAndDisplayBlockHeight();
void getAndDisplayBitcoinPrices();
void clearScreen();
void getBitcoinFees();
void displayLocalTime();
void wifiSignal();
// Declare variables
String ip = "";
int primaryBGColour = ((8 << 11) | (16 << 5) | 8); // Dark Grey from Figma // ((0 << 11) | (0 << 5) | 0); // Black
//int secondaryBGColour = ((26 << 11) | (54 << 5) | 26); // Light Grey from Figma // ((31 << 11) | (38 << 5) | 0); //
int secondaryBGColour = ((8 << 11) | (16 << 5) | 8); // Dark Grey from Figma //((0 << 11) | (0 << 5) | 0); // Black ((26 << 11) | (54 << 5) | 26); // Light Grey from Figma // ((31 << 11) | (38 << 5) | 0); //
int primaryTextColour = ((31 << 11) | (63 << 5) | 31); // White
int secondaryTextColour = ((31 << 11) | (38 << 5) | 0); // Bitcoin Orange
int currentScreen = 0;
long lastApiCallTime = 0;
const unsigned long apiCallInterval = 120000; // 2 minutes in milliseconds
long lastWhile1Time = 0; // Over 1 minute to ensure it executes the first time
const unsigned long while1Interval = 60000; // 1 minute
long lastWhile2Time = 0; // Over seconds to ensure it executes the first time
const unsigned long while2Interval = 5000; // 5 seconds
int touched = false;
bool feesBool = true;
double usd24HrVolume = 0;
double usdPrice = 0;
double usd24HrChange = 0;
double eurPrice = 0;
double eur24HrChange = 0;
double gbpPrice = 0;
double gbp24HrChange = 0;
double moscowTime = 0;
// API Links
const char* feesApiUrl = "https://mempool.space/api/v1/fees/recommended";
const char* pricesApiUrl = "https://mempool.space/api/v1/prices";
const char* blockHeightApiUrl = "https://mempool.space/api/blocks/tip/height";
const char* difficultyApiUrl = "https://mempool.space/api/v1/difficulty-adjustment";
const char* hashrateApiUrl = "https://mempool.space/api/v1/mining/hashrate/3d";
const char* coinGeckoApiUrl = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd%2Cgbp%2Ceur&include_market_cap=true&include_24hr_vol=true&include_24hr_change=true&include_last_updated_at=true";
String formatNumberWithCommas(int number) {
String result = String(number);
int length = result.length();
for (int i = length - 3; i > 0; i -= 3) {
result = result.substring(0, i) + "," + result.substring(i);
}
return result;
}
void setup() {
Serial.begin(115200);
tft.init();
tft.setRotation(1);
tft.setSwapBytes(true);
tft.pushImage(0, 0, 320, 240, Screen1_WiFi_2);
// Initialise LEDs
pinMode(CYD_LED_RED, OUTPUT);
pinMode(CYD_LED_GREEN, OUTPUT);
pinMode(CYD_LED_BLUE, OUTPUT);
//Turn LEDs Off (they're annoying)
digitalWrite(CYD_LED_RED, HIGH); // The LEDs are "active low", meaning HIGH == off, LOW == on
digitalWrite(CYD_LED_GREEN, HIGH);
digitalWrite(CYD_LED_BLUE, HIGH);
//Create WiFi Manager object
WiFiManager wm;
bool res;
wm.resetSettings(); // Reset WiFi credentials TO DO: Assign Button (held) to this action
// Set up Timezone Parameter - Text box (Number)
char charZone[6];
sprintf(charZone, "%d", gmtOffset_sec);
WiFiManagerParameter time_text_box_num("TimeZone", "TimeZone fromUTC (-12/+12)", charZone, 3);
// Set up Currency Parameter - Text box (String)
WiFiManagerParameter currency_text_box("currency", "Preferred Currency", DEFAULT_CURRENCY, 3);
//Check up Mempool Fees Parameter - Checkbox (Bool)
char *customHtml;
if (feesBool)
{
customHtml = "type=\"checkbox\" checked";
}
else
{
customHtml = "type=\"checkbox\"";
}
// Value is "T" if Checkbox == TRUE
WiFiManagerParameter fees_checkbox("key_bool", "Display Mempool Fees", "T", 2, customHtml);
wm.addParameter(&time_text_box_num);
wm.addParameter(¤cy_text_box);
wm.addParameter(&fees_checkbox);
res = wm.autoConnect("BitcoinTickerAP", "BitcoinTickers"); // Access Point SSID & Password
if(!res) {
Serial.println("Failed to connect");
tft.pushImage(0, 0, 320, 240, Screen1b_Fail_2); // WiFi Not Connected screen
}
else {
tft.pushImage(0, 0, 320, 240, Screen1a_Success_2); // WiFi Connected screen
delay(500);
tft.fillScreen(primaryBGColour);
}
// Convert Timezone number value
gmtOffset_sec = atoi(time_text_box_num.getValue());
gmtOffset_sec *= 3600; // Convert UTC offset to seconds (1 hour = 3600 seconds)
//Convert the string value
strncpy(prefCurrency, currency_text_box.getValue(), sizeof(prefCurrency));
Serial.println(prefCurrency);
//Convert the bool value
feesBool = (strncmp(fees_checkbox.getValue(), "T", 1) == 0);
ip=WiFi.localIP().toString();
configTime(gmtOffset_sec, 0, ntpServer);
displayLocalTime();
getAndDisplayBlockHeight();
wifiSignal();
//getAndDisplayBitcoinPrices();
}
void clearScreen() {
/*
tft.fillScreen(primaryBGColour);
// Top Light Grey
sprite.createSprite(320, 30);//20
sprite.fillSprite(secondaryBGColour);
sprite.pushSprite(0, 0);
sprite.deleteSprite();
*/
// Main Dark Grey
//tft.fillScreen(primaryBGColour);
sprite.createSprite(320, 100);
sprite.fillSprite(primaryBGColour);
sprite.pushSprite(0, 30);
sprite.deleteSprite();
}
void displayLocalTime(){
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
return;
}
char formattedTime[6]; // HH:MM format
char formattedLongTime[9]; // DD MMM YYYY HH:MM:SS format
char formattedDateTime[20]; // DD MMM YYYY HH:MM format
strftime(formattedTime, sizeof(formattedTime), "%H:%M", &timeinfo);
strftime(formattedLongTime, sizeof(formattedLongTime), "%H:%M:%S", &timeinfo);
strftime(formattedDateTime, sizeof(formattedDateTime), "%d %b %Y %H:%M", &timeinfo);
sprite.createSprite(132, 27); //130, 25
sprite.fillSprite(secondaryBGColour);
sprite.setTextColor(TFT_WHITE, secondaryBGColour); //Black to White text
sprite.drawString(formattedDateTime, sprite.width()/2, sprite.height()/2, 2); //formattedDateTime, , 2);
sprite.pushSprite(0, 0);
sprite.deleteSprite();
}
void displayBitcoinFees(int fastestFee, int halfHourFee, int hourFee, double difficultyChange, int difficultyChangeBlocks, int timeAverage, double currentHashrate, double currentDifficulty) {
// Convert double values to strings with limited precision (3 digits)
String hashrateString = String(currentHashrate, 2); // e.g., "123.45"
String difficultyString = String(currentDifficulty, 2); // e.g., "678.90"
// Truncate strings to the first 3 digits
String truncatedHashrate = hashrateString.substring(0, 3); // e.g., "123"
String truncatedDifficulty = difficultyString.substring(0, 2); // e.g., "67"
// Left
sprite.createSprite(160, 110); //300, 105
sprite.fillSprite(primaryBGColour);
sprite.loadFont(lemonMilkFont_20);
sprite.setTextColor(secondaryTextColour, primaryBGColour);
sprite.drawString(String("Difficulty"), sprite.width() / 2, (sprite.height() / 2)-11);
sprite.setTextColor(primaryTextColour, primaryBGColour);
sprite.drawString(String(truncatedDifficulty) + String("T"), sprite.width() / 2, (sprite.height() / 2)+11);
sprite.pushSprite(0, 80);
sprite.unloadFont();
sprite.deleteSprite();
// Right
sprite.createSprite(160, 110); //300, 105
sprite.fillSprite(primaryBGColour);
sprite.loadFont(lemonMilkFont_20);
sprite.setTextColor(secondaryTextColour, primaryBGColour);
sprite.drawString(String("Hashrate"), sprite.width() / 2, (sprite.height() / 2)-11);
sprite.setTextColor(primaryTextColour, primaryBGColour);
sprite.drawString(String(truncatedHashrate) + String(" EH/s"), sprite.width() / 2, (sprite.height() / 2)+11);
sprite.pushSprite(160, 80);
sprite.unloadFont();
sprite.deleteSprite();
/*
// 10mins
sprite.createSprite(100, 40);
sprite.setTextDatum(4);
sprite.fillSprite(primaryBGColour);
sprite.loadFont(lemonMilkFont_20);
sprite.setTextColor(secondaryTextColour, primaryBGColour);
sprite.drawString(String("10 mins"), sprite.width() / 2, (sprite.height() / 2)-11);
sprite.loadFont(lemonMilkFont_15);
sprite.setTextColor(TFT_WHITE, primaryBGColour);
sprite.drawString(String(fastestFee) + String(" sat/vB"), sprite.width() / 2, (sprite.height() / 2)+11);
sprite.unloadFont();
*/
// 10mins
sprite.createSprite(100, 20);
sprite.setTextDatum(4);
sprite.fillSprite(primaryBGColour);
sprite.loadFont(lemonMilkFont_20);
sprite.setTextColor(secondaryTextColour, primaryBGColour);
sprite.drawString(String("10 mins"), sprite.width() / 2, sprite.height() / 2);
sprite.pushSprite(10, 190);
sprite.unloadFont();
sprite.deleteSprite();
sprite.createSprite(100, 20);
sprite.setTextDatum(4);
sprite.fillSprite(primaryBGColour);
sprite.loadFont(lemonMilkFont_15);
sprite.setTextColor(TFT_WHITE, primaryBGColour);
sprite.drawString(String(fastestFee) + String(" sat/vB"), sprite.width() / 2, sprite.height() / 2);
sprite.pushSprite(10, 210);
sprite.unloadFont();
sprite.deleteSprite();
// 30mins
sprite.createSprite(100, 20);
sprite.setTextDatum(4);
sprite.fillSprite(primaryBGColour);
sprite.loadFont(lemonMilkFont_20);
sprite.setTextColor(secondaryTextColour, primaryBGColour);
sprite.drawString(String("30 mins"), sprite.width() / 2, sprite.height() / 2);
sprite.pushSprite(110, 190);
sprite.unloadFont();
sprite.deleteSprite();
sprite.createSprite(100, 20);
sprite.setTextDatum(4);
sprite.fillSprite(primaryBGColour);
sprite.loadFont(lemonMilkFont_15);
sprite.setTextColor(TFT_WHITE, primaryBGColour);
sprite.drawString(String(halfHourFee) + String(" sat/vB"), sprite.width() / 2, sprite.height() / 2);
sprite.pushSprite(110, 210);
sprite.unloadFont();
sprite.deleteSprite();
// 1 hour
sprite.createSprite(100, 20);
sprite.setTextDatum(4);
sprite.fillSprite(primaryBGColour);
sprite.loadFont(lemonMilkFont_20);
sprite.setTextColor(secondaryTextColour, primaryBGColour);
sprite.drawString(String("1 hour"), sprite.width() / 2, sprite.height() / 2);
sprite.pushSprite(210, 190);
sprite.unloadFont();
sprite.deleteSprite();
sprite.createSprite(100, 20);
sprite.setTextDatum(4);
sprite.fillSprite(primaryBGColour);
sprite.loadFont(lemonMilkFont_15);
sprite.setTextColor(TFT_WHITE, primaryBGColour);
sprite.drawString(String(hourFee) + String(" sat/vB"), sprite.width() / 2, sprite.height() / 2);
sprite.pushSprite(210, 210);
sprite.unloadFont();
sprite.deleteSprite();
// Difficulty Adjustment
sprite.createSprite(300, 20); //300, 20
sprite.setTextDatum(4);
sprite.fillSprite(primaryBGColour);
sprite.loadFont(lemonMilkFont_15);
sprite.setTextColor(secondaryTextColour, primaryBGColour);
sprite.drawString("Difficulty Adjustment", sprite.width() / 2, sprite.height() / 2);
sprite.pushSprite(10, 40);
sprite.unloadFont();
sprite.deleteSprite();
sprite.createSprite(300, 20);
sprite.setTextDatum(4);
sprite.fillSprite(primaryBGColour);
sprite.loadFont(lemonMilkFont_20);
sprite.setTextColor(TFT_WHITE, primaryBGColour);
if (difficultyChange < 0)
{
sprite.drawString(String("-") + String(difficultyChange) + String("%") + String(" in ") + String(difficultyChangeBlocks) + String(" Blocks") , sprite.width() / 2, sprite.height() / 2);
}
else {
sprite.drawString(String("+") + String(difficultyChange) + String("%") + String(" in ") + String(difficultyChangeBlocks) + String(" Blocks") , sprite.width() / 2, sprite.height() / 2);
}
sprite.pushSprite(10, 60);
sprite.unloadFont();
sprite.deleteSprite();
}
void displayBitcoinPrices(int usdPrice, double usd24HrChange, int eurPrice, double eur24HrChange, int gbpPrice, double gbp24HrChange, int moscowPrice) { //Remove "Price" to use Mempool prices
String formattedUSD = formatNumberWithCommas(usdPrice);
String formattedEUR = formatNumberWithCommas(eurPrice);
String formattedGBP = formatNumberWithCommas(gbpPrice);
String formattedMoscow = formatNumberWithCommas(moscowPrice);
Serial.println("Display Prices");
if (strcmp(prefCurrency, "USD") == 0)
{
sprite.createSprite(320, 80);
sprite.setTextDatum(4);
sprite.fillSprite(primaryBGColour);
sprite.loadFont(lemonMilkFont_60);
sprite.setTextColor(TFT_WHITE, primaryBGColour);
sprite.drawString(String("$") + String(formattedUSD), sprite.width() / 2, 50); // Add GFXFF to the end? // formattedUSD
sprite.pushSprite((tft.width() - sprite.width()) / 2, 30); // Centre the Sprite
sprite.unloadFont();
sprite.deleteSprite();
sprite.createSprite(300, 60);
sprite.setTextDatum(4);
sprite.fillSprite(primaryBGColour);
sprite.loadFont(lemonMilkFont_30);
sprite.setTextColor(TFT_WHITE, primaryBGColour);
sprite.drawString(String("€") + formattedEUR + String(" £") + formattedGBP, sprite.width() / 2, sprite.height() / 2);
sprite.pushSprite((tft.width() - sprite.width()) / 2, 135); //135 to 145// Centre the Sprite
sprite.unloadFont();
sprite.deleteSprite();
sprite.createSprite(300, 60);
sprite.setTextDatum(4);
sprite.fillSprite(primaryBGColour);
sprite.setTextColor(TFT_WHITE, primaryBGColour);
sprite.loadFont(lemonMilkFont_30);
sprite.drawString(formattedMoscow + " sats/$", sprite.width() / 2, sprite.height() / 2);
sprite.pushSprite((tft.width() - sprite.width()) / 2, 185); //175 to 185 // Centre the Sprite
sprite.unloadFont();
sprite.deleteSprite();
sprite.createSprite(320, 25);
sprite.setTextDatum(4);
sprite.fillSprite(primaryBGColour);
sprite.loadFont(lemonMilkFont_20);
if (usd24HrChange >= 0) {
sprite.setTextColor(TFT_GREEN, primaryBGColour);
sprite.drawString("24hrs +"+String(usd24HrChange)+"%", sprite.width()/2, sprite.height()/2);
}
else {
sprite.setTextColor(TFT_RED, primaryBGColour);
sprite.drawString("24hrs "+String(usd24HrChange)+"%", sprite.width()/2, sprite.height()/2);
}
sprite.pushSprite((tft.width() - sprite.width()) / 2, 110); //230, 100);
sprite.unloadFont();
sprite.deleteSprite();
}
else if (strcmp(prefCurrency, "EUR") == 0)
{
sprite.createSprite(320, 80);
sprite.setTextDatum(4);
sprite.fillSprite(primaryBGColour);
sprite.loadFont(lemonMilkFont_60);
sprite.setTextColor(TFT_WHITE, primaryBGColour);
sprite.drawString(String("€") + String(formattedEUR), sprite.width() / 2, 50); // Add GFXFF to the end?
sprite.pushSprite((tft.width() - sprite.width()) / 2, 30); // Centre the Sprite
sprite.unloadFont();
sprite.deleteSprite();
sprite.createSprite(300, 50);
sprite.setTextDatum(4);
sprite.fillSprite(primaryBGColour);
sprite.loadFont(lemonMilkFont_30);
sprite.setTextColor(TFT_WHITE, primaryBGColour);
sprite.drawString(String("$") + formattedUSD + String(" £") + formattedGBP, sprite.width() / 2, sprite.height() / 2);
sprite.pushSprite((tft.width() - sprite.width()) / 2, 145); // Centre the Sprite
sprite.unloadFont();
sprite.deleteSprite();
sprite.createSprite(300, 60);
sprite.setTextDatum(4);
sprite.fillSprite(primaryBGColour);
sprite.setTextColor(TFT_WHITE, primaryBGColour);
sprite.loadFont(lemonMilkFont_30);
sprite.drawString(formattedMoscow + " sats/$", sprite.width() / 2, sprite.height() / 2);
sprite.pushSprite((tft.width() - sprite.width()) / 2, 185); // Centre the Sprite
sprite.unloadFont();
sprite.deleteSprite();
sprite.createSprite(320, 25); //25
sprite.setTextDatum(4);
sprite.fillSprite(primaryBGColour);
sprite.loadFont(lemonMilkFont_20);
if (eur24HrChange >= 0) {
sprite.setTextColor(TFT_GREEN, primaryBGColour);
sprite.drawString("24hrs +"+String(eur24HrChange)+"%", sprite.width()/2, sprite.height()/2);
}
else {
sprite.setTextColor(TFT_RED, primaryBGColour);
sprite.drawString("24hrs "+String(eur24HrChange)+"%", sprite.width()/2, sprite.height()/2);
}
sprite.pushSprite((tft.width() - sprite.width()) / 2, 110); //230, 100);
sprite.unloadFont();
sprite.deleteSprite();
}
else if (strcmp(prefCurrency, "GBP") == 0)
{
sprite.createSprite(320, 80);
sprite.setTextDatum(4);
sprite.fillSprite(primaryBGColour);
sprite.loadFont(lemonMilkFont_60);
sprite.setTextColor(TFT_WHITE, primaryBGColour);
sprite.drawString(String("£") + String(formattedGBP), sprite.width() / 2, 50); // Add GFXFF to the end?
sprite.pushSprite((tft.width() - sprite.width()) / 2, 30); // Centre the Sprite
sprite.unloadFont();
sprite.deleteSprite();
sprite.createSprite(300, 50);
sprite.setTextDatum(4);
sprite.fillSprite(primaryBGColour);
sprite.loadFont(lemonMilkFont_30);
sprite.setTextColor(TFT_WHITE, primaryBGColour);
sprite.drawString(String("$") + formattedUSD + String(" €") + formattedEUR, sprite.width() / 2, sprite.height() / 2);
sprite.pushSprite((tft.width() - sprite.width()) / 2, 145); // Centre the Sprite
sprite.unloadFont();
sprite.deleteSprite();
sprite.createSprite(300, 60); //250, 40
sprite.setTextDatum(4);
sprite.fillSprite(primaryBGColour);
sprite.setTextColor(TFT_WHITE, primaryBGColour);
sprite.loadFont(lemonMilkFont_30);
sprite.drawString(formattedMoscow + " sats/$", sprite.width() / 2, sprite.height() / 2);
sprite.pushSprite((tft.width() - sprite.width()) / 2, 185); //195 // Centre the Sprite
sprite.unloadFont();
sprite.deleteSprite();
sprite.createSprite(320, 25);
sprite.setTextDatum(4);
sprite.fillSprite(primaryBGColour);
sprite.loadFont(lemonMilkFont_20);
if (gbp24HrChange >= 0) {
sprite.setTextColor(TFT_GREEN, primaryBGColour);
sprite.drawString("24hrs +"+String(gbp24HrChange)+"%", sprite.width()/2, sprite.height()/2);
}
else {
sprite.setTextColor(TFT_RED, primaryBGColour);
sprite.drawString("24hrs "+String(gbp24HrChange)+"%", sprite.width()/2, sprite.height()/2);
}
sprite.pushSprite((tft.width() - sprite.width()) / 2, 110); //230, 100);
sprite.unloadFont();
sprite.deleteSprite();
}
else //Default to USD
{
sprite.createSprite(320, 80);
sprite.setTextDatum(4);
sprite.fillSprite(primaryBGColour);
sprite.loadFont(lemonMilkFont_60);
sprite.setTextColor(TFT_WHITE, primaryBGColour);
sprite.drawString(String("$") + String(formattedUSD), sprite.width() / 2, 50); // Add GFXFF to the end?
sprite.pushSprite((tft.width() - sprite.width()) / 2, 30); // Centre the Sprite
sprite.unloadFont();
sprite.deleteSprite();
sprite.createSprite(300, 50);
sprite.setTextDatum(4);
sprite.fillSprite(primaryBGColour);
sprite.loadFont(lemonMilkFont_30);
sprite.setTextColor(TFT_WHITE, primaryBGColour);
sprite.drawString(String("€") + formattedEUR + String(" £") + formattedGBP, sprite.width() / 2, sprite.height() / 2);
sprite.pushSprite((tft.width() - sprite.width()) / 2, 145); //135 to 145// Centre the Sprite
sprite.unloadFont();
sprite.deleteSprite();
sprite.createSprite(300, 60);
sprite.setTextDatum(4);
sprite.fillSprite(primaryBGColour);
sprite.setTextColor(TFT_WHITE, primaryBGColour);
sprite.loadFont(lemonMilkFont_30);
sprite.drawString(formattedMoscow + " sats/$", sprite.width() / 2, sprite.height() / 2);
sprite.pushSprite((tft.width() - sprite.width()) / 2, 185); //175 to 185 // Centre the Sprite
sprite.unloadFont();
sprite.deleteSprite();
sprite.createSprite(320, 25);
sprite.setTextDatum(4);
sprite.fillSprite(primaryBGColour);
sprite.loadFont(lemonMilkFont_20);
if (usd24HrChange >= 0) {
sprite.setTextColor(TFT_GREEN, primaryBGColour);
sprite.drawString("24hrs +"+String(usd24HrChange)+"%", sprite.width()/2, sprite.height()/2);
}
else {
sprite.setTextColor(TFT_RED, primaryBGColour);
sprite.drawString("24hrs "+String(usd24HrChange)+"%", sprite.width()/2, sprite.height()/2);
}
sprite.pushSprite((tft.width() - sprite.width()) / 2, 110); //230, 100);
sprite.unloadFont();
sprite.deleteSprite();
}
}
void displayBlockHeight(int height) {
String formattedHeight = formatNumberWithCommas(height);
sprite.createSprite(130, 25);
sprite.fillSprite(secondaryBGColour);
sprite.setTextColor(TFT_WHITE, secondaryBGColour);
sprite.drawString(String(formattedHeight), sprite.width()/2, sprite.height()/2, 2); //formattedDateTime, , 2);
sprite.pushSprite(200, 2);
sprite.deleteSprite();
}
void getAndDisplayBitcoinFees() {
// Fees
HTTPClient http;
if (!http.begin(feesApiUrl)) {
Serial.println("Connection to API failed");
return;
}
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString();
http.end();
DynamicJsonDocument doc(1024);
deserializeJson(doc, response);
int fastestFee = doc["fastestFee"];
int halfHourFee = doc["halfHourFee"];
int hourFee = doc["hourFee"];
// Hashrate & Difficulty
HTTPClient http;
if (!http.begin(hashrateApiUrl)) {
Serial.println("Connection to API failed");
return;
}
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString();
http.end();
DynamicJsonDocument doc(1024);
deserializeJson(doc, response);
double currentHashrate = doc["currentHashrate"];
double currentDifficulty = doc["currentDifficulty"];
/*
if (hashrateString.length() == 14) { // 2 digit Trillions
String hashrate2Digits = hashrateString.substring(0, 2);
currentHashrateInt = hashrate2Digits.toInt();
}
else if (hashrateString.length() == 15) { // 3 digit Trillions
String hashrate3Digits = hashrateString.substring(0, 3);
currentHashrateInt = hashrate3Digits.toInt();
}
*/
//currentDifficulty
// Difficulty Adjustment
HTTPClient http;
if (!http.begin(difficultyApiUrl)) {
Serial.println("Connection to API failed");
return;
}
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString();
http.end();
DynamicJsonDocument doc(1024);
deserializeJson(doc, response);
double difficultyChange = doc["difficultyChange"];
int remainingBlocks = doc["remainingBlocks"];
int timeAverage = doc["timeAverage"];
displayBitcoinFees(fastestFee, halfHourFee, hourFee, difficultyChange, remainingBlocks, timeAverage, currentHashrate, currentDifficulty);
} else {
Serial.println("Error on HTTP request");
http.end();
}
}
}
}
void getAndDisplayBitcoinPrices() {
// Limit API calls to every 2 mins
unsigned long currentMillis = millis();
Serial.println("getPrices Start");
Serial.println(currentMillis);
Serial.println(lastApiCallTime);
Serial.println(apiCallInterval);
//if (currentMillis - lastApiCallTime >= apiCallInterval) {
Serial.println("getPrices IF");
lastApiCallTime = currentMillis;
HTTPClient http;
if (!http.begin(coinGeckoApiUrl)) {
Serial.println("Connection to API failed");
return;
}
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString();
http.end();
DynamicJsonDocument doc(1024);
deserializeJson(doc, response);
double usd24HrVolume = doc["bitcoin"]["usd_24h_vol"];
double usdPrice = doc["bitcoin"]["usd"];
double usd24HrChange = doc["bitcoin"]["usd_24h_change"];
double eurPrice = doc["bitcoin"]["eur"];
double eur24HrChange = doc["bitcoin"]["eur_24h_change"];
double gbpPrice = doc["bitcoin"]["gbp"];
double gbp24HrChange = doc["bitcoin"]["gbp_24h_change"];
double moscowTime = 100000000/usdPrice;
displayBitcoinPrices(usdPrice, usd24HrChange, eurPrice, eur24HrChange, gbpPrice, gbp24HrChange, moscowTime);
}
}
void getAndDisplayBlockHeight() {
HTTPClient http;
if (http.begin(blockHeightApiUrl)) {
int httpResponseCode = http.GET();
if (httpResponseCode == HTTP_CODE_OK) {
String response = http.getString();
http.end();
// Parse the response as a long directly (no identifier)
long height = response.toInt();
displayBlockHeight(height);
} else {
Serial.print("Error on HTTP request. Response code: ");
Serial.println(httpResponseCode);
http.end();
}
} else {
Serial.println("Unable to connect to API");
}
}
void wifiSignal() {
if(WiFi.RSSI() > -80) {
sprite.createSprite(5, 5);
sprite.setTextDatum(4);
sprite.fillSprite(TFT_GREEN);
sprite.pushSprite(295, 14);
sprite.deleteSprite();
sprite.createSprite(5, 10);
sprite.setTextDatum(4);
sprite.fillSprite(TFT_GREEN);
sprite.pushSprite(302, 9);
sprite.deleteSprite();
sprite.createSprite(5, 15);
sprite.setTextDatum(4);
sprite.fillSprite(TFT_GREEN);
sprite.pushSprite(309, 4);
sprite.deleteSprite();
}
else if (WiFi.RSSI() <= -80) {
sprite.createSprite(5, 5);
sprite.setTextDatum(4);
sprite.fillSprite(TFT_ORANGE);
sprite.pushSprite(295, 14);
sprite.deleteSprite();
sprite.createSprite(5, 10);
sprite.setTextDatum(4);
sprite.fillSprite(TFT_ORANGE);
sprite.pushSprite(302, 9);
sprite.deleteSprite();
sprite.createSprite(5, 15);
sprite.setTextDatum(4);
sprite.fillSprite(primaryBGColour);
sprite.pushSprite(309, 4);
sprite.deleteSprite();
}
else {
sprite.createSprite(5, 5);
sprite.setTextDatum(4);
sprite.fillSprite(TFT_RED);
sprite.pushSprite(295, 14);
sprite.deleteSprite();
sprite.createSprite(5, 10);
sprite.setTextDatum(4);
sprite.fillSprite(primaryBGColour);
sprite.pushSprite(302, 9);
sprite.deleteSprite();
sprite.createSprite(5, 15);
sprite.setTextDatum(4);
sprite.fillSprite(primaryBGColour);
sprite.pushSprite(309, 4);
sprite.deleteSprite();
}
}
void loop() {
if (feesBool) // If Mempool Fees checkbox == TRUE
{
displayLocalTime();
getAndDisplayBlockHeight();
wifiSignal();
getAndDisplayBitcoinPrices();
delay(55000);
//Does removing these fix the missing right side?
//displayLocalTime();
//getAndDisplayBlockHeight();
//wifiSignal();
getAndDisplayBitcoinFees();
delay(7000);
}
else {
displayLocalTime();
getAndDisplayBlockHeight();
wifiSignal();
getAndDisplayBitcoinPrices();
delay(55000);
}
}