Advertisement
Guest User

Untitled

a guest
May 1st, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.10 KB | None | 0 0
  1. #include <Servo.h>  // servo library
  2. #include <Wire.h>
  3. #include <PN532_I2C.h>
  4. #include <PN532.h>
  5. #include <NfcAdapter.h>
  6. #include <Ethernet.h>
  7. #include <SPI.h>
  8. #define BUFSIZ 100
  9.  
  10. PN532_I2C pn532_i2c(Wire);
  11. NfcAdapter nfc = NfcAdapter(pn532_i2c);
  12. int position;
  13. String id;
  14. String expDate;
  15. Servo servo1;  // servo control object
  16. int i=0;
  17. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  18. IPAddress server(192,168,1,4);  // numeric IP for Google (no DNS)
  19. IPAddress ip(192, 168, 1, 7);
  20. EthernetClient client;
  21.  
  22. void setup() {
  23.   servo1.attach(10);
  24.   Serial.begin(9600);
  25.   Serial.println("Smart Parking");
  26.   nfc.begin();
  27. }
  28.  
  29. void loop(void) {
  30.   Serial.println("\nScan a NFC tag\n");
  31.   static int places_left=5;
  32.   if (nfc.tagPresent())
  33.   {
  34.     read_tag();
  35.     if(parked_list(id) == 0)
  36.     {
  37.     //entering student
  38.       if(check_id(id)==0 && check_exp(expDate)==1 && places_left>0)
  39.       {
  40.        Serial.print("yaaaay");
  41.        Run_Motor();
  42.        places_left=places_left-1;
  43.        Serial.print(places_left);
  44.       }
  45.     }
  46.     else
  47.     {
  48.     //exiting student
  49.     }
  50.   }
  51.   delay(3000);
  52. }
  53.  
  54.  
  55. String read_tag(void)
  56. {
  57.  
  58.     int i=0;
  59.     NfcTag tag = nfc.read();
  60.  
  61.     if (tag.hasNdefMessage()) // every tag won't have a message
  62.     {
  63.  
  64.       NdefMessage message = tag.getNdefMessage();
  65.       int recordCount = message.getRecordCount();
  66.       for (i = 0; i < recordCount; i++)
  67.       {
  68.         NdefRecord record = message.getRecord(i);
  69.         int payloadLength = record.getPayloadLength();
  70.         byte payload[payloadLength];
  71.         record.getPayload(payload);
  72.         String payloadAsString = "";
  73.         for (int c = 3; c < payloadLength; c++) {
  74.           payloadAsString += (char)payload[c];
  75.         }
  76.         Serial.println(payloadAsString);
  77.         if(i==0){
  78.         id =payloadAsString;
  79.         id.trim();
  80.         }
  81.         if(i==1){
  82.         expDate =payloadAsString;
  83.         expDate.trim();
  84.         }      
  85.       }
  86.     }
  87.     return (id,expDate);
  88. }
  89.  
  90. int parked_list(String id)
  91. {
  92.    
  93.     char clientline[BUFSIZ];
  94.     int index = 0;
  95.     boolean flag = false;
  96.      if (Ethernet.begin(mac) == 0)
  97.     {
  98.       Ethernet.begin(mac, ip);
  99.     }
  100.     Serial.println("connecting...");
  101.     client.connect(server, 80);
  102.     client.println("GET /parking.php?id_tag=");
  103.     client.println(id);
  104.     client.println("HTTP/1.1");
  105.     client.println("Host: 192.168.1.4");
  106.     client.println();
  107.    
  108.     while(client.connected())
  109.     {
  110.       if (client.available()) {
  111.         char c = client.read();
  112.         // If it isn't a new line, add the character to the buffer
  113.         if (c != '\n' && c != '\r') {
  114.           if (c == '{') flag =true;
  115.  
  116.           if (flag){  
  117.           clientline[index] = c;
  118.           index++;
  119.           }
  120.           // continue to read more data!
  121.  
  122.           continue;
  123.         }
  124.  
  125.         // got a \n or \r new line, which means the string is done
  126.         clientline[index] = 0;    
  127.         }
  128.   }
  129.  
  130.      Serial.println();
  131.     Serial.println("disconnecting.");
  132.     client.stop();
  133.     client.println("Connection: close");
  134.     client.println();
  135.     if (clientline[1]=='0'){
  136.     Serial.print(clientline[1]);
  137.     return 0;
  138.     }
  139. }
  140.  
  141. int check_id(String id)
  142. {
  143.     if(id == "S1748")
  144.     {
  145.     return 0;
  146.     }
  147.    
  148.     else
  149.     {
  150.       return 0;
  151.     }
  152. }
  153.  
  154. int check_exp(String expDate)
  155. {
  156.     if(expDate <="09-2016")
  157.     {
  158.     return 1;
  159.     }
  160.    
  161.     else
  162.     {
  163.     return 0;
  164.     }
  165. }
  166.  
  167. void Run_Motor(void)
  168. {
  169.  
  170.    for(position = 0; position < 90; position += 1)
  171.                 {
  172.                   servo1.write(position);  // Move to next position
  173.                   delay(10);               // Short pause to allow it to move
  174.                 }
  175.  
  176.               // Tell servo to go to 0 degrees, stepping by one degree
  177.                 delay(1000);
  178.                 for(position = 90; position >= 0; position -= 1)
  179.                 {                                
  180.                   servo1.write(position);  // Move to next position
  181.                   delay(10);               // Short pause to allow it to move
  182.                 }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement