2016-08-19 97 views
0

我試圖在我的NodeMCU v3上使用RF522,使用SPI接口。我能夠將RF522連接到我的Pi Zero,並且可以掃描隨附的芯片以及手機的NFC,因此我相信硬件是好的。與RF522一起使用NodeMCU V3時出現奇怪的行爲

這裏是我發現迄今(連接在下面的代碼文件):

  • 如果我勾了一切,我不能上傳我的Arduino的草圖 (espcomm_upload_mem失敗),我無法運行腳本即在 NodeMCU上。
  • 如果我斷開RF522的3v電源,我可以上傳腳本並運行它。
  • 如果我離開了3V電源斷開,程序無法自檢:mfrc522.PCD_PerformSelfTest()
  • 如果我插上3V電源線在NodeMCU啓動後,但自測試之前,它通過自檢和跑!
  • 我從來沒有找到一張卡(永遠重複「無新卡...」)。嘗試了現有的芯片和手機,但沒有發現。

我不知道爲什麼安裝的電源讓所有的東西都不能啓動,我不知道爲什麼我的自檢通過時無法讀取卡!其他人有沒有經驗讓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

刪除RST線還允許器件啓動並通過自檢,並打開Vcc。在這裏看到最後的評論:https://forum.mysensors.org/topic/4299/nodemcu-and-nrf-spi-init-problem/4 – Jaron

+0

這是我用來使其與PI(只帶司機的工作1.44 ):http://bsd.ee/~hadara/blog/?p=1017我無法使普通的python實現工作。讓我懷疑我是否有一個不好的讀者? – Jaron

回答

0

問題是,我需要調用mfrc522.PCD_Init();執行自檢和版本轉儲後再次執行。一旦我做到了,一切都按預期工作!

相關問題