Created
October 20, 2025 13:29
-
-
Save mongonta0716/bff2d8b2000af8b65ac9cca1f525f32d to your computer and use it in GitHub Desktop.
Unit RFID2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <Arduino.h> | |
| #include <M5Unified.h> | |
| #include <MFRC522_I2C.h> | |
| /* | |
| ******************************************************************************* | |
| * Copyright (c) 2023 by M5Stack | |
| * Equipped with M5Core sample source code | |
| * 配套 M5Core 示例源代码 | |
| * Visit for more information: https://docs.m5stack.com/en/core/gray | |
| * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/core/gray | |
| * | |
| * Describe: RFID. | |
| * Date: 2023/1/28 | |
| ******************************************************************************* | |
| Please connect to Port A(22、21),Use the RFID Unit to read the Fudan card ID | |
| and display the ID on the screen. 请连接端口A(22、21),使用RFID Unit | |
| 读取ID卡并在屏幕上显示。 | |
| */ | |
| MFRC522_I2C mfrc522(0x28, -1); //RST_PIN); // Create MFRC522 instance. 创建MFRC522实例 | |
| void writeString(byte block_no, const char* message ,MFRC522_I2C::MIFARE_Key *key) { | |
| byte buffer[16]; // 書き込める長さは16byte | |
| MFRC522_I2C::StatusCode status; | |
| byte len = 0; | |
| status = (MFRC522_I2C::StatusCode)mfrc522.PCD_Authenticate(MFRC522_I2C::PICC_CMD_MF_AUTH_KEY_A, block_no, key, &(mfrc522.uid)); | |
| if (status != MFRC522_I2C::STATUS_OK) { | |
| M5.Lcd.println("Write:Authentication Failed"); | |
| Serial.print(F("Authentication failed: ")); | |
| M5.Lcd.println(mfrc522.GetStatusCodeName(status)); | |
| return; | |
| } | |
| Serial.println("convert"); | |
| for (int i = 0; i < 16; i++) { | |
| Serial.printf("%02x", message[i]); | |
| if (message[i] == 0) { | |
| for (int j=i;j<16;j++) { | |
| buffer[j] = 0; | |
| } | |
| break; | |
| } else { | |
| buffer[i] = message[i]; | |
| } | |
| len++; | |
| } | |
| Serial.printf("len:%d\n", len); | |
| status = (MFRC522_I2C::StatusCode)mfrc522.MIFARE_Write(block_no, buffer , 16); | |
| if (status != MFRC522_I2C::STATUS_OK) { | |
| M5.Lcd.println("Write:Error"); | |
| M5.Lcd.println(mfrc522.GetStatusCodeName(status)); | |
| return; | |
| } | |
| M5.Lcd.printf("Write SUCCESS Block:%d\n", block_no); | |
| delay(100); | |
| return; | |
| } | |
| String readString(byte block_no, MFRC522_I2C::MIFARE_Key *key) { | |
| MFRC522_I2C::StatusCode status; | |
| byte buffer[18]; // データは16byteのはずだが、18以下にするとバッファが少ないというエラーになる。 | |
| byte len = 18; | |
| String str = ""; | |
| status = (MFRC522_I2C::StatusCode)mfrc522.PCD_Authenticate(MFRC522_I2C::PICC_CMD_MF_AUTH_KEY_A, block_no, key, &(mfrc522.uid)); //line 834 | |
| if (status != MFRC522_I2C::STATUS_OK) { | |
| str = "Read:Authentication Failed"; | |
| Serial.print(F("Authentication failed: ")); | |
| Serial.println(mfrc522.GetStatusCodeName(status)); | |
| return str; | |
| } | |
| status = (MFRC522_I2C::StatusCode)mfrc522.MIFARE_Read(block_no, buffer, &len); | |
| if (status != MFRC522_I2C::STATUS_OK) { | |
| Serial.print(F("Reading failed: ")); | |
| Serial.println(mfrc522.GetStatusCodeName(status)); | |
| str = "Reading Failed"; | |
| str = str + String(mfrc522.GetStatusCodeName(status)); | |
| return str; | |
| } | |
| str = "Block " + String((char)block_no); | |
| for (uint8_t i = 0; i < 16; i++) | |
| { | |
| str = str + String(char(buffer[i])); | |
| } | |
| return str; | |
| } | |
| void setup() { | |
| auto cfg = M5.config(); | |
| M5.begin(cfg); // Init M5Stack. 初始化M5Stack | |
| M5.Lcd.setFont(&fonts::efontJA_24); | |
| M5.Lcd.println("MFRC522 Test"); | |
| M5.Ex_I2C.begin(); | |
| M5.Speaker.begin(); | |
| mfrc522.PCD_Init(); // Init MFRC522. 初始化 MFRC522 | |
| M5.Lcd.println("Please put the card\n\n"); | |
| } | |
| void loop() { | |
| M5.update(); | |
| MFRC522_I2C::StatusCode status; | |
| M5.Lcd.setCursor(0, 47); | |
| if (!mfrc522.PICC_IsNewCardPresent() || | |
| !mfrc522.PICC_ReadCardSerial()) { //如果没有读取到新的卡片 | |
| } else { | |
| MFRC522_I2C::MIFARE_Key key; | |
| for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF; | |
| status = (MFRC522_I2C::StatusCode)mfrc522.PCD_Authenticate(MFRC522_I2C::PICC_CMD_MF_AUTH_KEY_A, 4, &key, &(mfrc522.uid)); //line 834 of MFRC522.cpp file | |
| if (status != MFRC522_I2C::STATUS_OK) { | |
| M5.Lcd.println("Authentication Failed!"); | |
| } | |
| if (M5.BtnA.isPressed()) { | |
| M5.Speaker.tone(2000, 100); | |
| delay(100); | |
| M5.Speaker.tone(1000, 100); | |
| writeString(1, "Arduino", &key); | |
| writeString(2, "BtnA", &key); | |
| } else if(M5.BtnB.isPressed()) { | |
| M5.Speaker.tone(2000, 100); | |
| delay(100); | |
| M5.Speaker.tone(1000, 100); | |
| writeString(1, "Arduino", &key); | |
| writeString(2, "0123456789abcdef", &key); | |
| } else if(M5.BtnC.isPressed()) { | |
| M5.Speaker.tone(1000, 100); | |
| delay(100); | |
| M5.Speaker.tone(2000, 100); | |
| writeString(1, "", &key); | |
| writeString(2, "", &key); | |
| } else { | |
| M5.Speaker.tone(2000, 100); | |
| M5.Lcd.print("UID:"); | |
| for (byte i = 0; i < mfrc522.uid.size; | |
| i++) { // Output the stored UID data. 将存储的UID数据输出 | |
| M5.Lcd.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); | |
| M5.Lcd.print(mfrc522.uid.uidByte[i], HEX); | |
| } | |
| M5.Lcd.println(""); | |
| M5.Lcd.print(F("PICC type: ")); // Dump PIC type | |
| MFRC522_I2C::PICC_Type piccType = (MFRC522_I2C::PICC_Type)mfrc522.PICC_GetType(mfrc522.uid.sak); | |
| M5.Lcd.println(mfrc522.PICC_GetTypeName(piccType)); | |
| M5.Lcd.println(readString(1, &key)); | |
| M5.Lcd.println(readString(2, &key)); | |
| } | |
| delay(3000); | |
| M5.Lcd.fillRect(0, 47, 320, 200, BLACK); | |
| } | |
| mfrc522.PICC_HaltA(); | |
| mfrc522.PCD_StopCrypto1(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ; PlatformIO Project Configuration File | |
| ; | |
| ; Build options: build flags, source filter | |
| ; Upload options: custom upload port, speed and extra flags | |
| ; Library options: dependencies, extra library storages | |
| ; Advanced options: extra scripting | |
| ; | |
| ; Please visit documentation for the other options and examples | |
| ; https://docs.platformio.org/page/projectconf.html | |
| [platformio] | |
| default_envs = m5stack-grey | |
| [env] | |
| platform = espressif32 @ 6.5.0 | |
| framework = arduino | |
| upload_speed = 1500000 | |
| monitor_speed = 115200 | |
| ;board_build.extra_flags = | |
| ;board_build.flash_mode = qio | |
| board_build.f_flash = 80000000L | |
| board_build.filesystem = spiffs | |
| build_flags = -DCORE_DEBUG_LEVEL=0 | |
| board_build.partitions = default_16MB.csv | |
| lib_deps = | |
| m5stack/M5Unified@^0.2.10 | |
| https://github.com/kkloesener/MFRC522_I2C | |
| ; If you use AquesTalk, uncomment the following three lines | |
| ; and place libaquestalk.a in the lib folder.(https://www.a-quest.com/products/aquestalk_esp32.html) | |
| ;build_flags = | |
| ; -llibaquestalk | |
| ; -Llib/ | |
| [env:m5stack-core2] | |
| board = m5stack-core2 | |
| [env:m5stack-grey] | |
| board = m5stack-grey | |
| [env:m5stack-fire] | |
| board = m5stack-fire | |
| ;platform = https://github.com/platformio/platform-espressif32.git#feature/arduino-upstream | |
| ;platform_packages = framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#2.0.0 | |
| upload_speed = 921600 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment