我尝试使用SPI接口将RF522与我的NodeMCU v3一起使用。我能够将RF522连接到我的Pi Zero并让它扫描随附的芯片以及手机的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...);
}
答案 0 :(得分:0)
问题是我需要在执行自检和版本转储后再次调用 mfrc522.PCD_Init(); 。一旦我这样做,一切都按预期工作!