Untitled
unknown
plain_text
a year ago
6.1 kB
10
Indexable
#include <SPI.h>
#include <Ethernet.h>
#include <Wiegand.h>
#include <rdm6300.h>3000202
#define RDM6300_RX_PIN 7 // Cambiato da 10 a 9 per evitare conflitti con lo shield Ethernet
#define PIN_D0 2
#define PIN_D1 3
#define RELE_PIN 5 // Pin del relè per aprire la porta principale
#define RELE_PIN2 4 // Pin del relè controllato da RDM6300
#define LED_PIN 6 // Pin del LED
Rdm6300 rdm6300;
Wiegand wiegand;
unsigned long doorOpenMillis = 0; // Timestamp per quando la porta viene aperta
unsigned long doorOpenDuration = 5000; // Durata dell'apertura della porta in millisecondi
bool doorIsOpen = false; // Stato della porta principale
unsigned long lastTagDetectedMillis = 0; // Timestamp dell'ultima rilevazione del tag
unsigned long tagPresentTimeout = 10000; // Tempo dopo cui il relè e il LED si disattivano quando il tag viene rimosso
// Configurazione Ethernet
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC Address
IPAddress ip(192, 168, 1, 177); // Indirizzo IP statico
EthernetServer server(80); // Porta del server
void setup() {
Serial.begin(9600);
pinMode(RELE_PIN, OUTPUT);
digitalWrite(RELE_PIN, HIGH); // Assicurati che il relè sia inizialmente disattivato
pinMode(RELE_PIN2, OUTPUT);
digitalWrite(RELE_PIN2, HIGH); // Assicurati che il relè sia inizialmente disattivato
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH); // Assicurati che il LED sia inizialmente spento
rdm6300.begin(RDM6300_RX_PIN);
wiegand.onReceive(receivedData, "Card read: ");
wiegand.onReceiveError(receivedDataError, "Card read error: ");
wiegand.onStateChange(stateChanged, "State changed: ");
wiegand.begin(Wiegand::LENGTH_ANY, true);
pinMode(PIN_D0, INPUT);
pinMode(PIN_D1, INPUT);
attachInterrupt(digitalPinToInterrupt(PIN_D0), pinStateChanged, CHANGE);
attachInterrupt(digitalPinToInterrupt(PIN_D1), pinStateChanged, CHANGE);
// Avvia Ethernet
Ethernet.begin(mac, ip);
server.begin();
Serial.print("Server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
unsigned long currentMillis = millis();
// Gestione RFID RDM6300
if (rdm6300.get_tag_id()) { // Controlla se c'è un tag vicino
digitalWrite(RELE_PIN2, LOW); // Attiva il relè se un tag è rilevato
digitalWrite(LED_PIN, HIGH); // Accende il LED
lastTagDetectedMillis = currentMillis; // Aggiorna il timestamp dell'ultima rilevazione
} else if (currentMillis - lastTagDetectedMillis > tagPresentTimeout) {
digitalWrite(RELE_PIN2, HIGH); // Disattiva il relè se il tag non è rilevato da 10 secondi
digitalWrite(LED_PIN, LOW); // Spegne il LED
}
// Controllo della porta principale per disattivare il relè dopo 5 secondi
if (doorIsOpen && currentMillis - doorOpenMillis > doorOpenDuration) {
digitalWrite(RELE_PIN, HIGH);
doorIsOpen = false; // Imposta lo stato della porta come chiusa
Serial.println("Door closed");
}
// Gestione Wiegand
noInterrupts();
wiegand.flush();
interrupts();
// Gestione delle richieste Ethernet
EthernetClient client = server.available();
if (client) {
Serial.println("New client");
boolean currentLineIsBlank = true;
String request = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
request += c;
if (c == '\n' && currentLineIsBlank) {
if (request.indexOf("GET /open") >= 0) {
openDoor(); // Apri la porta se ricevi una richiesta a /open
}
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<h1>Arduino Ethernet Server</h1>");
client.println("<p>Door is ");
client.println(doorIsOpen ? "open" : "closed");
client.println("</p>");
client.println("</html>");
break;
}
if (c == '\n') {
currentLineIsBlank = true;
} else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(1);
client.stop();
Serial.println("Client disconnected");
}
}
void pinStateChanged() {
wiegand.setPin0State(digitalRead(PIN_D0));
wiegand.setPin1State(digitalRead(PIN_D1));
}
void stateChanged(bool plugged, const char* message) {
Serial.print(message);
Serial.println(plugged ? "CONNECTED" : "DISCONNECTED");
}
void receivedData(uint8_t* data, uint8_t bits, const char* message) {
Serial.print(message);
Serial.print(bits);
Serial.print(" bits / ");
uint8_t bytes = (bits+7)/8;
// Codice badge esadecimale atteso
uint8_t expectedCode[3] = {0xD5, 0x1E, 0x2C};
bool isAuthorized = (bytes == 3);
for (int i = 0; i < bytes && isAuthorized; i++) {
Serial.print(data[i] >> 4, 16);
Serial.print(data[i] & 0xF, 16);
if (data[i] != expectedCode[i]) {
isAuthorized = false;
}
}
Serial.println();
if (isAuthorized) {
openDoor();
Serial.println("Access granted: Door opened");
} else {
digitalWrite(RELE_PIN, HIGH);
Serial.println("Access denied");
}
}
void receivedDataError(Wiegand::DataError error, uint8_t* rawData, uint8_t rawBits, const char* message) {
Serial.print(message);
Serial.print(Wiegand::DataErrorStr(error));
Serial.print(" - Raw data: ");
Serial.print(rawBits);
Serial.print(" bits / ");
uint8_t bytes = (rawBits+7)/8;
for (int i=0; i<bytes; i++) {
Serial.print(rawData[i] >> 4, 16);
Serial.print(rawData[i] & 0xF, 16);
}
Serial.println();
}
void openDoor() {
digitalWrite(RELE_PIN, LOW);
doorOpenMillis = millis(); // Imposta il timestamp per l'apertura della porta
doorIsOpen = true; // Imposta lo stato della porta come aperta
Serial.println("Door opened");
}
Editor is loading...
Leave a Comment