Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "PCF8574.h"
- PCF8574 pcf8574(0x20);
- byte rel[] = {P0, P1, P2, P3, P4, P5, P6, P7};
- bool stateWDT = 0;
- #include <Wiegand.h>
- // These are the pins connected to the Wiegand D0 and D1 signals.
- // Ensure your board supports external Interruptions on these pins
- #define RFID1_DO 32
- #define RFID1_D1 12
- // The object that handles the wiegand protocol
- Wiegand wiegand;
- unsigned cur_time_wdt, old_time_wdt;
- // Initialize Wiegand reader
- void setup() {
- Serial.begin(115200);
- Serial.println("init pcf: ");
- for (int i = 0; i < sizeof(rel); i++) {
- Serial.print(rel[i]);
- pcf8574.pinMode(rel[i], OUTPUT);
- }
- Serial.print("Init pcf8574...");
- if (pcf8574.begin()) {
- Serial.println("OK");
- } else {
- Serial.println("KO");
- }
- delay(100);
- //Install listeners and initialize Wiegand reader
- wiegand.onReceive(receivedData, "Card readed: ");
- wiegand.onReceiveError(receivedDataError, "Card read error: ");
- wiegand.onStateChange(stateChanged, "State changed: ");
- wiegand.begin(Wiegand::LENGTH_ANY, true);
- //initialize pins as INPUT and attaches interruptions
- pinMode(RFID1_DO, INPUT);
- pinMode(RFID1_D1, INPUT);
- attachInterrupt(digitalPinToInterrupt(RFID1_DO), pinStateChanged, CHANGE);
- attachInterrupt(digitalPinToInterrupt(RFID1_D1), pinStateChanged, CHANGE);
- //Sends the initial pin state to the Wiegand library
- pinStateChanged();
- }
- // Every few milliseconds, check for pending messages on the wiegand reader
- // This executes with interruptions disabled, since the Wiegand library is not thread-safe
- void loop() {
- noInterrupts();
- wiegand.flush();
- interrupts();
- //Sleep a little -- this doesn't have to run very often.
- delay(100);
- cur_time_wdt = millis();
- if (cur_time_wdt - old_time_wdt >= 150) {
- seed_wdt();
- old_time_wdt = millis();
- }
- }
- // When any of the pins have changed, update the state of the wiegand library
- void pinStateChanged() {
- wiegand.setPin0State(digitalRead(RFID1_DO));
- wiegand.setPin1State(digitalRead(RFID1_D1));
- }
- // Notifies when a reader has been connected or disconnected.
- // Instead of a message, the seconds parameter can be anything you want -- Whatever you specify on `wiegand.onStateChange()`
- void stateChanged(bool plugged, const char* message) {
- Serial.print(message);
- Serial.println(plugged ? "CONNECTED" : "DISCONNECTED");
- }
- // Notifies when a card was read.
- // Instead of a message, the seconds parameter can be anything you want -- Whatever you specify on `wiegand.onReceive()`
- void receivedData(uint8_t* data, uint8_t bits, const char* message) {
- Serial.print(message);
- Serial.print(bits);
- Serial.print("bits / ");
- //Print value in HEX
- uint8_t bytes = (bits + 7) / 8;
- for (int i = 0; i < bytes; i++) {
- Serial.print(data[i] >> 4, 16);
- Serial.print(data[i] & 0xF, 16);
- }
- Serial.println();
- }
- // Notifies when an invalid transmission is detected
- void receivedDataError(Wiegand::DataError error, uint8_t* rawData, uint8_t rawBits, const char* message) {
- Serial.print(message);
- Serial.print(Wiegand::DataErrorStr(error));
- Serial.print(" - Raw data: ");
- Serial.print(rawBits);
- Serial.print("bits / ");
- //Print value in HEX
- uint8_t bytes = (rawBits + 7) / 8;
- for (int i = 0; i < bytes; i++) {
- Serial.print(rawData[i] >> 4, 16);
- Serial.print(rawData[i] & 0xF, 16);
- }
- Serial.println();
- }
- void seed_wdt() {
- stateWDT = !stateWDT;
- pcf8574.digitalWrite(P7, stateWDT);
- pcf8574.digitalWrite(P4, stateWDT);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement