-
Notifications
You must be signed in to change notification settings - Fork 4
/
lntrigger.ino
340 lines (294 loc) · 8.64 KB
/
lntrigger.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//////////////LOAD LIBRARIES////////////////
#include <M5Stack.h>
#include "FS.h"
#include <WiFiManager.h>
#include <ArduinoJson.h>
#include <WiFiClientSecure.h>
#include "SPIFFS.h"
/////////////////SOME DEFINES///////////////////
#define LED_PIN 25
/////////////////SOME VARIABLES///////////////////
char lnbits_server[40] = "lnbits.com";
char invoice_key[500] = "";
char lnbits_description[100] = "";
char lnbits_amount[500] = "1000";
char high_pin[5] = "16";
char time_pin[20] = "3000";
char static_ip[16] = "10.0.1.56";
char static_gw[16] = "10.0.1.1";
char static_sn[16] = "255.255.255.0";
String payReq = "";
String dataId = "";
bool paid = false;
bool shouldSaveConfig = false;
bool down = false;
const char* spiffcontent = "";
String spiffing;
/////////////////////SETUP////////////////////////
void setup() {
M5.begin();
logo_screen();
delay(3000);
lnbits_screen();
// START PORTAL
portal();
}
///////////////////MAIN LOOP//////////////////////
void loop() {
pinMode (atoi(high_pin), OUTPUT);
digitalWrite(atoi(high_pin), LOW);
if(String(invoice_key) == ""){
error_screen();
}
Serial.println(String(invoice_key));
getinvoice();
if(down){
error_screen();
getinvoice();
delay(5000);
}
if(payReq != ""){
qrdisplay_screen();
delay(5000);
}
while(paid == false && payReq != ""){
checkinvoice();
if(paid){
complete_screen();
digitalWrite(atoi(high_pin), HIGH);
delay(atoi(time_pin));
digitalWrite(atoi(high_pin), LOW);
}
delay(3000);
}
payReq = "";
dataId = "";
paid = false;
delay(4000);
}
//////////////////DISPLAY///////////////////
void logo_screen()
{
M5.Lcd.fillScreen(WHITE);
M5.Lcd.setCursor(0, 80);
M5.Lcd.setTextSize(4);
M5.Lcd.setTextColor(TFT_PURPLE);
M5.Lcd.println("LNbitsTrigger");
}
void processing_screen()
{
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(40, 80);
M5.Lcd.setTextSize(4);
M5.Lcd.setTextColor(TFT_WHITE);
M5.Lcd.println("PROCESSING");
}
void lnbits_screen()
{
M5.Lcd.fillScreen(WHITE);
M5.Lcd.setCursor(10, 90);
M5.Lcd.setTextSize(3);
M5.Lcd.setTextColor(TFT_BLACK);
M5.Lcd.println("POWERED BY LNBITS");
}
void portal_screen()
{
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(30, 80);
M5.Lcd.setTextSize(3);
M5.Lcd.setTextColor(TFT_WHITE);
M5.Lcd.println("PORTAL LAUNCHED");
}
void complete_screen()
{
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(60, 80);
M5.Lcd.setTextSize(4);
M5.Lcd.setTextColor(TFT_WHITE);
M5.Lcd.println("COMPLETE");
}
void error_screen()
{
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(70, 80);
M5.Lcd.setTextSize(4);
M5.Lcd.setTextColor(TFT_WHITE);
M5.Lcd.println("ERROR");
}
void qrdisplay_screen()
{
M5.Lcd.fillScreen(BLACK);
M5.Lcd.qrcode(payReq,45,0,240,14);
delay(100);
}
//////////////////NODE CALLS///////////////////
void getinvoice() {
WiFiClientSecure client;
client.setInsecure();
const char* lnbitsserver = lnbits_server;
const char* invoicekey = invoice_key;
const char* lnbitsamount = lnbits_amount;
const char* lnbitsdescription = lnbits_description;
if (!client.connect(lnbitsserver, 443)){
down = true;
return;
}
String topost = "{\"out\": false,\"amount\" : " + String(lnbitsamount) + ", \"memo\" :\""+ String(lnbitsdescription) + String(random(1,1000)) + "\"}";
String url = "/api/v1/payments";
client.print(String("POST ") + url +" HTTP/1.1\r\n" +
"Host: " + lnbitsserver + "\r\n" +
"User-Agent: ESP32\r\n" +
"X-Api-Key: "+ invoicekey +" \r\n" +
"Content-Type: application/json\r\n" +
"Connection: close\r\n" +
"Content-Length: " + topost.length() + "\r\n" +
"\r\n" +
topost + "\n");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
if (line == "\r") {
break;
}
}
String line = client.readString();
StaticJsonDocument<1000> doc;
DeserializationError error = deserializeJson(doc, line);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
const char* payment_hash = doc["checking_id"];
const char* payment_request = doc["payment_request"];
payReq = payment_request;
dataId = payment_hash;
}
void checkinvoice(){
WiFiClientSecure client;
client.setInsecure();
const char* lnbitsserver = lnbits_server;
const char* invoicekey = invoice_key;
if (!client.connect(lnbitsserver, 443)){
down = true;
return;
}
String url = "/api/v1/payments/";
client.print(String("GET ") + url + dataId +" HTTP/1.1\r\n" +
"Host: " + lnbitsserver + "\r\n" +
"User-Agent: ESP32\r\n" +
"Content-Type: application/json\r\n" +
"Connection: close\r\n\r\n");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
if (line == "\r") {
break;
}
}
String line = client.readString();
Serial.println(line);
StaticJsonDocument<200> doc;
DeserializationError error = deserializeJson(doc, line);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
bool charPaid = doc["paid"];
paid = charPaid;
}
void portal(){
WiFiManager wm;
Serial.println("mounting FS...");
while(!SPIFFS.begin(true)){
Serial.println("failed to mount FS");
delay(200);
}
//CHECK IF RESET IS TRIGGERED/WIPE DATA
for (int i = 0; i <= 100; i++) {
if (M5.BtnA.wasPressed()){
portal_screen();
delay(1000);
File file = SPIFFS.open("/config.txt", FILE_WRITE);
file.print("placeholder");
wm.resetSettings();
i = 100;
}
delay(50);
M5.update();
}
//MOUNT FS AND READ CONFIG.JSON
File file = SPIFFS.open("/config.txt");
spiffing = file.readStringUntil('\n');
spiffcontent = spiffing.c_str();
DynamicJsonDocument json(1024);
deserializeJson(json, spiffcontent);
if(String(spiffcontent) != "placeholder"){
strcpy(lnbits_server, json["lnbits_server"]);
strcpy(lnbits_description, json["lnbits_description"]);
strcpy(invoice_key, json["invoice_key"]);
strcpy(lnbits_amount, json["lnbits_amount"]);
strcpy(high_pin, json["high_pin"]);
strcpy(time_pin, json["time_pin"]);
}
//ADD PARAMS TO WIFIMANAGER
wm.setSaveConfigCallback(saveConfigCallback);
WiFiManagerParameter custom_lnbits_server("server", "LNbits server", lnbits_server, 40);
WiFiManagerParameter custom_lnbits_description("description", "Memo", lnbits_description, 200);
WiFiManagerParameter custom_invoice_key("invoice", "LNbits invoice key", invoice_key, 500);
WiFiManagerParameter custom_lnbits_amount("amount", "Amount to charge (sats)", lnbits_amount, 10);
WiFiManagerParameter custom_high_pin("high", "Pin to turn on", high_pin, 5);
WiFiManagerParameter custom_time_pin("time", "Time for pin to turn on for (milisecs)", time_pin, 20);
wm.addParameter(&custom_lnbits_server);
wm.addParameter(&custom_lnbits_description);
wm.addParameter(&custom_invoice_key);
wm.addParameter(&custom_lnbits_amount);
wm.addParameter(&custom_high_pin);
wm.addParameter(&custom_time_pin);
//IF RESET WAS TRIGGERED, RUN PORTAL AND WRITE FILES
if (!wm.autoConnect("⚡lntrigger⚡", "password1")) {
Serial.println("failed to connect and hit timeout");
delay(3000);
ESP.restart();
delay(5000);
}
Serial.println("connected :)");
strcpy(lnbits_server, custom_lnbits_server.getValue());
strcpy(lnbits_description, custom_lnbits_description.getValue());
strcpy(invoice_key, custom_invoice_key.getValue());
strcpy(lnbits_amount, custom_lnbits_amount.getValue());
strcpy(high_pin, custom_high_pin.getValue());
strcpy(time_pin, custom_time_pin.getValue());
if (shouldSaveConfig) {
Serial.println("saving config");
DynamicJsonDocument json(1024);
json["lnbits_server"] = lnbits_server;
json["lnbits_description"] = lnbits_description;
json["invoice_key"] = invoice_key;
json["lnbits_amount"] = lnbits_amount;
json["high_pin"] = high_pin;
json["time_pin"] = time_pin;
File configFile = SPIFFS.open("/config.txt", "w");
if (!configFile) {
Serial.println("failed to open config file for writing");
}
serializeJsonPretty(json, Serial);
serializeJson(json, configFile);
configFile.close();
shouldSaveConfig = false;
}
Serial.println("local ip");
Serial.println(WiFi.localIP());
Serial.println(WiFi.gatewayIP());
Serial.println(WiFi.subnetMask());
}
void saveConfigCallback () {
processing_screen();
Serial.println("Should save config");
shouldSaveConfig = true;
}