Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <Ethernet.h>
- #include <LiquidCrystal.h>
- #define TONEPIN 6
- #define GREENPIN 8
- #define REDPIN 9
- #define RELAYPIN 22
- #define LCDWIDTH 20
- LiquidCrystal lcd(44, 45, 49, 48, 47, 46);
- byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x02, 0xED };
- IPAddress server( 192, 168, 16, 202 );
- EthernetClient client;
- #define doorId 100
- #define doorInterval 5000
- unsigned long currentTime;
- unsigned long doorClose = 0;
- unsigned long addCard = 0;
- #define addTime 6000
- unsigned long delCard = 0;
- #define delTime 6000
- unsigned long lastClockFlash;
- boolean clockState = 1;
- #define checkinInterval 1000
- unsigned long lastCheckIn = 0;
- #define rateLimit 3000
- unsigned long lastSwipe = 0;
- #define nameTimeout 5000
- unsigned long lastNamePrint = 0;
- boolean namePlusFourth = 0;
- String lastCard;
- boolean doorState = 0;
- char newhour[3];
- char newmin[3];
- char curhour[3];
- char curmin[3];
- void setup()
- {
- newhour[2] = '\0';
- newmin[2] = '\0';
- curhour[2] = '\0';
- curmin[2] = '\0';
- lcd.begin(20, 4);
- lcd.setCursor(1,0);
- lcd.write("* Some Company *");
- lcd.setCursor(5,2);
- lcd.write("Loading..");
- lcd.setCursor(3,3);
- lcd.write("[# ]");
- Serial.begin(9600);
- Serial3.begin(9600);
- lcd.setCursor(5,3);
- lcd.write("#");
- //Handle ethernet connection
- Ethernet.begin(mac);
- for(int i=4;i<=12;i++)
- {
- lcd.setCursor(i,3);
- lcd.write("#");
- delay(100);
- }
- pinMode(GREENPIN, OUTPUT);
- pinMode(REDPIN, OUTPUT);
- pinMode(RELAYPIN, OUTPUT);
- digitalWrite(RELAYPIN, LOW);
- digitalWrite(GREENPIN, HIGH);
- digitalWrite(REDPIN, HIGH);
- Serial.println("Ready...");
- lastCheckIn = millis();
- lastSwipe = lastCheckIn - rateLimit;
- lastNamePrint = lastCheckIn;
- lcd.setCursor(13,3);
- lcd.write("#");
- delay(500);
- lcd.clear();
- }
- void loop()
- {
- //Ethernet print debugging
- if( client.available() ) {
- char c = client.read();
- Serial.print(c);
- }
- currentTime = millis();
- checkDoorClose();
- httpReceive();
- rfidRead();
- intervalCheckIn();
- ledChecks();
- clockFlash();
- checkReady();
- checkNameTimeout();
- }
- void clearLine(int line)
- {
- lcd.setCursor(0,line);
- lcd.print(" ");
- lastNamePrint = 0;
- }
- void clear2and3()
- {
- clearLine(2);
- clearLine(3);
- }
- String centerText( String text, int width=LCDWIDTH )
- {
- int strlen = text.length();
- if( strlen < width )
- {
- int padding = (width-strlen) /2;
- String space = String(" ");
- for(int i=1;i<=padding;i++)
- {
- text = String(space + text + space);
- }
- if( text.length() < width )
- text = String( text + space );
- }
- return text;
- }
- void checkNameTimeout()
- {
- currentTime = millis();
- if( lastNamePrint > 0 && currentTime - lastNamePrint > nameTimeout )
- {
- lastNamePrint = 0;
- clearLine(2);
- if( namePlusFourth == 1 )
- {
- clearLine(3);
- namePlusFourth = 0;
- }
- }
- }
- void checkReady()
- {
- currentTime=millis();
- if( currentTime - lastSwipe > rateLimit )
- {
- lcd.setCursor(0,0);
- lcd.print("Ready..");
- }
- }
- void clockFlash()
- {
- long currentTime = millis();
- if( currentTime - lastClockFlash > 1000 )
- {
- lastClockFlash = currentTime;
- lcd.setCursor(17,0);
- if( clockState == 1 )
- {
- lcd.write(":");
- clockState = 0;
- }
- else
- {
- lcd.write(" ");
- clockState = 1;
- }
- }
- }
- void ledChecks() //check LED states for required state changes.
- {
- currentTime = millis();
- if( addCard > 0 && currentTime - addCard > addTime ) // reset LEDs if addCard has timed out
- {
- addCard = 0;
- clear2and3();
- lcd.setCursor(0,2);
- lcd.print(centerText("Cancelled"));
- lastNamePrint = currentTime;
- digitalWrite(GREENPIN, HIGH);
- digitalWrite(REDPIN, HIGH);
- tone(TONEPIN, 2000);
- delay(200);
- tone(TONEPIN, 1800, 200);
- }
- if( delCard > 0 && currentTime - delCard > delTime ) // reset LEDs if delCard has timed out
- {
- clear2and3();
- lcd.setCursor(0,2);
- lcd.print(centerText("Cancelled"));
- lastNamePrint = currentTime;
- delCard = 0;
- digitalWrite(GREENPIN, HIGH);
- digitalWrite(REDPIN, HIGH);
- tone(TONEPIN, 2000);
- delay(200);
- tone(TONEPIN, 1800, 200);
- }
- }
- void rfidRead()
- {
- byte i = 0;
- byte val = 0;
- byte code[6];
- byte checksum = 0;
- byte bytesread = 0;
- byte tempbyte = 0;
- String fullcode;
- if(Serial3.available() > 0) {
- if((val = Serial3.read()) == 2) { // check for header
- bytesread = 0;
- while (bytesread < 12) { // read 10 digit code + 2 digit checksum
- if( Serial3.available() > 0) {
- val = Serial3.read();
- if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) { // if header or stop bytes before the 10 digit reading
- break; // stop reading
- }
- // Do Ascii/Hex conversion:
- if ((val >= '0') && (val <= '9')) {
- val = val - '0';
- } else if ((val >= 'A') && (val <= 'F')) {
- val = 10 + val - 'A';
- }
- // Every two hex-digits, add byte to code:
- if (bytesread & 1 == 1) {
- // make some space for this hex-digit by
- // shifting the previous hex-digit with 4 bits to the left:
- code[bytesread >> 1] = (val | (tempbyte << 4));
- /*if (bytesread >> 1 != 5) { // If we're at the checksum byte,
- checksum ^= code[bytesread >> 1]; // Calculate the checksum... (XOR)
- };*/
- } else {
- tempbyte = val; // Store the first hex digit first...
- };
- bytesread++; // ready to read next digit
- }
- }
- // Output to Serial:
- if (bytesread == 12) { // if 12 digit read is complete
- String tZero = String(0);
- for (i=0; i<5; i++) {
- if (code[i] < 16)
- {
- fullcode = String(fullcode + tZero);
- }
- String tCode = String(code[i], HEX);
- fullcode = String(fullcode + tCode);
- }
- currentTime = millis();
- if( /*code[5] == checksum && */ currentTime - lastSwipe > rateLimit && doorState == 0)
- {
- lastSwipe = currentTime;
- lastCard = fullcode;
- authenticateDetails( 446, fullcode );
- }
- else
- {
- lcd.setCursor(0,0);
- lcd.print("Wait..");
- }
- }
- bytesread = 0;
- val = 0;
- }
- }
- }
- void pulseDoor()
- {
- if( doorState == 0 || doorClose > 0 )
- {
- openDoor();
- doorClose = millis();
- }
- }
- void openDoor()
- {
- doorState = 1;
- digitalWrite(RELAYPIN, HIGH);
- doorStateUpdate(1);
- lcd.setCursor(0,0);
- lcd.print("Door open");
- }
- void closeDoor()
- {
- doorState = 0;
- doorClose = 0;
- digitalWrite(RELAYPIN, LOW);
- doorStateUpdate(0);
- lcd.setCursor(0,0);
- lcd.print("Ready.. ");
- }
- void checkDoorClose()
- {
- currentTime = millis();
- if( doorClose > 0 && currentTime - doorClose > doorInterval )
- {
- doorClose = 0;
- closeDoor();
- }
- }
- void doorStateUpdate( int state ) // 0 = closed, 1=open
- {
- if( client.connect( server, 80 ) )
- {
- Serial.println("Updating door status");
- client.println("POST / HTTP/1.1");
- client.println("Host: entry.somecompany.net");
- client.println("User-Agent: SomeCompany EntrySystem");
- client.println("Content-Type: application/x-www-form-urlencoded");
- client.print("doorid: ");
- client.println( doorId );
- client.println("command: stateupdate");
- client.print("state: ");
- client.println(state);
- client.println();
- }
- else
- {
- Serial.println("Update connection failed");
- client.stop();
- client.flush();
- doorStateUpdate(state);
- }
- }
- void intervalCheckIn() // checks in to the server at a short (defined) interval. Server will respond with commands if there is anything to do.
- {
- currentTime = millis();
- if( lastCheckIn > 0 && currentTime - lastCheckIn > checkinInterval )
- {
- if( client.connect( server, 80 ) )
- {
- client.println("POST / HTTP/1.1");
- client.println("Host: entry.somecompany.net");
- client.println("User-Agent: SomeCompany EntrySystem");
- client.println("Content-Type: application/x-www-form-urlencoded");
- client.print("doorid: ");
- client.println(doorId);
- client.println("command: checkin");
- client.print("Millis: ");
- client.println(currentTime);
- client.println();
- }
- else
- {
- client.stop();
- client.flush();
- }
- lastCheckIn = millis();
- }
- }
- void authenticateDetails( int siteCode, String serialNumber )
- {
- if( client.connect( server, 80 ) ) {
- Serial.print( serialNumber );
- client.println("POST / HTTP/1.1");
- client.println("Host: entry.somecompany.net");
- client.println("User-Agent: SomeCompany EntrySystem");
- client.println("Content-Type: application/x-www-form-urlencoded");
- //client.print("doorid: ");
- //client.println(doorId);
- currentTime = millis();
- if( addCard > 0 && currentTime - addCard < addTime ) //check if we're in an open window for adding a card
- {
- client.println("command: addcard");
- }
- else if( delCard > 0 && currentTime - delCard < delTime ) //check if we're in an open window for deleting a card
- {
- client.println("command: delcard");
- }
- client.print("sitecode: ");
- client.println( siteCode );
- client.print("cardnumber: ");
- client.println( serialNumber );
- client.println();
- } else {
- Serial.println("Connection failed");
- client.stop();
- authenticateDetails( siteCode, serialNumber );
- }
- }
- void setTime()
- {
- if( strcmp( newhour, curhour) != 0 )
- {
- curhour[0] = newhour[0];
- curhour[1] = newhour[1];
- lcd.setCursor( 15, 0 );
- lcd.write(newhour);
- Serial.println(newhour);
- }
- if( strcmp( newmin, curmin ) != 0 )
- {
- curmin[0] = newmin[0];
- curmin[1] = newmin[1];
- lcd.setCursor(18, 0);
- lcd.write(newmin);
- }
- }
- void httpReceive()
- {
- char auth[2];
- auth[2] = '\0';
- char cmd[4];
- cmd[4] = '\0';
- while( client.connected() )
- {
- if( client.available() )
- {
- //find the time
- client.find("Hour: ");
- client.readBytes( newhour, 2);
- client.find("Min: ");
- client.readBytes( newmin, 2);
- setTime();
- client.find("Auth: ");
- client.readBytes( auth, 2 );
- if( strcmp(auth, "CM") == 0 ) // process other commands
- {
- client.find("cmd: ");
- client.readBytes( cmd, 4 );
- if( strcmp(cmd, "puls") == 0 ) // pulse door
- {
- pulseDoor();
- }
- else if( strcmp(cmd, "open") == 0 ) // open door
- {
- openDoor();
- }
- else if( strcmp(cmd, "clos") == 0 ) // close door
- {
- closeDoor();
- }
- client.stop();
- client.flush();
- }
- else if( strcmp(auth,"OK") == 0 )
- {
- //define the length variable, the length sent is always 3 bytes.
- char nameLen[4];
- nameLen[3] = '\0';
- //get the NameLen header
- client.find("NameLen: ");
- client.readBytes(nameLen, 3);
- //Convert the length to int
- int iNameLen;
- iNameLen = atoi(nameLen);
- //Define name to the specified length
- char name[iNameLen+1];
- name[iNameLen] = '\0';
- //Find the name and read the length specified above
- client.find("Name: ");
- client.readBytes(name, iNameLen);
- clear2and3();
- lastNamePrint = millis();
- lcd.setCursor(0, 2);
- lcd.print(centerText(name));
- tone(TONEPIN, 4000, 100);
- Serial.println(" - ACCEPTED");
- client.stop();
- client.flush();
- digitalWrite(REDPIN, LOW);
- pulseDoor();
- /*for(int i=0;i<30;i++)
- {
- if( (i % 2) == 0 )
- {
- digitalWrite(GREENPIN, HIGH);
- }
- else
- {
- digitalWrite(GREENPIN, LOW);
- }
- delay(30);
- }*/
- digitalWrite(GREENPIN, HIGH);
- digitalWrite(REDPIN, HIGH);
- }
- else if( strcmp(auth, "AD") == 0 ) // card authed as add card
- {
- client.stop();
- client.flush();
- clear2and3();
- lcd.setCursor(0,2);
- lcd.print(centerText("Swipe new card"));
- tone(TONEPIN, 3400, 200);
- addCard = millis();
- Serial.println(" - KEY CARD: ADD");
- digitalWrite(REDPIN, LOW );
- digitalWrite(GREENPIN, HIGH);
- lastSwipe = 1;
- }
- else if( strcmp(auth, "DL") == 0 ) // card authed as delete card
- {
- client.stop();
- client.flush();
- clear2and3();
- lcd.setCursor(0,2);
- lcd.print(centerText("Swipe card to delete"));
- tone(TONEPIN, 1600);
- delay(100);
- tone(TONEPIN, 1800, 200);
- delCard = millis();
- Serial.println(" - KEY CARD: DEL");
- digitalWrite(GREENPIN, LOW );
- digitalWrite(REDPIN, HIGH);
- lastSwipe = 1;
- }
- else if( strcmp(auth, "AO") == 0 ) // check if a card has been added
- {
- lastNamePrint = 0;
- clear2and3();
- lcd.setCursor(0,2);
- lcd.print(centerText("Card added"));
- lcd.setCursor(0,3);
- lcd.print(centerText(lastCard));
- namePlusFourth = 1;
- lastNamePrint = millis();
- addCard = 0;
- client.stop();
- client.flush();
- tone(TONEPIN, 3800, 200);
- digitalWrite(REDPIN,HIGH);
- }
- else if( strcmp(auth, "AE") == 0 ) //check if a card add was attempted, but errored on the server side
- {
- clear2and3();
- lastNamePrint = 0;
- lcd.setCursor(0,2);
- lcd.print(centerText("Error"));
- lcd.setCursor(0,3);
- lcd.print(centerText(lastCard));
- namePlusFourth = 1;
- lastNamePrint = millis();
- addCard = 0;
- client.stop();
- client.flush();
- digitalWrite(GREENPIN, LOW);
- digitalWrite(REDPIN, HIGH);
- tone(TONEPIN, 1600, 300);
- digitalWrite(GREENPIN, HIGH);
- }
- else if( strcmp(auth, "DO") == 0 ) // check if a card has been deleted
- {
- clear2and3();
- lastNamePrint = 0;
- lcd.setCursor(0,2);
- lcd.print(centerText("Card deleted"));
- lcd.setCursor(0,3);
- lcd.print(centerText(lastCard));
- namePlusFourth = 1;
- lastNamePrint = millis();
- delCard = 0;
- client.stop();
- client.flush();
- tone(TONEPIN, 2200, 200);
- digitalWrite(GREENPIN,HIGH);
- }
- else if( strcmp(auth, "DE") == 0 ) //check if a card del was attempted, but errored on the server side
- {
- lastNamePrint = 0;
- clear2and3();
- lcd.setCursor(0,2);
- lcd.print(centerText(" Error "));
- lcd.setCursor(0,3);
- lcd.print(centerText(lastCard));
- namePlusFourth = 1;
- lastNamePrint = millis();
- delCard = 0;
- client.stop();
- client.flush();
- digitalWrite(GREENPIN, LOW);
- digitalWrite(REDPIN, HIGH);
- tone(TONEPIN, 1600, 300);
- digitalWrite(GREENPIN, HIGH);
- }
- else if( strcmp(auth, "IG") == 0 ) // The server wants us to ignore this response
- {
- //do nothing except reset the connection;
- client.stop();
- client.flush();
- }
- else
- {
- clear2and3();
- lcd.setCursor(0,2);
- lcd.print(centerText("Access Denied"));
- lastNamePrint = millis();
- tone(TONEPIN, 400, 400);
- Serial.println(" - DENIED");
- client.stop();
- client.flush();
- digitalWrite(GREENPIN, LOW);
- /*for(int i=0;i<30;i++)
- {
- if( (i % 2) == 0 )
- {
- digitalWrite(REDPIN, HIGH);
- }
- else
- {
- digitalWrite(REDPIN, LOW);
- }
- delay(30);
- }*/
- digitalWrite(GREENPIN, HIGH);
- digitalWrite(REDPIN, HIGH);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement