• Witaj na Forum Arduino Polska! Zapraszamy do rejestracji!
  • Znajdziesz tutaj wiele informacji na temat hardware / software.
Witaj! Logowanie Rejestracja


Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Inteligentna kontrola dostępu w oparciu o arduino i STONE TFT LCD
#1
Wymagane materiały

Arduino UNO
Zamek elektroniczny KOB
Przekaźnik
Panel dotykowy STONE STWI070WT-01
Moduł MFRC522

Realizowane funkcje

1. rejestracja kart.
2. rejestracja nazwy użytkownika i hasła.
3. przeciągnięcie karty w celu odblokowania zamka elektronicznego.
4. Rejestracja nazwy użytkownika i hasła w celu odblokowania zamka elektronicznego.

Schemat połączeń

   

GUI design

[Obrazek: file.php?id=7003]

code sharing

Kod:
#include <SPI.h>
#include <MFRC522.h>
//#include <ArduinoJson.h>
//#include <SoftwareSerial.h>

#define SS_PIN 10
#define RST_PIN 9

#define clocks 7

MFRC522 rfid(SS_PIN, RST_PIN);


byte nuidPICC[4];
byte nuidPICC2[4];
unsigned char RX1_BUF[50];

unsigned char RX3_BUF[10];

uint8_t count_i=0;
uint8_t tag=0;

int idsnum = 4;

//uint32_t nuidPICC2=0;

//int numwei = 32;
//unsigned char names[100]={'a',"lbw"};
//unsigned char names[100]={"zbw","lbw"};
//unsigned char *cnames=names[0];
unsigned char names[30][10] = {"zbw",0};
unsigned char names2[30][10] = {"zbw",0};
unsigned char ids[50]={0x13,0xA5,0x5D,0x04};
unsigned char password1[30][20]={"zbw3366",0};
unsigned char admin[2][5]={"yyds",0};
int names_nums = 0;

void card_read();
void password_read();
void password_regit();
void printHex(byte *buffer, byte bufferSize);

void setup()
{
   Serial.begin(115200);
   SPI.begin();
  rfid.PCD_Init();
  pinMode(clocks, OUTPUT);

}

void loop(){

while (Serial.available())
  {
    RX1_BUF[count_i]=Serial.read();
    Serial.println(RX1_BUF[count_i]);
    count_i++;
    }
    //memcpy(RX2_BUF,RX1_BUF,count_i);
   
    if ((RX1_BUF[3] == 16)&&(RX1_BUF[4] == 1))
  {
    Serial.println();
    Serial.println("button");
    for(tag=0;tag<RX1_BUF[6]-1;tag++)
        {
          RX3_BUF[tag]=RX1_BUF[tag+7];
        }
    }
    memset(RX1_BUF,0,count_i);
   

    if(((strcmp("card2",(const char *)RX3_BUF))==0)){
    memset(RX3_BUF,0,count_i);
    count_i=0;
    card_read((const char*)*names2, 100, 10);
    }
    else if(((strcmp("password2",(const char *)RX3_BUF))==0)){
    memset(RX3_BUF,0,count_i);
    count_i=0;
    Serial.println(count_i);
    password_read((const char*)*names, 100, 10, (const char*)*password1, 100, 20);
    }
    else if(((strcmp("password1",(const char *)RX3_BUF))==0)){
    memset(RX3_BUF,0,count_i);
    count_i=0;
    Serial.println(count_i);
    password_regit((const char*)*admin, 2, 2, *names);
    }
    else if(((strcmp("card1",(const char *)RX3_BUF))==0)){
    memset(RX3_BUF,0,count_i);
    count_i=0;
    Serial.println(count_i);
    card_regit((const char*)*admin, 2, 2);
    }
    count_i=0;
}

void card_read(char *namess, int n, int m){
  Serial.println("card_read");
  count_i=0;

  while(1){

    while (Serial.available())
  {
    RX1_BUF[count_i]=Serial.read();
    Serial.println(RX1_BUF[count_i]);
    count_i++;
    }
    if ((RX1_BUF[3] == 16)&&(RX1_BUF[4] == 1))
  {
    Serial.println();
    Serial.println("button");
    for(tag=0;tag<RX1_BUF[6]-1;tag++)
        {
          RX3_BUF[tag]=RX1_BUF[tag+7];
        }
    }
    memset(RX1_BUF,0,count_i);
    count_i=0;

    if(((strcmp("back3",(const char *)RX3_BUF))==0)){
    Serial.println("back_home");
    return;
    }

 
  if ( ! rfid.PICC_IsNewCardPresent())
    continue;

 
  if ( ! rfid.PICC_ReadCardSerial())
    continue;

  MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);

 
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI && 
    piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
    piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    Serial.println("not support");
    continue;
  }
 
 
  for (byte i = 0,nuidPICC2 = 0,numwei = 32; i < 4; i++) {
    //Serial.println(rfid.uid.uidByte[i]);
    nuidPICC[i] = rfid.uid.uidByte[i];
    Serial.println(nuidPICC[i],HEX);
    //nuidPICC2 = rfid.uid.uidByte[i];
    //nuidPICC2 = nuidPICC2 << (numwei-8);
  }

  Serial.println();

  Serial.println(&(*(namess+1*10)));

 
  rfid.PICC_HaltA();

 
  rfid.PCD_StopCrypto1();
  for (int id_num = 0;id_num < 100; id_num++){
  if ((nuidPICC[0] == ids[id_num])&&(nuidPICC[1] == ids[id_num+1])&&(nuidPICC[2] == ids[id_num+2])&&(nuidPICC[3] == ids[id_num+3]))
  {
    //cnames = names[id_num];
    if(((strcmp("",&(*(namess+(id_num/4)*10))))==0))break;
    //if(&(*(namess+(id_num/4)*10)) == "")break;
    Serial.print(id_num);
    Serial.print("UID:");
    Serial.print(F("ST<{\"cmd_code\":\"set_text\",\"type\":\"label\",\"widget\":\"cardname1\",\"text\":\""));
    //for(int id_nums = 0;id_nums < 10;id_nums++)
    //{
   // Serial.print(&(*(namess + id_num + id_nums - 1)));
    //Serial.print(&(*(namess + id_num + id_nums - 1)));
    //}
    Serial.print(&(*(namess + (id_num/4)*10 )));
    Serial.print(F("\"}>ET"));
    printHex(rfid.uid.uidByte, rfid.uid.size);
    digitalWrite(clocks, HIGH);
    Serial.println(F("ST<{\"cmd_code\":\"set_visible\",\"type\":\"widget\",\"widget\":\"gif4\",\"visible\":true}>ET"));
    delay(3000);
    digitalWrite(clocks, LOW);
    Serial.println(F("ST<{\"cmd_code\":\"set_text\",\"type\":\"label\",\"widget\":\"cardid1\",\"text\":\"\"}>ET"));
    Serial.println(F("ST<{\"cmd_code\":\"set_text\",\"type\":\"label\",\"widget\":\"cardname1\",\"text\":\"\"}>ET"));
    Serial.println(F("ST<{\"cmd_code\":\"set_visible\",\"type\":\"widget\",\"widget\":\"gif4\",\"visible\":false}>ET"));
    //nuidPICC2 = 0;
    break;
  }
  //nuidPICC2 = 0;
  }
}
}

void printHex(byte *buffer, byte bufferSize) {
  Serial.println();
  Serial.print(F("ST<{\"cmd_code\":\"set_text\",\"type\":\"label\",\"widget\":\"cardid1\",\"text\":\""));
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? "0" : "");
    Serial.print(buffer[i], HEX);
  }
  Serial.print(F("\"}>ET"));
}

void password_read(char *namess, int n, int m, char *passwords, int o, int p){
 
//  unsigned char RX4_BUF[10] = {0};
  unsigned char RX5_BUF[20] = {0};
//  unsigned char RX6_BUF[20] = {0};
  int name_true = 0;
  int password_true = 0;
  int i, j;
  int name_s = 0;
  Serial.println("password_read");
 

  while(1){

    while (Serial.available())
  {
    RX1_BUF[count_i]=Serial.read();
    Serial.println(RX1_BUF[count_i]);
    count_i++;
    }
    if ((RX1_BUF[3] == 16)&&(RX1_BUF[4] == 1))
  {
    Serial.println();
    Serial.println("button");
    for(tag=0;tag<RX1_BUF[6]-1;tag++)
        {
          RX3_BUF[tag]=RX1_BUF[tag+7];
        }
        RX3_BUF[tag] = NULL;
        RX3_BUF[tag+1] = NULL;
        RX3_BUF[tag+2] = NULL;
    //Serial.println(((const char *)RX3_BUF));
    }
    else if ((RX1_BUF[3] == 16)&&(RX1_BUF[4] == 112))
  {
    Serial.println();
    Serial.println("edit");
    for(name_s = 0; name_s < RX1_BUF[6]-8; name_s++)
    {
      RX5_BUF[name_s] = RX1_BUF[name_s+15];
    }
        RX5_BUF[name_s] = NULL;
        RX5_BUF[name_s+1] = NULL;
        RX5_BUF[name_s+2] = NULL;
   
        Serial.println(((const char *)RX5_BUF));
        if (RX1_BUF[12]==53){
    for (i=0;i<100;i++){
      for (j=0;j<10;j++){
        if (strcmp(&(*(namess + i*10 )),(const char*)RX5_BUF)==0)
        {name_true = 1;
        break;
        }
      }
      if(name_true == 1)break;
    }
        }

        else if (RX1_BUF[12]==54){
          for (;i<100;i++){
      for (j=0;j<20;j++){
        if (strcmp(&(*(passwords + i*20 )),(const char*)RX5_BUF)==0)
        {password_true = 1;
        break;
        }
      }
      if(name_true == 1)break;
    }
        }
    Serial.println(name_true);
    Serial.println(password_true);
    Serial.println(&(*(namess + 1*10 )));
    Serial.println(&(*(passwords + 1*20 )));
    if ((name_true == 1)&&(password_true == 1)){
    digitalWrite(clocks, HIGH);
    Serial.println(F("ST<{\"cmd_code\":\"set_visible\",\"type\":\"widget\",\"widget\":\"lock2\",\"visible\":true}>ET"));
    delay(3000);
    digitalWrite(clocks, LOW);
    Serial.println(F("ST<{\"cmd_code\":\"set_visible\",\"type\":\"widget\",\"widget\":\"lock2\",\"visible\":false}>ET"));
    Serial.println(F("ST<{\"cmd_code\":\"set_text\",\"type\":\"edit\",\"widget\":\"edit5\",\"text\":\"\"}>ET"));
    Serial.println(F("ST<{\"cmd_code\":\"set_text\",\"type\":\"edit\",\"widget\":\"edit6\",\"text\":\"\"}>ET"));
    name_true = password_true = 0;
    }
    }
    memset(RX1_BUF,0,count_i);
    count_i=0;

    if(((strcmp("back4",(const char *)RX3_BUF))==0)){
    Serial.println("back_home");
    return;


void password_regit(char *admins, int w, int e,char *namess){

  unsigned char RX6_BUF[20] = {0};
  unsigned char rname_BUF[10] = {0};
  unsigned char rpass_BUF[20] = {0};
//  unsigned char RX6_BUF[20] = {0};
  int name1_true = 0;
  int password1_true = 0;
  int admin1_true = 0;
  int name_s;
  //int n, p;
  Serial.println("password_regit");
 

  while(1){

    while (Serial.available())
  {
    RX1_BUF[count_i]=Serial.read();
    Serial.println(RX1_BUF[count_i]);
    count_i++;
    }
    if ((RX1_BUF[3] == 16)&&(RX1_BUF[4] == 1))
  {
    Serial.println();
    Serial.println("button");
    for(tag=0;tag<RX1_BUF[6]-1;tag++)
        {
          RX3_BUF[tag]=RX1_BUF[tag+7];
        }
        RX3_BUF[tag] = NULL;
        RX3_BUF[tag+1] = NULL;
        RX3_BUF[tag+2] = NULL;
    }
    else if ((RX1_BUF[3] == 16)&&(RX1_BUF[4] == 112))
  {
    Serial.println();
    Serial.println("edit");
    for(name_s = 0; name_s < RX1_BUF[6]-8; name_s++)
    {
      RX6_BUF[name_s] = RX1_BUF[name_s+15];
    }
   
        Serial.println(((const char *)RX6_BUF));
        if (RX1_BUF[12]==51)
        {
          memcpy(rname_BUF,RX6_BUF,name_s);
          name1_true = 1;
          rname_BUF[name_s] = NULL;
          rname_BUF[name_s+1] = NULL;
          rname_BUF[name_s+2] = NULL;
        }
        else if (RX1_BUF[12]==52)
        {
          memcpy(rpass_BUF,RX6_BUF,name_s);
          password1_true = 1;
          rpass_BUF[name_s] = NULL;
          rpass_BUF[name_s+1] = NULL;
          rpass_BUF[name_s+2] = NULL;
        }
        else if (RX1_BUF[12]==55)
        {
          if (strcmp(&(*(admins)),(const char*)RX6_BUF)==0)
          admin1_true = 1;
        }
       
        Serial.println(name1_true);
        Serial.println(password1_true);
        Serial.println(admin1_true);
//        Serial.println(&(*(admins)));
        Serial.println((const char*)rname_BUF);
        Serial.println((const char*)rpass_BUF);

        if ((name1_true == 1)&&(password1_true == 1)&&(admin1_true == 1))
        {
          names_nums++;

          strncpy(&(names[names_nums][0]), &rname_BUF[0], strlen(rname_BUF));
          strncpy(&(password1[names_nums][0]), &rpass_BUF[0], strlen(rpass_BUF));

          name1_true = password1_true = admin1_true = 0;
        }

   
 
}
    memset(RX1_BUF,0,count_i);
    count_i=0;
    if(((strcmp("back2",(const char *)RX3_BUF))==0)){
    Serial.println("back_home");
    return;
  }
}
}

void card_regit(char *admins, int w, int e){

  unsigned char RX7_BUF[20] = {0};
  unsigned char rname_BUF[10] = {0};
  unsigned char rpass_BUF[20] = {0};
  int name1_true = 0;
  int password1_true = 0;
  int admin1_true = 0;
  int name_s;
  int set_label = 0;
  Serial.println("card_regit");
 

  while(1){

    while (Serial.available())
  {
    RX1_BUF[count_i]=Serial.read();
    Serial.println(RX1_BUF[count_i]);
    count_i++;
    }
    if ((RX1_BUF[3] == 16)&&(RX1_BUF[4] == 1))
  {
    Serial.println();
    Serial.println("button");
    for(tag=0;tag<RX1_BUF[6]-1;tag++)
        {
          RX3_BUF[tag]=RX1_BUF[tag+7];
        }
        RX3_BUF[tag] = NULL;
        RX3_BUF[tag+1] = NULL;
        RX3_BUF[tag+2] = NULL;
    //Serial.println(((const char *)RX3_BUF));
    }
    else if ((RX1_BUF[3] == 16)&&(RX1_BUF[4] == 112))
  {
    Serial.println();
    Serial.println("edit");
    for(name_s = 0; name_s < RX1_BUF[6]-8; name_s++)
    {
      RX7_BUF[name_s] = RX1_BUF[name_s+15];
    }
   
        Serial.println(((const char *)RX7_BUF));
        if (RX1_BUF[12]==49)
        {
          memcpy(rname_BUF,RX7_BUF,name_s);
          name1_true = 1;
          rname_BUF[name_s] = NULL;
          rname_BUF[name_s+1] = NULL;
          rname_BUF[name_s+2] = NULL;
        }
        else if (RX1_BUF[12]==50)
        {
          if (strcmp(&(*(admins)),(const char*)RX7_BUF)==0)
          admin1_true = 1;
        }
       
        Serial.println(name1_true);
        Serial.println(admin1_true);
        Serial.println(&(*(admins)));

       

   
 
}

    memset(RX1_BUF,0,count_i);
    count_i=0;
    if(((strcmp("back1",(const char *)RX3_BUF))==0)){
    Serial.println("back_home");
    return;
  }
if ((name1_true == 1)&&(admin1_true == 1))
        {
         
          if (set_label == 0)Serial.println(F("ST<{\"cmd_code\":\"set_text\",\"type\":\"label\",\"widget\":\"cardid\",\"text\":\"please place card!\"}>ET"));
          set_label = 1;
          if ( ! rfid.PICC_IsNewCardPresent())
    continue;

 
  if ( ! rfid.PICC_ReadCardSerial())
    continue;

  MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);

 
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI && 
    piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
    piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    Serial.println("not support");
    continue;
  }
 
 
  for (byte i = 0,nuidPICC2 = 0,numwei = 32; i < 4; i++,idsnum++) {
    nuidPICC[i] = rfid.uid.uidByte[i];
    ids[idsnum] = nuidPICC[i];
    Serial.println(ids[idsnum],HEX);
  }
  strncpy(&(names2[1][0]), &rname_BUF[0], strlen(rname_BUF));
  Serial.println(F("ST<{\"cmd_code\":\"set_text\",\"type\":\"label\",\"widget\":\"cardid\",\"text\":\"regit ok!\"}>ET"));
  delay(2000);
  Serial.println(F("ST<{\"cmd_code\":\"set_text\",\"type\":\"label\",\"widget\":\"cardid\",\"text\":\"\"}>ET"));
  Serial.println(F("ST<{\"cmd_code\":\"set_text\",\"type\":\"edit\",\"widget\":\"edit1\",\"text\":\"\"}>ET"));
    Serial.println(F("ST<{\"cmd_code\":\"set_text\",\"type\":\"edit\",\"widget\":\"edit2\",\"text\":\"\"}>ET"));
 
 
  rfid.PICC_HaltA();
  rfid.PCD_StopCrypto1();

        }

   
}
}

Video demo

https://www.youtube.com/watch?v=k2oEEy4Xijg
 
Odpowiedź
  


Skocz do:


Przeglądający: 1 gości