open-access-control-minimal.../Open_Access_Control/Open_Access_Control.pde

425 lines
12 KiB
Plaintext
Raw Normal View History

2011-09-15 05:10:08 +00:00
/*
2011-11-28 03:00:55 +00:00
* Open Source RFID Access Controller - MINIMAL HTTP EDITION
2011-09-15 05:10:08 +00:00
*
2011-11-28 03:00:55 +00:00
* 11/27/2011 v0.01
* Last build test with Arduino v00.22
*
* Based on Open Source RFID Access Controller code by:
2011-09-15 05:10:08 +00:00
* Arclight - arclight@23.org
* Danozano - danozano@gmail.com
2011-11-28 03:00:55 +00:00
* See: http://code.google.com/p/open-access-control/
*
* Minimal HTTP Edition by:
* Will Bradley - bradley.will@gmail.com
* See: https://github.com/zyphlar/open-access-control-minimal-http
2011-09-15 05:10:08 +00:00
*
* Notice: This is free software and is probably buggy. Use it at
* at your own peril. Use of this software may result in your
* doors being left open, your stuff going missing, or buggery by
* high seas pirates. No warranties are expressed on implied.
* You are warned.
*
*
*
* This program interfaces the Arduino to RFID, PIN pad and all
* other input devices using the Wiegand-26 Communications
* Protocol. It is recommended that the keypad inputs be
* opto-isolated in case a malicious user shorts out the
* input device.
2011-11-28 03:00:55 +00:00
* Outputs go to relays for door hardware/etc control.
2011-09-15 05:10:08 +00:00
*
2011-11-28 08:37:35 +00:00
* Relay outputs on digital pins 6,7,8,9 //TODO: fix this conflict -WB
2011-09-15 05:10:08 +00:00
* Reader 1: pins 2,3
2011-11-28 03:00:55 +00:00
* Ethernet: pins 10,11,12,13 (reserved for the Ethernet shield)
2011-12-03 09:23:34 +00:00
* LCD: pins 4, 3, 2
* Warning buzzer: 8
* Warning led: 9
2011-09-15 05:10:08 +00:00
*
* Quickstart tips:
* Compile and upload the code, then log in via serial console at 57600,8,N,1
*
*/
2011-11-28 05:28:21 +00:00
/////////////////
// Includes
/////////////////
2011-09-15 05:10:08 +00:00
#include <EEPROM.h> // Needed for saving to non-voilatile memory on the Arduino.
2011-11-28 03:00:55 +00:00
#include <Ethernet.h>
2011-09-15 05:10:08 +00:00
#include <SPI.h>
#include <Server.h>
#include <Client.h>
#include <WIEGAND26.h> // Wiegand 26 reader format libary
#include <PCATTACH.h> // Pcint.h implementation, allows for >2 software interupts.
2011-11-30 05:55:38 +00:00
#include <ShiftLCD.h> // LCD via shift register
2011-11-28 07:04:01 +00:00
2011-11-28 05:28:21 +00:00
// Create an instance of the various C++ libraries we are using.
WIEGAND26 wiegand26; // Wiegand26 (RFID reader serial protocol) library
PCATTACH pcattach; // Software interrupt library
2011-09-15 05:10:08 +00:00
2011-11-28 05:28:21 +00:00
/////////////////
// Global variables
/////////////////
2011-09-15 05:10:08 +00:00
2011-11-28 05:28:21 +00:00
// pin assignments
byte reader1Pins[]={2,3}; // Reader 1 pins
2011-11-30 07:02:22 +00:00
byte RELAYPIN1 = 7;
2011-12-03 09:05:35 +00:00
byte buzzerPin = 8;
byte warningLED = 9;
2011-11-30 07:02:22 +00:00
byte extendButton = A5;
byte logoutButton = A4;
2011-11-30 05:55:38 +00:00
// initialize the ShiftLCD library with the numbers of the interface pins
ShiftLCD lcd(4, 6, 5);
2011-09-15 05:10:08 +00:00
2011-11-28 05:28:21 +00:00
// statics
2011-11-28 05:37:23 +00:00
#define RELAYDELAY 1800000 // How long to open door lock once access is granted. (1000 = 1sec)
2011-09-15 05:10:08 +00:00
// Serial terminal buffer (needs to be global)
char inString[40]={0}; // Size of command buffer (<=128 for Arduino)
byte inCount=0;
boolean privmodeEnabled = false; // Switch for enabling "priveleged" commands
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
2011-11-28 02:49:02 +00:00
byte ip[] = { 10,1,1,2 };
byte server[] = { 10,1,1,1 }; // hsl-access
2011-09-15 05:10:08 +00:00
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
Client client(server, 80);
// strings for storing results from web server
String httpresponse = "";
String username = "";
2011-09-15 05:10:08 +00:00
2011-11-28 05:28:21 +00:00
// variables for storing system status
volatile long reader1 = 0;
volatile int reader1Count = 0;
bool authorized = false;
bool relay1high = false;
bool relay2high = false;
unsigned long relay1timer=0;
int extendButtonDebounce = 0;
2011-09-15 05:10:08 +00:00
2011-11-28 05:28:21 +00:00
void setup(){ // Runs once at Arduino boot-up
2011-09-15 05:10:08 +00:00
/* Attach pin change interrupt service routines from the Wiegand RFID readers
*/
pcattach.PCattachInterrupt(reader1Pins[0], callReader1Zero, CHANGE);
pcattach.PCattachInterrupt(reader1Pins[1], callReader1One, CHANGE);
2011-11-30 06:46:08 +00:00
2011-09-15 05:10:08 +00:00
//Clear and initialize readers
wiegand26.initReaderOne(); //Set up Reader 1 and clear buffers.
2011-11-30 06:46:08 +00:00
// Initialize button input
2011-11-30 07:02:22 +00:00
pinMode(extendButton, INPUT);
digitalWrite(extendButton, HIGH);
pinMode(logoutButton, INPUT);
digitalWrite(logoutButton, HIGH);
2011-12-03 09:05:35 +00:00
// Initialize led and buzzer
pinMode(warningLED, OUTPUT);
digitalWrite(warningLED, LOW);
pinMode(buzzerPin, OUTPUT);
digitalWrite(buzzerPin, LOW);
2011-11-30 06:46:08 +00:00
2011-09-15 05:10:08 +00:00
//Initialize output relays
2011-11-30 07:02:22 +00:00
pinMode(RELAYPIN1, OUTPUT);
digitalWrite(RELAYPIN1, LOW); // Sets the relay outputs to LOW (relays off)
2011-09-15 05:10:08 +00:00
Serial.begin(57600); // Set up Serial output at 8,N,1,57600bps
// start the Ethernet connection:
Ethernet.begin(mac, ip);
// set up the LCD's number of rows and columns:
lcd.begin(16, 2);
2011-09-15 05:10:08 +00:00
}
void loop() // Main branch, runs over and over again
{
2011-11-28 08:37:35 +00:00
//////////////////////////
// Normal operation section
//////////////////////////
2011-11-28 05:28:21 +00:00
// check timer -- if expired, remove authorization
if(authorized && relay1high) {
2011-11-30 07:02:22 +00:00
// Detect logout button push
2011-11-30 07:02:22 +00:00
if (analogRead(logoutButton) < 50) {
authorized = false;
}
// Detect extend button push with debounce/repeat
2011-11-30 07:02:22 +00:00
if (analogRead(extendButton) < 50) {
extendButtonDebounce++;
} else {
extendButtonDebounce = 0;
}
if(extendButtonDebounce > 5){
relay1timer += RELAYDELAY;
2011-12-03 09:49:38 +00:00
extendButtonDebounce = -15;
2011-11-30 06:46:08 +00:00
}
2011-11-28 05:37:23 +00:00
// calculate current time elapsed
long currentTime = millis() - relay1timer;
// if time entirely elapsed, deauthorize.
if(currentTime >= RELAYDELAY) {
authorized = false;
}
// calculate for display
long remaining = (RELAYDELAY - currentTime) / 1000;
long secRemaining = (RELAYDELAY - currentTime) / 1000 % 60;
long minRemaining = (RELAYDELAY - currentTime) / 1000 / 60 % 60;
long hrsRemaining = (RELAYDELAY - currentTime) / 1000 / 60 / 60;
2011-11-28 05:28:21 +00:00
// display timer & username
2011-11-30 05:55:38 +00:00
lcd.setCursor(0, 0);
lcd.print(username);
lcd.setCursor(0, 1);
lcd.print(hrsRemaining);
lcd.print(":");
lcd.print(minRemaining);
lcd.print(":");
lcd.print(secRemaining);
2011-11-30 06:46:08 +00:00
lcd.print(" remain ");
2011-12-03 09:23:34 +00:00
2011-12-03 09:05:35 +00:00
if(remaining == 300) {
for(int berp=0; berp<3; berp++){
tone(buzzerPin, 784, 300);
delay(300);
digitalWrite(warningLED, HIGH);
tone(buzzerPin, 659, 600);
lcd.setCursor(15, 1);
lcd.print("!");
delay(1000);
digitalWrite(warningLED, LOW);
lcd.setCursor(15, 1);
lcd.print(" ");
}
}
if(remaining == 60) {
for(int berp=0; berp<5; berp++){
digitalWrite(warningLED, HIGH);
2011-12-03 09:23:34 +00:00
lcd.setCursor(15, 1);
2011-12-03 09:05:35 +00:00
lcd.print("!");
tone(buzzerPin, 1047, 100);
delay(130);
tone(buzzerPin, 1109, 100);
delay(130);
tone(buzzerPin, 1109, 100);
delay(130);
tone(buzzerPin, 1109, 100);
digitalWrite(warningLED, LOW);
delay(500);
}
}
2011-12-03 09:49:38 +00:00
if(remaining == 15) {
2011-12-03 09:05:35 +00:00
for(int berp=0; berp<4; berp++){
digitalWrite(warningLED, HIGH);
tone(buzzerPin, 1661, 800);
delay(800);
digitalWrite(warningLED, LOW);
delay(200);
}
}
2011-11-28 05:28:21 +00:00
}
if(!authorized && relay1high) {
2011-11-30 05:55:38 +00:00
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Turning off.");
delay(500);
lcd.clear();
2011-11-28 05:28:21 +00:00
// not authorized -- turn off relay
relayLow(1);
wiegand26.initReaderOne(); // Reset for next tag scan
2011-11-28 05:28:21 +00:00
}
if(authorized && !relay1high) {
2011-11-30 05:55:38 +00:00
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Turning on.");
delay(500);
lcd.clear();
2011-11-28 05:28:21 +00:00
// authorized -- turn on relay
relayHigh(1);
wiegand26.initReaderOne(); // Reset for next tag scan
2011-11-28 05:28:21 +00:00
}
if(!authorized && !relay1high) {
// display login message
2011-11-30 05:55:38 +00:00
lcd.setCursor(0, 0);
lcd.print("Please login.");
2011-11-30 06:46:08 +00:00
//////////////////////////
// Reader input/authentication section
//////////////////////////
if(reader1Count >= 26)
{ // When tag presented to reader1 (No keypad on this reader)
lcd.clear();
lcd.print("connecting...");
delay(150);
lcd.clear();
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect())
{
Serial.println("connected");
Serial.print("GET /~access/access?device=laser&id=");
Serial.print(reader1, HEX);
Serial.println(" HTTP/1.0");
Serial.println();
client.print("GET /~access/access?device=laser&id=");
client.print(reader1, HEX);
client.println(" HTTP/1.0");
client.println();
// reset values coming from http
httpresponse = "";
username = "";
authorized = false;
}
else
{
// kf you didn't get a connection to the server:
Serial.println("connection failed");
}
wiegand26.initReaderOne(); // Reset for next tag scan
}
while (client.available()) {
char thisChar = client.read();
// only fill up httpresponse with data after a ^ sign.
if (httpresponse.charAt(0) == '^' || thisChar == '^') {
httpresponse += thisChar;
}
}
if(!client.available() && httpresponse.length()>0) {
Serial.println("Response: ");
Serial.println(httpresponse);
int c = httpresponse.indexOf('^');
int d = httpresponse.indexOf('|');
int e = httpresponse.indexOf('$');
Serial.print("IndexOf:");
Serial.println(c);
Serial.println(d);
Serial.println(e);
Serial.println("SubStr:");
Serial.println(httpresponse.substring(c+1,d));
username = httpresponse.substring(c+1,d);
Serial.print("User: ");
Serial.println(username);
Serial.println("SubStr:");
Serial.println(httpresponse.substring(d+1,e));
if(httpresponse.substring(d+1,e) == "OK") {
authorized = true;
}
Serial.print("Auth: ");
Serial.println(authorized);
Serial.println("End Response");
httpresponse = "";
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
client.stop();
}
2011-09-15 05:10:08 +00:00
}
2011-11-30 06:46:08 +00:00
} // End of loop()
2011-09-15 05:10:08 +00:00
2011-11-30 06:46:08 +00:00
2011-11-30 05:55:38 +00:00
void lcdprint(int x, int y, char text) {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(x, y);
// print the number of seconds since reset:
lcd.print(text);
}
2011-09-15 05:10:08 +00:00
/* Access System Functions - Modify these as needed for your application.
These function control lock/unlock and user lookup.
*/
2011-11-28 05:28:21 +00:00
void relayHigh(int input) { //Send an unlock signal to the door and flash the Door LED
2011-11-28 05:28:21 +00:00
relay1timer = millis();
2011-09-15 05:10:08 +00:00
byte dp=1;
if(input == 1) {
2011-11-28 05:28:21 +00:00
dp=RELAYPIN1; }
2011-11-30 07:02:22 +00:00
2011-09-15 05:10:08 +00:00
digitalWrite(dp, HIGH);
2011-11-28 05:28:21 +00:00
if (input == 1) {
relay1high = true;
}
Serial.print("Relay ");
2011-09-15 05:10:08 +00:00
Serial.print(input,DEC);
2011-11-28 05:28:21 +00:00
Serial.println(" high");
2011-09-15 05:10:08 +00:00
}
2011-11-28 05:28:21 +00:00
void relayLow(int input) { //Send an unlock signal to the door and flash the Door LED
2011-09-15 05:10:08 +00:00
byte dp=1;
if(input == 1) {
2011-11-28 05:28:21 +00:00
dp=RELAYPIN1; }
2011-09-15 05:10:08 +00:00
digitalWrite(dp, LOW);
2011-11-28 05:28:21 +00:00
if (input == 1) {
relay1high = false;
}
Serial.print("Relay ");
2011-09-15 05:10:08 +00:00
Serial.print(input,DEC);
2011-11-28 05:28:21 +00:00
Serial.println(" low");
2011-09-15 05:10:08 +00:00
}
/* Wrapper functions for interrupt attachment
Could be cleaned up in library?
*/
void callReader1Zero(){wiegand26.reader1Zero();}
void callReader1One(){wiegand26.reader1One();}
void callReader2Zero(){wiegand26.reader2Zero();}
void callReader2One(){wiegand26.reader2One();}
void callReader3Zero(){wiegand26.reader3Zero();}
void callReader3One(){wiegand26.reader3One();}