Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compatibilidad con teclado wiegand.
#5
Code:
/*
* Example program for reading Wiegand data using ESP32
* Made by KinCony IoT: https://www.kincony.com
*
* This program demonstrates how to read Wiegand data (commonly used in RFID readers) using GPIO pins.
* It is configured for Wiegand 26-bit and 34-bit formats.
*
* You can freely modify and distribute this code under the terms of the MIT license.
*/

#define WIEGAND_D0_PIN 32  // GPIO pin for Wiegand Data 0 (D0)
#define WIEGAND_D1_PIN 33  // GPIO pin for Wiegand Data 1 (D1)

volatile unsigned long cardData = 0;  // Variable to store the card data
volatile unsigned int bitCount = 0;   // Counter for the number of bits received
unsigned long lastWiegandTime = 0;    // Time of the last bit received

#define WIEGAND_TIMEOUT 3000  // 3-second timeout to consider a Wiegand packet complete

void setup() {
  // Initialize serial communication for debugging
  Serial.begin(115200);
 
  // Configure Wiegand pins as inputs
  pinMode(WIEGAND_D0_PIN, INPUT);
  pinMode(WIEGAND_D1_PIN, INPUT);
 
  // Attach interrupts to handle Wiegand signals
  attachInterrupt(digitalPinToInterrupt(WIEGAND_D0_PIN), handleWiegandD0, FALLING);  // Trigger on falling edge for D0
  attachInterrupt(digitalPinToInterrupt(WIEGAND_D1_PIN), handleWiegandD1, FALLING);  // Trigger on falling edge for D1
}

void loop() {
  // Check if the last bit was received more than 3 seconds ago (Wiegand timeout)
  if ((millis() - lastWiegandTime > WIEGAND_TIMEOUT) && bitCount > 0) {
    // If the timeout has occurred and bits were received, process the data
    processWiegandData();
   
    // Reset the bit counter and card data for the next packet
    bitCount = 0;
    cardData = 0;
  }
}

// Interrupt service routine (ISR) for handling Wiegand D0 signal
void IRAM_ATTR handleWiegandD0() {
  bitCount++;            // Increment the bit count
  cardData <<= 1;        // Shift the current data to the left (adding a 0)
  lastWiegandTime = millis();  // Record the time of this bit reception
}

// Interrupt service routine (ISR) for handling Wiegand D1 signal
void IRAM_ATTR handleWiegandD1() {
  bitCount++;            // Increment the bit count
  cardData <<= 1;        // Shift the current data to the left
  cardData |= 1;         // Set the least significant bit to 1 (since this is D1)
  lastWiegandTime = millis();  // Record the time of this bit reception
}

// Function to process the received Wiegand data once a complete packet is received
void processWiegandData() {
  Serial.print("Received Wiegand data: ");
  Serial.print(cardData);
  Serial.print(" with ");
  Serial.print(bitCount);
  Serial.println(" bits.");
 
  // Check if the bit count corresponds to the common Wiegand formats
  if (bitCount == 26) {
    // Wiegand 26-bit format
    Serial.println("Wiegand 26-bit format detected.");
  } else if (bitCount == 34) {
    // Wiegand 34-bit format
    Serial.println("Wiegand 34-bit format detected.");
  } else {
    // Unknown or unsupported format
    Serial.println("Unknown Wiegand format.");
  }
}
Reply


Messages In This Thread
RE: Compatibilidad con teclado wiegand. - by admin - 09-12-2024, 01:15 PM

Forum Jump:


Users browsing this thread:
1 Guest(s)