#include <SPI.h>
#include <MFRC522.h>
#include <LiquidCrystal.h>
#include <Wire.h>
#include "RTClib.h"
#include <SoftwareSerial.h>
#include <EEPROM.h>
#define rs485_RX A0
#define rs485_TX A1
#define rs485_DE A2
#define rs485_RE A3
SoftwareSerial rs485(rs485_RX, rs485_TX); // RX, TX
//RFID
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
String cards[10] = {"", "", "", "", "", "", "", "", "", ""};
String check;
String check_card;
String check_time;
String point = "Point A"; //Point B, Point C
String PORT = "01"; //FROM 01,02,03 TO FE - Total of 254 Slave stations
String MASTER_PORT = "FF";
//Time
RTC_DS1307 rtc;
String currentTime, DD, MM, YYYY, hh, mm;
//LCD
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
//Buzzer
#define buzzer 8
//Serial Communication
char incomingByte;
String command;
boolean messageCompleted = false;
boolean newMessage = false;
void setup() {
pinMode(rs485_RE, OUTPUT);
pinMode(rs485_DE, OUTPUT);
digitalWrite(rs485_RE, LOW);
digitalWrite(rs485_DE, LOW);
Serial.begin(9600); // Initiate a serial communication
rs485.begin(19200);
//RFID
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
//RTC
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
}
//LCD
lcd.begin(16, 2);
//Buzzer
pinMode(buzzer, OUTPUT);
//Read last check from EEPROM memory (card id and timestamp)
delay(10);
check_card = readFromMemory(0, 8);
delay(10);
check_time = readFromMemory(10, 16);
delay(10);
//Read Card id list from memory
for (int i = 0; i < 10; i++) {
cards[i] = readFromMemory(30 + (i * 10), 8);
//storeInMemory(30 + (i*10), "xxxxxxxx");
delay(10);
Serial.println(cards[i]);
}
Serial.println("System is up and running");
}
void setTime(int YYYY, int MM, int DD, int hh, int mm) {
rtc.adjust(DateTime(YYYY, MM, DD, hh, mm, 0));
}
void readTime() {
//DAY
DateTime now = rtc.now();
if (now.day() <= 9) {
DD = "0" + String(now.day());
}
else {
DD = String(now.day());
}
//MONTH
if (now.month() <= 9) {
MM = "0" + String(now.month());
}
else {
MM = String(now.month());
}
//HOUR
if (now.hour() <= 9) {
hh = "0" + String(now.hour());
}
else {
hh = String(now.hour());
}
//MINUTE
if (now.minute() <= 9) {
mm = "0" + String(now.minute());
}
else {
mm = String(now.minute());
}
//YEAR
if (now.year()) {
YYYY = String(now.year());
}
currentTime = DD + "/" + MM + "/" + YYYY + " " + hh + ":" + mm;
}
void checkCard() {
if ( ! mfrc522.PICC_IsNewCardPresent())
{
check = "";
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
check = "";
return;
}
//Show UID on serial monitor
String card = "";
for (byte i = 0; i < mfrc522.uid.size; i++)
{
card.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
card.concat(String(mfrc522.uid.uidByte[i], HEX));
}
card.toUpperCase();
card = card.substring(1);
card.replace(" ", "");
for (int i = 0; i < 10; i++) {
if (card == cards[i]) {
check = "VALID: " + String(card);
check_card = String(card);
check_time = currentTime;
storeInMemory(0, check_card); //Start store card id from memory addr 0
storeInMemory(10, check_time); //Start store time stamp from memory addr 10
return;
}
else {
check = "Card Not found...";
}
}
}
void printLCD(String line1, String line2) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(line1);
lcd.setCursor(0, 1);
lcd.print(line2);
}
void buzzerTone() {
tone(buzzer, 1000); // Send 1KHz sound signal...
delay(1000); // ...for 1 sec
noTone(buzzer); // Stop sound...
}
void storeInMemory(char addr, String data) {
int _size = data.length();
int i;
for (i = 0; i < _size; i++)
{
EEPROM.write(addr + i, data[i]);
}
EEPROM.write(addr + _size, '\0'); //Add termination null character for String Data
}
String readFromMemory(char addr, int size)
{
int i;
char data[size + 1];
int len = 0;
unsigned char k;
k = EEPROM.read(addr);
while (len < size) //Read until null character
{
k = EEPROM.read(addr + len);
data[len] = k;
len++;
}
data[len] = '\0';
return String(data);
}
void serialCommunication() {
if (rs485.available()) {
incomingByte = rs485.read();
if (incomingByte == '>') {
messageCompleted = true;
newMessage = false;
}
else if (incomingByte == '<') {
newMessage = true;
}
if (newMessage) {
command.concat(incomingByte);
}
}
if (messageCompleted) {
if (command.substring(1, 3) == PORT || command.substring(1, 3) == "00" ) {
//Set time
if (command.charAt(3) == 'T') {
int YYYY = (command.substring(4, 8)).toInt();
int MM = (command.substring(8, 10)).toInt();
int DD = (command.substring(10, 12)).toInt();
int hh = (command.substring(12, 14)).toInt();
int mm = (command.substring(14)).toInt();
setTime(YYYY, MM, DD, hh, mm);
}
//Set cards ids
else if (command.charAt(3) == 'C') {
int index = (command.substring(4, 5)).toInt();
String id = command.substring(5);
cards[index] = id;
//Store in memory!
storeInMemory(30 + (index * 10), id); //Start store card id from memory addr 30
}
}
if (command.substring(1, 3) == PORT) {
//Report time of this slave station to master
if (command.charAt(3) == 't') {
digitalWrite(rs485_DE, HIGH);
rs485.println("<" + MASTER_PORT + "t" + currentTime + ">");
digitalWrite(rs485_DE, LOW);
}
//Report card ids of this slave station to master
else if (command.charAt(3) == 'c') {
int index = (command.substring(4,5)).toInt();
String MSG = "<" + MASTER_PORT + "c"+String(index);
MSG += cards[index] + ">";
digitalWrite(rs485_DE, HIGH);
rs485.println(MSG);
digitalWrite(rs485_DE, LOW);
}
//Report last check to master - time and card id
else if (command.charAt(3) == 'r') {
digitalWrite(rs485_DE, HIGH);
rs485.println("<" + MASTER_PORT + "r" + check_card + " " + check_time + ">");
digitalWrite(rs485_DE, LOW);
}
}
command = "";
messageCompleted = false;
}
}
void loop() {
serialCommunication();
checkCard();
printLCD("Waiting for Card", point);
readTime();
if (check != "") {
printLCD(currentTime, check);
buzzerTone();
delay(2000);
}
}
Comments
Please log in or sign up to comment.