#include <Arduino.h>
#include <qrcodeoled.h>
#include <SSD1306.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#define OLEDDISPLAY
// Pin Definitions for Buttons
#define BUTTON0_PIN 25
#define BUTTON1_PIN 26
#define BUTTON2_PIN 27
SSD1306 display(0x3c, 21, 22); // Display initialization (address 0x3C, SDA on D21, SCL on D22)
QRcodeOled qrcode(&display);
int button1State = 0;
int button2State = 0;
int lastButton1State = 0;
int lastButton2State = 0;
int buttonPressCount = 0;
// WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Global variable for QR ID
String id = "";
void setup() {
Serial.begin(115200);
Serial.println("Starting...");
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
display.init();
display.clear();
display.display();
// Setup Buttons as Inputs
pinMode(BUTTON1_PIN, INPUT_PULLUP);
pinMode(BUTTON2_PIN, INPUT_PULLUP);
pinMode(BUTTON0_PIN, INPUT_PULLUP);
// Initialize QR Code
qrcode.init();
qrcode.debug();
Serial.println("QR Code Initialized...");
displayWelcome();
}
void loop() {
button1State = digitalRead(BUTTON1_PIN);
button2State = digitalRead(BUTTON2_PIN);
// Check for Button 1 Press
if (button1State == LOW && lastButton1State == HIGH) {
buttonPressCount = 1;
sendApiRequest(buttonPressCount); // Send API request
delay(200); // Debounce delay
}
// Check for Button 2 Press
if (button2State == LOW && lastButton2State == HIGH) {
buttonPressCount = 2;
sendApiRequest(buttonPressCount); // Send API request
delay(200); // Debounce delay
}
lastButton1State = button1State;
lastButton2State = button2State;
delay(10); // Loop delay
}
void displayQRCode(String qrText) {
qrcode.create(qrText.c_str()); // Create and display QR code
display.display();
Serial.println("QR Code: " + qrText);
}
void sendApiRequest(int buttonPressCount) {
HTTPClient http;
String apiUrl = "http://vm.xdrtech.com/api/add-new-order"; // Replace with your API endpoint
// Start HTTP request
http.begin(apiUrl);
http.addHeader("Content-Type", "application/json");
http.setTimeout(7500); // Set timeout to 5000 ms (5 seconds)
String payload = "{\"quantity\": " + String(buttonPressCount) + "}";
int httpResponseCode = http.POST(payload); // Send POST request
Serial.println("API URL: " + apiUrl);
Serial.println("Payload: " + payload);
Serial.print("httpResponseCode: " );
Serial.println( httpResponseCode);
if (httpResponseCode > 0) {
String response = http.getString(); // Get the response
Serial.println("API Response: " + response);
// Parse the JSON response
DynamicJsonDocument doc(1024); // Create a JSON document
DeserializationError error = deserializeJson(doc, response);
if (error) {
Serial.println("Failed to parse JSON");
} else {
// Extract the URL and ID from the JSON
const char* url = doc["url"]; // Adjust this if your JSON structure differs
id = String(doc["id"]);
Serial.println("URL: " + String(url));
Serial.println("QR ID: " + id);
// Display the URL as a QR code
displayQRCode(url); // Display the extracted URL as QR code
checkPaymentStatus(id); // Immediately check payment status
}
} else {
Serial.println("Error sending request");
Serial.println("HTTP Error: " + String(httpResponseCode));
}
// End HTTP request
http.end();
}
void checkPaymentStatus(String qrID) {
HTTPClient http;
String apiUrl = "http://vm.xdrtech.com/api/fetch-qr-payment-status"; // Replace with your API endpoint
int maxAttempts = 15; // Number of retries
int delayInterval = 3000; // 3 seconds between retries
for (int attempt = 1; attempt <= maxAttempts; attempt++) {
// Start HTTP request
http.begin(apiUrl);
http.addHeader("Content-Type", "application/json");
http.setTimeout(7500); // Set timeout to 7.5 seconds
String payload = "{\"qrID\": \"" + qrID + "\"}";
int httpResponseCode = http.POST(payload); // Send POST request
Serial.println("API URL: " + apiUrl);
Serial.println("Payload: " + payload);
Serial.println("Attempt: " + String(attempt));
if (httpResponseCode > 0) {
String response = http.getString(); // Get the response
Serial.println("API Response: " + response);
// Parse the JSON response
DynamicJsonDocument doc(1024); // Create a JSON document
DeserializationError error = deserializeJson(doc, response);
if (error) {
Serial.println("Failed to parse JSON");
} else {
const char* status = doc["status"]; // Replace "status" with the key used in your API response
if (String(status) == "success") { // Replace "PAID" with the correct success value
Serial.println("Payment Successful");
displayMessage("Payment Successful");
delay(2000);
displayWelcome();
http.end();
return; // Exit the loop if payment is successful
}
}
} else {
Serial.println("Error sending request");
Serial.println("HTTP Error: " + String(httpResponseCode));
}
http.end();
// Wait for the next attempt
delay(delayInterval);
}
// If payment is not successful after 10 attempts
Serial.println("Payment Failed");
displayError("Payment Failed");
}
void displayError(String errorMessage) {
display.clear();
display.drawString(0, 0, errorMessage); // Display the error message
display.display();
delay(2000); // Wait for 2 seconds
display.clear();
display.drawString(0, 0, "Welcome"); // Display "Welcome"
display.display();
}
void displayMessage(String message) {
display.clear();
display.drawString(0, 0, message); // Display the error message
display.display();
}
void displayWelcome() {
display.clear();
display.drawString(0, 0, "Welcome..."); // Display the error message
display.display();
}
Qty-1
Qty-2
Test LED