将 NodeMCU V3 与 RF522 一起使用时的奇怪行为

Weird behavior when using NodeMCU V3 with RF522

我正在尝试通过 SPI 接口将 RF522 与我的 NodeMCU v3 结合使用。我能够将 RF522 连接到我的 Pi Zero 并让它扫描它附带的芯片以及我的 phone 的 NFC,所以我非常有信心硬件是好的。

这是我目前的发现(连接记录在下面的代码中):

我不知道为什么安装电源后其他一切都无法启动,我也不知道为什么自检通过后我无法读取卡!还有其他人有让 RF522 和 NodeMCU 协同工作的经验吗?

代码:

#include <ESP8266WiFi.h>          //ESP8266 Core WiFi Library (you most likely already have this in your sketch)
#include <DNSServer.h>            //Local DNS Server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h>     //Local WebServer used to serve the configuration portal
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
#include <PubSubClient.h>
#include <SPI.h>
#include <MFRC522.h>

/*
*RST GPIO15 -- D8
*SDA(SS) GPIO2 -- D4
*MOSI GPIO13 -- D7
*MISO GPIO12 -- D6
*SCK GPIO14 -- D5
*GND GND
*3,3V 3,3V
 */
#define SS_PIN  2 // D4 -- SDA-PIN for RC522
#define RST_PIN  15 // D8 -- RST-PIN for RC522
MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance.

WiFiClient espClient;
PubSubClient mqtt(espClient);

const char* MQTT_user = "rfid.local";
const char* MQTT_server = "mqtt.local";
const char* topic_base = "home/rfid/";

void setup() {
  Serial.begin(115200);
  Serial.println("Lets get started!!");

  WiFiManager wifiManager;
  wifiManager.setTimeout(180); 
  if(!wifiManager.autoConnect("RFID", "RFID")) {
    Serial.println("Failed to connect and hit timeout");
    delay(3000);
    ESP.reset();
    delay(5000);
  } 
  Serial.println("Connected...yippie!");

  mqtt.setServer(MQTT_server, 1883);

  SPI.begin();     // Init SPI bus
  mfrc522.PCD_Init(); // Init MFRC522 card
  if (mfrc522.PCD_PerformSelfTest())
    Serial.println("RF522 Passed self test!");
  else {
    Serial.println("RF522 Failed self test!");
    delay(3000);
    ESP.reset();
    delay(5000);    
  }
  Serial.println("Waiting for someone to scan...");
}

void reconnectMQTT() {
  // Loop until we're reconnected
  while (!mqtt.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (mqtt.connect(MQTT_user, MQTT_user, MQTT_user)) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(mqtt.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void loop() {
  // Make sure we are still connected!
  if (!mqtt.connected()) {
    reconnectMQTT();
  }
  mqtt.loop();

  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    Serial.println("No new card...");
    delay(1000);
    return;
  }

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    Serial.println("Can't read card...");
    delay(1000);
    return;
  }

  // Dump debug info about the card. PICC_HaltA() is automatically called.
  mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
  //mqtt.publish(topic_base...);
}

问题是我需要在执行自检和版本转储后再次调用mfrc522.PCD_Init();。一旦我这样做了,一切都按预期工作了!