-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgetBTCPrice_m5stack.ino
216 lines (181 loc) · 5.48 KB
/
getBTCPrice_m5stack.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
using namespace std;
#include "Free_Fonts.h"
#include <WiFi.h>
#include <M5Stack.h>
#include <string>
#include <math.h>
#include <vector>
#include <string>
int lastPrice = 0;
int currentPrice;
int minPrice = 999999999;
int maxPrice = 0;
char servername[] = "api.coindesk.com"; // Google
String answer;
WiFiClient client;
int status = WL_IDLE_STATUS; // the Wifi radio's status
char ssid[] = "SSID";
char password[] = "***********";
vector<string> split(const char *str, char c = '|')
{
vector<string> result;
do
{
const char *begin = str;
while(*str != c && *str)
str++;
result.push_back(string(begin, str));
} while (0 != *str++);
return result;
}
void setup() {
Serial.begin(115200);
delay(100);
Serial.println(readFile(SD, "/LowHigh.txt").c_str());
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
M5.begin();
M5.lcd.setBrightness(25);
M5.Lcd.setTextDatum(MC_DATUM);
M5.update();
vector<string> LowHigh = split(readFile(SD, "/LowHigh.txt").c_str());
String low = String(LowHigh.at(0).c_str());
String high = String(LowHigh.at(1).c_str());
minPrice = low.toInt();
maxPrice = high.toInt();
ConnectToClient();
}
void ConnectToClient() {
if (client.connect(servername, 80)) {
// Make a HTTP request:
client.print(String("GET ") + "/v1/bpi/currentprice.json HTTP/1.1\r\n" +
"Host: api.coindesk.com\r\n" +
"Connection: close\r\n\r\n");
}
}
void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
answer += c;
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
M5.update();
client.stop();
String jsonAnswer;
int jsonIndex;
for (int i = 0; i < answer.length(); i++) {
if (answer[i] == '{') {
jsonIndex = i;
break;
}
}
jsonAnswer = answer.substring(jsonIndex);
jsonAnswer.trim();
int rateIndex = jsonAnswer.indexOf("rate_float");
String priceString = jsonAnswer.substring(rateIndex + 12, rateIndex + 19);
Serial.println(jsonAnswer);
Serial.println("");
Serial.println(priceString);
priceString.trim();
int decimalplace = priceString.indexOf(".");
String Dollars = priceString.substring(0, decimalplace);
String Cents = priceString.substring(decimalplace+1);
while (Cents.length() < 2) {
Cents += "0";
}
String Amount = "$" + Dollars + "." + Cents;
currentPrice = (Dollars + Cents).toInt();
if (currentPrice < minPrice || currentPrice > maxPrice ){
minPrice = min(minPrice,currentPrice);
maxPrice = max(maxPrice,currentPrice);
writeFile(SD, "/LowHigh.txt", ( String(minPrice) + "|" + String(maxPrice)).c_str());
}
else{
minPrice = min(minPrice,currentPrice);
maxPrice = max(maxPrice,currentPrice);
}
/*DRAW
*
*/
M5.Lcd.fillScreen(TFT_BLACK);
M5.Lcd.setFreeFont(FSSBO9);
int ypos = 200;
int fHeight = M5.Lcd.fontHeight(GFXFF);
M5.Lcd.setTextColor(RED);
String str_minPrice = String(minPrice);
String str_minPriceDollars = str_minPrice.substring(0,str_minPrice.length()-2);
String str_minPriceCents = str_minPrice.substring(str_minPrice.length()-2);
M5.Lcd.drawString("Min:",260,ypos,GFXFF);
M5.Lcd.drawString("$" + str_minPriceDollars + "." + str_minPriceCents, 260, ypos + fHeight, GFXFF);
M5.Lcd.setTextColor(GREEN);
String str_maxPrice = String(maxPrice);
String str_maxPriceDollars = str_maxPrice.substring(0,str_maxPrice.length()-2);
String str_maxPriceCents = str_maxPrice.substring(str_maxPrice.length()-2);
M5.Lcd.drawString("Max:",60,ypos,GFXFF);
M5.Lcd.drawString("$" + str_maxPriceDollars + "." + str_maxPriceCents, 60, ypos + fHeight, GFXFF);
M5.Lcd.setTextColor(WHITE);
M5.Lcd.setFreeFont(FSSBO18);
M5.Lcd.drawString("BTC Price", 160, 60, GFXFF);
M5.Lcd.setFreeFont(FSSBO24);
M5.Lcd.drawString(Amount, 160, 120, GFXFF);
if (currentPrice >= lastPrice) //UP
{
M5.Lcd.fillTriangle(140, 205, 180, 205, 160, 180, GREEN);
}
else if (currentPrice < lastPrice) //Down
{
M5.Lcd.fillTriangle(140, 205, 180, 205, 160, 230, RED);
}
lastPrice = currentPrice;
// delay 10 seconds
for (int i = 0; i < 30; i++){
if(M5.BtnA.wasPressed()) {
M5.lcd.setBrightness(0);
}
if(M5.BtnB.wasPressed()) {
M5.lcd.setBrightness(25);
}
if(M5.BtnC.wasPressed()) {
M5.lcd.setBrightness(150);
}
M5.update();
delay(1000);
}
answer = "";
Amount = "";
currentPrice = 0;
ConnectToClient();
}
}
String readFile(fs::FS &fs, const char * path) {
File file = fs.open(path);
if(!file){
writeFile(SD, "/LowHigh.txt", ( String(minPrice) + "|" + String(maxPrice)).c_str());
return "";
}
String stringbuilder = "";
while(file.available()){
char ch = file.read();
stringbuilder += String(ch);
}
return stringbuilder;
}
void writeFile(fs::FS &fs, const char * path, const char * message){
File file = fs.open(path, FILE_WRITE);
if(!file){
return;
}
if(file.print(message)){
Serial.println("new record logged");
} else {
Serial.println("Error writing record");
}
}