-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitcoin.ino
200 lines (157 loc) · 4.86 KB
/
bitcoin.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
/******************************************************************************
bitcoinprice.ino
This sketch connects to WiFi, gets the last bitcoin price from Bitstamp and
displays it on a MicroOLED. Updates every 10 seconds.
Development environment specifics:
WEMOS D1 Mini
OLED Shield for WeMos D1 mini 0.66" inch 64X48 IIC I2C
Distributed as-is; no warranty is given.
******************************************************************************/
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <SFE_MicroOLED.h>
#include <TimeLib.h>
const char* ssid = "xxxxxxxx";
const char* password = "xxxxxxxxxx";
const char* host = "www.bitstamp.net";
const int httpsPort = 443;
const int fontSize = 2; // 0 Micro, 1 tiny, 2 medium 3 large
// 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";
// MicroOLED Definition
#define PIN_RESET 255
#define DC_JUMPER 0
// MicroOLED Object Declaration //
MicroOLED oled(PIN_RESET, DC_JUMPER); // I2C declaration
void setup()
{
// Setup serial-output
Serial.begin(115200);
delay(10);
oled.begin(); // Initialize the OLED
oled.clear(ALL); // Clear the display's internal memory
oled.clear(PAGE); // Clear the buffer.
printTitle("Connecting", 0);
oled.display();
WiFi.mode(WIFI_STA);
// Connect to WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);
// Set the hostname
WiFi.hostname("weemos");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
printTitle("Getting price fromBitstamp", 0);
oled.display();
// Serial.println("IP address: ");
// Serial.println(WiFi.localIP());
// long rssi = WiFi.RSSI();
// Serial.print("signal strength (RSSI):");
// Serial.print(rssi);
// Serial.println(" dBm");
}
void printPrice()
{
// Use WiFiClientSecure class to create TLS connection
WiFiClientSecure client;
// Serial.print("connecting to ");
// Serial.println(host);
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
ESP.reset() ;
return;
}
if (client.verify(fingerprint, host)) {
// Serial.println("certificate matches");
} else {
Serial.println("certificate doesn't match");
}
String url = "/api/ticker";
// Serial.print("requesting URL: ");
// Serial.println(url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: DisplayWemosESP8266\r\n" +
"Connection: close\r\n\r\n");
// Serial.println("request sent");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
// Serial.println("headers received");
break;
}
}
String line = client.readStringUntil('\n');
if (line.startsWith("{\"high\":")) {
// Serial.println("API call successfull!");
} else {
Serial.println("API call has failed");
ESP.reset() ;
}
int start = line.indexOf("last") + 8;
int end = line.indexOf("timestamp") - 7;
if (fontSize == 1) {
// font is small enough to show 1 decimal
end = line.indexOf("timestamp") - 5;
}
if (fontSize == 0) {
// font is small enough to show 2 decimal2
end = line.indexOf("timestamp") - 4;
}
String price = line.substring(start, end);
printTitle(price, fontSize);
start = line.indexOf("timestamp") + 13;
end = line.indexOf("bid") - 4;
int timeStamp = line.substring(start, end).toInt() + 10*3600;
;
oled.setFontType(0);
oled.setCursor(6,38);
//oled.print(day(timeStamp));
//oled.print("-");
//oled.print(month(timeStamp));
//oled.print("-");
//oled.print(year(timeStamp));
//oled.print(" ");
oled.print(hour(timeStamp));
oled.print(":");
oled.printf("%02d",minute(timeStamp));
oled.print(":");
oled.printf("%02d",second(timeStamp));
oled.display();
}
void loop()
{
printPrice();
delay(5000);
}
// Center and print a small title
void printTitle(String title, int font)
{
// Font 0. 5x8 font. 255 possible characters
// Font 1. 8x16
// Font 2. 10x16. Only numbers and '.' are defined. Looks like 7-segment displays.
// Font 3. 12x48. The biggest font
// Font 4. 31x48.
int middleX = oled.getLCDWidth() / 2;
int middleY = oled.getLCDHeight() / 2;
oled.clear(PAGE);
oled.setFontType(font);
// Try to set the cursor in the middle of the screen
int newX = 0;
int newY = 0;
int lines = 1 + (title.length() * oled.getFontWidth()) / oled.getLCDWidth();
if (lines <= 1) {
// Single line so we try to center it
newX = middleX - ((1 + oled.getFontWidth()) * (title.length() / 2));
}
newY = middleY - (oled.getFontHeight() * lines / 2);
oled.setCursor(newX, newY );
// Print the title:
oled.print(title);
}