#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
uint64_t tickCounter = 0;
const String securityKey = "ed81a1824685d2aa780e73b471de925239ab";
const String userId = "8";
#define BTN_AUTHENTICATE_PIN 5
#define BTN_DECLINE_PIN 25
#define TFT_DC 2
#define TFT_CS 15
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
const String url = "https://21e8-109-87-120-18.eu.ngrok.io";
const String authenticationEndpoint = "/api/device/authenticate?userId=" + userId + "&securityKey=" + securityKey;
const String getStatusEndpoint = "/api/device/getStatus?userId=" + userId;
const String declineEndpoint = "/api/device/decline?userId=" + userId;
void cleanScreen(){
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 0);
}
bool sendAuthenticationRequest() {
String body = "{ \"securityKey\" : \"" +
securityKey +
"\", \"userId\" : \"" +
userId +
"\"}";
HTTPClient http;
http.addHeader("Content-Type", "application/json");
http.addHeader("Content-Lenght", String(body.length()));
http.begin(url + authenticationEndpoint);
http.POST(body);
String result = http.getString();
return result == "true";
}
void authenticate() {
cleanScreen();
tft.setTextColor(ILI9341_WHITE);
tft.println("\nSending request...");
if(sendAuthenticationRequest()) {
tft.setTextColor(ILI9341_GREEN);
tft.println("Authenticated successfully");
} else {
tft.setTextColor(ILI9341_RED);
tft.println("Not authenticated");
}
}
bool sendDeclineRequest() {
HTTPClient http;
http.begin(url + declineEndpoint);
http.POST("");
String result = http.getString();
return result == "true";
}
void decline() {
cleanScreen();
tft.setTextColor(ILI9341_WHITE);
tft.println("\Decline request...");
if(sendDeclineRequest()) {
tft.setTextColor(ILI9341_RED);
tft.println("Declined successfully");
} else {
tft.setTextColor(ILI9341_WHITE);
tft.println("No requests pending");
}
}
void checkStatus() {
tft.setTextColor(ILI9341_WHITE);
HTTPClient http;
http.useHTTP10(true);
http.begin(url + getStatusEndpoint);
http.GET();
String result = http.getString();
DynamicJsonDocument doc(2048);
DeserializationError error = deserializeJson(doc, result);
String id = doc["id"].as<String>();
String username = doc["username"].as<String>();
bool is2FAPending = doc["is2FAPending"].as<bool>();
http.end();
cleanScreen();
if(is2FAPending){
tft.setTextColor(ILI9341_GREEN);
tft.println("Pending authentication request from user: "
+ username + " | id: " + id);
} else {
tft.setTextColor(ILI9341_WHITE);
tft.println("No pending requests");
}
}
void setup() {
pinMode(BTN_AUTHENTICATE_PIN, INPUT_PULLUP);
pinMode(BTN_DECLINE_PIN, INPUT_PULLUP);
WiFi.begin(ssid, password, 6);
tft.begin();
tft.setRotation(1);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(100);
tft.print(".");
}
tft.print("\nOK! IP=");
tft.println(WiFi.localIP());
checkStatus();
}
void loop() {
if (digitalRead(BTN_AUTHENTICATE_PIN) == LOW) {
authenticate();
} else if(digitalRead(BTN_DECLINE_PIN) == LOW) {
decline();
} else if(tickCounter % 10 == 0){
checkStatus();
}
++tickCounter;
delay(300);
}