-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCryptoExchangeMonitor.ino
220 lines (194 loc) · 5.6 KB
/
CryptoExchangeMonitor.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
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
#include <ArduinoJson.h>
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#include <IRremote.h>
#include <math.h>
#define DEBUG false
SoftwareSerial esp(3,2);
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
IRrecv remote(6);
// currently loaded prices on the chart
float prices[12];
short priceUpdatePos = 0, coinIndex = 1, totalHours = 1;
float updateInterval = 300000;
long lastUpdated;
boolean displayOn = true;
const char* coins[][2] = {
{ "ADA", "cardano" },
{ "BTC", "bitcoin" },
{ "DOGE", "dogecoin" }
};
void setup() {
esp.begin(9600);
Serial.begin(9600);
remote.enableIRIn();
lcd.begin(16, 2);
pinMode(5, OUTPUT);
createBars();
loadSelectedCoin();
}
void loop() {
// Handle remote signals
if (remote.decode()) {
switch (remote.decodedIRData.command) {
case 67: // switch foward between coins
if (coinIndex < sizeof(coins)-1)
coinIndex++;
else coinIndex = 1;
loadSelectedCoin();
break;
case 68: // switch backwards between coins
if (coinIndex > 0)
coinIndex--;
else coinIndex = sizeof(coins)-1;
loadSelectedCoin();
break;
case 70: // scale up one hour
totalHours++;
loadSelectedCoin();
break;
case 21: // scale down one hour
if (coinIndex > 0) {
totalHours--;
loadSelectedCoin();
} break;
case 64: // reload chart
loadSelectedCoin();
break;
case 69: // turn display on/off
displayOn = !displayOn;
digitalWrite(5, displayOn? LOW : HIGH);
if (displayOn)
drawChart();
delay(500);
break;
default:
Serial.println(remote.decodedIRData.command);
break;
}
remote.resume(); // receive the next value
}
}
void loadSelectedCoin() {
Serial.printf("Loading %s (%s)...\n", coins[coinIndex][1], coins[coinIndex][0]);
loadCoinCandles();
drawCoinInfo();
lastUpdated = millis();
}
void loadCoinCandles() {
// 1. get current time
esp.println("getTime");
long unixTimestamp = deserializeResponse(collectResponse(), "").as<JsonObject>()["unixTimestamp"].as<long>();
Serial.printf("Current timestamp: %ld\n", unixTimestamp);
if (unixTimestamp == 0) return;
// 2. get prices
esp.printf("getMarketData|%s|%ld|%ld", coins[coinIndex][1], unixTimestamp-totalHours*3600, unixTimestamp);
int timeOut = millis()+10000, i = 0, c = 1;
while (millis() < timeOut) {
if (esp.available()) {
if (DEBUG)
Serial.print((char)esp.read());
else {
esp.find("\"prices\":[");
do {
DynamicJsonDocument json(128);
deserializeJson(json, esp);
float price = json.as<JsonArray>()[1].as<float>();
if (c%totalHours == 0
&& price > 0 && !isinf(price))
prices[i++] = price;
c++;
if (i == 12) break;
} while (esp.findUntil(",","]"));
}
}
}
// 3. redraw
drawChart();
}
String collectResponse() {
int timeOut = millis() + 10000;
String PROGMEM json;
while (millis() < timeOut) {
if (esp.available()) {
// read fully
while (esp.available()) {
char c = esp.read();
if (DEBUG)
Serial.print(c);
if (c == '{' || json[0] == '{')
json += c;
if (c == '}') // otherwise repeats till timeout
return json;
}
}
}
Serial.println("\nFailed to obtain any response data!");
}
DynamicJsonDocument deserializeResponse(String response, String filter) {
DynamicJsonDocument json(128);
DeserializationError fail;
if (filter.length() > 0) {
StaticJsonDocument<64> filterJson;
filterJson[filter] = true;
fail = deserializeJson(json, response, DeserializationOption::Filter(filterJson));
} else fail = deserializeJson(json, response);
if (fail)
Serial.printf("Failed to parse the response: %s\n", fail.c_str());
return json;
}
void drawChart() {
// calc min/max
float topPrice, botPrice = prices[0];
for (int i = 0; i < 12; i++) {
Serial.println(prices[i]);
if (prices[i] > topPrice)
topPrice = prices[i];
else if (prices[i] < botPrice)
botPrice = prices[i];
}
// draw current prices scaled
for (int i = 0; i < 12; i++)
drawBar(i, round(map(prices[i], botPrice, topPrice, 1, 16)));
}
void drawCoinInfo() {
lcd.setCursor(12, 0);
lcd.write(coins[coinIndex][0]);
if (priceUpdatePos > 0) {
double priceDiff = prices[priceUpdatePos]-prices[priceUpdatePos-1];
double percentage = prices[priceUpdatePos-1]/100;
float prcChange = priceDiff/percentage;
String prcFormatted = String(prcChange, 2);
Serial.println(priceDiff);
Serial.println(percentage);
if (prcChange < 1 && prcChange > 0)
prcFormatted.remove(0, 1);
else if (prcChange > -1 && prcChange < 0)
prcFormatted.remove(1, 1);
//lcd.setCursor(15, 0);
lcd.print((char)(prcChange > 0 ? 94 : prcChange < 0 ? 118 : 45));
lcd.setCursor(12, 1);
if (prcChange < 100 && prcChange >= 1)
lcd.write("+");
lcd.print(prcFormatted);
lcd.write("%");
}
}
// draw a bar with a given height in given position
void drawBar(int pos, int height) {
lcd.setCursor(pos, 1);
lcd.write(height > 8 ? 8 : height);
if (height > 8) {
lcd.setCursor(pos, 0);
lcd.write(height-8);
}
}
// Bytes for a block starting from the bottom with a defined height
void createBars() {
for (int height = 1; height <= 8; height++) {
byte* block = new byte[8];
for (int i = 0; i < 8; i++)
block[i] = (8 - i <= height) ? B11111 : B00000;
lcd.createChar(height, block);
}
}