2024年1月30日 星期二

2024作業2-2 ESP32 + RFID + (Node-Red & SQLite ) + Line Notify

2024作業2-2 ESP32 + RFID  + (Node-Red & SQLite ) + Line Notify 



(Arduino  ,  Node-Red  程式中的 alex9ufo 全部改成自己的英文命名)






使用Node-Red 第一次一定要先建立資料庫 不然看不到資料

資料庫的目錄位置查sqlite節點

每一個sqlite節點都要修改成自己的目錄, alex9ufo MQTT Topic都要修改成自己的命名

 




arduino_secrets.h 裡面的內容

#define SECRET_SSID ""
#define SECRET_PASS ""




Arduino 程式需要 arduino_secrets.h 

// Wifi 與 MQttClient 程式庫
#include <ArduinoMqttClient.h>
#include <WiFi.h>
#include "arduino_secrets.h"

Node-Red 程式畫面



新增 RFID 卡片 UID

刪除一筆資料



刪除資料庫 與  建立資料庫


自動比對模式 (查詢模式)


手動比對模式 (查詢模式)



自動比對模式 / 手動比對模式 ==>查不到資料

Line Notify  (需改成自己的權杖 不然我會收到各位的Line)

msg.headers = {'content-type':'application/x-www-form-urlencoded','Authorization':'Bearer A4wwPNh2WqB7dlfeQyyIAwtggn1kfZSI5LkkCdia1gB'};
msg.payload = {"message":msg.payload};
return msg;

(Set Line API function內)
我的權杖A4wwPNh2WqB7dlfeQyyIAwtggn1kfZSI5LkkCdia1gB
需改成自己的權杖




Line 權杖的取得

https://alex9ufoexploer.blogspot.com/2020/10/line-notify.html

Node-Red 程式

[{"id":"c8fa9aba54ec59a7","type":"comment","z":"e580410840f1977a","name":"TABLE  RFIDtable","info":"\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","x":100,"y":40,"wires":[]},{"id":"2352757b9ad7a413","type":"comment","z":"e580410840f1977a","name":"資料庫位置 C:\\Users\\User\\.node-red\\EX2_1.db","info":"","x":200,"y":80,"wires":[]},{"id":"17d9e4c5934e31b7","type":"ui_button","z":"e580410840f1977a","name":"","group":"a453c7f575918a4b","order":5,"width":3,"height":1,"passthru":false,"label":"建立資料庫","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"建立資料庫","payloadType":"str","topic":"topic","topicType":"msg","x":90,"y":160,"wires":[["cec32cf0368a6160","428e36df65bc5d0a"]]},{"id":"3d724916bd0ceba4","type":"ui_button","z":"e580410840f1977a","name":"","group":"a453c7f575918a4b","order":7,"width":6,"height":2,"passthru":false,"label":"檢視資料庫資料","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"檢視資料","payloadType":"str","topic":"topic","topicType":"msg","x":100,"y":600,"wires":[["e8144d7ac36b0990","0dd203ac6f82de67","9546f9f1689f5abd"]]},{"id":"27863b993a3145c5","type":"ui_button","z":"e580410840f1977a","name":"","group":"a453c7f575918a4b","order":4,"width":3,"height":1,"passthru":false,"label":"刪除所有資料","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"刪除所有資料","payloadType":"str","topic":"topic","topicType":"msg","x":100,"y":860,"wires":[["f61681d67b6dfe6a","e95a213155e2ae2a"]]},{"id":"cec32cf0368a6160","type":"function","z":"e580410840f1977a","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":280,"y":160,"wires":[["2e873ff2215f1627"]]},{"id":"7c7b9e7101f99220","type":"debug","z":"e580410840f1977a","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":890,"y":200,"wires":[]},{"id":"e910514ceb4ba7e4","type":"function","z":"e580410840f1977a","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":460,"y":240,"wires":[["aceead06c5b4ca45","86a04561d5732afb"]]},{"id":"348309a2df49a7ef","type":"function","z":"e580410840f1977a","name":"刪除所有資料","func":"//DELETE from RFIDtable\nmsg.topic = \"DELETE from RFIDtable\";\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":560,"y":880,"wires":[["e6c4b8d95973acc8"]]},{"id":"833f20c5fc9f52d1","type":"ui_button","z":"e580410840f1977a","name":"","group":"a453c7f575918a4b","order":3,"width":3,"height":1,"passthru":false,"label":"刪除資料庫","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"刪除資料庫","payloadType":"str","topic":"topic","topicType":"msg","x":90,"y":960,"wires":[["1a152f4111ca81fd","fde22b8219ffe519"]]},{"id":"497862d022c85886","type":"function","z":"e580410840f1977a","name":"刪除資料庫","func":"//DROP TABLE RFIDtable\nmsg.topic = \"DROP TABLE RFIDtable\";\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":590,"y":960,"wires":[["ac223b4659d0d7a2"]]},{"id":"e89357f8bf99cd44","type":"debug","z":"e580410840f1977a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":890,"y":940,"wires":[]},{"id":"9546f9f1689f5abd","type":"function","z":"e580410840f1977a","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":740,"y":600,"wires":[["8caa729f53e3c29e","db51b6b49c4a1679"]]},{"id":"3d80f359772cdc4d","type":"function","z":"e580410840f1977a","name":"SELECT ALL","func":"var del_idtemp=msg.payload;\nflow.set(\"idtemp\", del_idtemp);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":800,"wires":[["d5f8a1e57c898a43"]]},{"id":"a1fd876403c87858","type":"function","z":"e580410840f1977a","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":540,"y":740,"wires":[["2d75f8fea620282a"]]},{"id":"e4f6b192711dfd88","type":"ui_numeric","z":"e580410840f1977a","name":"","label":"刪除的database_id","tooltip":"","group":"cb0bc005d4b7ba13","order":7,"width":6,"height":1,"wrap":true,"passthru":true,"topic":"topic","topicType":"msg","format":"{{value}}","min":"1","max":"1000","step":1,"className":"","x":110,"y":800,"wires":[["3d80f359772cdc4d"]]},{"id":"4a31b56857469cb3","type":"ui_button","z":"e580410840f1977a","name":"","group":"a453c7f575918a4b","order":2,"width":3,"height":1,"passthru":false,"label":"刪除一筆資料","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"刪除一筆資料","payloadType":"str","topic":"topic","topicType":"msg","x":100,"y":660,"wires":[["e8144d7ac36b0990","4c4db109522eac71"]]},{"id":"d5f8a1e57c898a43","type":"debug","z":"e580410840f1977a","name":"debug 220","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":490,"y":800,"wires":[]},{"id":"11c055356daf7399","type":"mqtt in","z":"e580410840f1977a","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":240,"wires":[["37adfdab4179c6ec","da9dbbae9a283e86"]]},{"id":"8367becc9030c688","type":"ui_audio","z":"e580410840f1977a","name":"","group":"26e583680376a2a5","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":540,"y":400,"wires":[]},{"id":"7c2bb0d4fded7582","type":"function","z":"e580410840f1977a","name":"function ","func":"var temp= msg.payload;\nmsg.payload= \"新增一筆資料\" + temp;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":320,"wires":[["8367becc9030c688","9b70f6dff006576b","9a309750a2b99426"]]},{"id":"e8144d7ac36b0990","type":"ui_audio","z":"e580410840f1977a","name":"","group":"26e583680376a2a5","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":300,"y":640,"wires":[]},{"id":"428e36df65bc5d0a","type":"ui_audio","z":"e580410840f1977a","name":"","group":"26e583680376a2a5","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":240,"y":120,"wires":[]},{"id":"7096b1af0ab0453a","type":"function","z":"e580410840f1977a","name":"比對 function","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":330,"y":1040,"wires":[["0f892898e5714cfd"]]},{"id":"cc68c73eaae49094","type":"ui_button","z":"e580410840f1977a","name":"","group":"a453c7f575918a4b","order":1,"width":3,"height":1,"passthru":false,"label":"比對資料庫","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"比對資料庫","payloadType":"str","topic":"topic","topicType":"msg","x":90,"y":1040,"wires":[["7096b1af0ab0453a","1a152f4111ca81fd","58c96606c91c99d4"]]},{"id":"5ff1d4ea2ba3eb45","type":"function","z":"e580410840f1977a","name":"SELECT ALL","func":"var query_uidtemp=msg.payload;\nflow.set(\"uidtemp\", query_uidtemp);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":440,"wires":[["8beff873e7fdd27c","7dc7477636c7404f"]]},{"id":"5544fea4843e4855","type":"ui_text_input","z":"e580410840f1977a","name":"","label":"手動查詢資料的uidname","tooltip":"","group":"cb0bc005d4b7ba13","order":4,"width":3,"height":1,"passthru":true,"mode":"text","delay":300,"topic":"topic","sendOnBlur":true,"className":"","topicType":"msg","x":330,"y":500,"wires":[["5ff1d4ea2ba3eb45"]]},{"id":"b2fe54510f211974","type":"function","z":"e580410840f1977a","name":"function ","func":"var query=msg.payload;\n\nif (query== '1' )\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":540,"y":1460,"wires":[["6c6b07341575977b","af59cbe0eeea4671"]]},{"id":"8b510792cab8c4c8","type":"ui_switch","z":"e580410840f1977a","name":"","label":"新增模式  /自動比對模式 ","tooltip":"","group":"cb0bc005d4b7ba13","order":1,"width":4,"height":1,"passthru":true,"decouple":"false","topic":"s1","topicType":"str","style":"","onvalue":"1","onvalueType":"str","onicon":"","oncolor":"","offvalue":"0","offvalueType":"str","officon":"","offcolor":"","animate":false,"className":"","x":270,"y":1460,"wires":[["b2fe54510f211974","a334729b99de6386","9a3042cad990b6e4"]]},{"id":"6c6b07341575977b","type":"debug","z":"e580410840f1977a","name":"debug 221","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":690,"y":1440,"wires":[]},{"id":"573df0b38289efb2","type":"switch","z":"e580410840f1977a","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":130,"y":360,"wires":[["e910514ceb4ba7e4","7c2bb0d4fded7582"],["e73e704ab4e766ad"]]},{"id":"37adfdab4179c6ec","type":"function","z":"e580410840f1977a","name":"Query or Insert ","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":240,"y":240,"wires":[["573df0b38289efb2"]]},{"id":"af59cbe0eeea4671","type":"ui_audio","z":"e580410840f1977a","name":"","group":"26e583680376a2a5","voice":"Google US English","always":true,"x":680,"y":1480,"wires":[]},{"id":"8beff873e7fdd27c","type":"debug","z":"e580410840f1977a","name":"debug 222","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":750,"y":460,"wires":[]},{"id":"7dc7477636c7404f","type":"ui_text","z":"e580410840f1977a","group":"cb0bc005d4b7ba13","order":8,"width":6,"height":1,"name":"","label":"自動感應RFID查詢的uid","format":"{{msg.payload}}","layout":"row-left","className":"","x":790,"y":360,"wires":[]},{"id":"eb435a5d96be0c13","type":"function","z":"e580410840f1977a","name":"function ","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":420,"y":1180,"wires":[["f815556e7bd20e4c"]]},{"id":"4ac1e40f5421fd71","type":"function","z":"e580410840f1977a","name":"function ","func":"var num=msg.payload[0].總共有幾筆資料;\nmsg.payload=num;\nreturn msg; \n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":700,"y":1180,"wires":[["9136307096e69bad","6206a4be4bb20775"]]},{"id":"0fa86f77787f9532","type":"ui_text","z":"e580410840f1977a","group":"cb0bc005d4b7ba13","order":6,"width":3,"height":1,"name":"","label":"查詢結果","format":"<font face='arial'><font size=3><font color={{fcolor}}>{{msg.payload}}","layout":"row-left","className":"","x":1040,"y":1240,"wires":[]},{"id":"cc84c8e5de754878","type":"ui_text","z":"e580410840f1977a","group":"cb0bc005d4b7ba13","order":5,"width":3,"height":1,"name":"","label":"查詢結果:筆數","format":"<font face='arial'><font size=6><font color={{fcolor}}>{{msg.payload}}","layout":"row-left","className":"","x":1060,"y":1180,"wires":[]},{"id":"edced61c20b76390","type":"mqtt in","z":"e580410840f1977a","name":"","topic":"alex9ufo/esp32/Starting","qos":"2","datatype":"auto-detect","broker":"841df58d.ee5e98","nl":false,"rap":true,"rh":0,"inputs":0,"x":120,"y":1300,"wires":[["693ab072c6384c5a","313bba70c138c36a"]]},{"id":"693ab072c6384c5a","type":"ui_audio","z":"e580410840f1977a","name":"","group":"26e583680376a2a5","voice":"Google US English","always":false,"x":320,"y":1340,"wires":[]},{"id":"9136307096e69bad","type":"change","z":"e580410840f1977a","name":"","rules":[{"t":"set","p":"fcolor","pt":"msg","to":"red","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":880,"y":1180,"wires":[["cc84c8e5de754878"]]},{"id":"069bd252f7e422bd","type":"change","z":"e580410840f1977a","name":"","rules":[{"t":"set","p":"fcolor","pt":"msg","to":"red","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":880,"y":1240,"wires":[["0fa86f77787f9532"]]},{"id":"58c96606c91c99d4","type":"function","z":"e580410840f1977a","name":"function","func":"\nvar a= flow.get(\"uidtemp\");\nmsg.payload=a;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":280,"y":1180,"wires":[["eb435a5d96be0c13"]]},{"id":"297871396face2a1","type":"ui_audio","z":"e580410840f1977a","name":"","group":"26e583680376a2a5","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":1040,"y":1280,"wires":[]},{"id":"ceac1b14fad49384","type":"delay","z":"e580410840f1977a","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":880,"y":1280,"wires":[["297871396face2a1"]]},{"id":"6206a4be4bb20775","type":"function","z":"e580410840f1977a","name":"function ","func":"var n=msg.payload;\n\nif (n>0)\n    msg.payload='RFID符合';\nelse\n    msg.payload='RFID錯誤';\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":700,"y":1240,"wires":[["ceac1b14fad49384","069bd252f7e422bd","b7aa65168b6f133d"]]},{"id":"9b70f6dff006576b","type":"function","z":"e580410840f1977a","name":"取得UID號碼","func":"var myRFID = flow.get('uid_temp');\nmsg.payload=myRFID;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":360,"wires":[["7dc7477636c7404f"]]},{"id":"1a152f4111ca81fd","type":"ui_audio","z":"e580410840f1977a","name":"","group":"26e583680376a2a5","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":240,"y":1000,"wires":[]},{"id":"f61681d67b6dfe6a","type":"ui_audio","z":"e580410840f1977a","name":"","group":"26e583680376a2a5","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":300,"y":840,"wires":[]},{"id":"aceead06c5b4ca45","type":"function","z":"e580410840f1977a","name":"增加 日期 時間","func":"var ms1=msg.payload[0];\nvar ms2=msg.payload[1];\nvar ms3=msg.payload[2];\n\nmsg.payload=\"新增一筆:\"+ms1+\", 日期: \"+ms2+\", 時間:\"+ms3;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":620,"y":280,"wires":[["193dbe1b059eeebb"]]},{"id":"193dbe1b059eeebb","type":"function","z":"e580410840f1977a","name":"Set Line API ","func":"msg.headers = {'content-type':'application/x-www-form-urlencoded','Authorization':'Bearer A4wwPNh2WqB7dlfeQyyIAwtggn1kfZSI5LkkCdia1gB'};\nmsg.payload = {\"message\":msg.payload};\nreturn msg;\n\n//oR7KdXvK1eobRr2sRRgsl4PMq23DjDlhfUs96SyUBZu","outputs":1,"noerr":0,"x":790,"y":280,"wires":[["c8ba1a37a7d21f76"]]},{"id":"c8ba1a37a7d21f76","type":"http request","z":"e580410840f1977a","name":"","method":"POST","ret":"txt","paytoqs":false,"url":"https://notify-api.line.me/api/notify","tls":"","persist":false,"proxy":"","authType":"","x":940,"y":280,"wires":[["4b0f1f7f15c069df"]]},{"id":"4b0f1f7f15c069df","type":"debug","z":"e580410840f1977a","name":"debug 225","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1090,"y":280,"wires":[]},{"id":"4473e94ad686cd24","type":"comment","z":"e580410840f1977a","name":"Line Notify Message ","info":"","x":810,"y":320,"wires":[]},{"id":"45da028e5ff2187b","type":"function","z":"e580410840f1977a","name":"Set Line API ","func":"msg.headers = {'content-type':'application/x-www-form-urlencoded','Authorization':'Bearer A4wwPNh2WqB7dlfeQyyIAwtggn1kfZSI5LkkCdia1gB'};\nmsg.payload = {\"message\":msg.payload};\nreturn msg;\n\n//oR7KdXvK1eobRr2sRRgsl4PMq23DjDlhfUs96SyUBZu","outputs":1,"noerr":0,"x":870,"y":1380,"wires":[["18fc9ffb4c17305c"]]},{"id":"5981d7ab159d482c","type":"comment","z":"e580410840f1977a","name":"Line Notify Message ","info":"","x":1050,"y":1420,"wires":[]},{"id":"18fc9ffb4c17305c","type":"http request","z":"e580410840f1977a","name":"","method":"POST","ret":"txt","paytoqs":false,"url":"https://notify-api.line.me/api/notify","tls":"","persist":false,"proxy":"","authType":"","x":1020,"y":1380,"wires":[["bfbd0054f0f82240"]]},{"id":"bfbd0054f0f82240","type":"debug","z":"e580410840f1977a","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1150,"y":1380,"wires":[]},{"id":"b7aa65168b6f133d","type":"function","z":"e580410840f1977a","name":"function","func":"\nvar a= flow.get(\"uidtemp\");\nvar b=msg.payload;\nmsg.payload=a+\"--->\"+b;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":700,"y":1340,"wires":[["45da028e5ff2187b","ec7c95943d3205b6"]]},{"id":"b821a2b279a4ae38","type":"ui_table","z":"e580410840f1977a","group":"26e583680376a2a5","name":"","order":1,"width":12,"height":11,"columns":[],"outputs":0,"cts":false,"x":1050,"y":600,"wires":[]},{"id":"70b242b47377c8fb","type":"inject","z":"e580410840f1977a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"1","topic":"","payload":"0","payloadType":"str","x":90,"y":1460,"wires":[["8b510792cab8c4c8"]]},{"id":"a334729b99de6386","type":"change","z":"e580410840f1977a","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"1","fromt":"num","to":"查詢模式","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"0","fromt":"num","to":"新增模式","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":1600,"wires":[["9f609962e464cb8c","960b814e224e0c5d"]]},{"id":"9f609962e464cb8c","type":"ui_text_input","z":"e580410840f1977a","name":"","label":"State:","tooltip":"","group":"cb0bc005d4b7ba13","order":3,"width":3,"height":1,"passthru":true,"mode":"text","delay":300,"topic":"","sendOnBlur":true,"className":"","topicType":"str","x":530,"y":1620,"wires":[[]]},{"id":"9a3042cad990b6e4","type":"debug","z":"e580410840f1977a","name":"debug ","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":530,"y":1520,"wires":[]},{"id":"53e87d564850e3d0","type":"inject","z":"e580410840f1977a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"1","topic":"","payload":"","payloadType":"date","x":480,"y":80,"wires":[["059e64992e70beee"]]},{"id":"059e64992e70beee","type":"function","z":"e580410840f1977a","name":"id SET ","func":"var del_idtemp=1;\nflow.set(\"idtemp\", del_idtemp);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":610,"y":80,"wires":[[]]},{"id":"313bba70c138c36a","type":"ui_toast","z":"e580410840f1977a","position":"top right","displayTime":"3","highlight":"","sendall":true,"outputs":0,"ok":"OK","cancel":"Cancel","raw":false,"className":"","topic":"","name":"","x":350,"y":1300,"wires":[]},{"id":"960b814e224e0c5d","type":"ui_toast","z":"e580410840f1977a","position":"top right","displayTime":"3","highlight":"","sendall":true,"outputs":0,"ok":"OK","cancel":"Cancel","raw":false,"className":"","topic":"","name":"","x":570,"y":1580,"wires":[]},{"id":"e95a213155e2ae2a","type":"ui_toast","z":"e580410840f1977a","position":"prompt","displayTime":"3","highlight":"","sendall":true,"outputs":1,"ok":"OK","cancel":"Cancel","raw":true,"className":"","topic":"","name":"","x":210,"y":900,"wires":[["2cdf9575d5c3ce4d"]]},{"id":"2cdf9575d5c3ce4d","type":"function","z":"e580410840f1977a","name":"OK or Cancel","func":"var topic=msg.payload;\nif (topic==\"\"){\n    return [msg,null];\n    \n}\nif (topic==\"Cancel\"){\n    return [null,msg];\n    \n}\nreturn msg;","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":900,"wires":[["348309a2df49a7ef"],[]]},{"id":"fde22b8219ffe519","type":"ui_toast","z":"e580410840f1977a","position":"prompt","displayTime":"3","highlight":"","sendall":true,"outputs":1,"ok":"OK","cancel":"Cancel","raw":true,"className":"","topic":"","name":"","x":250,"y":960,"wires":[["5c5e97885157f4b4"]]},{"id":"5c5e97885157f4b4","type":"function","z":"e580410840f1977a","name":"OK or Cancel","func":"var topic=msg.payload;\nif (topic==\"\"){\n    return [msg,null];\n    \n}\nif (topic==\"Cancel\"){\n    return [null,msg];\n    \n}\nreturn msg;","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":960,"wires":[["497862d022c85886"],[]]},{"id":"4c4db109522eac71","type":"ui_toast","z":"e580410840f1977a","position":"prompt","displayTime":"3","highlight":"","sendall":true,"outputs":1,"ok":"OK","cancel":"Cancel","raw":true,"className":"","topic":"","name":"","x":210,"y":740,"wires":[["29635cb1252e1e7b"]]},{"id":"29635cb1252e1e7b","type":"function","z":"e580410840f1977a","name":"OK or Cancel","func":"var topic=msg.payload;\nif (topic==\"\"){\n    return [msg,null];\n    \n}\nif (topic==\"Cancel\"){\n    return [null,msg];\n    \n}\nreturn msg;","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":740,"wires":[["a1fd876403c87858"],[]]},{"id":"71e3c9e772097c69","type":"link out","z":"e580410840f1977a","name":"link out 41","mode":"link","links":["2b440998c35377bf"],"x":845,"y":880,"wires":[]},{"id":"2b440998c35377bf","type":"link in","z":"e580410840f1977a","name":"link in 39","links":["71e3c9e772097c69","e4de9ca1fd6f1ad2","70487decb239f3c4","ce142a687d0b79f1"],"x":635,"y":560,"wires":[["9546f9f1689f5abd"]]},{"id":"e4de9ca1fd6f1ad2","type":"link out","z":"e580410840f1977a","name":"link out 43","mode":"link","links":["2b440998c35377bf"],"x":855,"y":240,"wires":[]},{"id":"ecdb23d852e3fa7f","type":"ui_toast","z":"e580410840f1977a","position":"top right","displayTime":"3","highlight":"","sendall":true,"outputs":0,"ok":"OK","cancel":"Cancel","raw":false,"className":"","topic":"","name":"","x":1070,"y":1340,"wires":[]},{"id":"0dd203ac6f82de67","type":"ui_toast","z":"e580410840f1977a","position":"top right","displayTime":"3","highlight":"","sendall":true,"outputs":0,"ok":"OK","cancel":"Cancel","raw":false,"className":"","topic":"","name":"","x":310,"y":560,"wires":[]},{"id":"9a309750a2b99426","type":"ui_toast","z":"e580410840f1977a","position":"top right","displayTime":"3","highlight":"","sendall":true,"outputs":0,"ok":"OK","cancel":"Cancel","raw":false,"className":"","topic":"","name":"","x":570,"y":320,"wires":[]},{"id":"136b7f887c7ac8af","type":"debug","z":"e580410840f1977a","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":160,"wires":[]},{"id":"e73e704ab4e766ad","type":"function","z":"e580410840f1977a","name":"get UID","func":"\nvar a= flow.get(\"uid_temp\");\nmsg.payload=a;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":440,"wires":[["5ff1d4ea2ba3eb45","330ac65349eea739"]]},{"id":"ec7c95943d3205b6","type":"function","z":"e580410840f1977a","name":"function","func":"var b=msg.payload;\nmsg.payload=\"Line Notify --->\"+b;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":880,"y":1340,"wires":[["ecdb23d852e3fa7f"]]},{"id":"da9dbbae9a283e86","type":"ui_toast","z":"e580410840f1977a","position":"top right","displayTime":"3","highlight":"","sendall":true,"outputs":0,"ok":"OK","cancel":"Cancel","raw":false,"className":"","topic":"","name":"","x":250,"y":200,"wires":[]},{"id":"330ac65349eea739","type":"link out","z":"e580410840f1977a","name":"link out 45","mode":"link","links":["7f20cbcaea30ce95"],"x":415,"y":420,"wires":[]},{"id":"7f20cbcaea30ce95","type":"link in","z":"e580410840f1977a","name":"link in 41","links":["330ac65349eea739"],"x":135,"y":1220,"wires":[["58c96606c91c99d4"]]},{"id":"ca9bc6e8a598b75d","type":"comment","z":"e580410840f1977a","name":"Set Default Value","info":"","x":480,"y":40,"wires":[]},{"id":"2e873ff2215f1627","type":"sqlite","z":"e580410840f1977a","mydb":"f87c808cc786ae80","sqlquery":"msg.topic","sql":"","name":"RFID dB","x":540,"y":160,"wires":[["136b7f887c7ac8af"]]},{"id":"86a04561d5732afb","type":"sqlite","z":"e580410840f1977a","mydb":"f87c808cc786ae80","sqlquery":"msg.topic","sql":"","name":"RFID dB","x":720,"y":240,"wires":[["e4de9ca1fd6f1ad2","7c7b9e7101f99220"]]},{"id":"8caa729f53e3c29e","type":"sqlite","z":"e580410840f1977a","mydb":"f87c808cc786ae80","sqlquery":"msg.topic","sql":"","name":"RFID dB","x":880,"y":600,"wires":[["b821a2b279a4ae38"]]},{"id":"2d75f8fea620282a","type":"sqlite","z":"e580410840f1977a","mydb":"f87c808cc786ae80","sqlquery":"msg.topic","sql":"","name":"RFID dB","x":680,"y":740,"wires":[["9546f9f1689f5abd"]]},{"id":"e6c4b8d95973acc8","type":"sqlite","z":"e580410840f1977a","mydb":"f87c808cc786ae80","sqlquery":"msg.topic","sql":"","name":"RFID dB","x":720,"y":880,"wires":[["e89357f8bf99cd44","71e3c9e772097c69"]]},{"id":"ac223b4659d0d7a2","type":"sqlite","z":"e580410840f1977a","mydb":"f87c808cc786ae80","sqlquery":"msg.topic","sql":"","name":"RFID dB","x":740,"y":960,"wires":[["e89357f8bf99cd44"]]},{"id":"0f892898e5714cfd","type":"sqlite","z":"e580410840f1977a","mydb":"f87c808cc786ae80","sqlquery":"msg.topic","sql":"","name":"RFID dB","x":720,"y":1040,"wires":[["b821a2b279a4ae38"]]},{"id":"db51b6b49c4a1679","type":"debug","z":"e580410840f1977a","name":"debug 227","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":890,"y":560,"wires":[]},{"id":"f815556e7bd20e4c","type":"sqlite","z":"e580410840f1977a","mydb":"f87c808cc786ae80","sqlquery":"msg.topic","sql":"","name":"RFID dB","x":560,"y":1180,"wires":[["4ac1e40f5421fd71"]]},{"id":"a453c7f575918a4b","type":"ui_group","name":"命命區","tab":"3113488b40e7e950","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"cb0bc005d4b7ba13","type":"ui_group","name":"新增或比對","tab":"3113488b40e7e950","order":3,"disp":true,"width":"6","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":"26e583680376a2a5","type":"ui_group","name":"顯示區","tab":"3113488b40e7e950","order":2,"disp":true,"width":12,"collapse":false,"className":""},{"id":"f87c808cc786ae80","type":"sqlitedb","db":"C:\\Users\\User\\.node-red\\EX2_2.db","mode":"RWC"},{"id":"3113488b40e7e950","type":"ui_tab","name":"作業2-2","icon":"dashboard","order":90,"disabled":false,"hidden":false}]

#include "arduino_secrets.h"

組譯錯誤代表

缺少 arduino_secrets.h 檔案

裡面內容有2行

#define SECRET_SSID ""

#define SECRET_PASS ""

Arduino 程式


//定義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>

//GPIO 2 D1 Build in LED

#define LED 13           //定義LED接腳

///////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)
// WiFi SSID password , SSID 和密碼進行Wi-Fi 設定
//const char ssid[] = "alex9ufo"; // Enter your Wi-Fi name
//const char pass[] = "alex9981";  // Enter Wi-Fi password

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/esp32/led";
const char *topic2 = "alex9ufo/esp32/led_status";
const char *topic3 = "alex9ufo/esp32/RFID";

//const char *topic1 = "alex9ufo/RFID/read";
//const char *topic3 = "alex9ufo/RFID/back";
const char willTopic[] = "alex9ufo/esp32/Starting";
//======================================================
#define RST_PIN      27        // 讀卡機的重置腳位
#define SS_PIN       5        // 晶片選擇腳位
MFRC522 mfrc522(SS_PIN, RST_PIN);    // 建立MFRC522物件
MFRC522::MIFARE_Key key;  // 儲存金鑰
MFRC522::StatusCode status;
//===========================================================
//布林代數 LED狀態 是否連上網路ESP32 ready ?
bool ledState = false;
bool atwork = false;
bool Flash = false;  //true
bool Timer = false;  //true
bool Send = false;  //true
String LEDjson = "";
int Count= 0;
//===========================================================
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 message="";
  // use the Stream interface to print the contents
  while (mqttClient.available()) {
    //Serial.print((char)mqttClient.read());
    message += (char)mqttClient.read();
  }

  Serial.println(message);
  message.trim();
  Topic.trim();

  if (Topic=="alex9ufo/esp32/led") {
  if (message == "on") {
    digitalWrite(LED, LOW);  // Turn on the LED
    ledState = true;  //ledState = ture HIGH
    //設定 各個 旗號
    Flash = false;
    Timer = false;
    LEDjson ="ON";
    Send = true ;
    Serial.print("LED =");
    Serial.println(LEDjson);
    }
  if (message == "off" ) {
    digitalWrite(LED, HIGH); // Turn off the LED
    ledState = false; //ledState = false LOW
    Flash = false;
    Timer = false;
    LEDjson ="OFF";
    Send = true ;
    Serial.print("LED =");
    Serial.println(LEDjson);
  }

  if (message == "flash" ) {
    digitalWrite(LED, LOW); // Turn off the LED
    Flash = true;
    Timer = false;
    LEDjson ="FLASH";
    Send = true ;  
    Serial.print("LED =");
    Serial.println(LEDjson);      
  }

  if (message == "timer" ) {
    digitalWrite(LED, LOW); // Turn off the LED
    Flash = false;
    Timer = true;
    LEDjson ="TIMER";
    Send = true ;
    Count= 11;
    Serial.print("LED =");
    Serial.println(LEDjson);
  }

  if (message == "toggle" ) {
    digitalWrite(LED, !digitalRead(LED));   // Turn the LED toggle
    Flash = false;
    Timer = false;
    LEDjson ="TOGGLE";
    Send = true ;
    Serial.print("LED =");
    Serial.println(LEDjson);      
  }

    Serial.println();
    Serial.println("-----------------------");
  }  
}

//===========================================================
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());
}  
//===========================================================
//判斷 旗號Flash , Timer 是否為真
void LED_Message() {
  if (Flash){
    digitalWrite(LED, !digitalRead(LED));
    delay(500);
    if (digitalRead(LED))
      ledState = true;
    else
      ledState = false;
  } //(Flash)

  if (Timer) {
    digitalWrite(LED,LOW );
    delay(500);
    if (digitalRead(LED))
      ledState = true;
    else
      ledState = false;

    Count=Count-1;
    if (Count == 0 ){
      Timer=false;
      digitalWrite(LED, HIGH);
      ledState = false;
    }
  } //(Timer)

  ////判斷 旗號 Send 是否為真 回傳MQTT訊息到MQTT Broker 
  if (Send) {
    // Convert JSON string to character array
    Serial.print("Publish message: ");
    Serial.println(LEDjson);
    LEDjson.trim();

    bool retained = false;
    int qos = 1;
    bool dup = false;
   
    // Publish JSON character array to MQTT topic
    mqttClient.beginMessage(topic2,  LEDjson.length(), retained, qos, dup);  //LED Status
    mqttClient.print(LEDjson);
    mqttClient.endMessage();
    Send = false;    //處理過後 旗號 Send為假
  }

}
//===========================================================
void setup() {
  pinMode(LED, OUTPUT);
  digitalWrite(LED, HIGH);  // Turn off the LED initially
  //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 = "ESP32 Start working....!";
  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);
  Serial.print("Subscribing to topic: ");
  Serial.println(topic1);
  // subscribe to a topic
  // the second parameter sets the QoS of the subscription,
  // the the library supports subscribing at QoS 0, 1, or 2
  int subscribeQos = 1;
  mqttClient.subscribe(topic1, subscribeQos);


  Serial.println();
  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();
 
  LED_Message();
  // 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
    // 令卡片進入停止狀態
    // Dump debug info about the card; PICC_HaltA() is automatically called
    // mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
   
    mfrc522.PICC_HaltA();
    mfrc522.PCD_StopCrypto1(); // stop encryption on PCD
  }
}
////===========================================================




沒有留言:

張貼留言

Messaging API作為替代方案

  LINE超好用功能要沒了!LINE Notify明年3月底終止服務,有什麼替代方案? LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。 //CHANNEL_ACCESS_TOKEN = 'Messaging ...