· 6 years ago · Sep 15, 2019, 09:54 PM
1#include "ONSplash.c"
2#include <M5Stack.h>
3#include <string.h>
4#include <ArduinoJson.h>
5#include <WiFiClientSecure.h>
6#include <math.h>
7
8#define KEYBOARD_I2C_ADDR 0X08
9#define KEYBOARD_INT 5
10
11//Wifi details
12char wifiSSID[] = "YOUR-WIFI";
13char wifiPASS[] = "YOUR-WIFI-PASSWORD";
14
15//BLITZ DETAILS
16const char* server = "api.opennode.co";
17const int httpsPort = 443;
18String apikey = "YOUR-OPENNODE-API-KEY";
19String description = "FLUX"; //invoice description
20String hints = "false";
21
22String choice = "";
23
24String on_currency = "BTCEUR"; //currency can be changed here ie BTCEUR BTCGBP etc
25String on_sub_currency = on_currency.substring(3);
26
27
28 String key_val;
29 String cntr = "0";
30 String inputs = "";
31 int keysdec;
32 int keyssdec;
33 float temp;
34 String fiat;
35 float satoshis;
36 String nosats = "";
37 float conversion;
38 String postid = "";
39 String data_id = "";
40 String data_lightning_invoice_payreq = "";
41 String data_status = "";
42 bool settle = false;
43 String payreq = "";
44 String hash = "";
45
46void page_input()
47{
48 M5.Lcd.fillScreen(BLACK);
49 M5.Lcd.setTextColor(TFT_WHITE);
50 M5.Lcd.setTextSize(3);
51 M5.Lcd.setCursor(0, 40);
52 M5.Lcd.println("Amount then C");
53 M5.Lcd.println("");
54 M5.Lcd.println("EUR: ");
55 M5.Lcd.println("");
56 M5.Lcd.println("SATS: ");
57 M5.Lcd.println("");
58 M5.Lcd.println("");
59 M5.Lcd.setTextSize(2);
60 M5.Lcd.setCursor(50, 200);
61 M5.Lcd.println("TO RESET PRESS A");
62}
63
64void page_processing()
65{
66 M5.Lcd.fillScreen(BLACK);
67 M5.Lcd.setCursor(40, 80);
68 M5.Lcd.setTextSize(4);
69 M5.Lcd.setTextColor(TFT_WHITE);
70 M5.Lcd.println("PROCESSING");
71
72}
73
74void get_keypad(){
75
76 if(digitalRead(KEYBOARD_INT) == LOW) {
77 Wire.requestFrom(KEYBOARD_I2C_ADDR, 1); // request 1 byte from keyboard
78 while (Wire.available()) {
79 uint8_t key = Wire.read(); // receive a byte as character
80 key_val = key;
81
82 if(key != 0) {
83 if(key >= 0x20 && key < 0x7F) { // ASCII String
84 if (isdigit((char)key)){
85 key_val = ((char)key);
86 }
87 else {
88 key_val = "";
89 }
90 }
91 }
92 }
93 }
94}
95
96void page_qrdisplay(String xxx)
97{
98
99 M5.Lcd.fillScreen(BLACK);
100 M5.Lcd.qrcode(payreq,45,0,240,10);
101 delay(100);
102
103}
104
105void setup() {
106 M5.begin();
107 M5.Lcd.drawBitmap(0, 0, 320, 240, (uint8_t *)ONSplash_map);
108 Wire.begin();
109
110
111 //connect to local wifi
112 WiFi.begin(wifiSSID, wifiPASS);
113 while (WiFi.status() != WL_CONNECTED) {
114 delay(2000);
115 }
116
117 pinMode(KEYBOARD_INT, INPUT_PULLUP);
118
119 on_rates();
120}
121
122void loop() {
123
124 page_input();
125
126 cntr = "1";
127 while (cntr == "1"){
128 M5.update();
129 get_keypad();
130
131 if (M5.BtnC.wasReleased()) {
132 // if (key_val == "="){
133
134 page_processing();
135
136 reqinvoice(nosats);
137
138 page_qrdisplay(payreq);
139
140 int counta = 0;
141 int tempi = 0;
142 settle = false;
143
144 while (tempi == 0){
145
146
147
148 checkpayment(data_id);
149
150
151
152 if (data_status == "unpaid"){
153 counta ++;
154 Serial.print(counta);
155 if (counta == 100){
156 tempi = 1;
157 }
158 // delay(300);
159 }
160
161 else{
162 tempi = 1;
163
164 M5.Lcd.fillScreen(BLACK);
165 M5.Lcd.setCursor(60, 80);
166 M5.Lcd.setTextSize(4);
167 M5.Lcd.setTextColor(TFT_GREEN);
168 M5.Lcd.println("COMPLETE");
169
170 delay(2000);
171 M5.Lcd.fillScreen(BLACK);
172 M5.Lcd.setTextColor(TFT_WHITE);
173 page_input();
174
175 }
176
177 int bee = 0;
178 while ((bee < 120) && (tempi==0)){
179
180 M5.update();
181
182
183 if (M5.BtnA.wasReleased()) {
184
185 tempi = -1;
186
187 M5.Lcd.fillScreen(BLACK);
188 M5.Lcd.setCursor(50, 80);
189 M5.Lcd.setTextSize(4);
190 M5.Lcd.setTextColor(TFT_RED);
191 M5.Lcd.println("CANCELLED");
192
193 delay(2000);
194
195 page_input();
196
197 }
198
199 delay(10);
200 bee++;
201 key_val = "";
202 inputs = "";
203
204 }
205
206
207 }
208
209 key_val = "";
210 inputs = "";
211 }
212
213 else if (M5.BtnA.wasReleased()) {
214 // else if (key_val == "+"){
215
216 M5.Lcd.fillScreen(BLACK);
217 M5.Lcd.setCursor(0, 0);
218 M5.Lcd.setTextColor(TFT_WHITE);
219 page_input();
220 key_val = "";
221 inputs = "";
222 nosats = "";
223 }
224
225
226
227 inputs += key_val;
228
229 temp = inputs.toInt();
230
231 temp = temp / 100;
232
233 fiat = temp;
234
235 satoshis = temp/conversion;
236
237 int intsats = (int) round(satoshis*100000000.0);
238
239 nosats = String(intsats);
240 M5.Lcd.setTextSize(3);
241 M5.Lcd.setTextColor(TFT_RED, TFT_BLACK);
242 M5.Lcd.setCursor(70, 88);
243 M5.Lcd.println(fiat);
244 M5.Lcd.setTextColor(TFT_GREEN, TFT_BLACK);
245 M5.Lcd.setCursor(87, 136);
246 M5.Lcd.println(nosats);
247
248
249 delay(100);
250 key_val = "";
251
252
253
254 }
255}
256
257
258
259//OPENNODE REQUESTS
260
261void on_rates(){
262 WiFiClientSecure client;
263 if (!client.connect(server, httpsPort)) {
264 return;
265 }
266 String url = "/v1/rates";
267 client.print(String("GET ") + url + " HTTP/1.1\r\n" +
268 "Host: " + server + "\r\n" +
269 "User-Agent: ESP32\r\n" +
270 "Connection: close\r\n\r\n");
271 while (client.connected()) {
272 String line = client.readStringUntil('\n');
273 if (line == "\r") {
274
275 break;
276 }
277 }
278 String line = client.readStringUntil('\n');
279 const size_t capacity = 169*JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(168) + 3800;
280 DynamicJsonDocument doc(capacity);
281 deserializeJson(doc, line);
282 conversion = doc["data"][on_currency][on_currency.substring(3)];
283
284}
285
286
287
288void reqinvoice(String value){
289
290 WiFiClientSecure client;
291
292 if (!client.connect(server, httpsPort)) {
293
294 return;
295 }
296
297 String topost = "{ \"amount\": \""+ value +"\", \"description\": \""+ description +"\", \"route_hints\": \""+ hints +"\"}";
298 String url = "/v1/charges";
299
300 client.print(String("POST ") + url + " HTTP/1.1\r\n" +
301 "Host: " + server + "\r\n" +
302 "User-Agent: ESP32\r\n" +
303 "Authorization: " + apikey + "\r\n" +
304 "Content-Type: application/json\r\n" +
305 "Connection: close\r\n" +
306 "Content-Length: " + topost.length() + "\r\n" +
307 "\r\n" +
308 topost + "\n");
309
310 while (client.connected()) {
311 String line = client.readStringUntil('\n');
312 if (line == "\r") {
313
314 break;
315 }
316 }
317 String line = client.readStringUntil('\n');
318
319
320 const size_t capacity = 169*JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(168) + 3800;
321 DynamicJsonDocument doc(capacity);
322
323 deserializeJson(doc, line);
324
325 String data_idd = doc["data"]["id"];
326 data_id = data_idd;
327 String data_lightning_invoice_payreqq = doc["data"]["lightning_invoice"]["payreq"];
328 payreq = data_lightning_invoice_payreqq;
329
330}
331
332
333void checkpayment(String PAYID){
334
335WiFiClientSecure client;
336
337 if (!client.connect(server, httpsPort)) {
338
339 return;
340 }
341
342 String url = "/v1/charge/" + PAYID;
343
344
345 client.print(String("GET ") + url + " HTTP/1.1\r\n" +
346 "Host: " + server + "\r\n" +
347 "Authorization: " + apikey + "\r\n" +
348 "User-Agent: ESP32\r\n" +
349 "Connection: close\r\n\r\n");
350
351
352 while (client.connected()) {
353
354
355 String line = client.readStringUntil('\n');
356 if (line == "\r") {
357 break;
358 }
359 }
360 String line = client.readStringUntil('\n');
361
362
363
364const size_t capacity = JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(4) + JSON_OBJECT_SIZE(14) + 650;
365 DynamicJsonDocument doc(capacity);
366
367 deserializeJson(doc, line);
368
369String data_statuss = doc["data"]["status"];
370data_status = data_statuss;
371
372
373}
374
375
376
377© 2019 GitHub, Inc.
378Terms
379Privacy
380Security
381Status
382Help
383Contact GitHub
384Pricing
385API
386Training
387Blog
388About