#include <LiquidCrystal_I2C.h>
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <Keypad.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
// Wifi
const char *SSID = "Wokwi-GUEST";
const char *pass = "";
// Ambil ip server
const char *host = "testprokoon.000webhostapp.com"; // Konek Kehosting
const int col = 16;
const int row = 2;
String Array;
LiquidCrystal_I2C lcd(0x27, col, row);
int Percobaan = 0;
const byte ROWS = 4; // four rows
const byte COLS = 4; // four columns
// define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}};
#define lampu 2
byte rowPins[ROWS] = {32, 33, 25, 26};
byte colPins[COLS] = {27, 14, 12, 13};
//Pin pada device asli
// byte rowPins[ROWS] = {13,12,14,27}; //connect to the row pinouts of the keypad
// byte colPins[COLS] = {26,25,33,32}; //connect to the column pinouts of the keypad
// inisialisasi instansi keypad
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
int posisition = 6;
TaskHandle_t keypadLCD_TaskHandle;
TaskHandle_t httpTaskHandle;
// Deklarasi Hasil baca di database
String responseToken;
// Deklarasi Http
String LinkToken;
HTTPClient httpToken;
// Task Functions
void httpTask(void *parameter);
void keypadLCD_Task(void *parameter);
String baca_token();
void setup()
{
Serial.begin(115200);
lcd.init();
lcd.backlight();
pinMode(lampu, OUTPUT);
WiFi.hostname("Konsep Token DB");
WiFi.begin(SSID, pass);
while (WiFi.status() != WL_CONNECTED)
{
// coba koneksi terus
Serial.print(".");
delay(500);
}
// apabila sudah terkoneksi
Serial.println("Berhasil Konek");
// Create Tasks
xTaskCreate(httpTask, "httpTask", 10000, NULL, 1, &httpTaskHandle);
xTaskCreate(keypadLCD_Task, "keypadLCD_Task", 10000, NULL, 2, &keypadLCD_TaskHandle);
}
void loop() {}
void httpTask(void *parameter)
{
WiFiClient client;
const int httpport = 80;
for (;;)
{
if (!client.connect(host, httpport))
{
Serial.println("Gagal Koneksi ke Server");
return;
}
Serial.println("Berhasil koneksi ke server");
vTaskDelay(500/portTICK_PERIOD_MS); // Delay for 1 second
}
}
String baca_token(){
LinkToken = "http://" + String(host) + "/bacaToken.php";
httpToken.begin(LinkToken);
httpToken.GET();
responseToken = httpToken.getString();
httpToken.end(); // Menutup koneksi HTTPClient
return responseToken;
}
void keypadLCD_Task(void *parameter)
{
for (;;)
{
char customKey = customKeypad.getKey();
lcd.setCursor(2,0);
lcd.print("MasukanToken");
if (customKey){
if (customKey != 'D' && customKey != '#'){ // agar huruf D dan # tidak masuk di lcd
lcd.setCursor(posisition, 1);
Array += customKey;
lcd.print(customKey);
posisition++;
vTaskDelay(100);
}
if (customKey == '#'){ //enter
// Array.remove(Array.length() - 1);
posisition = 20;
Percobaan++;
Serial.print("Inputan [");
Serial.print(Percobaan);
Serial.print("] : ");
Serial.println(Array);
digitalWrite(LED_BUILTIN, HIGH);
if (Array == baca_token())
{
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Berhasil");
Array = "";
posisition = 6;
digitalWrite(lampu, HIGH);
vTaskDelay(1000);
}
else
{
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("InputSalah");
Array = "";
posisition = 6;
digitalWrite(lampu, LOW);
vTaskDelay(1000);
}
} else if (customKey == 'D')
{
posisition = 6;
lcd.clear();
Array = "";
digitalWrite(LED_BUILTIN, LOW);
}
}
}
}