Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h> // servo library
- #include <Wire.h>
- #include <PN532_I2C.h>
- #include <PN532.h>
- #include <NfcAdapter.h>
- #include <Ethernet.h>
- #include <SPI.h>
- #define BUFSIZ 100
- PN532_I2C pn532_i2c(Wire);
- NfcAdapter nfc = NfcAdapter(pn532_i2c);
- int position;
- String id;
- String expDate;
- Servo servo1; // servo control object
- int i=0;
- byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
- IPAddress server(192,168,1,4); // numeric IP for Google (no DNS)
- IPAddress ip(192, 168, 1, 7);
- EthernetClient client;
- void setup() {
- servo1.attach(10);
- Serial.begin(9600);
- Serial.println("Smart Parking");
- nfc.begin();
- }
- void loop(void) {
- Serial.println("\nScan a NFC tag\n");
- static int places_left=5;
- if (nfc.tagPresent())
- {
- read_tag();
- if(parked_list(id) == 0)
- {
- //entering student
- if(check_id(id)==0 && check_exp(expDate)==1 && places_left>0)
- {
- Serial.print("yaaaay");
- Run_Motor();
- places_left=places_left-1;
- Serial.print(places_left);
- }
- }
- else
- {
- //exiting student
- }
- }
- delay(3000);
- }
- String read_tag(void)
- {
- int i=0;
- NfcTag tag = nfc.read();
- if (tag.hasNdefMessage()) // every tag won't have a message
- {
- NdefMessage message = tag.getNdefMessage();
- int recordCount = message.getRecordCount();
- for (i = 0; i < recordCount; i++)
- {
- NdefRecord record = message.getRecord(i);
- int payloadLength = record.getPayloadLength();
- byte payload[payloadLength];
- record.getPayload(payload);
- String payloadAsString = "";
- for (int c = 3; c < payloadLength; c++) {
- payloadAsString += (char)payload[c];
- }
- Serial.println(payloadAsString);
- if(i==0){
- id =payloadAsString;
- id.trim();
- }
- if(i==1){
- expDate =payloadAsString;
- expDate.trim();
- }
- }
- }
- return (id,expDate);
- }
- int parked_list(String id)
- {
- char clientline[BUFSIZ];
- int index = 0;
- boolean flag = false;
- if (Ethernet.begin(mac) == 0)
- {
- Ethernet.begin(mac, ip);
- }
- Serial.println("connecting...");
- client.connect(server, 80);
- client.println("GET /parking.php?id_tag=");
- client.println(id);
- client.println("HTTP/1.1");
- client.println("Host: 192.168.1.4");
- client.println();
- while(client.connected())
- {
- if (client.available()) {
- char c = client.read();
- // If it isn't a new line, add the character to the buffer
- if (c != '\n' && c != '\r') {
- if (c == '{') flag =true;
- if (flag){
- clientline[index] = c;
- index++;
- }
- // continue to read more data!
- continue;
- }
- // got a \n or \r new line, which means the string is done
- clientline[index] = 0;
- }
- }
- Serial.println();
- Serial.println("disconnecting.");
- client.stop();
- client.println("Connection: close");
- client.println();
- if (clientline[1]=='0'){
- Serial.print(clientline[1]);
- return 0;
- }
- }
- int check_id(String id)
- {
- if(id == "S1748")
- {
- return 0;
- }
- else
- {
- return 0;
- }
- }
- int check_exp(String expDate)
- {
- if(expDate <="09-2016")
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- void Run_Motor(void)
- {
- for(position = 0; position < 90; position += 1)
- {
- servo1.write(position); // Move to next position
- delay(10); // Short pause to allow it to move
- }
- // Tell servo to go to 0 degrees, stepping by one degree
- delay(1000);
- for(position = 90; position >= 0; position -= 1)
- {
- servo1.write(position); // Move to next position
- delay(10); // Short pause to allow it to move
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement