2023年7月28日 星期五

使用 ChatGPT 寫一node-red 程式 可以擷取台灣地區的氣象

使用 ChatGPT 寫一node-red 程式 可以擷取台灣地區的氣象 

User

寫一node-red 程式 可以擷取台灣地區的氣象

ChatGPT

很抱歉,作為一個文字模型,我無法直接在Node-RED中運行程式碼,但我可以提供給你一個範例,讓你在Node-RED中擷取台灣地區的氣象資料。在這個範例中,我們將使用「中央氣象局開放資料平台」來取得氣象資料。


首先,確保你已經安裝了Node-RED並運行成功。然後,遵循以下步驟:


安裝 node-red-contrib-http-request 套件:在Node-RED的左側功能面板中,點擊「管理套件」,搜尋「node-red-contrib-http-request」,然後按照指示安裝。


在Node-RED的工作區中建立以下流程:


css

Copy code

[Inject] -> [HTTP Request] -> [Debug]

配置Inject節點:將Inject節點設置為單次觸發(例如,「單個」),以便手動觸發資料擷取。


配置HTTP Request節點:點擊這個節點,將其設置為GET請求,並在URL欄位中輸入以下API網址,用於獲取台灣地區的氣象資料:


ruby

Copy code

https://opendata.cwb.gov.tw/api/v1/rest/datastore/O-A0003-001?Authorization=你的API金鑰&elementName=TEMP,HUMD,PRES,Weather

這個URL包含了「中央氣象局開放資料平台」提供的「臺灣地區測站-現在天氣觀測報告」的API網址,並指定了要獲取的元素,例如溫度(TEMP)、濕度(HUMD)、氣壓(PRES)和天氣狀況(Weather)。請確保替換「你的API金鑰」為你註冊API時收到的實際API金鑰。


配置Debug節點:將Debug節點設置為「完整的訊息物件」,以便查看獲取的氣象資料。


點擊右上角的「Deploy」按鈕以儲存和部署你的流程。


當你按下Inject節點時,HTTP Request節點將觸發API請求,並從中央氣象局獲取台灣地區的氣象資料。這些資料將顯示在Debug節點的輸出中。


請注意,你需要註冊並取得中央氣象局開放資料平台的API金鑰,以便在URL中使用。此外,中央氣象局開放資料平台的API使用可能會有限制和配額,請參考其官方文件以瞭解詳細資訊。


這只是一個簡單的範例,你可以根據需求進一步處理資料,將它顯示在網頁上或儲存到資料庫中。希望這可以幫助到你入門擷取台灣地區的氣象資料。





User

請繼續

ChatGPT

當然,我可以為你提供更多關於Node-RED程式的資訊。接下來,我將展示如何將獲取的氣象資料進一步處理並在Node-RED中顯示出來。


解析API回應:

當API回應返回後,我們需要將其解析為可讀取的格式。在HTTP Request節點的設定中,將Response選項設置為a parsed JSON object,這樣就可以自動將API回應轉換為JavaScript物件。這使得我們可以更輕鬆地處理資料。


處理氣象資料:

使用Change節點(在左側功能面板中的"function"類別下)來處理氣象資料。這個節點讓你可以使用JavaScript進行資料的轉換和過濾。例如,你可以從API回應物件中提取出溫度、濕度和天氣狀況等資料。


以下是一個處理資料的範例JavaScript程式碼:


js

Copy code

// 範例程式碼

msg.payload = {

    temperature: msg.payload.records.location[0].weatherElement[0].elementValue,

    humidity: msg.payload.records.location[0].weatherElement[1].elementValue,

    pressure: msg.payload.records.location[0].weatherElement[2].elementValue,

    weather: msg.payload.records.location[0].weatherElement[3].elementValue

};

return msg;

這個程式碼會提取API回應物件中的相關資料並將它們存儲在msg.payload物件中,以便後續使用。


顯示氣象資料:

使用Debug節點來檢查處理後的氣象資料,或者使用Dashboard節點(如果你已經安裝了Node-RED Dashboard套件)來建立一個儀表板,將氣象資料以圖形或數值的形式呈現。你可以使用Dashboard節點中的各種元件(例如Gauge、Chart、Text等)來自定義資料的顯示方式。


錯誤處理:

記得在處理API回應時,要考慮到可能的錯誤情況,例如無法連接API伺服器或API回應錯誤等。你可以使用Try-Catch節點或Function節點中的JavaScript程式碼來捕獲和處理這些錯誤。


總結:

以上提供的是一個簡單的Node-RED範例,用於擷取台灣地區的氣象資料。你可以根據需求進一步擴展和自定義你的程式,例如加入資料庫存儲、定時更新等功能。希望這些資訊能對你有所幫助!


User

請將上面的 溫度(TEMP)、濕度(HUMD)、氣壓(PRES)和天氣狀況(Weather) 利用 Sqlite資料庫 儲存起來  欄位有 日期 時間 溫度 濕度 氣壓  天氣狀況 備註 等7個欄位

ChatGPT

當將氣象資料儲存到SQLite資料庫中時,你可以使用Node-RED中的node-red-node-sqlite套件來實現。請確保已經在Node-RED中安裝了這個套件。


以下是將氣象資料儲存到SQLite資料庫的流程:


設定SQLite資料庫連接:

在Node-RED的左側功能面板中,找到「sqlite」類別,將sqlite節點拖放到工作區中。點擊節點進行設定。你需要提供SQLite資料庫的名稱和位置。選擇一個合適的路徑和資料庫名稱,例如/path/to/weather_data.db。


解析API回應:

與之前相同,使用HTTP Request節點和Change節點來解析API回應,提取溫度、濕度、氣壓和天氣狀況等資料。


格式化日期和時間:

在將資料存儲到資料庫之前,我們需要準備日期和時間的欄位。你可以使用Function節點來添加當前的日期和時間到資料。


js

Copy code

var currentDate = new Date();

msg.date = currentDate.toISOString().split('T')[0];

msg.time = currentDate.toISOString().split('T')[1].split('.')[0];

return msg;

建立SQLite資料表:

在Node-RED的左側功能面板中,找到「sqlite」類別,將sqlite節點拖放到工作區中。點擊節點進行設定,選擇「execute」模式。在Command欄位中輸入SQL語句來建立資料表。下面是一個建立資料表的SQL語句範例:

sql

Copy code

CREATE TABLE IF NOT EXISTS weather_data (

    id INTEGER PRIMARY KEY AUTOINCREMENT,

    date TEXT,

    time TEXT,

    temperature REAL,

    humidity REAL,

    pressure REAL,

    weather TEXT,

    note TEXT

);

插入資料到資料庫:

使用Function節點來準備要插入資料庫的資料,並使用SQLite節點進行插入。

js

Copy code

var sqliteData = {

    date: msg.date,

    time: msg.time,

    temperature: msg.payload.temperature,

    humidity: msg.payload.humidity,

    pressure: msg.payload.pressure,

    weather: msg.payload.weather,

    note: "這是一個備註範例。可以根據需求修改。"

};


msg.topic = "INSERT INTO weather_data (date, time, temperature, humidity, pressure, weather, note) VALUES ($date, $time, $temperature, $humidity, $pressure, $weather, $note)";

msg.payload = sqliteData;

return msg;

最後,使用SQLite節點進行SQL插入指令的執行。


點擊右上角的「Deploy」按鈕以儲存和部署你的流程。


這樣,每當你按下Inject節點,Node-RED將觸發API請求,獲取氣象資料,然後將它們儲存到SQLite資料庫中的weather_data表中。


請注意,上述程式碼僅供參考,你可能需要根據API回應的實際格式和SQLite資料表的需求進行調整。希望這可以幫助你將氣象資料儲存到SQLite資料庫中。










Node-Red程式


[{"id":"29524fb9ae25eac2","type":"inject","z":"be36ae0c358ba0e7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":270,"y":100,"wires":[["67b10f612aeba21d"]]},{"id":"67b10f612aeba21d","type":"http request","z":"be36ae0c358ba0e7","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://opendata.cwb.gov.tw/api/v1/rest/datastore/O-A0003-001?Authorization=CWB-40C225F2FF-1224-4250-B9D9-3735AAE17DBF&elementName=TEMP,HUMD,PRES,Weather","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"credentials":{},"x":460,"y":100,"wires":[["3a247ad8571a9d43","ebfcca1381b1cb9d"]]},{"id":"3a247ad8571a9d43","type":"debug","z":"be36ae0c358ba0e7","name":"debug 119","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":100,"wires":[]},{"id":"ebfcca1381b1cb9d","type":"function","z":"be36ae0c358ba0e7","name":"function 42","func":"// 範例程式碼\nmsg.payload = {\n    temperature: msg.payload.records.location[0].weatherElement[0].elementValue,\n    humidity: msg.payload.records.location[0].weatherElement[1].elementValue,\n    pressure: msg.payload.records.location[0].weatherElement[2].elementValue,\n    weather: msg.payload.records.location[0].weatherElement[3].elementValue\n};\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":610,"y":200,"wires":[["ed8ee569c17ccd6a"]]},{"id":"ed8ee569c17ccd6a","type":"debug","z":"be36ae0c358ba0e7","name":"debug 120","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":830,"y":200,"wires":[]}]


2023年7月18日 星期二

實驗 ESP32 + RFID (Create Mode 建立資料模式/ Query Mode查詢資料模式)

 實驗 ESP32 + RFID  (Create Mode 建立資料模式/ Query Mode查詢資料模式) 







[{"id":"fc2bc05aaeb53a69","type":"ui_button","z":"7aebcf1d53039222","name":"","group":"fe68da70e8d7a3c8","order":21,"width":"4","height":"1","passthru":false,"label":"建立資料庫","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"建立資料庫","payloadType":"str","topic":"topic","topicType":"msg","x":90,"y":60,"wires":[["829a7127d56826ac","14eacaf6ab54d79d"]]},{"id":"4438df7c6a76e928","type":"ui_button","z":"7aebcf1d53039222","name":"","group":"fe68da70e8d7a3c8","order":45,"width":"4","height":"1","passthru":false,"label":"檢視資料","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"檢視資料","payloadType":"str","topic":"topic","topicType":"msg","x":80,"y":400,"wires":[["494b685c1733dc8f","9e276d6d151de3a9"]]},{"id":"36c87a2c69aedce2","type":"ui_button","z":"7aebcf1d53039222","name":"","group":"fe68da70e8d7a3c8","order":47,"width":"4","height":"1","passthru":false,"label":"刪除所有資料","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"刪除所有資料","payloadType":"str","topic":"topic","topicType":"msg","x":100,"y":520,"wires":[["f6ae9a1ac227ccf2","9e276d6d151de3a9"]]},{"id":"8252e64dda450261","type":"sqlite","z":"7aebcf1d53039222","mydb":"dd7f8134e015a1cb","sqlquery":"msg.topic","sql":"","name":"My_RFID_database","x":540,"y":60,"wires":[["168b6d52dfb43542"]]},{"id":"829a7127d56826ac","type":"function","z":"7aebcf1d53039222","name":"CREATE DATABASE","func":"\n\n//CREATE TABLE \"RFIDtable\" (\n//\t\"id\"\tINT NOT NULL,\n//  \"uidname\"  TEXT,\n//  \"currentdate\" DATE, \n//  \"currenttime\" TIME\n//\tPRIMARY KEY(\"id\")\n//);\nmsg.topic = \"CREATE TABLE RFIDtable(id INTEGER PRIMARY KEY AUTOINCREMENT, uidname TEXT, currentdate DATE, currenttime TIME)\";\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":60,"wires":[["8252e64dda450261"]]},{"id":"168b6d52dfb43542","type":"debug","z":"7aebcf1d53039222","name":"debug 97","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":740,"y":60,"wires":[]},{"id":"c0527e21b1adaf97","type":"function","z":"7aebcf1d53039222","name":"INSERT","func":"var Today = new Date();\nvar yyyy = Today.getFullYear(); //年\nvar MM = Today.getMonth()+1;    //月\nvar dd = Today.getDate();       //日\nvar h = Today.getHours();       //時\nvar m = Today.getMinutes();     //分\nvar s = Today.getSeconds();     //秒\nif(MM<10)\n{\n   MM = '0'+MM;\n}\n\nif(dd<10)\n{\n   dd = '0'+dd;\n}\n\nif(h<10)\n{\n   h = '0'+h;\n}\n\nif(m<10)\n{\n  m = '0' + m;\n}\n\nif(s<10)\n{\n  s = '0' + s;\n}\nvar var_date = yyyy+'/'+MM+'/'+dd;\nvar var_time = h+':'+m+':'+s;\n\nvar myRFID = flow.get('uid_temp');\n\n\nmsg.topic = \"INSERT INTO RFIDtable ( uidname , currentdate, currenttime ) VALUES ($myRFID,  $var_date ,  $var_time ) \" ;\nmsg.payload = [myRFID, var_date , var_time ]\nreturn msg;\n\n//CREATE TABLE \"RFIDtable\" (\n//\t\"id\"\tINT NOT NULL,\n//  \"uidname\"  TEXT,\n//  \"currentdate\" DATE, \n//  \"currenttime\" TIME\n//\tPRIMARY KEY(\"id\")\n//);","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":100,"wires":[["4717378946fa2f42","8252e64dda450261"]]},{"id":"f6ae9a1ac227ccf2","type":"function","z":"7aebcf1d53039222","name":"刪除所有資料","func":"//DELETE from RFIDtable\nmsg.topic = \"DELETE from RFIDtable\";\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":520,"wires":[["b8a824f5153c6fd8"]]},{"id":"b8a824f5153c6fd8","type":"sqlite","z":"7aebcf1d53039222","mydb":"dd7f8134e015a1cb","sqlquery":"msg.topic","sql":"","name":"My_RFID_database","x":540,"y":540,"wires":[["9339112b5f11b666"]]},{"id":"22d6aa6b9b59b1ef","type":"ui_button","z":"7aebcf1d53039222","name":"","group":"fe68da70e8d7a3c8","order":47,"width":"4","height":"1","passthru":false,"label":"刪除資料庫","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"刪除資料庫","payloadType":"str","topic":"topic","topicType":"msg","x":90,"y":560,"wires":[["a19483da5e58cbf4","9e276d6d151de3a9"]]},{"id":"a19483da5e58cbf4","type":"function","z":"7aebcf1d53039222","name":"刪除資料庫","func":"//DROP TABLE RFIDtable\nmsg.topic = \"DROP TABLE RFIDtable\";\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":560,"wires":[["b8a824f5153c6fd8"]]},{"id":"47110d4be5ce7045","type":"ui_template","z":"7aebcf1d53039222","group":"fe68da70e8d7a3c8","name":"","order":0,"width":"12","height":"10","format":"<table style=\"width:100%\">\n  <tr>\n    <th>idex</th> \n    <th>database_id</th>\n    <th>UID </th>\n    <th>Date</th>\n    <th>Times</th>\n  </tr>\n  \n  <tr ng-repeat=\"x in msg.payload | limitTo:50\">\n    <td>{{$index}}</td>\n    <td>{{msg.payload[$index].id}}</td>\n    <td>{{msg.payload[$index].uidname}}</td>\n    <td>{{msg.payload[$index].currentdate}}</td>\n    <td>{{msg.payload[$index].currenttime}}</td>\n  </tr>\n</table>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"local","className":"","x":780,"y":600,"wires":[[]]},{"id":"9339112b5f11b666","type":"debug","z":"7aebcf1d53039222","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":540,"wires":[]},{"id":"494b685c1733dc8f","type":"function","z":"7aebcf1d53039222","name":"檢視資料","func":"\n//CREATE TABLE \"RFIDtable\" (\n//\t\"id\"\tINT NOT NULL,\n//  \"uidname\"  TEXT,\n//  \"currentdate\" DATE, \n//  \"currenttime\" TIME\n//\tPRIMARY KEY(\"id\")\n//);\n\n//SELECT * FROM RFIDtable ORDER BY  id DESC LIMIT 50;\n\nmsg.topic = \"SELECT * FROM RFIDtable ORDER BY id DESC LIMIT 50\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":360,"wires":[["313ed708cbe77e86"]]},{"id":"313ed708cbe77e86","type":"sqlite","z":"7aebcf1d53039222","mydb":"dd7f8134e015a1cb","sqlquery":"msg.topic","sql":"","name":"My_RFID_database","x":540,"y":360,"wires":[["47110d4be5ce7045"]]},{"id":"95fbcdf86ed8fbcb","type":"function","z":"7aebcf1d53039222","name":"SELECT ALL","func":"var del_idtemp=msg.payload;\nflow.set(\"idtemp\", del_idtemp);\n\nmsg.topic = \"SELECT * FROM RFIDtable \";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":480,"wires":[["b8a824f5153c6fd8","f9cbb712b6ee61a0"]]},{"id":"a1677f0f8fd20351","type":"function","z":"7aebcf1d53039222","name":"確認 刪除","func":"var del_id = flow.get(\"idtemp\");\n\n\nmsg.topic = \"DELETE FROM RFIDtable WHERE id= ($del_id) \" ;\nmsg.payload = [del_id]\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":440,"wires":[["a696ffa0a72fbd77"]]},{"id":"6395bc41a36b7b4f","type":"ui_numeric","z":"7aebcf1d53039222","name":"","label":"刪除的database_id","tooltip":"","group":"fe68da70e8d7a3c8","order":30,"width":"4","height":"1","wrap":false,"passthru":true,"topic":"topic","topicType":"msg","format":"{{value}}","min":"1","max":"100","step":1,"className":"","x":110,"y":480,"wires":[["95fbcdf86ed8fbcb","9e276d6d151de3a9"]]},{"id":"a696ffa0a72fbd77","type":"sqlite","z":"7aebcf1d53039222","mydb":"dd7f8134e015a1cb","sqlquery":"msg.topic","sql":"DELETE FROM dhtreadings WHERE id =  VALUES ($theid)","name":"My_RFID_database","x":540,"y":440,"wires":[["494b685c1733dc8f"]]},{"id":"dd7e69860ea9730d","type":"ui_button","z":"7aebcf1d53039222","name":"","group":"fe68da70e8d7a3c8","order":44,"width":"4","height":"1","passthru":false,"label":"刪除一筆資料","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"刪除一筆資料","payloadType":"str","topic":"topic","topicType":"msg","x":100,"y":440,"wires":[["a1677f0f8fd20351","9e276d6d151de3a9"]]},{"id":"f9cbb712b6ee61a0","type":"debug","z":"7aebcf1d53039222","name":"debug 98","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":500,"y":480,"wires":[]},{"id":"caa123a28d1ba978","type":"mqtt in","z":"7aebcf1d53039222","name":"新增 RFID","topic":"alex9ufo/esp32/RFID","qos":"2","datatype":"auto-detect","broker":"841df58d.ee5e98","nl":false,"rap":true,"rh":0,"inputs":0,"x":80,"y":100,"wires":[["9a20eee86e8ad641"]]},{"id":"4717378946fa2f42","type":"debug","z":"7aebcf1d53039222","name":"debug 100","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":100,"wires":[]},{"id":"2dddad4e20242887","type":"ui_text_input","z":"7aebcf1d53039222","name":"","label":"新增一筆資料","tooltip":"","group":"fe68da70e8d7a3c8","order":7,"width":"4","height":"1","passthru":true,"mode":"text","delay":300,"topic":"topic","sendOnBlur":true,"className":"","topicType":"msg","x":320,"y":180,"wires":[[]]},{"id":"db8cbbfbf7372391","type":"ui_audio","z":"7aebcf1d53039222","name":"","group":"fe68da70e8d7a3c8","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":520,"y":220,"wires":[]},{"id":"b59d1f7d9c7476ae","type":"function","z":"7aebcf1d53039222","name":"function ","func":"var temp= msg.payload;\nmsg.payload= \"新增一筆資料\" + temp;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":220,"wires":[["db8cbbfbf7372391"]]},{"id":"9e276d6d151de3a9","type":"ui_audio","z":"7aebcf1d53039222","name":"","group":"fe68da70e8d7a3c8","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":300,"y":400,"wires":[]},{"id":"14eacaf6ab54d79d","type":"ui_audio","z":"7aebcf1d53039222","name":"","group":"fe68da70e8d7a3c8","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":260,"y":20,"wires":[]},{"id":"4fe73a3f32e32aae","type":"function","z":"7aebcf1d53039222","name":"function 28","func":"//SELECT trackid,name FROM \ttracks WHERE name LIKE 'Wild%'\n//Query\n//CREATE TABLE \"RFIDtable\" (\n//\t\"id\"\tINT NOT NULL,\n//  \"uidname\"  TEXT,\n//  \"currentdate\" DATE, \n//  \"currenttime\" TIME\n//\tPRIMARY KEY(\"id\")\n//);\n//msg.topic = \"DELETE FROM RFIDtable WHERE id= ($del_id) \" ;\n\nvar query_uid = flow.get(\"uidtemp\");\nmsg.topic = \"SELECT id,uidname , currentdate,currenttime FROM RFIDtable WHERE uidname LIKE ($query_uid) \";\nmsg.payload = [query_uid]\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":620,"wires":[["11db58eea02242d6","8cc3b2d4a2e0c2ad"]]},{"id":"dff10ddc84b7725b","type":"ui_button","z":"7aebcf1d53039222","name":"","group":"fe68da70e8d7a3c8","order":47,"width":"4","height":"1","passthru":false,"label":"比對資料庫","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"比對資料庫","payloadType":"str","topic":"topic","topicType":"msg","x":90,"y":620,"wires":[["4fe73a3f32e32aae","9e276d6d151de3a9"]]},{"id":"11db58eea02242d6","type":"sqlite","z":"7aebcf1d53039222","mydb":"dd7f8134e015a1cb","sqlquery":"msg.topic","sql":"","name":"My_RFID_database","x":520,"y":600,"wires":[["47110d4be5ce7045"]]},{"id":"a83116faa47d545b","type":"function","z":"7aebcf1d53039222","name":"SELECT ALL","func":"var query_uidtemp=msg.payload;\nflow.set(\"uidtemp\", query_uidtemp);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":300,"wires":[["e1bd2312d9d75c33","1f6cab4a3faa5a34","849285c6d862a825"]]},{"id":"0dcfe7e23ec3c8c6","type":"ui_text_input","z":"7aebcf1d53039222","name":"","label":"手動查詢資料的uidname","tooltip":"","group":"fe68da70e8d7a3c8","order":7,"width":"3","height":"1","passthru":true,"mode":"text","delay":300,"topic":"topic","sendOnBlur":true,"className":"","topicType":"msg","x":130,"y":320,"wires":[["a83116faa47d545b"]]},{"id":"76d47afb96854c79","type":"function","z":"7aebcf1d53039222","name":"function 29","func":"var query=msg.payload;\n\nif (query===true)\n    msg.payload='Query';\nelse\n    msg.payload='NotQuery';\n\nflow.set(\"query_temp\", msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":270,"y":720,"wires":[["ec89465107e38418","947c484be235b8d6"]]},{"id":"59050f8131bad219","type":"ui_switch","z":"7aebcf1d53039222","name":"","label":"比對模式","tooltip":"","group":"fe68da70e8d7a3c8","order":13,"width":"4","height":"1","passthru":true,"decouple":"false","topic":"topic","topicType":"msg","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"className":"","x":120,"y":720,"wires":[["76d47afb96854c79"]]},{"id":"ec89465107e38418","type":"debug","z":"7aebcf1d53039222","name":"debug 101","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":450,"y":680,"wires":[]},{"id":"ee77739695d43996","type":"switch","z":"7aebcf1d53039222","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Create","vt":"str"},{"t":"eq","v":"Query","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":110,"y":240,"wires":[["c0527e21b1adaf97","2dddad4e20242887","b59d1f7d9c7476ae","d460ec040d8dfba3"],["128f986570836ed5","2dddad4e20242887","dc12c41266f3357e"]]},{"id":"9a20eee86e8ad641","type":"function","z":"7aebcf1d53039222","name":"function ","func":"var query = flow.get(\"query_temp\");\n\nflow.set(\"uid_temp\", msg.payload);\n\nif (query==='NotQuery')\n    msg.payload='Create'\nelse\n    msg.payload='Query'\n    \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":100,"y":160,"wires":[["ee77739695d43996"]]},{"id":"d460ec040d8dfba3","type":"debug","z":"7aebcf1d53039222","name":"debug 102","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":310,"y":140,"wires":[]},{"id":"947c484be235b8d6","type":"ui_audio","z":"7aebcf1d53039222","name":"","group":"fe68da70e8d7a3c8","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":440,"y":720,"wires":[]},{"id":"8cc3b2d4a2e0c2ad","type":"debug","z":"7aebcf1d53039222","name":"debug 103","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":490,"y":640,"wires":[]},{"id":"e1bd2312d9d75c33","type":"debug","z":"7aebcf1d53039222","name":"debug 104","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":300,"wires":[]},{"id":"128f986570836ed5","type":"function","z":"7aebcf1d53039222","name":"function","func":"\nvar a= flow.get(\"uid_temp\");\nmsg.payload=a;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":260,"wires":[["a83116faa47d545b","db8cbbfbf7372391"]]},{"id":"1f6cab4a3faa5a34","type":"ui_text","z":"7aebcf1d53039222","group":"fe68da70e8d7a3c8","order":15,"width":"6","height":"1","name":"","label":"自動感應RFID查詢的uid","format":"{{msg.payload}}","layout":"row-left","className":"","x":690,"y":260,"wires":[]},{"id":"849285c6d862a825","type":"link out","z":"7aebcf1d53039222","name":"link out 6","mode":"link","links":[],"x":555,"y":320,"wires":[]},{"id":"ff0de732e7d26279","type":"link in","z":"7aebcf1d53039222","name":"link in 8","links":["dc12c41266f3357e"],"x":195,"y":660,"wires":[["4fe73a3f32e32aae"]]},{"id":"80fbdfa158d7bc38","type":"function","z":"7aebcf1d53039222","name":"function 30","func":"//CREATE TABLE \"RFIDtable\" (\n//\t\"id\"\tINT NOT NULL,\n//  \"uidname\"  TEXT,\n//  \"currentdate\" DATE, \n//  \"currenttime\" TIME\n//\tPRIMARY KEY(\"id\")\n//);\n\n//msg.topic = \"INSERT INTO RFIDtable ( uidname , currentdate, currenttime ) VALUES ($myRFID,  $var_date ,  $var_time ) \" ;\n//msg.payload = [myRFID, var_date , var_time ]\n//return msg;\n\n\nvar tmp=msg.payload;\nmsg.topic = \"select count( * ) as 總共有幾筆資料 from RFIDtable where uidname=($tmp)\";\nmsg.payload=[tmp];\nreturn msg;\n\n// select count( * ) as 總共有幾筆資料 from Customers where address='Japan'","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":800,"wires":[["ac8bdacba184f913","44ad8d06ee76a719"]]},{"id":"ac8bdacba184f913","type":"sqlite","z":"7aebcf1d53039222","mydb":"dd7f8134e015a1cb","sqlquery":"msg.topic","sql":"","name":"My_RFID_database","x":660,"y":820,"wires":[["6f50e1c744ecda81"]]},{"id":"499a0d6a5fb36a58","type":"ui_button","z":"7aebcf1d53039222","name":"","group":"fe68da70e8d7a3c8","order":47,"width":"4","height":"1","passthru":false,"label":"查詢資料庫筆數","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"查詢資料庫筆數","payloadType":"str","topic":"topic","topicType":"msg","x":100,"y":800,"wires":[["2fe6c9d03dc59da1","e6e485ec7f2a200b"]]},{"id":"2fe6c9d03dc59da1","type":"ui_audio","z":"7aebcf1d53039222","name":"","group":"fe68da70e8d7a3c8","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":260,"y":840,"wires":[]},{"id":"6f50e1c744ecda81","type":"function","z":"7aebcf1d53039222","name":"function 32","func":"var num=msg.payload[0].總共有幾筆資料;\nmsg.payload=num;\nreturn msg; \n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":730,"y":860,"wires":[["9c04fa31529ec812","54d5490f22326093"]]},{"id":"f741e97ad96ae0ba","type":"ui_text","z":"7aebcf1d53039222","group":"fe68da70e8d7a3c8","order":18,"width":"4","height":"1","name":"","label":"查詢結果","format":"<font face='arial'><font size=3><font color={{fcolor}}>{{msg.payload}}","layout":"row-left","className":"","x":1100,"y":920,"wires":[]},{"id":"faa59de5d672528c","type":"ui_text","z":"7aebcf1d53039222","group":"fe68da70e8d7a3c8","order":18,"width":"4","height":"1","name":"","label":"查詢結果","format":"<font face='arial'><font size=6><font color={{fcolor}}>{{msg.payload}}","layout":"row-left","className":"","x":1100,"y":800,"wires":[]},{"id":"6375700e7c087c21","type":"mqtt in","z":"7aebcf1d53039222","name":"","topic":"alex9ufo/esp32/Starting","qos":"2","datatype":"auto-detect","broker":"841df58d.ee5e98","nl":false,"rap":true,"rh":0,"inputs":0,"x":120,"y":880,"wires":[["19ae5b4724b21220","5cbe733362b0a2c1","c7750dce80132922"]]},{"id":"19ae5b4724b21220","type":"ui_audio","z":"7aebcf1d53039222","name":"","group":"fe68da70e8d7a3c8","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":340,"y":920,"wires":[]},{"id":"5cbe733362b0a2c1","type":"debug","z":"7aebcf1d53039222","name":"debug 105","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":350,"y":960,"wires":[]},{"id":"c7750dce80132922","type":"change","z":"7aebcf1d53039222","name":"","rules":[{"t":"set","p":"fcolor","pt":"msg","to":"red","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":880,"wires":[["037e07e24e48cbf2"]]},{"id":"037e07e24e48cbf2","type":"ui_text","z":"7aebcf1d53039222","group":"fe68da70e8d7a3c8","order":7,"width":"3","height":"1","name":"","label":"","format":"<font face='arial'><font size=4><font color={{fcolor}}>{{msg.payload}}","layout":"row-left","className":"","x":530,"y":900,"wires":[]},{"id":"44ad8d06ee76a719","type":"debug","z":"7aebcf1d53039222","name":"debug 106","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":780,"wires":[]},{"id":"9c04fa31529ec812","type":"change","z":"7aebcf1d53039222","name":"","rules":[{"t":"set","p":"fcolor","pt":"msg","to":"red","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":840,"wires":[["faa59de5d672528c","7ffccaea91f35248"]]},{"id":"cfea639fc377d148","type":"change","z":"7aebcf1d53039222","name":"","rules":[{"t":"set","p":"fcolor","pt":"msg","to":"red","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":940,"y":920,"wires":[["f741e97ad96ae0ba"]]},{"id":"e6e485ec7f2a200b","type":"function","z":"7aebcf1d53039222","name":"function","func":"\nvar a= flow.get(\"uid_temp\");\nmsg.payload=a;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":280,"y":800,"wires":[["80fbdfa158d7bc38"]]},{"id":"dc12c41266f3357e","type":"link out","z":"7aebcf1d53039222","name":"link out 7","mode":"link","links":["65ab9064522e3976","ff0de732e7d26279"],"x":195,"y":280,"wires":[]},{"id":"65ab9064522e3976","type":"link in","z":"7aebcf1d53039222","name":"link in 9","links":["dc12c41266f3357e"],"x":165,"y":760,"wires":[["e6e485ec7f2a200b"]]},{"id":"f2f92a741a2661bf","type":"inject","z":"7aebcf1d53039222","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"2","topic":"true","payload":"true","payloadType":"str","x":80,"y":680,"wires":[["59050f8131bad219"]]},{"id":"7deca06e980ec3c7","type":"ui_audio","z":"7aebcf1d53039222","name":"","group":"fe68da70e8d7a3c8","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":1060,"y":980,"wires":[]},{"id":"7ffccaea91f35248","type":"debug","z":"7aebcf1d53039222","name":"debug 107","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1110,"y":840,"wires":[]},{"id":"6ba9d7a1d8dc4696","type":"delay","z":"7aebcf1d53039222","name":"","pauseType":"delay","timeout":"3","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":920,"y":980,"wires":[["7deca06e980ec3c7"]]},{"id":"54d5490f22326093","type":"function","z":"7aebcf1d53039222","name":"function 34","func":"var n=msg.payload;\nif (n>0)\n    msg.payload='RFID符合';\nelse\n    msg.payload='RFID錯誤';\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":750,"y":940,"wires":[["6ba9d7a1d8dc4696","cfea639fc377d148"]]},{"id":"fe68da70e8d7a3c8","type":"ui_group","name":"2023 RFID","tab":"eeb8e179.a47a4","order":3,"disp":true,"width":"14","collapse":false,"className":""},{"id":"dd7f8134e015a1cb","type":"sqlitedb","db":"C:\\Users\\User\\.node-red\\my_2023rfid.db","mode":"RWC"},{"id":"841df58d.ee5e98","type":"mqtt-broker","name":"","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"eeb8e179.a47a4","type":"ui_tab","name":"RFID Database","icon":"dashboard","disabled":false,"hidden":false}]



#include <WiFi.h>
#include <PubSubClient.h>
#include <SPI.h>
#include <MFRC522.h>
// GPIO 2 D1
#define LED 2
/* Wiring RFID RC522 module
=============================================================================
GND     = GND   3.3V    = 3.3V
The following table shows the typical pin layout used:
 *             MFRC522      ESP32     Arduino       Arduino   Arduino    Arduino          Arduino
 *             Reader/PCD             Uno/101       Mega      Nano v3    Leonardo/Micro   Pro Micro
 * Signal      Pin          Pin       Pin           Pin       Pin        Pin              Pin
 * -----------------------------------------------------------------------------------------
 * RST/Reset   RST          GPIO27    9             5         D9         RESET/ICSP-5     RST
 * SPI SS      SDA(SS)      GPIO5     10            53        D10        10               10
 * SPI MOSI    MOSI         GPIO23    11 / ICSP-4   51        D11        ICSP-4           16
 * SPI MISO    MISO         GPIO19    12 / ICSP-1   50        D12        ICSP-1           14
 * SPI SCK     SCK          GPIO18    13 / ICSP-3   52        D13        ICSP-3           15
 *
[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
=============================================================================
*/

#define SS_PIN  5  // ESP32 pin GIOP5
#define RST_PIN 27 // ESP32 pin GIOP27


// WiFi
const char *ssid = "alex9ufo"; // Enter your Wi-Fi name
const char *password = "alex9981";  // Enter Wi-Fi password

// MQTT Broker
const char *mqtt_broker = "broker.mqtt-dashboard.com";
const char *topic1 = "alex9ufo/esp32/led";
const char *topic2 = "alex9ufo/esp32/RFID";
const char *topic = "alex9ufo/esp32/Starting";


const char *mqtt_username = "alex9ufo";
const char *mqtt_password = "public";
const int mqtt_port = 1883;

bool ledState = false;
bool atwork = false;

WiFiClient espClient;
PubSubClient client(espClient);
MFRC522 rfid(SS_PIN, RST_PIN); // Create MFRC522 instance

long lastMsg = 0;
long lastMsg1= 0;

char msg[50];
String json = "";    
char jsonChar1[100];  //client.publish("alex9ufo/Esp32/RFID"
//===========================================================
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);
      }
      return id;
    }
//===========================================================
void setup() {
    // Set software serial baud to 115200;
    Serial.begin(115200);
    delay(1000); // Delay for stability
    //======================================================
    // Connecting to a WiFi network
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.println("Connecting to WiFi...");
    }
    Serial.println("Connected to the WiFi network");

    // Setting LED pin as output
    pinMode(LED, OUTPUT);
    digitalWrite(LED, LOW);  // Turn off the LED initially
    //======================================================
    SPI.begin();           // Init SPI bus
    rfid.PCD_Init();    // Init MFRC522

    Serial.println(F("Ready!"));
    Serial.println(F("======================================================"));
    Serial.println("Tap an RFID/NFC tag on the RFID-RC522 reader");


    // Connecting to an MQTT broker
    client.setServer(mqtt_broker, mqtt_port);
    client.setCallback(callback);
    while (!client.connected()) {
        String client_id = "esp32-client-";
        client_id += String(WiFi.macAddress());
        Serial.printf("The client %s connects to the public MQTT broker\n", client_id.c_str());
        if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
            Serial.println("Public HiveMQ MQTT broker (broker.mqtt-dashboard.com) connected");
        } else {
            Serial.print("Failed with state ");
            Serial.print(client.state());
            delay(2000);
        }
    }

    // Publish and subscribe
    client.subscribe(topic1);
    client.publish(topic,"ESP32 at work");
}
//===========================================================
void callback(char *topic, byte *payload, unsigned int length) {
    Serial.print("Message arrived in topic: ");
    Serial.println(topic);
    Serial.print("Message: ");
    String message;
    for (int i = 0; i < length; i++) {
        message += (char) payload[i];  // Convert *byte to string
    }
    Serial.print(message);
    if (message == "on" && !ledState) {
        digitalWrite(LED, HIGH);  // Turn on the LED
        ledState = true;
    }
    if (message == "off" && ledState) {
        digitalWrite(LED, LOW); // Turn off the LED
        ledState = false;
    }
    Serial.println();
    Serial.println("-----------------------");
}
//===========================================================
void loop() {
    client.loop();
    delay(100); // Delay for a short period in each loop iteration
    long now = millis();
    long now1 = millis();
    if (now1 - lastMsg1 > 80000) {
        lastMsg1 = now1;
        if (atwork) {
            client.publish(topic,"ESP32 at work");
            atwork=false;
        } else {
            client.publish(topic,"             ");
            atwork=true;
        }
     }
     
    if (now - lastMsg > 5000) {
      Serial.println("Reading RFIF card");
      lastMsg = now;
      if (rfid.PICC_IsNewCardPresent()) { // new tag is available
        if (rfid.PICC_ReadCardSerial()) { // NUID has been readed
          MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
          Serial.print("RFID/NFC Tag Type: ");
          Serial.println(rfid.PICC_GetTypeName(piccType));

          // print UID in Serial Monitor in the hex format
          Serial.print("UID:");
          for (int i = 0; i < rfid.uid.size; i++) {
            Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
            Serial.print(rfid.uid.uidByte[i], HEX);
          }
          Serial.println();

          String json = printHex(rfid.uid.uidByte, rfid.uid.size);
          // Convert JSON string to character array
          json.toCharArray(jsonChar1, json.length()+1);
   
          if  (client.connected()) {
            Serial.print("Publish message: ");
            Serial.println(json);
            // Publish JSON character array to MQTT topic
            client.publish(topic2,jsonChar1);
          }    
          rfid.PICC_HaltA(); // halt PICC
          rfid.PCD_StopCrypto1(); // stop encryption on PCD
         
         
        }
      }
       
  }

}
//===========================================================

2023年7月14日 星期五

實驗 ESP32 RFID + Node-Red 新增資料 自動比對資料庫的資料 (比對模式: Query + Create)

 實驗 ESP32 RFID + Node-Red 新增資料 自動比對資料庫的資料 (比對模式: Query + Create)






[{"id":"fc2bc05aaeb53a69","type":"ui_button","z":"7aebcf1d53039222","name":"","group":"fe68da70e8d7a3c8","order":21,"width":"3","height":"1","passthru":false,"label":"建立資料庫","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"建立資料庫","payloadType":"str","topic":"topic","topicType":"msg","x":90,"y":60,"wires":[["829a7127d56826ac","14eacaf6ab54d79d"]]},{"id":"4438df7c6a76e928","type":"ui_button","z":"7aebcf1d53039222","name":"","group":"fe68da70e8d7a3c8","order":45,"width":"3","height":"1","passthru":false,"label":"檢視資料","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"檢視資料","payloadType":"str","topic":"topic","topicType":"msg","x":80,"y":380,"wires":[["494b685c1733dc8f","9e276d6d151de3a9"]]},{"id":"36c87a2c69aedce2","type":"ui_button","z":"7aebcf1d53039222","name":"","group":"fe68da70e8d7a3c8","order":47,"width":"3","height":"1","passthru":false,"label":"刪除所有資料","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"刪除所有資料","payloadType":"str","topic":"topic","topicType":"msg","x":100,"y":500,"wires":[["f6ae9a1ac227ccf2","9e276d6d151de3a9"]]},{"id":"8252e64dda450261","type":"sqlite","z":"7aebcf1d53039222","mydb":"dd7f8134e015a1cb","sqlquery":"msg.topic","sql":"","name":"My_RFID_database","x":540,"y":60,"wires":[["168b6d52dfb43542"]]},{"id":"829a7127d56826ac","type":"function","z":"7aebcf1d53039222","name":"CREATE DATABASE","func":"\n\n//CREATE TABLE \"RFIDtable\" (\n//\t\"id\"\tINT NOT NULL,\n//  \"uidname\"  TEXT,\n//  \"currentdate\" DATE, \n//  \"currenttime\" TIME\n//\tPRIMARY KEY(\"id\")\n//);\nmsg.topic = \"CREATE TABLE RFIDtable(id INTEGER PRIMARY KEY AUTOINCREMENT, uidname TEXT, currentdate DATE, currenttime TIME)\";\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":60,"wires":[["8252e64dda450261"]]},{"id":"168b6d52dfb43542","type":"debug","z":"7aebcf1d53039222","name":"debug 97","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":740,"y":60,"wires":[]},{"id":"c0527e21b1adaf97","type":"function","z":"7aebcf1d53039222","name":"INSERT","func":"var Today = new Date();\nvar yyyy = Today.getFullYear(); //年\nvar MM = Today.getMonth()+1;    //月\nvar dd = Today.getDate();       //日\nvar h = Today.getHours();       //時\nvar m = Today.getMinutes();     //分\nvar s = Today.getSeconds();     //秒\n\nvar var_date = yyyy+'/'+MM+'/'+dd;\nvar var_time = h+':'+m+':'+s;\n\nvar myRFID = flow.get('uid_temp');\n\n\nmsg.topic = \"INSERT INTO RFIDtable ( uidname , currentdate, currenttime ) VALUES ($myRFID,  $var_date ,  $var_time ) \" ;\nmsg.payload = [myRFID, var_date , var_time ]\nreturn msg;\n\n//CREATE TABLE \"RFIDtable\" (\n//\t\"id\"\tINT NOT NULL,\n//  \"uidname\"  TEXT,\n//  \"currentdate\" DATE, \n//  \"currenttime\" TIME\n//\tPRIMARY KEY(\"id\")\n//);","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":100,"wires":[["4717378946fa2f42","8252e64dda450261"]]},{"id":"f6ae9a1ac227ccf2","type":"function","z":"7aebcf1d53039222","name":"刪除所有資料","func":"//DELETE from RFIDtable\nmsg.topic = \"DELETE from RFIDtable\";\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":500,"wires":[["b8a824f5153c6fd8"]]},{"id":"b8a824f5153c6fd8","type":"sqlite","z":"7aebcf1d53039222","mydb":"dd7f8134e015a1cb","sqlquery":"msg.topic","sql":"","name":"My_RFID_database","x":540,"y":500,"wires":[["9339112b5f11b666"]]},{"id":"22d6aa6b9b59b1ef","type":"ui_button","z":"7aebcf1d53039222","name":"","group":"fe68da70e8d7a3c8","order":47,"width":"3","height":"1","passthru":false,"label":"刪除資料庫","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"刪除資料庫","payloadType":"str","topic":"topic","topicType":"msg","x":90,"y":540,"wires":[["a19483da5e58cbf4","9e276d6d151de3a9"]]},{"id":"a19483da5e58cbf4","type":"function","z":"7aebcf1d53039222","name":"刪除資料庫","func":"//DROP TABLE RFIDtable\nmsg.topic = \"DROP TABLE RFIDtable\";\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":540,"wires":[["b8a824f5153c6fd8"]]},{"id":"47110d4be5ce7045","type":"ui_template","z":"7aebcf1d53039222","group":"fe68da70e8d7a3c8","name":"","order":0,"width":"12","height":"10","format":"<table style=\"width:100%\">\n  <tr>\n    <th>idex</th> \n    <th>database_id</th>\n    <th>UID </th>\n    <th>Date</th>\n    <th>Times</th>\n  </tr>\n  \n  <tr ng-repeat=\"x in msg.payload | limitTo:50\">\n    <td>{{$index}}</td>\n    <td>{{msg.payload[$index].id}}</td>\n    <td>{{msg.payload[$index].uidname}}</td>\n    <td>{{msg.payload[$index].currentdate}}</td>\n    <td>{{msg.payload[$index].currenttime}}</td>\n  </tr>\n</table>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"local","className":"","x":780,"y":460,"wires":[[]]},{"id":"9339112b5f11b666","type":"debug","z":"7aebcf1d53039222","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":500,"wires":[]},{"id":"494b685c1733dc8f","type":"function","z":"7aebcf1d53039222","name":"檢視資料","func":"\n//CREATE TABLE \"RFIDtable\" (\n//\t\"id\"\tINT NOT NULL,\n//  \"uidname\"  TEXT,\n//  \"currentdate\" DATE, \n//  \"currenttime\" TIME\n//\tPRIMARY KEY(\"id\")\n//);\n\n//SELECT * FROM RFIDtable ORDER BY  id DESC LIMIT 50;\n\nmsg.topic = \"SELECT * FROM RFIDtable ORDER BY id DESC LIMIT 50\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":320,"wires":[["313ed708cbe77e86"]]},{"id":"313ed708cbe77e86","type":"sqlite","z":"7aebcf1d53039222","mydb":"dd7f8134e015a1cb","sqlquery":"msg.topic","sql":"","name":"My_RFID_database","x":540,"y":320,"wires":[["47110d4be5ce7045","1a261bc18a1f5965"]]},{"id":"95fbcdf86ed8fbcb","type":"function","z":"7aebcf1d53039222","name":"SELECT ALL","func":"var del_idtemp=msg.payload;\nflow.set(\"idtemp\", del_idtemp);\n\nmsg.topic = \"SELECT * FROM RFIDtable \";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":460,"wires":[["b8a824f5153c6fd8","f9cbb712b6ee61a0"]]},{"id":"a1677f0f8fd20351","type":"function","z":"7aebcf1d53039222","name":"確認 刪除","func":"var del_id = flow.get(\"idtemp\");\n\n\nmsg.topic = \"DELETE FROM RFIDtable WHERE id= ($del_id) \" ;\nmsg.payload = [del_id]\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":400,"wires":[["a696ffa0a72fbd77"]]},{"id":"6395bc41a36b7b4f","type":"ui_numeric","z":"7aebcf1d53039222","name":"","label":"刪除的database_id","tooltip":"","group":"fe68da70e8d7a3c8","order":30,"width":"3","height":"1","wrap":false,"passthru":true,"topic":"topic","topicType":"msg","format":"{{value}}","min":"1","max":"100","step":1,"className":"","x":110,"y":460,"wires":[["95fbcdf86ed8fbcb","9e276d6d151de3a9"]]},{"id":"a696ffa0a72fbd77","type":"sqlite","z":"7aebcf1d53039222","mydb":"dd7f8134e015a1cb","sqlquery":"msg.topic","sql":"DELETE FROM dhtreadings WHERE id =  VALUES ($theid)","name":"My_RFID_database","x":540,"y":400,"wires":[["1a261bc18a1f5965"]]},{"id":"1a261bc18a1f5965","type":"debug","z":"7aebcf1d53039222","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":790,"y":400,"wires":[]},{"id":"dd7e69860ea9730d","type":"ui_button","z":"7aebcf1d53039222","name":"","group":"fe68da70e8d7a3c8","order":44,"width":"3","height":"1","passthru":false,"label":"刪除一筆資料","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"刪除一筆資料","payloadType":"str","topic":"topic","topicType":"msg","x":100,"y":420,"wires":[["a1677f0f8fd20351","9e276d6d151de3a9"]]},{"id":"f9cbb712b6ee61a0","type":"debug","z":"7aebcf1d53039222","name":"debug 98","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":500,"y":460,"wires":[]},{"id":"caa123a28d1ba978","type":"mqtt in","z":"7aebcf1d53039222","name":"新增 RFID","topic":"alex9ufo/esp32/RFID","qos":"2","datatype":"auto-detect","broker":"841df58d.ee5e98","nl":false,"rap":true,"rh":0,"inputs":0,"x":80,"y":100,"wires":[["9a20eee86e8ad641"]]},{"id":"4717378946fa2f42","type":"debug","z":"7aebcf1d53039222","name":"debug 100","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":100,"wires":[]},{"id":"2dddad4e20242887","type":"ui_text_input","z":"7aebcf1d53039222","name":"","label":"新增一筆資料","tooltip":"","group":"fe68da70e8d7a3c8","order":7,"width":"3","height":"1","passthru":true,"mode":"text","delay":300,"topic":"topic","sendOnBlur":true,"className":"","topicType":"msg","x":320,"y":180,"wires":[[]]},{"id":"db8cbbfbf7372391","type":"ui_audio","z":"7aebcf1d53039222","name":"","group":"fe68da70e8d7a3c8","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":480,"y":220,"wires":[]},{"id":"b59d1f7d9c7476ae","type":"function","z":"7aebcf1d53039222","name":"function ","func":"var temp= msg.payload;\nmsg.payload= \"新增一筆資料\" + temp;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":220,"wires":[["db8cbbfbf7372391"]]},{"id":"9e276d6d151de3a9","type":"ui_audio","z":"7aebcf1d53039222","name":"","group":"fe68da70e8d7a3c8","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":300,"y":360,"wires":[]},{"id":"14eacaf6ab54d79d","type":"ui_audio","z":"7aebcf1d53039222","name":"","group":"fe68da70e8d7a3c8","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":260,"y":20,"wires":[]},{"id":"4fe73a3f32e32aae","type":"function","z":"7aebcf1d53039222","name":"function 28","func":"//SELECT trackid,name FROM \ttracks WHERE name LIKE 'Wild%'\n//Query\n//CREATE TABLE \"RFIDtable\" (\n//\t\"id\"\tINT NOT NULL,\n//  \"uidname\"  TEXT,\n//  \"currentdate\" DATE, \n//  \"currenttime\" TIME\n//\tPRIMARY KEY(\"id\")\n//);\n//msg.topic = \"DELETE FROM RFIDtable WHERE id= ($del_id) \" ;\n\nvar query_uid = flow.get(\"uidtemp\");\nmsg.topic = \"SELECT id,uidname , currentdate,currenttime FROM RFIDtable WHERE uidname LIKE ($query_uid) \";\nmsg.payload = [query_uid]\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":580,"wires":[["11db58eea02242d6","8cc3b2d4a2e0c2ad"]]},{"id":"dff10ddc84b7725b","type":"ui_button","z":"7aebcf1d53039222","name":"","group":"fe68da70e8d7a3c8","order":47,"width":"3","height":"1","passthru":false,"label":"比對資料庫","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"比對資料庫","payloadType":"str","topic":"topic","topicType":"msg","x":90,"y":580,"wires":[["4fe73a3f32e32aae","9e276d6d151de3a9"]]},{"id":"11db58eea02242d6","type":"sqlite","z":"7aebcf1d53039222","mydb":"dd7f8134e015a1cb","sqlquery":"msg.topic","sql":"","name":"My_RFID_database","x":540,"y":580,"wires":[["47110d4be5ce7045"]]},{"id":"a83116faa47d545b","type":"function","z":"7aebcf1d53039222","name":"SELECT ALL","func":"var query_uidtemp=msg.payload;\nflow.set(\"uidtemp\", query_uidtemp);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":280,"wires":[["e1bd2312d9d75c33","1f6cab4a3faa5a34","849285c6d862a825"]]},{"id":"0dcfe7e23ec3c8c6","type":"ui_text_input","z":"7aebcf1d53039222","name":"","label":"手動查詢資料的uidname","tooltip":"","group":"fe68da70e8d7a3c8","order":7,"width":"3","height":"1","passthru":true,"mode":"text","delay":300,"topic":"topic","sendOnBlur":true,"className":"","topicType":"msg","x":130,"y":300,"wires":[["a83116faa47d545b"]]},{"id":"76d47afb96854c79","type":"function","z":"7aebcf1d53039222","name":"function 29","func":"var query=msg.payload;\n\nif (query===true)\n    msg.payload='Query';\nelse\n    msg.payload='NotQuery';\n\nflow.set(\"query_temp\", msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":270,"y":640,"wires":[["ec89465107e38418","947c484be235b8d6"]]},{"id":"59050f8131bad219","type":"ui_switch","z":"7aebcf1d53039222","name":"","label":"比對模式","tooltip":"","group":"fe68da70e8d7a3c8","order":13,"width":"3","height":"1","passthru":true,"decouple":"false","topic":"topic","topicType":"msg","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"className":"","x":80,"y":640,"wires":[["76d47afb96854c79"]]},{"id":"ec89465107e38418","type":"debug","z":"7aebcf1d53039222","name":"debug 101","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":450,"y":640,"wires":[]},{"id":"ee77739695d43996","type":"switch","z":"7aebcf1d53039222","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Create","vt":"str"},{"t":"eq","v":"Query","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":110,"y":240,"wires":[["c0527e21b1adaf97","2dddad4e20242887","b59d1f7d9c7476ae","d460ec040d8dfba3"],["128f986570836ed5","2dddad4e20242887"]]},{"id":"9a20eee86e8ad641","type":"function","z":"7aebcf1d53039222","name":"function ","func":"var query = flow.get(\"query_temp\");\n\nflow.set(\"uid_temp\", msg.payload);\n\nif (query==='NotQuery')\n    msg.payload='Create'\nelse\n    msg.payload='Query'\n    \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":100,"y":160,"wires":[["ee77739695d43996"]]},{"id":"d460ec040d8dfba3","type":"debug","z":"7aebcf1d53039222","name":"debug 102","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":310,"y":140,"wires":[]},{"id":"947c484be235b8d6","type":"ui_audio","z":"7aebcf1d53039222","name":"","group":"fe68da70e8d7a3c8","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":440,"y":680,"wires":[]},{"id":"8cc3b2d4a2e0c2ad","type":"debug","z":"7aebcf1d53039222","name":"debug 103","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":620,"wires":[]},{"id":"e1bd2312d9d75c33","type":"debug","z":"7aebcf1d53039222","name":"debug 104","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":690,"y":280,"wires":[]},{"id":"128f986570836ed5","type":"function","z":"7aebcf1d53039222","name":"function","func":"\nvar a= flow.get(\"uid_temp\");\nmsg.payload=a;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":260,"wires":[["a83116faa47d545b","db8cbbfbf7372391"]]},{"id":"1f6cab4a3faa5a34","type":"ui_text","z":"7aebcf1d53039222","group":"fe68da70e8d7a3c8","order":15,"width":"6","height":"1","name":"","label":"自動感應RFID查詢的uid","format":"{{msg.payload}}","layout":"row-left","className":"","x":710,"y":220,"wires":[]},{"id":"849285c6d862a825","type":"link out","z":"7aebcf1d53039222","name":"link out 6","mode":"link","links":["ff0de732e7d26279"],"x":655,"y":180,"wires":[]},{"id":"ff0de732e7d26279","type":"link in","z":"7aebcf1d53039222","name":"link in 8","links":["849285c6d862a825"],"x":185,"y":620,"wires":[["4fe73a3f32e32aae"]]},{"id":"fe68da70e8d7a3c8","type":"ui_group","name":"2023 RFID","tab":"eeb8e179.a47a4","order":3,"disp":true,"width":"10","collapse":false,"className":""},{"id":"dd7f8134e015a1cb","type":"sqlitedb","db":"C:\\Users\\User\\.node-red\\my_2023rfid.db","mode":"RWC"},{"id":"841df58d.ee5e98","type":"mqtt-broker","name":"","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"eeb8e179.a47a4","type":"ui_tab","name":"RFID Database","icon":"dashboard","disabled":false,"hidden":false}]


#include <WiFi.h>
#include <PubSubClient.h>
#include <SPI.h>
#include <MFRC522.h>
// GPIO 2 D1
#define LED 2
/* Wiring RFID RC522 module
=============================================================================
GND     = GND   3.3V    = 3.3V
The following table shows the typical pin layout used:
 *             MFRC522      ESP32     Arduino       Arduino   Arduino    Arduino          Arduino
 *             Reader/PCD             Uno/101       Mega      Nano v3    Leonardo/Micro   Pro Micro
 * Signal      Pin          Pin       Pin           Pin       Pin        Pin              Pin
 * -----------------------------------------------------------------------------------------
 * RST/Reset   RST          GPIO27    9             5         D9         RESET/ICSP-5     RST
 * SPI SS      SDA(SS)      GPIO5     10            53        D10        10               10
 * SPI MOSI    MOSI         GPIO23    11 / ICSP-4   51        D11        ICSP-4           16
 * SPI MISO    MISO         GPIO19    12 / ICSP-1   50        D12        ICSP-1           14
 * SPI SCK     SCK          GPIO18    13 / ICSP-3   52        D13        ICSP-3           15
 *
[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
=============================================================================
*/

#define SS_PIN  5  // ESP32 pin GIOP5
#define RST_PIN 27 // ESP32 pin GIOP27


// WiFi
const char *ssid = "alex9ufo"; // Enter your Wi-Fi name
const char *password = "alex9981";  // Enter Wi-Fi password

// MQTT Broker
const char *mqtt_broker = "broker.mqtt-dashboard.com";
const char *topic1 = "alex9ufo/esp32/led";
const char *topic2 = "alex9ufo/esp32/RFID";

const char *mqtt_username = "alex9ufo";
const char *mqtt_password = "public";
const int mqtt_port = 1883;

bool ledState = false;

WiFiClient espClient;
PubSubClient client(espClient);
MFRC522 rfid(SS_PIN, RST_PIN); // Create MFRC522 instance

long lastMsg = 0;
char msg[50];
String json = "";    
char jsonChar1[100];  //client.publish("alex9ufo/Esp32/RFID"
//===========================================================
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);
      }
      return id;
    }
//===========================================================
void setup() {
    // Set software serial baud to 115200;
    Serial.begin(115200);
    delay(1000); // Delay for stability
    //======================================================
    // Connecting to a WiFi network
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.println("Connecting to WiFi...");
    }
    Serial.println("Connected to the WiFi network");

    // Setting LED pin as output
    pinMode(LED, OUTPUT);
    digitalWrite(LED, LOW);  // Turn off the LED initially
    //======================================================
    SPI.begin();           // Init SPI bus
    rfid.PCD_Init();    // Init MFRC522

    Serial.println(F("Ready!"));
    Serial.println(F("======================================================"));
    Serial.println("Tap an RFID/NFC tag on the RFID-RC522 reader");


    // Connecting to an MQTT broker
    client.setServer(mqtt_broker, mqtt_port);
    client.setCallback(callback);
    while (!client.connected()) {
        String client_id = "esp32-client-";
        client_id += String(WiFi.macAddress());
        Serial.printf("The client %s connects to the public MQTT broker\n", client_id.c_str());
        if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
            Serial.println("Public HiveMQ MQTT broker (broker.mqtt-dashboard.com) connected");
        } else {
            Serial.print("Failed with state ");
            Serial.print(client.state());
            delay(2000);
        }
    }

    // Publish and subscribe
    client.subscribe(topic1);

}
//===========================================================
void callback(char *topic, byte *payload, unsigned int length) {
    Serial.print("Message arrived in topic: ");
    Serial.println(topic);
    Serial.print("Message: ");
    String message;
    for (int i = 0; i < length; i++) {
        message += (char) payload[i];  // Convert *byte to string
    }
    Serial.print(message);
    if (message == "on" && !ledState) {
        digitalWrite(LED, HIGH);  // Turn on the LED
        ledState = true;
    }
    if (message == "off" && ledState) {
        digitalWrite(LED, LOW); // Turn off the LED
        ledState = false;
    }
    Serial.println();
    Serial.println("-----------------------");
}
//===========================================================
void loop() {
    client.loop();
    delay(100); // Delay for a short period in each loop iteration
    long now = millis();
    if (now - lastMsg > 5000) {
      Serial.println("Reading RFIF card");
      lastMsg = now;
      if (rfid.PICC_IsNewCardPresent()) { // new tag is available
        if (rfid.PICC_ReadCardSerial()) { // NUID has been readed
          MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
          Serial.print("RFID/NFC Tag Type: ");
          Serial.println(rfid.PICC_GetTypeName(piccType));

          // print UID in Serial Monitor in the hex format
          Serial.print("UID:");
          for (int i = 0; i < rfid.uid.size; i++) {
            Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
            Serial.print(rfid.uid.uidByte[i], HEX);
          }
          Serial.println();

          String json = printHex(rfid.uid.uidByte, rfid.uid.size);
          // Convert JSON string to character array
          json.toCharArray(jsonChar1, json.length()+1);
   
          if  (client.connected()) {
            Serial.print("Publish message: ");
            Serial.println(json);
            // Publish JSON character array to MQTT topic
            client.publish(topic2,jsonChar1);
          }    
          rfid.PICC_HaltA(); // halt PICC
          rfid.PCD_StopCrypto1(); // stop encryption on PCD

        }
      }
       
  }

}
//===========================================================

Node-Red --> MQTT --> Fuxa

Node-Red --> MQTT --> Fuxa      FUXA(一個開源的 Web HMI / SCADA 自動化監控軟體)的專案設定檔 。 這份設定檔完整定義了 HMI 監控畫面的 後端通訊(MQTT 連線、點位標籤) 與 前端網頁圖形介面(SVG 畫布...