Advertisement
mikroavr

wiegand_rfid_v2

Mar 22nd, 2024
538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "PCF8574.h"
  2. PCF8574 pcf8574(0x20);
  3. byte rel[] = {P0, P1, P2, P3, P4, P5, P6, P7};
  4. bool stateWDT = 0;
  5.  
  6. #include <Wiegand.h>
  7.  
  8. // These are the pins connected to the Wiegand D0 and D1 signals.
  9. // Ensure your board supports external Interruptions on these pins
  10. #define RFID1_DO 32
  11. #define RFID1_D1 12
  12.  
  13. // The object that handles the wiegand protocol
  14. Wiegand wiegand;
  15.  
  16. unsigned cur_time_wdt, old_time_wdt;
  17.  
  18. // Initialize Wiegand reader
  19. void setup() {
  20.   Serial.begin(115200);
  21.   Serial.println("init pcf: ");
  22.   for (int i = 0; i < sizeof(rel); i++) {
  23.     Serial.print(rel[i]);
  24.     pcf8574.pinMode(rel[i], OUTPUT);
  25.   }
  26.   Serial.print("Init pcf8574...");
  27.   if (pcf8574.begin()) {
  28.     Serial.println("OK");
  29.   } else {
  30.     Serial.println("KO");
  31.   }
  32.   delay(100);
  33.   //Install listeners and initialize Wiegand reader
  34.   wiegand.onReceive(receivedData, "Card readed: ");
  35.   wiegand.onReceiveError(receivedDataError, "Card read error: ");
  36.   wiegand.onStateChange(stateChanged, "State changed: ");
  37.   wiegand.begin(Wiegand::LENGTH_ANY, true);
  38.  
  39.   //initialize pins as INPUT and attaches interruptions
  40.   pinMode(RFID1_DO, INPUT);
  41.   pinMode(RFID1_D1, INPUT);
  42.   attachInterrupt(digitalPinToInterrupt(RFID1_DO), pinStateChanged, CHANGE);
  43.   attachInterrupt(digitalPinToInterrupt(RFID1_D1), pinStateChanged, CHANGE);
  44.  
  45.   //Sends the initial pin state to the Wiegand library
  46.   pinStateChanged();
  47. }
  48.  
  49. // Every few milliseconds, check for pending messages on the wiegand reader
  50. // This executes with interruptions disabled, since the Wiegand library is not thread-safe
  51. void loop() {
  52.   noInterrupts();
  53.   wiegand.flush();
  54.   interrupts();
  55.   //Sleep a little -- this doesn't have to run very often.
  56.   delay(100);
  57.  
  58.   cur_time_wdt = millis();
  59.   if (cur_time_wdt - old_time_wdt >= 150) {
  60.     seed_wdt();
  61.     old_time_wdt = millis();
  62.   }
  63. }
  64.  
  65. // When any of the pins have changed, update the state of the wiegand library
  66. void pinStateChanged() {
  67.   wiegand.setPin0State(digitalRead(RFID1_DO));
  68.   wiegand.setPin1State(digitalRead(RFID1_D1));
  69. }
  70.  
  71. // Notifies when a reader has been connected or disconnected.
  72. // Instead of a message, the seconds parameter can be anything you want -- Whatever you specify on `wiegand.onStateChange()`
  73. void stateChanged(bool plugged, const char* message) {
  74.   Serial.print(message);
  75.   Serial.println(plugged ? "CONNECTED" : "DISCONNECTED");
  76. }
  77.  
  78. // Notifies when a card was read.
  79. // Instead of a message, the seconds parameter can be anything you want -- Whatever you specify on `wiegand.onReceive()`
  80. void receivedData(uint8_t* data, uint8_t bits, const char* message) {
  81.   Serial.print(message);
  82.   Serial.print(bits);
  83.   Serial.print("bits / ");
  84.   //Print value in HEX
  85.   uint8_t bytes = (bits + 7) / 8;
  86.   for (int i = 0; i < bytes; i++) {
  87.     Serial.print(data[i] >> 4, 16);
  88.     Serial.print(data[i] & 0xF, 16);
  89.   }
  90.   Serial.println();
  91. }
  92.  
  93. // Notifies when an invalid transmission is detected
  94. void receivedDataError(Wiegand::DataError error, uint8_t* rawData, uint8_t rawBits, const char* message) {
  95.   Serial.print(message);
  96.   Serial.print(Wiegand::DataErrorStr(error));
  97.   Serial.print(" - Raw data: ");
  98.   Serial.print(rawBits);
  99.   Serial.print("bits / ");
  100.  
  101.   //Print value in HEX
  102.   uint8_t bytes = (rawBits + 7) / 8;
  103.   for (int i = 0; i < bytes; i++) {
  104.     Serial.print(rawData[i] >> 4, 16);
  105.     Serial.print(rawData[i] & 0xF, 16);
  106.   }
  107.   Serial.println();
  108. }
  109.  
  110. void seed_wdt() {
  111.   stateWDT = !stateWDT;
  112.   pcf8574.digitalWrite(P7, stateWDT);
  113.   pcf8574.digitalWrite(P4, stateWDT);
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement