Advertisement
Guest User

Untitled

a guest
Mar 14th, 2012
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 16.56 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include <LiquidCrystal.h>
  4.  
  5. #define TONEPIN 6
  6. #define GREENPIN 8
  7. #define REDPIN 9
  8. #define RELAYPIN 22
  9. #define LCDWIDTH 20
  10.  
  11. LiquidCrystal lcd(44, 45, 49, 48, 47, 46);
  12.  
  13. byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x02, 0xED };
  14. IPAddress server( 192, 168, 16, 202 );
  15. EthernetClient client;
  16.  
  17. #define doorId 100
  18.  
  19. #define doorInterval 5000
  20.  
  21. unsigned long currentTime;
  22. unsigned long doorClose = 0;
  23.  
  24. unsigned long addCard = 0;
  25. #define addTime 6000
  26. unsigned long delCard = 0;
  27. #define delTime 6000
  28.  
  29. unsigned long lastClockFlash;
  30. boolean clockState = 1;
  31.  
  32. #define checkinInterval 1000
  33. unsigned long lastCheckIn = 0;
  34.  
  35. #define rateLimit 3000
  36. unsigned long lastSwipe = 0;
  37.  
  38. #define nameTimeout 5000
  39. unsigned long lastNamePrint = 0;
  40. boolean namePlusFourth = 0;
  41.  
  42. String lastCard;
  43.  
  44. boolean doorState = 0;
  45.  
  46. char newhour[3];
  47. char newmin[3];
  48. char curhour[3];
  49. char curmin[3];
  50.  
  51. void setup()
  52. {
  53.   newhour[2] = '\0';
  54.   newmin[2] = '\0';
  55.   curhour[2] = '\0';
  56.   curmin[2] = '\0';
  57.   lcd.begin(20, 4);
  58.   lcd.setCursor(1,0);
  59.   lcd.write("* Some Company *");
  60.   lcd.setCursor(5,2);
  61.   lcd.write("Loading..");
  62.   lcd.setCursor(3,3);
  63.   lcd.write("[#         ]");
  64.  
  65.   Serial.begin(9600);
  66.   Serial3.begin(9600);
  67.   lcd.setCursor(5,3);
  68.   lcd.write("#");
  69.  
  70.  
  71.  
  72.  
  73.   //Handle ethernet connection
  74.   Ethernet.begin(mac);
  75.   for(int i=4;i<=12;i++)
  76.   {
  77.     lcd.setCursor(i,3);
  78.     lcd.write("#");
  79.     delay(100);
  80.   }
  81.  
  82.   pinMode(GREENPIN, OUTPUT);
  83.   pinMode(REDPIN, OUTPUT);
  84.   pinMode(RELAYPIN, OUTPUT);
  85.   digitalWrite(RELAYPIN, LOW);
  86.   digitalWrite(GREENPIN, HIGH);
  87.   digitalWrite(REDPIN, HIGH);
  88.  
  89.   Serial.println("Ready...");
  90.  
  91.   lastCheckIn = millis();
  92.   lastSwipe = lastCheckIn - rateLimit;
  93.   lastNamePrint = lastCheckIn;
  94.   lcd.setCursor(13,3);
  95.   lcd.write("#");
  96.   delay(500);
  97.   lcd.clear();
  98. }
  99.  
  100. void loop()
  101. {
  102.   //Ethernet print debugging
  103.   if( client.available() ) {
  104.     char c = client.read();
  105.     Serial.print(c);
  106.   }
  107.   currentTime = millis();
  108.   checkDoorClose();
  109.   httpReceive();
  110.   rfidRead();
  111.   intervalCheckIn();
  112.   ledChecks();
  113.   clockFlash();
  114.   checkReady();
  115.   checkNameTimeout();
  116. }
  117.  
  118. void clearLine(int line)
  119. {
  120.   lcd.setCursor(0,line);
  121.   lcd.print("                    ");
  122.   lastNamePrint = 0;
  123. }
  124.  
  125. void clear2and3()
  126. {
  127.   clearLine(2);
  128.   clearLine(3);
  129. }
  130.  
  131. String centerText( String text, int width=LCDWIDTH )
  132. {
  133.    int strlen = text.length();
  134.    if( strlen < width )
  135.    {
  136.      int padding = (width-strlen) /2;
  137.      String space = String(" ");
  138.      for(int i=1;i<=padding;i++)
  139.      {
  140.        text = String(space + text + space);
  141.      }
  142.      if( text.length() < width )
  143.        text = String( text + space );
  144.    }
  145.    return text;
  146. }
  147.  
  148. void checkNameTimeout()
  149. {
  150.   currentTime = millis();
  151.   if( lastNamePrint > 0 && currentTime - lastNamePrint > nameTimeout )
  152.   {
  153.     lastNamePrint = 0;
  154.     clearLine(2);
  155.     if( namePlusFourth == 1 )
  156.     {
  157.        clearLine(3);
  158.        namePlusFourth = 0;
  159.     }
  160.   }
  161. }
  162.  
  163. void checkReady()
  164. {
  165.   currentTime=millis();
  166.   if( currentTime - lastSwipe > rateLimit )
  167.   {
  168.      lcd.setCursor(0,0);
  169.      lcd.print("Ready..");
  170.   }
  171. }
  172.  
  173. void clockFlash()
  174. {
  175.  long currentTime = millis();
  176.  if( currentTime - lastClockFlash > 1000 )
  177.  {
  178.    lastClockFlash = currentTime;
  179.    lcd.setCursor(17,0);
  180.    if( clockState == 1 )
  181.    {
  182.      lcd.write(":");
  183.      clockState = 0;
  184.    }
  185.    else
  186.    {
  187.      lcd.write(" ");
  188.      clockState = 1;
  189.    }
  190.  
  191.  }
  192.  
  193. }
  194.  
  195. void ledChecks() //check LED states for required state changes.
  196. {
  197.   currentTime = millis();
  198.   if( addCard > 0 && currentTime - addCard > addTime ) // reset LEDs if addCard has timed out
  199.   {
  200.      addCard = 0;
  201.      clear2and3();
  202.      lcd.setCursor(0,2);
  203.      lcd.print(centerText("Cancelled"));
  204.      lastNamePrint = currentTime;
  205.      digitalWrite(GREENPIN, HIGH);
  206.      digitalWrite(REDPIN, HIGH);
  207.      tone(TONEPIN, 2000);
  208.      delay(200);
  209.      tone(TONEPIN, 1800, 200);  
  210.   }
  211.   if( delCard > 0 && currentTime - delCard > delTime ) // reset LEDs if delCard has timed out
  212.   {
  213.      clear2and3();
  214.      lcd.setCursor(0,2);
  215.      lcd.print(centerText("Cancelled"));
  216.      lastNamePrint = currentTime;
  217.      delCard = 0;
  218.      digitalWrite(GREENPIN, HIGH);
  219.      digitalWrite(REDPIN, HIGH);
  220.      tone(TONEPIN, 2000);
  221.      delay(200);
  222.      tone(TONEPIN, 1800, 200);  
  223.   }
  224. }
  225.  
  226. void rfidRead()
  227. {
  228.  
  229.   byte i = 0;
  230.   byte val = 0;
  231.   byte code[6];
  232.   byte checksum = 0;
  233.   byte bytesread = 0;
  234.   byte tempbyte = 0;
  235.   String fullcode;
  236.  
  237.   if(Serial3.available() > 0) {
  238.     if((val = Serial3.read()) == 2) {                  // check for header
  239.       bytesread = 0;
  240.       while (bytesread < 12) {                        // read 10 digit code + 2 digit checksum
  241.         if( Serial3.available() > 0) {
  242.           val = Serial3.read();
  243.           if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) { // if header or stop bytes before the 10 digit reading
  244.             break;                                    // stop reading
  245.           }
  246.  
  247.           // Do Ascii/Hex conversion:
  248.           if ((val >= '0') && (val <= '9')) {
  249.             val = val - '0';
  250.           } else if ((val >= 'A') && (val <= 'F')) {
  251.             val = 10 + val - 'A';
  252.           }
  253.  
  254.           // Every two hex-digits, add byte to code:
  255.           if (bytesread & 1 == 1) {
  256.             // make some space for this hex-digit by
  257.             // shifting the previous hex-digit with 4 bits to the left:
  258.             code[bytesread >> 1] = (val | (tempbyte << 4));
  259.  
  260.             /*if (bytesread >> 1 != 5) {                // If we're at the checksum byte,
  261.               checksum ^= code[bytesread >> 1];       // Calculate the checksum... (XOR)
  262.             };*/
  263.           } else {
  264.             tempbyte = val;                           // Store the first hex digit first...
  265.           };
  266.  
  267.           bytesread++;                                // ready to read next digit
  268.         }
  269.       }
  270.  
  271.       // Output to Serial:
  272.  
  273.       if (bytesread == 12) {                          // if 12 digit read is complete
  274.         String tZero = String(0);
  275.         for (i=0; i<5; i++) {
  276.           if (code[i] < 16)
  277.           {
  278.             fullcode = String(fullcode + tZero);
  279.           }
  280.           String tCode = String(code[i], HEX);
  281.           fullcode = String(fullcode + tCode);
  282.         }
  283.        
  284.         currentTime = millis();
  285.         if( /*code[5] == checksum && */ currentTime - lastSwipe > rateLimit && doorState == 0)
  286.         {
  287.             lastSwipe = currentTime;
  288.             lastCard = fullcode;
  289.             authenticateDetails( 446, fullcode );
  290.         }
  291.         else
  292.         {
  293.           lcd.setCursor(0,0);
  294.           lcd.print("Wait..");
  295.         }
  296.       }
  297.  
  298.       bytesread = 0;
  299.       val = 0;
  300.     }
  301.   }
  302.  
  303. }
  304.  
  305. void pulseDoor()
  306. {
  307.   if( doorState == 0 || doorClose > 0 )
  308.   {
  309.     openDoor();
  310.     doorClose = millis();
  311.   }
  312. }
  313.  
  314. void openDoor()
  315. {
  316.   doorState = 1;
  317.   digitalWrite(RELAYPIN, HIGH);
  318.   doorStateUpdate(1);
  319.   lcd.setCursor(0,0);
  320.   lcd.print("Door open");
  321. }
  322.  
  323. void closeDoor()
  324. {
  325.   doorState = 0;
  326.   doorClose = 0;
  327.   digitalWrite(RELAYPIN, LOW);
  328.   doorStateUpdate(0);
  329.   lcd.setCursor(0,0);
  330.   lcd.print("Ready..   ");
  331. }
  332.  
  333. void checkDoorClose()
  334. {
  335.   currentTime = millis();
  336.   if( doorClose > 0 && currentTime - doorClose > doorInterval )
  337.   {
  338.     doorClose = 0;
  339.     closeDoor();
  340.   }
  341. }
  342.  
  343. void doorStateUpdate( int state ) // 0 = closed, 1=open
  344. {
  345.  if( client.connect( server, 80 ) )
  346.  {
  347.    Serial.println("Updating door status");
  348.    client.println("POST / HTTP/1.1");
  349.    client.println("Host: entry.somecompany.net");
  350.    client.println("User-Agent: SomeCompany EntrySystem");
  351.    client.println("Content-Type: application/x-www-form-urlencoded");
  352.    client.print("doorid: ");
  353.    client.println( doorId );
  354.    client.println("command: stateupdate");
  355.    client.print("state: ");
  356.    client.println(state);
  357.    client.println();
  358.  }
  359.  else
  360.  {
  361.    Serial.println("Update connection failed");
  362.    client.stop();
  363.    client.flush();
  364.    doorStateUpdate(state);
  365.  }
  366.  
  367. }
  368.  
  369.  
  370. void intervalCheckIn() // checks in to the server at a short (defined) interval. Server will respond with commands if there is anything to do.
  371. {
  372.   currentTime = millis();
  373.   if( lastCheckIn > 0 && currentTime - lastCheckIn > checkinInterval )
  374.   {
  375.      if( client.connect( server, 80 ) )
  376.      {
  377.        client.println("POST / HTTP/1.1");
  378.        client.println("Host: entry.somecompany.net");
  379.        client.println("User-Agent: SomeCompany EntrySystem");
  380.        client.println("Content-Type: application/x-www-form-urlencoded");
  381.        client.print("doorid: ");
  382.        client.println(doorId);
  383.        client.println("command: checkin");
  384.        client.print("Millis: ");
  385.        client.println(currentTime);
  386.        client.println();
  387.        
  388.      }
  389.      else
  390.      {
  391.        client.stop();
  392.        client.flush();
  393.      }
  394.      lastCheckIn = millis();
  395.   }
  396.  
  397. }
  398.  
  399. void authenticateDetails( int siteCode, String serialNumber )
  400. {
  401.   if( client.connect( server, 80 ) ) {
  402.      Serial.print( serialNumber );
  403.      client.println("POST / HTTP/1.1");
  404.      client.println("Host: entry.somecompany.net");
  405.      client.println("User-Agent: SomeCompany EntrySystem");
  406.      client.println("Content-Type: application/x-www-form-urlencoded");
  407.      //client.print("doorid: ");
  408.      //client.println(doorId);
  409.      currentTime = millis();
  410.      if( addCard > 0 && currentTime - addCard < addTime  ) //check if we're in an open window for adding a card
  411.      {
  412.        client.println("command: addcard");
  413.      }
  414.      else if( delCard > 0 && currentTime - delCard < delTime ) //check if we're in an open window for deleting a card
  415.      {
  416.        client.println("command: delcard");
  417.      }
  418.        client.print("sitecode: ");
  419.        client.println( siteCode );
  420.        client.print("cardnumber: ");
  421.        client.println( serialNumber );
  422.      client.println();
  423.    } else {
  424.      Serial.println("Connection failed");
  425.      client.stop();
  426.      authenticateDetails( siteCode, serialNumber );
  427.    }
  428. }
  429.  
  430. void setTime()
  431. {
  432.  
  433.   if( strcmp( newhour, curhour) != 0 )
  434.   {
  435.      curhour[0] = newhour[0];
  436.      curhour[1] = newhour[1];
  437.      lcd.setCursor( 15, 0 );
  438.      lcd.write(newhour);
  439.      Serial.println(newhour);
  440.   }
  441.  
  442.   if( strcmp( newmin, curmin ) != 0 )
  443.   {
  444.     curmin[0] = newmin[0];
  445.     curmin[1] = newmin[1];
  446.     lcd.setCursor(18, 0);
  447.     lcd.write(newmin);
  448.   }
  449.  
  450. }
  451.  
  452.  
  453.  
  454. void httpReceive()
  455. {
  456.   char auth[2];
  457.   auth[2] = '\0';
  458.   char cmd[4];
  459.   cmd[4] = '\0';
  460.   while( client.connected() )
  461.   {
  462.     if( client.available() )
  463.     {
  464.       //find the time
  465.       client.find("Hour: ");
  466.       client.readBytes( newhour, 2);
  467.       client.find("Min: ");
  468.       client.readBytes( newmin, 2);
  469.       setTime();
  470.       client.find("Auth: ");
  471.       client.readBytes( auth, 2 );
  472.       if( strcmp(auth, "CM") == 0 ) // process other commands
  473.       {
  474.         client.find("cmd: ");
  475.         client.readBytes( cmd, 4 );
  476.         if( strcmp(cmd, "puls") == 0 ) // pulse door
  477.         {
  478.           pulseDoor();
  479.         }
  480.         else if( strcmp(cmd, "open") == 0 ) // open door
  481.         {
  482.          
  483.           openDoor();
  484.         }
  485.         else if( strcmp(cmd, "clos") == 0 ) // close door
  486.         {
  487.           closeDoor();
  488.         }
  489.         client.stop();
  490.         client.flush();
  491.       }
  492.       else if( strcmp(auth,"OK") == 0 )
  493.       {
  494.         //define the length variable, the length sent is always 3 bytes.
  495.         char nameLen[4];
  496.         nameLen[3] = '\0';
  497.         //get the NameLen header
  498.         client.find("NameLen: ");
  499.         client.readBytes(nameLen, 3);
  500.         //Convert the length to int
  501.         int iNameLen;
  502.         iNameLen = atoi(nameLen);
  503.         //Define name to the specified length
  504.         char name[iNameLen+1];
  505.         name[iNameLen] = '\0';
  506.         //Find the name and read the length specified above
  507.         client.find("Name: ");
  508.         client.readBytes(name, iNameLen);        
  509.        
  510.         clear2and3();
  511.         lastNamePrint = millis();
  512.         lcd.setCursor(0, 2);
  513.         lcd.print(centerText(name));
  514.        
  515.         tone(TONEPIN, 4000, 100);
  516.         Serial.println(" - ACCEPTED");
  517.         client.stop();
  518.         client.flush();
  519.         digitalWrite(REDPIN, LOW);
  520.         pulseDoor();
  521.         /*for(int i=0;i<30;i++)
  522.         {
  523.           if( (i % 2) == 0 )
  524.           {
  525.             digitalWrite(GREENPIN, HIGH);
  526.  
  527.           }
  528.           else
  529.           {
  530.             digitalWrite(GREENPIN, LOW);
  531.           }
  532.           delay(30);
  533.         }*/
  534.         digitalWrite(GREENPIN, HIGH);
  535.         digitalWrite(REDPIN, HIGH);
  536.       }
  537.       else if( strcmp(auth, "AD") == 0 ) // card authed as add card
  538.       {      
  539.         client.stop();
  540.         client.flush();
  541.         clear2and3();
  542.         lcd.setCursor(0,2);
  543.         lcd.print(centerText("Swipe new card"));
  544.        
  545.         tone(TONEPIN, 3400, 200);
  546.         addCard = millis();
  547.         Serial.println(" - KEY CARD: ADD");
  548.         digitalWrite(REDPIN, LOW );
  549.         digitalWrite(GREENPIN, HIGH);
  550.         lastSwipe = 1;
  551.       }
  552.       else if( strcmp(auth, "DL") == 0 ) // card authed as delete card
  553.       {
  554.         client.stop();
  555.         client.flush();
  556.         clear2and3();
  557.         lcd.setCursor(0,2);
  558.         lcd.print(centerText("Swipe card to delete"));
  559.         tone(TONEPIN, 1600);
  560.         delay(100);
  561.         tone(TONEPIN, 1800, 200);
  562.         delCard = millis();
  563.         Serial.println(" - KEY CARD: DEL");
  564.         digitalWrite(GREENPIN, LOW );
  565.         digitalWrite(REDPIN, HIGH);
  566.         lastSwipe = 1;
  567.       }
  568.       else if( strcmp(auth, "AO") == 0 ) // check if a card has been added
  569.       {
  570.         lastNamePrint = 0;
  571.         clear2and3();
  572.         lcd.setCursor(0,2);
  573.         lcd.print(centerText("Card added"));
  574.         lcd.setCursor(0,3);
  575.         lcd.print(centerText(lastCard));
  576.         namePlusFourth = 1;
  577.         lastNamePrint = millis();
  578.        
  579.         addCard = 0;
  580.         client.stop();
  581.         client.flush();
  582.         tone(TONEPIN, 3800, 200);
  583.         digitalWrite(REDPIN,HIGH);
  584.       }
  585.       else if( strcmp(auth, "AE") == 0 ) //check if a card add was attempted, but errored on the server side
  586.       {
  587.         clear2and3();
  588.         lastNamePrint = 0;
  589.         lcd.setCursor(0,2);
  590.         lcd.print(centerText("Error"));
  591.         lcd.setCursor(0,3);
  592.         lcd.print(centerText(lastCard));
  593.         namePlusFourth = 1;
  594.         lastNamePrint = millis();
  595.        
  596.         addCard = 0;
  597.         client.stop();
  598.         client.flush();
  599.         digitalWrite(GREENPIN, LOW);
  600.         digitalWrite(REDPIN, HIGH);
  601.         tone(TONEPIN, 1600, 300);
  602.         digitalWrite(GREENPIN, HIGH);
  603.       }
  604.       else if( strcmp(auth, "DO") == 0 ) // check if a card has been deleted
  605.       {
  606.         clear2and3();
  607.         lastNamePrint = 0;
  608.         lcd.setCursor(0,2);
  609.         lcd.print(centerText("Card deleted"));
  610.         lcd.setCursor(0,3);
  611.         lcd.print(centerText(lastCard));
  612.         namePlusFourth = 1;
  613.         lastNamePrint = millis();
  614.        
  615.         delCard = 0;
  616.         client.stop();
  617.         client.flush();
  618.         tone(TONEPIN, 2200, 200);
  619.         digitalWrite(GREENPIN,HIGH);
  620.       }
  621.       else if( strcmp(auth, "DE") == 0 ) //check if a card del was attempted, but errored on the server side
  622.       {
  623.         lastNamePrint = 0;
  624.         clear2and3();
  625.         lcd.setCursor(0,2);
  626.         lcd.print(centerText("    Error    "));
  627.         lcd.setCursor(0,3);
  628.         lcd.print(centerText(lastCard));
  629.         namePlusFourth = 1;
  630.         lastNamePrint = millis();
  631.        
  632.         delCard = 0;
  633.         client.stop();
  634.         client.flush();
  635.         digitalWrite(GREENPIN, LOW);
  636.         digitalWrite(REDPIN, HIGH);
  637.  
  638.         tone(TONEPIN, 1600, 300);
  639.         digitalWrite(GREENPIN, HIGH);
  640.       }
  641.       else if( strcmp(auth, "IG") == 0 ) // The server wants us to ignore this response
  642.       {
  643.          //do nothing except reset the connection;
  644.          client.stop();
  645.          client.flush();
  646.       }
  647.       else
  648.       {
  649.         clear2and3();
  650.         lcd.setCursor(0,2);
  651.         lcd.print(centerText("Access Denied"));
  652.         lastNamePrint = millis();
  653.         tone(TONEPIN, 400, 400);
  654.         Serial.println(" - DENIED");
  655.         client.stop();
  656.         client.flush();
  657.         digitalWrite(GREENPIN, LOW);
  658.         /*for(int i=0;i<30;i++)
  659.         {
  660.           if( (i % 2) == 0 )
  661.           {
  662.             digitalWrite(REDPIN, HIGH);
  663.           }
  664.           else
  665.           {
  666.             digitalWrite(REDPIN, LOW);
  667.           }
  668.           delay(30);
  669.         }*/
  670.         digitalWrite(GREENPIN, HIGH);
  671.         digitalWrite(REDPIN, HIGH);
  672.       }
  673.     }
  674.   }
  675. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement