· 5 years ago · Nov 15, 2020, 10:22 PM
1/*
2 11/14/2020
3 Author: V3XED
4 Platforms: ESP8266
5 Language: C++
6*/
7
8/// ---------------------------- READ-ME ----------------------------
9///
10/// This program will display the current BTC price once every minute.
11/// No other coins are supported at this time unfortunately.
12///
13/// This is designed to work on a "SH1106" based OLED display!!
14/// Many displays look identical, so check to see what interface your display uses.
15/// Also, this should be a 'plug and play' code replacement for broken project below...
16/// https://www.instructables.com/Simple-10-Crypto-Currency-Display/
17///
18/// -----------------------------------------------------------------
19
20
21#include <ESP8266WiFi.h>
22#include <WiFiClientSecure.h>
23#include <Wire.h>
24
25#include "SH1106Wire.h"
26// The driver for the OLED display
27// Available on the library manager (Search for "oled ssd1306")
28// https://github.com/squix78/esp8266-oled-ssd1306
29
30#include <ArduinoJson.h>
31// !! NOTE !!: When installing this select an older version than V6 from the drop down
32// Required by the CoinMarketCapApi Library for parsing the response
33// Available on the library manager (Search for "arduino json")
34// https://github.com/squix78/esp8266-oled-ssd1306
35
36#include <ESP8266HTTPClient.h>
37// Required to fetch data from the Coindesk API
38
39#include "SSD1306.h"
40// Progress bar library
41
42
43// ----------------------------
44// Configurations - Update these
45// ----------------------------
46
47char ssid[] = "EXAMPLE"; // your network SSID (name)
48char password[] = "EXAMPLE"; // your network key
49
50#define SCL_PIN D5 // Designate the pin on NodeMcu board to receive the display's SCL connection
51#define SDA_PIN D3 // Designate the pin on NodeMcu board to receive the display's SDA connection
52
53// ----------------------------
54// End of area you need to change
55// ----------------------------
56
57
58WiFiClientSecure client;
59String ipAddressString;
60String bitcoin;
61int progress = 0;
62float coin;
63
64SH1106Wire display(0x3c, SDA_PIN, SCL_PIN);
65
66void setup()
67{
68 Serial.begin(115200);
69
70 // Initialising the display
71 display.init();
72 display.setTextAlignment(TEXT_ALIGN_CENTER);
73 display.setFont(ArialMT_Plain_16);
74 display.drawString(64, 0, F("CRYPTO"));
75 display.setFont(ArialMT_Plain_10);
76 display.drawString(64, 18, F("By V3XED"));
77 display.display();
78 Serial.println("Splash Displayed");
79 delay (1000);
80 display.setTextAlignment(TEXT_ALIGN_CENTER);
81 display.setFont(ArialMT_Plain_10);
82 display.drawString(64, 50, F("Connecting to Wifi..."));
83 display.display();
84
85 // Set WiFi to station mode and disconnect from an AP if it was Previously
86 // connected
87 WiFi.mode(WIFI_STA);
88 WiFi.disconnect();
89 delay(100);
90
91 // Attempt to connect to Wifi network:
92 Serial.print("Connecting Wifi: ");
93 Serial.println(ssid);
94 WiFi.begin(ssid, password);
95 while (WiFi.status() != WL_CONNECTED) {
96 Serial.print(".");
97 delay(500);
98 }
99
100 Serial.println("");
101 Serial.println("WiFi connected");
102 Serial.println("IP address: ");
103 IPAddress ip = WiFi.localIP();
104 Serial.println(ip);
105 Serial.println("");
106 ipAddressString = ip.toString();
107}
108
109
110void loop()
111{
112 // wait for WiFi connection
113 if (WiFi.status() == WL_CONNECTED) {
114 ///Serial.println("Starting Loop");
115 HTTPClient http;
116
117 http.begin("http://api.coindesk.com/v1/bpi/currentprice/USD.json"); //HTTP URL for hosted server(local server)
118 //192.168.43.161 - HOST PORT: 3000 and /api is the target api we need to hit to get response
119 int httpCode = http.GET();
120 // Serial.println("After GET Request");
121 // httpCode will be negative on error
122 if (httpCode > 0) {
123 if (httpCode == HTTP_CODE_OK) {
124 //HTTP_CODE_OK means code == 200
125 String payload = http.getString();// gives us the message received by the GET Request
126
127 const size_t capacity = JSON_OBJECT_SIZE(2) + 2 * JSON_OBJECT_SIZE(3) + 2 * JSON_OBJECT_SIZE(4) + 480;
128 DynamicJsonDocument doc(capacity);
129
130 String json = payload;
131
132 deserializeJson(doc, json);
133
134 JsonObject time = doc["time"];
135
136 const char* time_updateduk = time["updateduk"]; // "Mar 21, 2020 at 02:43 GMT"
137
138 JsonObject bpi_USD = doc["bpi"]["USD"];
139 const char* bpi_USD_code = bpi_USD["code"]; // "USD"
140 const char* bpi_USD_rate = bpi_USD["rate"]; // "8,991.6930"
141 const char* bpi_USD_description = bpi_USD["description"]; // "United States Dollar"
142 float bpi_USD_rate_float = bpi_USD["rate_float"]; // 8991.693
143
144 bitcoin = String(bpi_USD_rate_float);
145
146 Serial.println(time_updateduk);
147 Serial.print("Current BTC Price: ");
148 Serial.print("$");
149 Serial.print(bitcoin);
150 Serial.println(" USD");
151 Serial.println("");
152
153 progress = 0;
154
155 display.clear();
156 display.setTextAlignment(TEXT_ALIGN_CENTER);
157 display.setFont(ArialMT_Plain_10);
158 display.drawString(64, 0, F("Bitcoin Price"));
159 display.setFont(ArialMT_Plain_24);
160 display.drawString(64, 16, "$ " + bitcoin);
161 display.drawProgressBar(14, 50, 100, 4, progress);
162 display.display();
163
164 delay(6000); // Waits 6 seconds and updates the progress bar.
165 progress = (progress + 10);
166 display.drawProgressBar(14, 50, 100, 4, progress);
167 display.display();
168 Serial.print("Updating... ");
169 Serial.print(progress);
170 Serial.print("% ");
171
172 delay(6000); // Waits 6 seconds and updates the progress bar.
173 progress = (progress + 10);
174 display.drawProgressBar(14, 50, 100, 4, progress);
175 display.display();
176 Serial.print(progress);
177 Serial.print("% ");
178
179 delay(6000); // Waits 6 seconds and updates the progress bar.
180 progress = (progress + 10);
181 display.drawProgressBar(14, 50, 100, 4, progress);
182 display.display();
183 Serial.print(progress);
184 Serial.print("% ");
185
186 delay(6000); // Waits 6 seconds and updates the progress bar.
187 progress = (progress + 10);
188 display.drawProgressBar(14, 50, 100, 4, progress);
189 display.display();
190 Serial.print(progress);
191 Serial.print("% ");
192
193 delay(6000); // Waits 6 seconds and updates the progress bar.
194 progress = (progress + 10);
195 display.drawProgressBar(14, 50, 100, 4, progress);
196 display.display();
197 Serial.print(progress);
198 Serial.print("% ");
199
200 delay(6000); // Waits 6 seconds and updates the progress bar.
201 progress = (progress + 10);
202 display.drawProgressBar(14, 50, 100, 4, progress);
203 display.display();
204 Serial.print(progress);
205 Serial.print("% ");
206
207 delay(6000); // Waits 6 seconds and updates the progress bar.
208 progress = (progress + 10);
209 display.drawProgressBar(14, 50, 100, 4, progress);
210 display.display();
211 Serial.print(progress);
212 Serial.print("% ");
213
214 delay(6000); // Waits 6 seconds and updates the progress bar.
215 progress = (progress + 10);
216 display.drawProgressBar(14, 50, 100, 4, progress);
217 display.display();
218 Serial.print(progress);
219 Serial.print("% ");
220
221 delay(6000); // Waits 6 seconds and updates the progress bar.
222 progress = (progress + 10);
223 display.drawProgressBar(14, 50, 100, 4, progress);
224 display.display();
225 Serial.print(progress);
226 Serial.print("% ");
227
228 delay(6000); // Waits 6 seconds and updates the progress bar.
229 progress = (progress + 10);
230 display.drawProgressBar(14, 50, 100, 4, progress);
231 display.display();
232 Serial.print(progress);
233 Serial.println("% ");
234 Serial.println("");
235
236
237
238 }
239
240 }
241 else {
242 Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
243
244 display.clear();
245 display.setTextAlignment(TEXT_ALIGN_CENTER);
246 display.setFont(ArialMT_Plain_10);
247 display.drawString(64, 0, F("WiFi Connection"));
248 display.setFont(ArialMT_Plain_24);
249 display.drawString(64, 16, "FAILED");
250 display.display();
251 }
252
253
254 http.end();
255 }
256
257}