2024年1月29日 星期一

2024 RFID 作業1 (ESP32 + MFRC522 + MQTT + Node-Red )

2024 RFID 作業1 (ESP32 + MFRC522 + MQTT + Node-Red )

系統圖:



硬體:    usb 線  MFRC522 + 卡片  ESP32 







  • Vcc <-> 3V3 (or Vin(5V) depending on the module version)
  • RST (Reset) <-> D0
  • GND (Masse) <-> GND
  • MISO (Master Input Slave Output) <-> 19
  • MOSI (Master Output Slave Input) <-> 23
  • SCK (Serial Clock) <-> 18
  • SS/SDA (Slave select) <-> 5


軟體:

Arduino IDE  https://www.arduino.cc/en/software



MQTT Explorer  https://mqtt-explorer.com/

MQTT-explorer

https://mqtt-explorer.com/

MQTT-explorer提供了更完整的功能

  • 可視化Topic和Topic活動
  • 刪除保留的Topic
  • 搜索/過濾Topic
  • 發布Topic
  • 繪製數值類型的Topic折線圖
  • 保留每個Topic的歷史記錄
  • 深色/淺色外觀主題



Node-Red 安裝方法  

https://ithelp.ithome.com.tw/articles/10201795


Node-Red Dashboard 安裝方法

 http://blog.3dgowl.com/node-red-dashboard%E5%84%80%E8%A1%A8%E6%9D%BF/








 ESP32與MFRC522連接方式   (請 留意  每個人買到的ESP32 腳位 會不太一樣)


ESP32 Pinout Reference – A Complete Guide

https://linuxhint.com/esp32-pinout-reference/#1.1


//定義MFRC522 RFID read 與 ESP32 介面 接腳連接Pin assign
/* Wiring RFID RC522 module  
==============================================================
GND     = GND   3.3V    = 3.3V
The following table shows the typical pin layout used:
 *             MFRC522      ESP32    
 *             Reader/PCD            
 * Signal      Pin          Pin        
 * -----------------------------------
 * RST/Reset   RST          GPIO27  
 * SPI SS      SDA(SS)      GPIO5    
 * SPI MOSI    MOSI         GPIO23    
 * SPI MISO    MISO         GPIO19    
 * SPI SCK     SCK          GPIO18    
 *
[1] (1, 2) Configurable, typically defined as RST_PIN in sketch/program.
[2] (1, 2) Configurable, typically defined as SS_PIN in sketch/program.
[3] The SDA pin might be labeled SS on some/older MFRC522 boards
==== =========================================================








arduino_secrets.h 檔案內容用wordpad 編輯存檔

#define SECRET_SSID ""

#define SECRET_PASS ""

沒有此檔案 compile (Verify)會錯誤





//定義MFRC522 RFID read 與 ESP32 介面 接腳連接Pin assign
/* Wiring RFID RC522 module  
==============================================================
GND     = GND   3.3V    = 3.3V
The following table shows the typical pin layout used:
 *             MFRC522      ESP32    
 *             Reader/PCD            
 * Signal      Pin          Pin        
 * -----------------------------------
 * RST/Reset   RST          GPIO27  
 * SPI SS      SDA(SS)      GPIO5    
 * SPI MOSI    MOSI         GPIO23    
 * SPI MISO    MISO         GPIO19    
 * SPI SCK     SCK          GPIO18    
 *
[1] (1, 2) Configurable, typically defined as RST_PIN in sketch/program.
[2] (1, 2) Configurable, typically defined as SS_PIN in sketch/program.
[3] The SDA pin might be labeled SS on some/older MFRC522 boards
=============================================================
*/
// Wifi 與 MQttClient 程式庫
#include <ArduinoMqttClient.h>
#include <WiFi.h>
#include "arduino_secrets.h"

//MFRC522 程式庫
#include <SPI.h>
#include <MFRC522.h>



///////please enter your sensitive data in the Secret tab/arduino_secrets.h
//char ssid[] = "TOTOLINK_A3002MU";    // your network SSID (name)
//char pass[] = "24063173";    // your network password (use for WPA, or use as key for WEP)

char ssid[] = "dlink-103A";    // your network SSID (name)
char pass[] = "bdcce12882";    // your network password (use for WPA, or use as key for WEP)

WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);

//const char broker[] = "test.mosquitto.org";
const char broker[] = "broker.mqtt-dashboard.com";
int        port     = 1883;
String json = "";
const char *topic1 = "alex9ufo/RFID/read";
const char *topic3 = "alex9ufo/RFID/back";
const char willTopic[] = "alex9ufo/will";
//======================================================
#define RST_PIN      27        // 讀卡機的重置腳位
#define SS_PIN       5        // 晶片選擇腳位
MFRC522 mfrc522(SS_PIN, RST_PIN);    // 建立MFRC522物件
MFRC522::MIFARE_Key key;  // 儲存金鑰
MFRC522::StatusCode status;
//===========================================================
void onMqttMessage(int messageSize) {
  // we received a message, print out the topic and contents
  Serial.print("Received a message with topic '");
  Serial.print(mqttClient.messageTopic());
  String Topic= mqttClient.messageTopic();
  Serial.print("', duplicate = ");
  Serial.print(mqttClient.messageDup() ? "true" : "false");
  Serial.print(", QoS = ");
  Serial.print(mqttClient.messageQoS());
  Serial.print(", retained = ");
  Serial.print(mqttClient.messageRetain() ? "true" : "false");
  Serial.print("', length ");
  Serial.print(messageSize);
  Serial.println(" bytes:");
  String topicIN="";
  // use the Stream interface to print the contents
  while (mqttClient.available()) {
    //Serial.print((char)mqttClient.read());
    topicIN=topicIN+(char)mqttClient.read();

  }

  Serial.println(topicIN);
  topicIN.trim();
}

//===========================================================
String printHex(byte *buffer, byte bufferSize) {
      String id = "";
      for (byte i = 0; i < bufferSize; i++) {
        id += buffer[i] < 0x10 ? "0" : "";
        id += String(buffer[i], HEX);
        id +=" ";
      }
      return id;
}
//===========================================================
//副程式  setup wifi
void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);     //print ssid
  WiFi.begin(ssid, pass);  //初始化WiFi 函式庫並回傳目前的網路狀態
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }   //假設 wifi 未連接 show ………

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}  
//===========================================================
void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(115200);   // Initialize serial communications with the PC
  while (!Serial);    // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
 
  setup_wifi();
  Serial.println("You're connected to the network");
  Serial.println();
 
  String willPayload = "oh no!";
  bool willRetain = true;
  int willQos = 1;

  mqttClient.beginWill(willTopic, willPayload.length(), willRetain, willQos);
  mqttClient.print(willPayload);
  mqttClient.endWill();

  Serial.print("Attempting to connect to the MQTT broker: ");
  Serial.println(broker);

  if (!mqttClient.connect(broker, port)) {
    Serial.print("MQTT connection failed! Error code = ");
    Serial.println(mqttClient.connectError());

    while (1);
  }

  Serial.println("You're connected to the MQTT broker!");
  Serial.println();

  // set the message receive callback
  mqttClient.onMessage(onMqttMessage);


  SPI.begin();      // Init SPI bus
  mfrc522.PCD_Init();   // Init MFRC522
  delay(4);       // Optional delay. Some board do need more time after init to be ready, see Readme
  mfrc522.PCD_DumpVersionToSerial();  // Show details of PCD - MFRC522 Card Reader details
  Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
}
//===========================================================
void loop() {
 
  // call poll() regularly to allow the library to receive MQTT messages and
  // send MQTT keep alives which avoids being disconnected by the broker
  mqttClient.poll();

  // to avoid having delays in loop, we'll use the strategy from BlinkWithoutDelay
  // see: File -> Examples -> 02.Digital -> BlinkWithoutDelay for more info
  unsigned long currentMillis = millis();


 if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
   
     Serial.println(F("Please scan MIFARE Classic card..."));
    // 確認是否有新卡片
   
    byte *id = mfrc522.uid.uidByte;   // 取得卡片的UID
    byte idSize = mfrc522.uid.size;   // 取得UID的長度
    String Type;
    Serial.print("PICC type: ");      // 顯示卡片類型
    // 根據卡片回應的SAK值(mfrc522.uid.sak)判斷卡片類型
    MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
    Type= mfrc522.PICC_GetTypeName(piccType);
    Serial.println(mfrc522.PICC_GetTypeName(piccType));

    Serial.print("UID Size: ");       // 顯示卡片的UID長度值
    Serial.println(idSize);
 
    for (byte i = 0; i < idSize; i++) {  // 逐一顯示UID碼
      Serial.print("id[");
      Serial.print(i);
      Serial.print("]: ");
      Serial.println(id[i], HEX);       // 以16進位顯示UID值
    }
    Serial.println();



    json="";
    json = json +(" Card UID: ");
    String json1=printHex(mfrc522.uid.uidByte, mfrc522.uid.size);
    json1.toUpperCase();
    json = json + json1;
    json.trim();
    json = json +(" , PICC type: ");
    json =  json + Type;
    json.trim();

    bool retained = false;
    int qos = 1;
    bool dup = false;

    mqttClient.beginMessage(topic3,  json.length(), retained, qos, dup);
    mqttClient.print(json);
    mqttClient.endMessage();

    Serial.println();
    // Dump debug info about the card; PICC_HaltA() is automatically called
    mfrc522.PICC_DumpToSerial(&(mfrc522.uid));


// Dump debug info about the card; PICC_HaltA() is automatically called
    // 令卡片進入停止狀態
    mfrc522.PICC_HaltA();
    mfrc522.PCD_StopCrypto1(); // stop encryption on PCD
  }
}
////===========================================================

Node-Red程式:

[{"id":"4499ce1a4824f4e6","type":"ui_text","z":"3c83a362ab19a958","group":"c3d8097ebce346d7","order":1,"width":10,"height":2,"name":"","label":"ESP回來字串","format":"{{msg.payload}}","layout":"row-spread","className":"","x":400,"y":120,"wires":[]},{"id":"088e9a8c1ed9784e","type":"mqtt in","z":"3c83a362ab19a958","name":"","topic":"alex9ufo/RFID/back","qos":"1","datatype":"auto-detect","broker":"841df58d.ee5e98","nl":false,"rap":true,"rh":0,"inputs":0,"x":170,"y":120,"wires":[["4499ce1a4824f4e6"]]},{"id":"c3d8097ebce346d7","type":"ui_group","name":"RFID UID READ","tab":"048debf3dd7e9641","order":3,"disp":true,"width":10,"collapse":false,"className":""},{"id":"841df58d.ee5e98","type":"mqtt-broker","name":"","broker":"broker.hivemq.com","port":"1883","clientid":"","autoConnect":true,"usetls":false,"compatmode":false,"protocolVersion":"4","keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""},{"id":"048debf3dd7e9641","type":"ui_tab","name":"2024 RFID ","icon":"dashboard","disabled":false,"hidden":false}]


把 node-red 程式 copy 後 匯入到 node-red中 

一、安裝/刪除節點
1.點選右上角的三條橫線下拉選單→「節點管理」(圖1)
2.進入後可以看到已安裝的節點,在此可以禁用或刪除(圖2)
3.被刪除的節點可以再次重新安裝
4.切換至安裝頁面,上方可輸入關鍵字搜尋(圖3)
5.選取要得節點後按下右下角安裝即會進入安裝狀態
特別提醒:安裝和刪除時請確保Node-RED正確運作,切勿關閉或斷線,會造成系統異常而無法開啟和編輯
6.新增的節點將會出現於主畫面左側的節點區
二、匯入/匯出流程
1.點選右上角的三條橫線下拉選單→「匯入、匯出」(圖4)
(1)匯入:將外部檔案放入自己的Node-RED中編輯使用
(2)匯出:將自己的專案轉成jason或文字碼供他人使用
2.點選「匯入」→「匯入所選檔案」→「匯入」(圖5)
特別提醒:若匯入他人資料時本身沒有資料中的結點,Node-RED會無法辨識而跳出警示,需要針對該節點去新增安裝後重新匯入一次,始得正常使用
3.點選「匯出」→「現在的節點」→「下載」(圖6)
(1)按下下載後會匯出jason檔案,需要時再使用匯入功能即可取回資料至Node-RED中
(2)下載旁的「匯出到剪貼簿」會生成文字碼(記事本可開啟),兩種都可以作為匯出的方式
(3)建議當專案完成後即可匯出檔案作為備份以免遺失


實驗結果:

1) Arduino  Serial monitor


You're connected to the MQTT broker!

Firmware Version: 0x92 = v2.0
Scan PICC to see UID, SAK, type, and data blocks...
Please scan MIFARE Classic card...
PICC type: MIFARE 1KB
UID Size: 4
id[0]: 86
id[1]: D5
id[2]: EA
id[3]: 6A


Card UID: 86 D5 EA 6A
Card SAK: 08
PICC type: MIFARE 1KB
Sector Block   0  1  2  3   4  5  6  7   8  9 10 11  12 13 14 15  AccessBits
  15   63   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
         62   61 6C 65 78  39 75 66 6F  74 65 73 74  69 6E 67 35     [ 0 0 0 ] 
         61   61 6C 65 78  39 75 66 6F  74 65 73 74  69 6E 67 31     [ 0 0 0 ] 
         60   61 6C 65 78  39 75 66 6F  74 65 73 74  69 6E 67 35     [ 0 0 0 ] 
  14   59   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
         58   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00       [ 0 0 0 ] 
         57   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00      [ 0 0 0 ] 
         56   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00       [ 0 0 0 ] 
  13   55   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
         54   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00       [ 0 0 0 ] 
         53   4B 65 65 70  20 48 61 63  6B 69 6E 67  21 00 00 00     [ 0 0 0 ] 
         52   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00        [ 0 0 0 ] 
  12   51   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF   [ 0 0 1 ] 
         50   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00        [ 0 0 0 ] 
         49   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00        [ 0 0 0 ] 
         48   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00        [ 0 0 0 ] 
  11   47   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
         46   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00        [ 0 0 0 ] 
         45   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00        [ 0 0 0 ] 
         44   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00        [ 0 0 0 ] 
  10    43   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
         42   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00        [ 0 0 0 ] 
         41   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00        [ 0 0 0 ] 
         40   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00        [ 0 0 0 ] 
   9     39   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
         38   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00        [ 0 0 0 ] 
         37   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00        [ 0 0 0 ] 
         36   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00        [ 0 0 0 ] 
   8     35   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
         34   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00        [ 0 0 0 ] 
         33   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00        [ 0 0 0 ] 
         32   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00        [ 0 0 0 ] 
   7     31   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
         30   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00        [ 0 0 0 ] 
         29   74 65 73 74  00 48 61 63  6B 69 6E 67  21 00 00 00        [ 0 0 0 ] 
         28   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00        [ 0 0 0 ] 
   6    27   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
         26   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00        [ 0 0 0 ] 
         25   61 6C 65 78  39 75 66 6F  20 74 65 73  74 69 6E 67        [ 0 0 0 ] 
         24   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00        [ 0 0 0 ] 
   5     23   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
         22   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00        [ 0 0 0 ] 
         21   61 6C 65 78  39 75 66 6F  20 74 65 73  74 69 6E 67      [ 0 0 0 ] 
         20   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00        [ 0 0 0 ] 
   4    19   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
         18   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00        [ 0 0 0 ] 
         17   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00        [ 0 0 0 ] 
         16   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00        [ 0 0 0 ] 
   3    15   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
         14   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00        [ 0 0 0 ] 
         13   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00        [ 0 0 0 ] 
         12   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00         [ 0 0 0 ] 
   2     11   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF   [ 0 0 1 ] 
         10   2B 39 32 33  30 30 34 34  34 37 37 37  37 20 20 20        [ 0 0 0 ] 
          9   32 38 2D 30  39 2D 31 39  38 39 20 20  20 20 20 20        [ 0 0 0 ] 
          8   4A 68 6F 6E  20 41 62 72  61 68 75 6D  20 20 20 20        [ 0 0 0 ] 
   1     7   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF     [ 0 0 1 ] 
          6   41 6E 74 68  6F 6E 79 20  20 20 20 20  20 20 20 20        [ 0 0 0 ] 
          5   20 20 20 20  20 20 20 20  20 20 20 20  20 20 00 00         [ 0 0 0 ] 
          4   01 02 03 04  05 06 07 08  00 00 00 00  00 00 00 00         [ 0 0 0 ] 
   0     3   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF     [ 0 0 1 ] 
          2   61 6C 65 78  39 75 66 6F  20 74 65 73  74 69 6E 67        [ 0 0 0 ] 
          1   61 6C 65 78  20 20 20 20  20 20 20 20  20 20 20 20         [ 0 0 0 ] 
          0   86 D5 EA 6A  D3 08 04 00  62 63 64 65  66 67 68 69        [ 0 0 0 ] 

2) Node-Red 畫面





3) MQTT Explorer畫面






將實驗過程及結果拍攝影片上傳至yt 並將連結寫於作業上


作業格式



作業一:  RFID基礎實驗之實驗

學生: 學號: 姓名: 班級:

一、 實習目的

 

二、 實習設備及器材

 

三、實習步驟(硬體連接 軟體設定)

 

四、程式

1.    Arduino 程式

2.    Node-Red程式

五、實習結果

1.    Arduino 程式  monitor 輸出

2.    Node-Red程式 畫面

3.    軟體或APP 畫面

 

六、實習心得

       (實驗過程遇到的問題) 不要寫官話

七、 參考文獻




沒有留言:

張貼留言

WOKWI LED + MQTT Node-Red SQLite

WOKWI LED + MQTT Node-Red SQLite const char *mqtt_broker = "broker.mqtt-dashboard.com" ; const char *topic1 = "alex9ufo/e...