/**
Arduino Electronic Safe
Copyright (C) 2020, Uri Shaked.
Released under the MIT License.
*/
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include "SafeState.h"
#include "icons.h"
#include <LcdMenu.h>
/* Locking mechanism definitions */
#define SERVO_PIN 6
#define SERVO_LOCK_POS 20
#define SERVO_UNLOCK_POS 90
/* Display */
LiquidCrystal_I2C lcd(0x27, 20, 4);
/* Keypad setup */
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
byte rowPins[KEYPAD_ROWS] = {5, 4, 3, 2};
byte colPins[KEYPAD_COLS] = {A3, A2, A1, A0};
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
/* Output: Room (relays) setup. */
#define ROOM1_PIN = 13
#define ROOM2_PIN = 12
/* SafeState stores the secret code in EEPROM */
SafeState safeState;
void lock() {
// lockServo.write(SERVO_LOCK_POS);
safeState.lock();
}
void unlock() {
// lockServo.write(SERVO_UNLOCK_POS);
}
void showStartupMessage() {
lcd.setCursor(5, 0);
lcd.print("Welcome!");
delay(1000);
lcd.setCursor(0, 2);
String message = "Digital Submetering";
for (byte i = 0; i < message.length(); i++) {
lcd.print(message[i]);
delay(100);
}
delay(500);
}
String inputSecretCode() {
lcd.setCursor(5, 1);
lcd.print("[____]");
lcd.setCursor(6, 1);
String result = "";
while (result.length() < 4) {
char key = keypad.getKey();
if (key >= '0' && key <= '9') {
lcd.print('*');
result += key;
}
}
return result;
}
void showWaitScreen(int delayMillis) {
lcd.setCursor(2, 1);
lcd.print("[..........]");
lcd.setCursor(3, 1);
for (byte i = 0; i < 10; i++) {
delay(delayMillis);
lcd.print("=");
}
}
bool setNewCode() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter new code:");
String newCode = inputSecretCode();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Confirm new code");
String confirmCode = inputSecretCode();
if (newCode.equals(confirmCode)) {
safeState.setCode(newCode);
return true;
} else {
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Code mismatch");
lcd.setCursor(0, 1);
lcd.print("Safe not locked!");
delay(2000);
return false;
}
}
void showUnlockMessage() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(ICON_UNLOCKED_CHAR);
lcd.setCursor(4, 0);
lcd.print("Unlocked!");
lcd.setCursor(15, 0);
lcd.write(ICON_UNLOCKED_CHAR);
delay(1000);
}
void safeUnlockedLogic() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Digital Submetering");
lcd.setCursor(1, 2);
lcd.write(ICON_UNLOCKED_CHAR);
lcd.setCursor(2, 2);
lcd.print(" # to lock");
lcd.setCursor(18, 2);
lcd.write(ICON_UNLOCKED_CHAR);
bool newCodeNeeded = true;
if (safeState.hasCode()) {
lcd.setCursor(0, 1);
lcd.print(" A = new code");
newCodeNeeded = false;
}
auto key = keypad.getKey();
while (key != 'A' && key != '#') {
key = keypad.getKey();
}
bool readyToLock = true;
if (key == 'A' || newCodeNeeded) {
readyToLock = setNewCode();
}
if (readyToLock) {
lcd.clear();
lcd.setCursor(5, 0);
lcd.write(ICON_UNLOCKED_CHAR);
lcd.print(" ");
lcd.write(ICON_RIGHT_ARROW);
lcd.print(" ");
lcd.write(ICON_LOCKED_CHAR);
safeState.lock();
lock();
showWaitScreen(100);
}
}
void safeLockedLogic() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(ICON_LOCKED_CHAR);
lcd.print(" Safe Locked! ");
lcd.write(ICON_LOCKED_CHAR);
String userCode = inputSecretCode();
bool unlockedSuccessfully = safeState.unlock(userCode);
showWaitScreen(200);
if (unlockedSuccessfully) {
showUnlockMessage();
unlock();
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Access Denied!");
showWaitScreen(1000);
}
}
// path: /
void showMainMenuPage() {
lcd.setCursor(0, 0);
lcd.print("======= MENU =======");
lcd.setCursor(0, 1);
lcd.print(" A - Room Info");
lcd.setCursor(0, 2);
lcd.print(" B - Current Rate");
lcd.setCursor(0, 3);
lcd.print(" C - Admin Screen");
char key = keypad.getKey();
switch (key) {
case 'A':
Serial.println("A: Goto Input Load");
break;
case 'B':
Serial.println("B: Goto Settings");
case 'C':
Serial.println("C: Goto Admin");
}
}
// path: /info/room
void showRoomMenuPage() {
lcd.setCursor(0, 0);
lcd.print("=== ROOM INFO ====");
lcd.setCursor(0, 1);
lcd.print(" 1 - Room 1");
lcd.setCursor(0, 2);
lcd.print(" 2 - Room 2");
lcd.setCursor(0, 3);
lcd.print(" [D] Back");
}
// path: /info/room/<room_number>
void showRoomInfoPage() {
lcd.setCursor(0, 0);
lcd.print("=== ROOM 1 INFO ====");
lcd.setCursor(0, 1);
lcd.print(" Balance: ");
lcd.setCursor(0, 2);
lcd.print(" Consumption: ");
lcd.setCursor(0, 3);
lcd.print(" [D] Back");
}
// path: /info/rate
void showRateInfoPage() {
lcd.setCursor(0, 0);
lcd.print("=== RATE INFO ====");
lcd.setCursor(0, 1);
lcd.print(" Rate per kWh: ");
lcd.setCursor(0, 2);
lcd.print(" PHP 10.23/kWh ");
lcd.setCursor(0, 3);
lcd.print(" [D] Back");
}
// path: /admin
void showAdminMenuPage() {
lcd.setCursor(0, 0);
lcd.print("======= MENU =======");
lcd.setCursor(0, 1);
lcd.print(" A - Input Load");
lcd.setCursor(0, 2);
lcd.print(" B - Settings");
char key = keypad.getKey();
switch (key) {
case 'A':
Serial.println("A: Goto Input Load");
break;
case 'B':
Serial.println("B: Goto Settings");
}
}
// path: /admin/input
void showInputLoadPage() {
lcd.setCursor(0, 0);
lcd.print("===== Input Load =====");
lcd.setCursor(0, 1);
lcd.print(" 1 - Room 1");
lcd.setCursor(0, 2);
lcd.print(" 2 - Room 2");
char key = keypad.getKey();
switch (key) {
case '1':
Serial.println("1: Goto New Password");
break;
case '2':
Serial.println("2: Goto New Rate");
}
}
// path: /admin/input/<room_number>
void showRoomInputLoadPage() {
lcd.setCursor(0, 0);
lcd.print("===== Input Load : ROOM 1 =====");
lcd.setCursor(0, 1);
lcd.print(" Input (PHP): ");
lcd.setCursor(0, 2);
lcd.print(" _____");
lcd.setCursor(0, 3);
lcd.print("[*] Back [#] Save");
char key = keypad.getKey();
switch (key) {
case '1':
Serial.println("1: Goto New Password");
break;
case '2':
Serial.println("2: Goto New Rate");
}
// case #
// if (input < 100) {
// error
// }
// else {
// store
// }
}
// path: /admin/settings
void showSettingsPage() {
lcd.setCursor(0, 0);
lcd.print("===== Settings =====");
lcd.setCursor(0, 1);
lcd.print(" A - New Password");
lcd.setCursor(0, 2);
lcd.print(" B - New Rate");
char key = keypad.getKey();
switch (key) {
case 'A':
Serial.println("A: Goto New Password");
break;
case 'B':
Serial.println("B: Goto New Rate");
}
}
// path: /admin/settings/password
void showSettingsPasswordPage() {
lcd.setCursor(0, 0);
lcd.print("===== Password Settings =====");
lcd.setCursor(0, 1);
lcd.print(" Current password");
lcd.setCursor(0, 2);
lcd.print("NewPassword");
// save prompt for 2 seconds then
// go to main menu
}
// path: /admin/settings/rate
void showSettingsRatePage() {
lcd.setCursor(0, 0);
lcd.print("===== Rate Settings =====");
lcd.setCursor(0, 1);
lcd.print(" Current rate: ");
lcd.setCursor(0, 2);
lcd.print(" Input rate");
lcd.setCursor(0, 3);
lcd.print("[*] Back [#] Save");
// go to main menu
}
void setup() {
lcd.init();
lcd.backlight();
init_icons(lcd);
// lockServo.attach(SERVO_PIN);
/* Make sure the physical lock is sync with the EEPROM state */
Serial.begin(115200);
Serial.println("System startup.");
// if (safeState.locked()) {
// lock();
// } else {
// unlock();
// }
}
void loop() {
// if (safeState.locked()) {
// safeLockedLogic();
// } else {
// safeUnlockedLogic();
// }
showMainMenuPage();
}