天天看点

Arduino UNO 单片机 读RFID RC522卡号发送Json数据

Arduino UNO 单片机 读RFID RC522卡号发送Json数据

记得添加库文件哦

/*
        Arduino Uno <—> RFID-RC522
        10 <—> SDA
        13 <—> SCK
        11 <—> MOSI
        12 <—> MISO
        null <—> IRQ
        GND <—> GND
        9 <—> RST
        3.3V <—> 3.3V
   Typical pin layout used:
   -----------------------------------------------------------------------------------------
               MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
               Reader/PCD   Uno/101       Mega      Nano v3    Leonardo/Micro   Pro Micro
   Signal      Pin          Pin           Pin       Pin        Pin              Pin
   -----------------------------------------------------------------------------------------
   RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
   SPI SS      SDA(SS)      10            53        D10        10               10
   SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
   SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
   SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15
*/
#include <deprecated.h>
#include <MFRC522.h>
#include <MFRC522Extended.h>
#include <require_cpp11.h>
#include <SPI.h>
#include <MFRC522.h>
#include "ArduinoJson.h"

#define  uchar unsigned char
#define uint  unsigned int

#define RST_PIN         9          // Configurable, see typical pin layout above
#define SS_PIN          10         // Configurable, see typical pin layout above
#define buzzer 8     //蜂鸣器 

MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance

void setup() {
  Serial.begin(9600);   // Initialize serial communications with the PC
  pinMode(buzzer, OUTPUT);
  digitalWrite(buzzer, LOW);

  uchar i, tmp;
  while (!Serial);    // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  SPI.begin();      // Init SPI bus
  mfrc522.PCD_Init();   // Init MFRC522
}

void loop() {
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return;
  }

  //获取UID卡号通过串口发送函数
  mfrc522.PICC_DumpDetailsToSerialUid(&(mfrc522.uid));   //https://www.javaroad.cn/questions/137837
  // 停止 PICC  使放置在读卡区的IC卡进入休眠状态,不再重复读卡
  mfrc522.PICC_HaltA();
  //停止加密PCD 停止读卡模块编码
  mfrc522.PCD_StopCrypto1();
}

void MFRC522::PICC_DumpDetailsToSerialUid(Uid *uid) {

  StaticJsonDocument<200> doc; //声明一个JsonDocument对象

  //Json格式参考:  {"flag":"A","cardId":"233127150128"}

  String flag = "come";//标识
  String data = "{\"flag\":\""+flag+"\",\"cardId\":\"";  
  Serial.print(data);

  for (byte i = 0; i < uid->size; i++) {
    if (uid->uidByte[i] < 0x10)
    { delay(500);
      Serial.print(F("0"));
    } else {
      Serial.print(F(""));
    }
    Serial.print(uid->uidByte[i], HEX);
  }
  Serial.println(F("\"}"));

  digitalWrite(buzzer, HIGH);
  delay(200);
  digitalWrite(buzzer, LOW);
}