diff --git a/M5StackSatsON/M5StackSatsON_Autoconnect.ino b/M5StackSatsON/M5StackSatsON_Autoconnect.ino new file mode 100644 index 0000000..d0e8d72 --- /dev/null +++ b/M5StackSatsON/M5StackSatsON_Autoconnect.ino @@ -0,0 +1,385 @@ +#include "ONSplash.c" +#include +#include +#include +#include +#include +#include +#include +#include + +#define KEYBOARD_I2C_ADDR 0X08 +#define KEYBOARD_INT 5 + +WebServer Server; +AutoConnect Portal(Server); + + +//BLITZ DETAILS +const char* server = "api.opennode.co"; +const int httpsPort = 443; +String apikey = "YOUR-OPENNODE-API-KEY"; +String description = "FLUX"; //invoice description +String hints = "false"; + +String choice = ""; + +String on_currency = "BTCUSD"; //currency can be changed here ie BTCEUR BTCGBP etc +String on_sub_currency = on_currency.substring(3); + + + String key_val; + String cntr = "0"; + String inputs = ""; + int keysdec; + int keyssdec; + float temp; + String fiat; + float satoshis; + String nosats = ""; + float conversion; + String postid = ""; + String data_id = ""; + String data_lightning_invoice_payreq = ""; + String data_status = ""; + bool settle = false; + String payreq = ""; + String hash = ""; + +void rootPage() { + char content[] = "Hello, world"; + Server.send(200, "text/plain", content); +} + + + +void page_input() +{ + M5.Lcd.fillScreen(BLACK); + M5.Lcd.setTextColor(TFT_WHITE); + M5.Lcd.setTextSize(3); + M5.Lcd.setCursor(0, 40); + M5.Lcd.println("Amount then C"); + M5.Lcd.println(""); + M5.Lcd.println("USD: "); + M5.Lcd.println(""); + M5.Lcd.println("SATS: "); + M5.Lcd.println(""); + M5.Lcd.println(""); + M5.Lcd.setTextSize(2); + M5.Lcd.setCursor(50, 200); + M5.Lcd.println("TO RESET PRESS A"); +} + +void page_processing() +{ + M5.Lcd.fillScreen(BLACK); + M5.Lcd.setCursor(40, 80); + M5.Lcd.setTextSize(4); + M5.Lcd.setTextColor(TFT_WHITE); + M5.Lcd.println("PROCESSING"); + +} + +void get_keypad(){ + + if(digitalRead(KEYBOARD_INT) == LOW) { + Wire.requestFrom(KEYBOARD_I2C_ADDR, 1); // request 1 byte from keyboard + while (Wire.available()) { + uint8_t key = Wire.read(); // receive a byte as character + key_val = key; + + if(key != 0) { + if(key >= 0x20 && key < 0x7F) { // ASCII String + if (isdigit((char)key)){ + key_val = ((char)key); + } + else { + key_val = ""; + } + } + } + } + } +} + +void page_qrdisplay(String xxx) +{ + + M5.Lcd.fillScreen(BLACK); + M5.Lcd.qrcode(payreq,45,0,240,10); + delay(100); + +} + +void setup() { + delay(1000); + Serial.begin(115200); + Serial.println(); + + Server.on("/", rootPage); + Portal.begin(); + Serial.println("Web server started:" + + WiFi.localIP().toString()); + M5.begin(); + M5.Lcd.drawBitmap(0, 0, 320, 240, (uint8_t *)ONSplash_map); + Wire.begin(); + + pinMode(KEYBOARD_INT, INPUT_PULLUP); + + on_rates(); +} + +void loop() { + Portal.handleClient(); + + page_input(); + + cntr = "1"; + while (cntr == "1"){ + M5.update(); + get_keypad(); + + if (M5.BtnC.wasReleased()) { + // if (key_val == "="){ + + page_processing(); + + reqinvoice(nosats); + + page_qrdisplay(payreq); + + int counta = 0; + int tempi = 0; + settle = false; + + while (tempi == 0){ + + + + checkpayment(data_id); + + + + if (data_status == "unpaid"){ + counta ++; + Serial.print(counta); + if (counta == 100){ + tempi = 1; + } + // delay(300); + } + + else{ + tempi = 1; + + M5.Lcd.fillScreen(BLACK); + M5.Lcd.setCursor(60, 80); + M5.Lcd.setTextSize(4); + M5.Lcd.setTextColor(TFT_GREEN); + M5.Lcd.println("COMPLETE"); + + delay(2000); + M5.Lcd.fillScreen(BLACK); + M5.Lcd.setTextColor(TFT_WHITE); + page_input(); + + } + + int bee = 0; + while ((bee < 120) && (tempi==0)){ + + M5.update(); + + + if (M5.BtnA.wasReleased()) { + + tempi = -1; + + M5.Lcd.fillScreen(BLACK); + M5.Lcd.setCursor(50, 80); + M5.Lcd.setTextSize(4); + M5.Lcd.setTextColor(TFT_RED); + M5.Lcd.println("CANCELLED"); + + delay(2000); + + page_input(); + + } + + delay(10); + bee++; + key_val = ""; + inputs = ""; + + } + + + } + + key_val = ""; + inputs = ""; + } + + else if (M5.BtnA.wasReleased()) { + // else if (key_val == "+"){ + + M5.Lcd.fillScreen(BLACK); + M5.Lcd.setCursor(0, 0); + M5.Lcd.setTextColor(TFT_WHITE); + page_input(); + key_val = ""; + inputs = ""; + nosats = ""; + } + + + + inputs += key_val; + + temp = inputs.toInt(); + + temp = temp / 100; + + fiat = temp; + + satoshis = temp/conversion; + + int intsats = (int) round(satoshis*100000000.0); + + nosats = String(intsats); + M5.Lcd.setTextSize(3); + M5.Lcd.setTextColor(TFT_RED, TFT_BLACK); + M5.Lcd.setCursor(70, 88); + M5.Lcd.println(fiat); + M5.Lcd.setTextColor(TFT_GREEN, TFT_BLACK); + M5.Lcd.setCursor(87, 136); + M5.Lcd.println(nosats); + + + delay(100); + key_val = ""; + + + + } +} + + + +//OPENNODE REQUESTS + +void on_rates(){ + WiFiClientSecure client; + if (!client.connect(server, httpsPort)) { + return; + } + String url = "/v1/rates"; + client.print(String("GET ") + url + " HTTP/1.1\r\n" + + "Host: " + server + "\r\n" + + "User-Agent: ESP32\r\n" + + "Connection: close\r\n\r\n"); + while (client.connected()) { + String line = client.readStringUntil('\n'); + if (line == "\r") { + + break; + } + } + String line = client.readStringUntil('\n'); + const size_t capacity = 169*JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(168) + 3800; + DynamicJsonDocument doc(capacity); + deserializeJson(doc, line); + conversion = doc["data"][on_currency][on_currency.substring(3)]; + +} + + + +void reqinvoice(String value){ + + WiFiClientSecure client; + + if (!client.connect(server, httpsPort)) { + + return; + } + + String topost = "{ \"amount\": \""+ value +"\", \"description\": \""+ description +"\", \"route_hints\": \""+ hints +"\"}"; + String url = "/v1/charges"; + + client.print(String("POST ") + url + " HTTP/1.1\r\n" + + "Host: " + server + "\r\n" + + "User-Agent: ESP32\r\n" + + "Authorization: " + apikey + "\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; + } + } + String line = client.readStringUntil('\n'); + + + const size_t capacity = 169*JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(168) + 3800; + DynamicJsonDocument doc(capacity); + + deserializeJson(doc, line); + + String data_idd = doc["data"]["id"]; + data_id = data_idd; + String data_lightning_invoice_payreqq = doc["data"]["lightning_invoice"]["payreq"]; + payreq = data_lightning_invoice_payreqq; + +} + + +void checkpayment(String PAYID){ + +WiFiClientSecure client; + + if (!client.connect(server, httpsPort)) { + + return; + } + + String url = "/v1/charge/" + PAYID; + + + client.print(String("GET ") + url + " HTTP/1.1\r\n" + + "Host: " + server + "\r\n" + + "Authorization: " + apikey + "\r\n" + + "User-Agent: ESP32\r\n" + + "Connection: close\r\n\r\n"); + + + while (client.connected()) { + + + String line = client.readStringUntil('\n'); + if (line == "\r") { + break; + } + } + String line = client.readStringUntil('\n'); + + + +const size_t capacity = JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(4) + JSON_OBJECT_SIZE(14) + 650; + DynamicJsonDocument doc(capacity); + + deserializeJson(doc, line); + +String data_statuss = doc["data"]["status"]; +data_status = data_statuss; + + +}