#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h>
#endif
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Keypad.h>
#include <PubSubClient.h>
#define ssid "Wokwi-GUEST"
#define wifiPassword ""
#define TOKEN "eN8M33rIKJ2jA0iclLIR" //Access token of device Display
#define mqtt_server "thingsboard.cloud"
#define TINGGI_LAYAR 64 // Tinggi layar OLED yang digunakan
#define LEBAR_LAYAR 128 // Lebar layar OLED yang digunakan
#define ROWS 4 //Jumlah baris keypad
#define COLS 4 //Jumlah kolom keypad
#define RELAY_PIN 18
Adafruit_SSD1306 oled(LEBAR_LAYAR, TINGGI_LAYAR, &Wire, -1);
char Keys[ROWS][COLS] = { //Membuat array keypad
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = { 14, 12, 26, 27 };
byte colPins[COLS] = { 5, 4, 2, 15 };
Keypad customKeypad = Keypad( makeKeymap(Keys), rowPins, colPins, ROWS, COLS); //Masukkan info keypad pada library
WiFiClient wifiClient;
PubSubClient client(wifiClient);
char customKey; //Variabel penampung input keypad
int number = 0; //Variabel penampung nilai angka
int password = 1; //Password
int status = WL_IDLE_STATUS;
long startUnlock;
long unlockDuration = 3000;
bool unlockStatus = false;
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
Serial.println("-----------------------");
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
status = WiFi.status();
if ( status != WL_CONNECTED) {
WiFi.begin(ssid, wifiPassword);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to AP");
}
Serial.print("Connecting to ThingsBoard node ...");
// Attempt to connect (clientId, username, password)
if ( client.connect("3a90f960-1ad1-11ed-b480-cbe4cea4aa70", TOKEN, "") ) {
Serial.println( "[DONE]" );
client.subscribe("v1/devices/me/attributes/response/+");
} else {
Serial.print( "[FAILED] [ rc = " );
Serial.println( " : retrying in 5 seconds]" );
delay( 500 );
}
}
}
void setup() {
Serial.begin(9600);
Serial.print(" Connect to : ");
Serial.println(ssid);
WiFi.begin(ssid, wifiPassword);
while (WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print("...");
}
client.setServer( mqtt_server, 1883);
client.setCallback(callback);
// initialize OLED display with I2C address 0x3C
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
pinMode(RELAY_PIN, OUTPUT);
oled.clearDisplay();
oled.setTextSize(1); // Atur ukuran text
oled.setTextColor(WHITE); // Atur warna text
oled.setCursor(20, 20); // Atur posisi text pada display
oled.println("SELAMAT DATANG"); // Text yang dicetak
oled.display();
delay(2000);
oled.clearDisplay();// menampilkan display OLED
}
void sendData(bool unlockStatus, String message){
char attributes[1000];
String payload = "{";
payload += "\"unlockStatus\":";
payload += unlockStatus;
payload += ", \"message\":";
payload += "\""+message+"\"";
payload += "}";
payload.toCharArray( attributes, 1000 );
client.publish( "v1/devices/me/telemetry", attributes);
client.publish( "v1/devices/me/attributes/request/1", ((String)"{\"sharedKeys\":\"testKey\"}").c_str());
Serial.println( attributes );
}
void unlockDoor(bool status){
digitalWrite(RELAY_PIN, status);
if(status){
unlockStatus = true;
startUnlock = millis();
printRow("Access Accepted "); //Tampilan LCD
sendData(true, "Access Accepted");
}else{
unlockStatus = false;
printRow("Invalid Password"); //Tampilan LCD
sendData(false, "Access Denied");
}
}
void loop() {
if(unlockStatus && millis()-startUnlock>unlockDuration){
unlockDoor(LOW);
}
if ( !client.connected()){
reconnect();
}
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(0,0);
oled.print("Input Password"); //Tampilan pada layar LCD
oled.display();
customKey = customKeypad.getKey(); //Baca input keypad
//------------Prosedur jika input berupa angka------------//
switch(customKey){
case '0' ... '9':
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(0,10);
number = number * 10 + (customKey - '0');
oled.print(number);
oled.display();
break;
//------------Jika input '#' maka cek password------------//
case '#':
oled.clearDisplay();
if(number == password){ //Jika password benar, maka
unlockDoor(HIGH);
}
else{
unlockDoor(LOW);
}
delay(2000);
number = 0;
oled.clearDisplay(); break;
//------------Jika input '*' maka hapus tampilan------------//
case '*':
number = 0;
oled.clearDisplay();
break;
}
}
void printRow(String s){
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(0,10);
oled.print(s);
oled.display();
}