commit
db5fc58b16
2 changed files with 150 additions and 0 deletions
@ -0,0 +1,28 @@
@@ -0,0 +1,28 @@
|
||||
# RFIDSwitch |
||||
|
||||
使用Arduino开发的RFID读卡识别开关模块 |
||||
|
||||
# 使用硬件 |
||||
ESP8266-NodeMCU x 1,ssd1306 x 1,RFID-RC522(13.56Mhz)x 1,LED灯 x 1 |
||||
|
||||
# 接线说明 |
||||
|
||||
## 1.ssd1306(I2C模拟) |
||||
VCC-3v |
||||
GND-G |
||||
SCL-D1--GPIO5 |
||||
SDA-D2--GPIO4 |
||||
|
||||
## 2.RFID-RC522(SPI) |
||||
SDA-D8--GPIO15 |
||||
SCK-D5--GPIO14 |
||||
MOSI-D7--GPIO13 |
||||
MISO-D6--GPIO12 |
||||
IRQ-不接(或者读信号口) |
||||
GND-G |
||||
RST-D3--GPIO0 |
||||
3V3-3V |
||||
|
||||
## 3.LED |
||||
VCC-D0--GPIO16 |
||||
GND-G |
@ -0,0 +1,122 @@
@@ -0,0 +1,122 @@
|
||||
|
||||
#include <U8g2lib.h> |
||||
#include <Wire.h> |
||||
#include <SPI.h> |
||||
#include <MFRC522.h> |
||||
|
||||
#define SS_PIN 15 |
||||
#define RST_PIN 0 |
||||
#define LIGHT1_PIN 16 |
||||
|
||||
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
|
||||
MFRC522::MIFARE_Key key; |
||||
String decResult = ""; |
||||
bool isLED1_open = false; |
||||
|
||||
// Please UNCOMMENT one of the contructor lines below
|
||||
// U8g2 Contructor List (Frame Buffer)
|
||||
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); |
||||
|
||||
// End of constructor list
|
||||
|
||||
void drawBattery(int posX,int posY,int energy){ |
||||
u8g2.drawBox(posX, posY+3, 2, 4); |
||||
u8g2.drawFrame(posX+2, posY, 20, 10); |
||||
//energy
|
||||
u8g2.drawBox(posX+4+(16-energy), posY+2, energy, 6); |
||||
} |
||||
|
||||
void updateOledMsg(const char *s){ |
||||
u8g2.begin(); |
||||
u8g2.clearBuffer(); // clear the internal memory
|
||||
u8g2.setFont(u8g2_font_6x12_mr); |
||||
u8g2.drawStr(0,46,s); |
||||
u8g2.sendBuffer(); // transfer internal memory to the display
|
||||
} |
||||
|
||||
void printDec(byte *buffer, byte bufferSize) { |
||||
decResult = ""; |
||||
for (byte i = 0; i < bufferSize; i++) { |
||||
//Serial.print(buffer[i] < 0x10 ? " 0" : " ");
|
||||
Serial.print(buffer[i]); |
||||
Serial.print(" "); |
||||
//strcat(decResult, " ");
|
||||
// itoa((int)buffer[i],decResult,10);
|
||||
decResult.concat(String(buffer[i])); |
||||
decResult.concat(" "); |
||||
} |
||||
Serial.print("\n"); |
||||
Serial.print(decResult); |
||||
|
||||
} |
||||
|
||||
void printHex(byte *buffer, byte bufferSize) { |
||||
for (byte i = 0; i < bufferSize; i++) { |
||||
Serial.print(buffer[i] < 0x10 ? " 0" : " "); |
||||
Serial.print(buffer[i], HEX); |
||||
} |
||||
} |
||||
|
||||
void verifiedLight(){ |
||||
if(decResult == "156 43 24 50 "){ |
||||
if(!isLED1_open){ |
||||
digitalWrite(LIGHT1_PIN, HIGH); |
||||
isLED1_open = true; |
||||
}else{ |
||||
digitalWrite(LIGHT1_PIN, LOW); |
||||
isLED1_open = false; |
||||
} |
||||
|
||||
} |
||||
} |
||||
|
||||
void setup(void) { |
||||
Serial.begin(115200); // Initiate a serial communication
|
||||
SPI.begin(); // Initiate SPI bus
|
||||
mfrc522.PCD_Init(); // Initiate MFRC522
|
||||
Serial.println("Approximate your card to the reader..."); |
||||
Serial.println(); |
||||
for (byte i = 0; i < 6; i++) { |
||||
key.keyByte[i] = 0xFF; |
||||
} |
||||
Serial.println(F("This code scan the MIFARE Classsic NUID.")); |
||||
Serial.print(F("Using the following key:")); |
||||
printHex(key.keyByte, MFRC522::MF_KEY_SIZE); |
||||
|
||||
u8g2.begin(); |
||||
u8g2.clearBuffer(); // clear the internal memory
|
||||
u8g2.setFont(u8g2_font_freedoomr25_tn); // choose a suitable font
|
||||
u8g2.drawStr(19,40,"12:59"); // write something to the internal memory
|
||||
u8g2.setFont(u8g2_font_6x12_mr); |
||||
u8g2.drawStr(0,46,"No Cards info"); |
||||
drawBattery(0,0,6); |
||||
u8g2.sendBuffer(); // transfer internal memory to the display
|
||||
|
||||
pinMode(LIGHT1_PIN, OUTPUT);
|
||||
digitalWrite(LIGHT1_PIN, LOW); |
||||
|
||||
} |
||||
|
||||
void loop(void) { |
||||
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
|
||||
if (!mfrc522.PICC_IsNewCardPresent()) |
||||
return; |
||||
// Verify if the NUID has been readed
|
||||
if (!mfrc522.PICC_ReadCardSerial()) |
||||
return; |
||||
|
||||
Serial.print(F("PICC type: ")); |
||||
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak); |
||||
Serial.println(mfrc522.PICC_GetTypeName(piccType)); |
||||
printDec(mfrc522.uid.uidByte, mfrc522.uid.size); |
||||
Serial.print(mfrc522.uid.sak); |
||||
updateOledMsg(decResult.c_str()); |
||||
// 停止 PICC
|
||||
mfrc522.PICC_HaltA(); |
||||
//停止加密PCD
|
||||
mfrc522.PCD_StopCrypto1(); |
||||
verifiedLight(); |
||||
delay(3000); // Wait for a second
|
||||
updateOledMsg("No cards info"); |
||||
decResult = ""; |
||||
} |
Loading…
Reference in new issue