#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
#include <Keypad.h>
#include "SafeState.h"
#include "icons.h"
#include <Arduino.h>
#include <EEPROM.h>
const int sensorMag = 6; //Sensor Magnetico
const int ledMag = 7; //Led testigo sensor magnetico
const int sensorMov = 8; //Sensor de movimiento
const int ledMov = 9; //Led testigo sensor de movimiento
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
/* sensor magnetico*/
int stateMag; // 0 cerrado - 1 abierto
int stateMov; // 0 cerrado - 1 abierto
/* Indicacion que hubo disparo */
int flagHuboDisparoZ1; // 0 No hubo - 1 Hubo Disparo
int flagHuboDisparoZ2; // 0 No hubo - 1 Hubo Disparo
/* Safe state */
#define EEPROM_ADDR_LOCKED 0
#define EEPROM_ADDR_CODE_LEN 1
#define EEPROM_ADDR_CODE 2
#define EEPROM_EMPTY 0xff
#define SAFE_STATE_OPEN (char)0
#define SAFE_STATE_LOCKED (char)1
String dataString;
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
RTC_DS1307 rtc;
/* 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);
/* SafeState stores the secret code in EEPROM */
SafeState safeState;
SafeState::SafeState() {
this->_locked = EEPROM.read(EEPROM_ADDR_LOCKED) == SAFE_STATE_LOCKED;
}
void SafeState::lock() {
this->setLock(true);
}
bool SafeState::locked() {
return this->_locked;
}
bool SafeState::hasCode() {
auto codeLength = EEPROM.read(EEPROM_ADDR_CODE_LEN);
return codeLength != EEPROM_EMPTY;
}
void SafeState::setCode(String newCode) {
EEPROM.write(EEPROM_ADDR_CODE_LEN, newCode.length());
for (byte i = 0; i < newCode.length(); i++) {
EEPROM.write(EEPROM_ADDR_CODE + i, newCode[i]);
}
}
bool SafeState::unlock(String code) {
auto codeLength = EEPROM.read(EEPROM_ADDR_CODE_LEN);
if (codeLength == EEPROM_EMPTY) {
// There was no code, so unlock always succeeds
this->setLock(false);
return true;
}
if (code.length() != codeLength) {
return false;
}
for (byte i = 0; i < code.length(); i++) {
auto digit = EEPROM.read(EEPROM_ADDR_CODE + i);
if (digit != code[i]) {
return false;
}
}
this->setLock(false);
return true;
}
void SafeState::setLock(bool locked) {
this->_locked = locked;
EEPROM.write(EEPROM_ADDR_LOCKED, locked ? SAFE_STATE_LOCKED : SAFE_STATE_OPEN);
}
void lock() {
safeState.lock();
}
void unlock() {
}
void showStartupMessage() {
lcd.setCursor(2, 0);
lcd.print("Bienvenido!");
delay(300);
lcd.setCursor(0, 1);
String message = "CamuAlarma v 1.0";
for (byte i = 0; i < message.length(); i++) {
lcd.print(message[i]);
delay(100);
}
delay(300);
}
String inputSecretCode() {
lcd.setCursor(5, 1);
lcd.print("[____]");
lcd.setCursor(6, 1);
String result = "";
while (result.length() < 4) {
checkSensorMag();
checkSensorMov();
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("Ingresar codigo nuevo:");
String newCode = inputSecretCode();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Confirmar");
String confirmCode = inputSecretCode();
if (newCode.equals(confirmCode)) {
safeState.setCode(newCode);
return true;
} else {
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Error");
lcd.setCursor(0, 1);
lcd.print(" NO Activada!");
delay(500);
return false;
}
}
void showUnlockMessage() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(ICON_UNLOCKED_CHAR);
lcd.setCursor(3, 0);
lcd.print("Desactivada");
lcd.setCursor(15, 0);
lcd.write(ICON_UNLOCKED_CHAR);
delay(500);
}
void safeUnlockedLogic() {
time();
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(ICON_UNLOCKED_CHAR);
lcd.setCursor(2, 0);
lcd.print(" # Activar");
lcd.setCursor(15, 0);
lcd.write(ICON_UNLOCKED_CHAR);
bool newCodeNeeded = true;
if (safeState.hasCode() and flagHuboDisparoZ1 == 0 and flagHuboDisparoZ2 == 0) {
lcd.setCursor(0, 1);
lcd.print("A = codigo nuevo");
newCodeNeeded = false;
} else {
if (flagHuboDisparoZ1 == 1 || flagHuboDisparoZ2 == 1 ) {
lcd.setCursor(0, 1);
lcd.print(" Hubo Disparo !!");
if (flagHuboDisparoZ1 == 1) {
delay(1000);
lcd.noDisplay();
delay(300);
lcd.display();
lcd.setCursor(0, 1);
lcd.print(" Zona 1 ");
}
if (flagHuboDisparoZ2 == 1 ) {
delay(1000);
lcd.noDisplay();
delay(300);
lcd.display();
lcd.setCursor(0, 1);
lcd.print(" Zona 2 ");
}
delay(1000);
lcd.noDisplay();
delay(300);
lcd.display();
lcd.setCursor(0, 1);
lcd.print(" Hubo Disparo !!");
newCodeNeeded = false;
}
}
auto key = keypad.getKey();
while (key != 'A' && key != '#') {
checkSensorMag();
checkSensorMov();
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(" Activada! ");
lcd.write(ICON_LOCKED_CHAR);
flagHuboDisparoZ1 = 0;
flagHuboDisparoZ2 = 0;
String userCode = inputSecretCode();
bool unlockedSuccessfully = safeState.unlock(userCode);
showWaitScreen(200);
if (unlockedSuccessfully) {
showUnlockMessage();
unlock();
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Codigo erroneo!");
showWaitScreen(1000);
}
}
void time() {
DateTime now = rtc.now();
char date[10] = "hh:mm:ss";
rtc.now().toString(date);
lcd.home();
lcd.print(now.year(), DEC);
lcd.print('/');
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.day(), DEC);
lcd.print(' ');
lcd.print(date);
delay(1000);
lcd.noDisplay();
delay(300);
lcd.display();
}
void huboDisparo(String zonaDisparo){
lcd.setCursor(0, 0);
lcd.write(ICON_LOCKED_CHAR);
lcd.print("HUBO DISPARO");
delay(300);
lcd.setCursor(0, 0);
lcd.write(ICON_LOCKED_CHAR);
lcd.print(zonaDisparo);
delay(300);
lcd.setCursor(0, 0);
lcd.write(ICON_LOCKED_CHAR);
lcd.print("HUBO DISPARO");
delay(300);
lcd.setCursor(6, 1);
}
void checkSensorMag() {//chequea el sensor magnetico Zona 1
stateMag = digitalRead(sensorMag);
if (stateMag == HIGH){
digitalWrite(ledMag, HIGH); // turn the LED on (HIGH is the voltage level)
if (safeState.locked()) { // si la alarma esta activa, lanzar disparo de alarma
huboDisparo(" Zona 1 ");
if (flagHuboDisparoZ1 == 0) {
flagHuboDisparoZ1 = 1;
}
}
}
else{
digitalWrite(ledMag, LOW); // turn the LED off by making the voltage LOW
}
}
void checkSensorMov() { //chequea el sensor de movimiento Zona 2
stateMov = digitalRead(sensorMov);
if (stateMov == HIGH){//se detecto movimiento
digitalWrite(ledMov, HIGH); // turn the LED on (HIGH is the voltage level)
if (safeState.locked()) { // si la alarma esta activa, lanzar disparo de alarma
huboDisparo(" Zona 2 ");
if (flagHuboDisparoZ2 == 0) {
flagHuboDisparoZ2 = 1;
}
}
}
else{
digitalWrite(ledMov, LOW); // turn the LED off by making the voltage LOW
}
}
void setup() {
Serial.begin(9600);
/* Sensor Magnetico*/
pinMode(sensorMag, INPUT);
pinMode(ledMag, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
/* Sensor Movimiento*/
pinMode(sensorMov, INPUT);
pinMode(ledMov, OUTPUT);
if (! rtc.begin()) {
lcd.print("Couldn't find RTC");
Serial.flush();
abort();
}
// Init
lcd.init();
lcd.backlight();
init_icons(lcd);
showStartupMessage();
flagHuboDisparoZ1 = 0;
flagHuboDisparoZ2 = 0;
}
void loop() {
time();
checkSensorMag();
checkSensorMov();
if (safeState.locked()) {
safeLockedLogic();
} else {
safeUnlockedLogic();
}
}