#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>
#include <Keypad.h> //Library keypad
#include <LiquidCrystal_I2C.h> //Library LCD I2C
LiquidCrystal_I2C lcd(0x27,16,2); //Alamat I2C
const byte ROWS = 4; //Jumlah baris keypad
const byte COLS = 4; //Jumlah kolom keypad
char Keys[ROWS][COLS] = { //Membuat array keypad
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {19,18,5,17 };
byte colPins[COLS] = { 16, 4, 2, 15};
Keypad customKeypad = Keypad( makeKeymap(Keys), rowPins, colPins, ROWS, COLS); //Masukkan info keypad pada library
char customKey; //Variabel penampung input keypad
int number = 0; //Variabel penampung nilai angka
int passworddor = 1234; //Password
int newpass = 0;
int updatestat = 0;
int trying = 0;
int myRelay = 12;
int kali = 0;
int berhasilkirim = 0;
const char* ssid = "Wokwi-GUEST";
const char* password = "";
String serverName = "https://sgp1.blynk.cloud/external/api/batch/update?token=P0sMvU-4IKRArRSmTMID0O6kUpNSJSAg";
String serverName2 = "https://sgp1.blynk.cloud/external/api/get?token=P0sMvU-4IKRArRSmTMID0O6kUpNSJSAg&v0&v1&v2";
void setup() {
Serial.begin(115200);
lcd.init (); //Mulai LCD
lcd.setBacklight(HIGH); //Nyalakan backlight
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
pinMode(myRelay, OUTPUT);
digitalWrite(myRelay, LOW);
if(WiFi.status()== WL_CONNECTED)
{
HTTPClient http;
String serverPath = serverName2;
http.begin(serverPath);
int httpResponseCode = http.GET();
if (httpResponseCode>0)
{
String payload = http.getString();
JSONVar myObject = JSON.parse(payload);
if (JSON.typeof(myObject) == "undefined")
{
Serial.println("Parsing input failed!");
return;
}
JSONVar keys = myObject.keys();
JSONVar value = myObject[keys[0]];
Serial.print("keys: ");
Serial.println(keys);
newpass = value;
Serial.print("newpass: ");
Serial.println(newpass);
value = myObject[keys[2]];
trying = value;
passworddor = newpass;
updatestat = 1;
}
else
{
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
}
else
{
Serial.println("WiFi Disconnected");
}
}
void loop() {
if (trying<3){
lcd.setCursor(0,0);
lcd.print("Input Password = "); //Tampilan pada layar LCD
customKey = customKeypad.getKey(); //Baca input keypad
//------------Prosedur jika input berupa angka------------//
switch(customKey){
case '0' ... '9':
lcd.setCursor(0,1);
number = number * 10 + (customKey - '0');
lcd.print(number);
Serial.print("Input Password = ");
Serial.println(number);
break;
//------------Jika input '#' maka cek password------------//
case '#':
if(number == passworddor){ //Jika password benar, maka
lcd.setCursor(0,1);
lcd.print("Access Accepted "); //Tampilan LCD
number = 0;
lcd.clear();
int detik = 5;
while (kali <= 5000){
lcd.setCursor(0,0);
lcd.print("Pintu Terbuka");
Serial.println("Pintu Terbuka");
lcd.setCursor(0,1);
lcd.print("Selama " + String(detik) + " dtk");
digitalWrite(myRelay, HIGH); // buka kunci pintu
delay (1000); //delay selama 5 detik sebelum pintu di buka lagi
kali = 1000 + kali;
detik = detik - 1;
}
digitalWrite(myRelay, LOW);
lcd.clear();
kali = 0;
}
else{ //Jika salah, maka
lcd.setCursor(0,1);
lcd.print("Invalid Password"); //Tampilan LCD
Serial.println("Password Salah");
delay(2000);
number = 0;
trying = trying + 1;
lcd.clear();
berhasilkirim = 0;
}
break;
//------------Jika input '*' maka hapus tampilan------------//
case '*':
number = 0;
lcd.clear();
break;
// ----------Jika Menekan Tombol A, Untuk Update Password----//
case 'A':
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Update Password"); //Tampilan LCD
Serial.println("Update Password");
// Membaca Blynk
if(WiFi.status()== WL_CONNECTED)
{
HTTPClient http;
String serverPath = serverName2;
http.begin(serverPath);
int httpResponseCode = http.GET();
if (httpResponseCode>0)
{
String payload = http.getString();
JSONVar myObject = JSON.parse(payload);
if (JSON.typeof(myObject) == "undefined")
{
Serial.println("Parsing input failed!");
return;
}
JSONVar keys = myObject.keys();
JSONVar value = myObject[keys[0]];
Serial.print("keys: ");
Serial.println(keys);
newpass = value;
Serial.print("newpass: ");
Serial.println(newpass);
passworddor = newpass;
updatestat = 1;
}
else
{
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
}
else
{
Serial.println("WiFi Disconnected");
}
// Menulis Blynk
if(WiFi.status()== WL_CONNECTED)
{
HTTPClient http;
String serverPath = serverName +"&v1="+ String(updatestat) ;
http.begin(serverPath);
Serial.println(serverPath);
int httpResponseCode = http.GET();
if (httpResponseCode>0)
{
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);
}
else
{
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Update Password"); //Tampilan LCD
lcd.setCursor(0,1);
lcd.print("Berhasil"); //Tampilan LCD
Serial.println("Update Password Berhasil");
delay(2000);
lcd.clear();
break;
}
// ----- Jika Sudah Tiga Kali Gagal Masukin Password ------------
}
else if (trying == 3){
Serial.println("Akses Terkunci");
lcd.setCursor(0,0);
lcd.print("Akses Terkunci"); //Tampilan LCD
lcd.setCursor(0,1);
lcd.print("Buka Akses di App"); //Tampilan LCD
// Menulis Blynk
if (berhasilkirim != 1 )
if(WiFi.status()== WL_CONNECTED)
{
HTTPClient http;
String serverPath = serverName + "&v2="+ String(trying);
http.begin(serverPath);
Serial.println(serverPath);
int httpResponseCode = http.GET();
if (httpResponseCode>0)
{
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);
berhasilkirim = 1;
Serial.println(berhasilkirim);
}
else
{
Serial.print("Error code: ");
Serial.println(httpResponseCode);
Serial.println("Akses Terkunci Buka Akses Melalui Aplikasi");
}
http.end();
}
// Membaca Blynk
if (berhasilkirim == 1){
if(WiFi.status()== WL_CONNECTED)
{
HTTPClient http;
String serverPath = serverName2;
http.begin(serverPath);
int httpResponseCode = http.GET();
if (httpResponseCode>0)
{
String payload = http.getString();
JSONVar myObject = JSON.parse(payload);
if (JSON.typeof(myObject) == "undefined")
{
Serial.println("Parsing input failed!");
return;
}
JSONVar keys = myObject.keys();
JSONVar value = myObject[keys[2]];
Serial.print("keys: ");
Serial.println(keys);
trying = value;
Serial.print("trying: ");
Serial.println(trying);
if (trying == 0){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Akses Terbuka"); //Tampilan LCD
Serial.println("Akses Kembali di buka");
delay (2000);
lcd.clear();
}
}
else
{
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
}
else
{
Serial.println("WiFi Disconnected");
}
}
lcd.clear();
}
}/* Program Kunci Pintu dengan password input Keypad 4x4 dibuat oleh Indobot */