ESP32 RFID
来自Jack's Lab
(版本间的差异)
(→Quick start) |
(→Quick start) |
||
| 第43行: | 第43行: | ||
* Restart Arduino IDE | * Restart Arduino IDE | ||
| + | |||
| + | * File --> New, input following code in the new windows in Arduino IDE: | ||
| + | |||
| + | <source lang=cpp> | ||
| + | #include <SPI.h> | ||
| + | #include <MFRC522.h> | ||
| + | |||
| + | #define RST_PIN SCL // Configurable, see typical pin layout above | ||
| + | |||
| + | #define NR_OF_READERS 1 | ||
| + | |||
| + | byte ssPins[] = {SS}; | ||
| + | |||
| + | MFRC522 mfrc522[NR_OF_READERS]; // Create MFRC522 instance. | ||
| + | |||
| + | void setup() { | ||
| + | |||
| + | Serial.begin(9600); // Initialize serial communications with the PC | ||
| + | while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4) | ||
| + | |||
| + | SPI.begin(); // Init SPI bus | ||
| + | |||
| + | for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) { | ||
| + | mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN); // Init each MFRC522 card | ||
| + | Serial.print(F("Reader ")); | ||
| + | Serial.print(reader); | ||
| + | Serial.print(F(": ")); | ||
| + | mfrc522[reader].PCD_DumpVersionToSerial(); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | |||
| + | for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) { | ||
| + | // Look for new cards | ||
| + | |||
| + | if (mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()) { | ||
| + | Serial.print(F("Reader ")); | ||
| + | Serial.print(reader); | ||
| + | // Show some details of the PICC (that is: the tag/card) | ||
| + | Serial.print(F(": Card UID:")); | ||
| + | dump_byte_array(mfrc522[reader].uid.uidByte, mfrc522[reader].uid.size); | ||
| + | Serial.println(); | ||
| + | Serial.print(F("PICC type: ")); | ||
| + | MFRC522::PICC_Type piccType = mfrc522[reader].PICC_GetType(mfrc522[reader].uid.sak); | ||
| + | Serial.println(mfrc522[reader].PICC_GetTypeName(piccType)); | ||
| + | |||
| + | // Halt PICC | ||
| + | mfrc522[reader].PICC_HaltA(); | ||
| + | // Stop encryption on PCD | ||
| + | mfrc522[reader].PCD_StopCrypto1(); | ||
| + | } //if (mfrc522[reader].PICC_IsNewC | ||
| + | } //for(uint8_t reader | ||
| + | } | ||
| + | |||
| + | void dump_byte_array(byte *buffer, byte bufferSize) { | ||
| + | for (byte i = 0; i < bufferSize; i++) { | ||
| + | Serial.print(buffer[i] < 0x10 ? " 0" : " "); | ||
| + | Serial.print(buffer[i], HEX); | ||
| + | } | ||
| + | } | ||
| + | </source> | ||
| + | |||
| + | |||
| + | Press Ctrl + U to upload the firmware into Quantum board | ||
<br><br> | <br><br> | ||
2016年11月15日 (二) 17:06的版本
1 Overview
2 Quick start
- Install Git for windows from: https://git-for-windows.github.io
- Install python 2.7 from https://www.python.org/ftp/python/2.7/python-2.7.msi The installation direcory is: C:\Python27
- Run the git for windows, add this line 'export PATH=/c/python27/:$PATH' into /etc/bash.bashrc
- Enter 'Control Panel' ---> All Control Panel Items ---> System ---> Advanced System Settings (System Properties) ---> Advanced ---> Environment Variables, add the python into PATH:
- Install Arduino IDE
- Run the git for windows, go to Arduino IDE installation directory:
$ cd /PATH/TO/Arduino $ cd hardware $ mkdir espressif $ cd espressif $ git clone git://github.com/espressif/arduino-esp32.git esp32 $ cd esp32/tools $ python get.py # waitting for generating the toolchain $ $ cd ../../../../libraries $ git clone git://github.com/miguelbalboa/rfid.git Cloning into 'rfid'... remote: Counting objects: 1105, done. remote: Total 1105 (delta 0), reused 0 (delta 0), pack-reused 1105eceiving objects: K100% Receiving objects: 100% (1105/1105), 1.35 MiB | 459.00 KiB/s, done. Resolving deltas: 100% (582/582), done. Checking connectivity... done.
- Restart Arduino IDE
- File --> New, input following code in the new windows in Arduino IDE:
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN SCL // Configurable, see typical pin layout above
#define NR_OF_READERS 1
byte ssPins[] = {SS};
MFRC522 mfrc522[NR_OF_READERS]; // Create MFRC522 instance.
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN); // Init each MFRC522 card
Serial.print(F("Reader "));
Serial.print(reader);
Serial.print(F(": "));
mfrc522[reader].PCD_DumpVersionToSerial();
}
}
void loop() {
for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
// Look for new cards
if (mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()) {
Serial.print(F("Reader "));
Serial.print(reader);
// Show some details of the PICC (that is: the tag/card)
Serial.print(F(": Card UID:"));
dump_byte_array(mfrc522[reader].uid.uidByte, mfrc522[reader].uid.size);
Serial.println();
Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = mfrc522[reader].PICC_GetType(mfrc522[reader].uid.sak);
Serial.println(mfrc522[reader].PICC_GetTypeName(piccType));
// Halt PICC
mfrc522[reader].PICC_HaltA();
// Stop encryption on PCD
mfrc522[reader].PCD_StopCrypto1();
} //if (mfrc522[reader].PICC_IsNewC
} //for(uint8_t reader
}
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
Press Ctrl + U to upload the firmware into Quantum board
3 Reference
- For more information please refer to