-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtc-eth_pricewatch.ino
211 lines (193 loc) · 5.91 KB
/
btc-eth_pricewatch.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
/*
* Fetch the latest BTC/ETH price from Bitstamp and display them on a 96x64px
* SSD1331 OLED display.
* It depends on the SSD_13XX library by sumotoy, a faster alternative to the
* Adafruit
* Download link: https://github.com/sumotoy/SSD_13XX
*/
#include <SPI.h>
#include <SSD_13XX.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
// Load configuration
#include "config.h"
#define __CS 4 //GPIO4 (D2) or GPIO2
#define __DC 5 //(D1)
//SCLK, CLK:connect to D5
//MOSI, RES:connect to D7
SSD_13XX tft = SSD_13XX(__CS, __DC);
const char* host = "www.bitstamp.net";
const int httpsPort = 443;
// Use web browser to view and copy
// SHA1 fingerprint of the certificate
const char* fingerprint = "D0 26 AB 06 64 07 BC 88 56 6D 83 BE 0A 29 00 B5 10 E5 27 D2";
WiFiClientSecure client;
String dataLine1 = "{\"high\": \"0000.00\", \"last\": \"0000.00\", \"low\": \"0000.00\"}";
String dataLine2 = "{\"high\": \"0000.00\", \"last\": \"0000.00\", \"low\": \"0000.00\"}";
void setup() {
Serial.begin(115200);
tft.begin();
tft.clearScreen();
tft.setCursor(0, 0);
tft.println("Connecting");
Serial.println("Connecting...");
//WiFi.mode(WIFI_STA);
WiFi.begin(essid, wifiKey);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
tft.print(WiFi.localIP());
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
}
void loop() {
bool fresh1, fresh2;
if (fresh1 = connectToHost()) {
dataLine1 = fetchUrl(tickerUrl1);
}
if (fresh2 = connectToHost()) {
dataLine2 = fetchUrl(tickerUrl2);
}
// Switch screen every 10 seconds, 6 times. This means 1 refresh
// of the data per minute. We do 2 requests, and Bitstamp has a
// limit of 600 requests per 10 minutes before it blocks your IP.
for (int i = 0; i < 6; i++) {
tft.clearScreen();
if (dataLine1.startsWith("{")) {
printPriceData(fresh1, 0, 0, coinName1, dataLine1, i % 2);
}
if (dataLine2.startsWith("{")) {
printPriceData(fresh2, 0, 1, coinName2, dataLine2, i % 2);
}
delay(10000);
}
}
bool connectToHost() {
/*
* Connect to the host over HTTPS. Also verify the certificate's
* fingerprint. Return true if the connection succeeds.
*/
Serial.print("Connecting to Bitstamp ");
bool connection = client.connect(host, httpsPort);
if (connection) {
Serial.println("OK");
} else {
Serial.println("FAIL");
}
Serial.print("Verifying fingerprint ");
connection = connection && client.verify(fingerprint, host);
if (connection) {
Serial.println("OK");
} else {
Serial.println("FAIL");
}
return connection;
}
void printPriceData(bool fresh, int x, int y, char* unit, String line, int stage) {
/*
* Print the current price, and some extra data on the second row.
* Second row alternates between (low, high) and (total_diff, diff_since_open)
* Total diff is the percentage difference between the daily low and high.
* Diff since open is the percentage difference between the opening price and
* the current one.
*/
tft.setCursor(x, y * 32);
tft.setTextColor(MAGENTA);
tft.print(unit);
tft.print(": ");
String high = getStringFromJSON("high", line);
String low = getStringFromJSON("low", line);
String last = getStringFromJSON("last", line);
String open = getStringFromJSON("open", line);
// See if the price went up or down so we can color the price accordingly.
float last_f = last.toFloat();
float open_f = open.toFloat();
float pct_since_open = 100.0 * (last_f - open_f) / open_f;
// First print the current price.
tft.setCursor(48, y * 32);
if (fresh) {
if (pct_since_open >= 0.1) {
tft.setTextColor(GREEN);
} else if (pct_since_open <= -0.1) {
tft.setTextColor(RED);
} else {
tft.setTextColor(WHITE);
}
} else {
tft.setTextColor(YELLOW);
}
tft.print(last);
// Pick the correct data to display on the 2nd line.
if (stage == 0) {
if (fresh) {
tft.setTextColor(WHITE);
} else {
tft.setTextColor(YELLOW);
}
tft.setCursor(0, y * 32 + 16);
tft.print(low);
tft.setCursor(48, y * 32 + 16);
tft.print(high);
} else if (stage == 1) {
float high_f = high.toFloat();
float low_f = low.toFloat();
float low_high_spread = 100.0 * (high_f - low_f) / low_f;
tft.setCursor(0, y * 32 + 16);
if (fresh) {
tft.setTextColor(WHITE);
} else {
tft.setTextColor(YELLOW);
}
tft.print(low_high_spread, 2);
tft.print("%");
tft.setCursor(48, y * 32 + 16);
if (fresh) {
if (pct_since_open >= 0.1) {
tft.setTextColor(GREEN);
} else if (pct_since_open <= -0.1){
tft.setTextColor(RED);
} else {
tft.setTextColor(WHITE);
}
} else {
tft.setTextColor(YELLOW);
}
if (pct_since_open >=0) {
tft.print("+");
} else {
tft.print("-");
}
tft.print(abs(pct_since_open), 2);
tft.print("%");
}
}
String fetchUrl(char* url) {
/*
* Fetch a URL and return the data. In our case,
* everything comes on one line, which saves us
* some headaches.
*/
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: ESP8266 Bitstamp Ticker <https://github.com/jrial/ESP8266BitstampTicker>\r\n" +
"Connection: close\r\n\r\n");
Serial.println(String("Fetched https://") + host + "/" + url);
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("Headers received");
break;
}
}
String line = client.readStringUntil('\n');
Serial.println("Received data:");
Serial.println(line);
return line;
}
String getStringFromJSON(String needle, String haystack) {
String searchStr = "\"" + needle + "\":";
int beginPos = haystack.indexOf(searchStr) + searchStr.length() + 2;
int endPos = haystack.indexOf("\"", beginPos);
String result = haystack.substring(beginPos, endPos);
return result;
}