2023年8月1日 星期二

實驗 2-2 使用Dashboard Table方式 Node-RED , MQTT , ESP32 MFRC522 ,SQLite , Line Notify

 










[{"id":"4e54c967b1626af6","type":"ui_button","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","order":21,"width":3,"height":1,"passthru":false,"label":"建立資料庫","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"建立資料庫","payloadType":"str","topic":"topic","topicType":"msg","x":90,"y":420,"wires":[["94067eb7c7c4f8bd","ba2e883e5331bb11"]]},{"id":"1ecd88f00201384e","type":"ui_button","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","order":24,"width":7,"height":2,"passthru":false,"label":"檢視資料庫資料","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"檢視資料","payloadType":"str","topic":"topic","topicType":"msg","x":100,"y":740,"wires":[["c87b80839d7841ac","b28951d3afee5ba0"]]},{"id":"2deebbd16b375147","type":"ui_button","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","order":20,"width":3,"height":1,"passthru":false,"label":"刪除所有資料","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"刪除所有資料","payloadType":"str","topic":"topic","topicType":"msg","x":100,"y":980,"wires":[["d4b03f7f13a61517","7d62d25d9e1c45bd"]]},{"id":"02588c019abc3e8d","type":"sqlite","z":"de6b68dedc3c24af","mydb":"dd7f8134e015a1cb","sqlquery":"msg.topic","sql":"","name":"My_RFID_database","x":540,"y":420,"wires":[["77e55fedaddea6af","c87b80839d7841ac"]]},{"id":"94067eb7c7c4f8bd","type":"function","z":"de6b68dedc3c24af","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":420,"wires":[["02588c019abc3e8d"]]},{"id":"77e55fedaddea6af","type":"debug","z":"de6b68dedc3c24af","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":750,"y":420,"wires":[]},{"id":"de6d85fb8d8c21d1","type":"function","z":"de6b68dedc3c24af","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":460,"wires":[["6ec71d078918cbb7","02588c019abc3e8d","31d81b4cef1ac635"]]},{"id":"d4b03f7f13a61517","type":"function","z":"de6b68dedc3c24af","name":"刪除所有資料","func":"//DELETE from RFIDtable\nmsg.topic = \"DELETE from RFIDtable\";\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":980,"wires":[["e98dd3a35d819282"]]},{"id":"e98dd3a35d819282","type":"sqlite","z":"de6b68dedc3c24af","mydb":"dd7f8134e015a1cb","sqlquery":"msg.topic","sql":"","name":"My_RFID_database","x":540,"y":980,"wires":[["edacf88b26879f53"]]},{"id":"f09af2e9a89a0d21","type":"ui_button","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","order":19,"width":3,"height":1,"passthru":false,"label":"刪除資料庫","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"刪除資料庫","payloadType":"str","topic":"topic","topicType":"msg","x":90,"y":1060,"wires":[["296f1c2d6e1196b3","001ff89bacb2f192"]]},{"id":"296f1c2d6e1196b3","type":"function","z":"de6b68dedc3c24af","name":"刪除資料庫","func":"//DROP TABLE RFIDtable\nmsg.topic = \"DROP TABLE RFIDtable\";\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":1060,"wires":[["e98dd3a35d819282"]]},{"id":"edacf88b26879f53","type":"debug","z":"de6b68dedc3c24af","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":730,"y":980,"wires":[]},{"id":"c87b80839d7841ac","type":"function","z":"de6b68dedc3c24af","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":560,"y":740,"wires":[["1937eb60bf90a4ea"]]},{"id":"1937eb60bf90a4ea","type":"sqlite","z":"de6b68dedc3c24af","mydb":"dd7f8134e015a1cb","sqlquery":"msg.topic","sql":"","name":"My_RFID_database","x":780.9005737304688,"y":873.5454711914062,"wires":[["6a690fd190600030"]]},{"id":"3a28746c5fadde64","type":"function","z":"de6b68dedc3c24af","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":900,"wires":[["e98dd3a35d819282","750f9598fb9877e9"]]},{"id":"08325cef072ffe08","type":"function","z":"de6b68dedc3c24af","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":800,"wires":[["1b293eb9d944ccd4"]]},{"id":"639703d60b687034","type":"ui_numeric","z":"de6b68dedc3c24af","name":"","label":"刪除的database_id","tooltip":"","group":"9eb91d4467c6eec5","order":18,"width":5,"height":1,"wrap":false,"passthru":true,"topic":"topic","topicType":"msg","format":"{{value}}","min":"1","max":"100","step":1,"className":"","x":110,"y":900,"wires":[["3a28746c5fadde64","7d62d25d9e1c45bd"]]},{"id":"1b293eb9d944ccd4","type":"sqlite","z":"de6b68dedc3c24af","mydb":"dd7f8134e015a1cb","sqlquery":"msg.topic","sql":"DELETE FROM dhtreadings WHERE id =  VALUES ($theid)","name":"My_RFID_database","x":500,"y":800,"wires":[["c87b80839d7841ac"]]},{"id":"a9194f9c58dca945","type":"ui_button","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","order":17,"width":3,"height":1,"passthru":false,"label":"刪除一筆資料","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"刪除一筆資料","payloadType":"str","topic":"topic","topicType":"msg","x":100,"y":800,"wires":[["08325cef072ffe08","b28951d3afee5ba0"]]},{"id":"750f9598fb9877e9","type":"debug","z":"de6b68dedc3c24af","name":"debug 148","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":900,"wires":[]},{"id":"6950681a0cfa74e8","type":"mqtt in","z":"de6b68dedc3c24af","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":460,"wires":[["023ff18076f47a28"]]},{"id":"6ec71d078918cbb7","type":"debug","z":"de6b68dedc3c24af","name":"debug 149","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":530,"y":460,"wires":[]},{"id":"01312e69f1a91d6f","type":"ui_text_input","z":"de6b68dedc3c24af","name":"","label":"新增一筆資料","tooltip":"","group":"9eb91d4467c6eec5","order":22,"width":3,"height":1,"passthru":true,"mode":"text","delay":300,"topic":"topic","sendOnBlur":true,"className":"","topicType":"msg","x":320,"y":540,"wires":[[]]},{"id":"a53cf8c89d5e51db","type":"ui_audio","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":500,"y":620,"wires":[]},{"id":"0726d97b0d7ac659","type":"function","z":"de6b68dedc3c24af","name":"function ","func":"var temp= msg.payload;\nmsg.payload= \"新增一筆資料\" + temp;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":580,"wires":[["a53cf8c89d5e51db","7c450c7cd77f061e"]]},{"id":"b28951d3afee5ba0","type":"ui_audio","z":"de6b68dedc3c24af","name":"","group":"85b1efd8d6d7c0dd","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":300,"y":760,"wires":[]},{"id":"ba2e883e5331bb11","type":"ui_audio","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":260,"y":380,"wires":[]},{"id":"8197d851517adcd6","type":"function","z":"de6b68dedc3c24af","name":"function 59","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":1140,"wires":[["55adfc13fd175119","3196806f61d06fa0"]]},{"id":"e34d781fec5002e3","type":"ui_button","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","order":14,"width":3,"height":1,"passthru":false,"label":"比對資料庫","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"比對資料庫","payloadType":"str","topic":"topic","topicType":"msg","x":90,"y":1140,"wires":[["8197d851517adcd6","001ff89bacb2f192","76cf80ba60d90816"]]},{"id":"55adfc13fd175119","type":"sqlite","z":"de6b68dedc3c24af","mydb":"dd7f8134e015a1cb","sqlquery":"msg.topic","sql":"","name":"My_RFID_database","x":760,"y":1140,"wires":[["6a690fd190600030"]]},{"id":"3f37ee591b72395b","type":"function","z":"de6b68dedc3c24af","name":"SELECT ALL","func":"var query_uidtemp=msg.payload;\nflow.set(\"uidtemp\", query_uidtemp);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":490,"y":680,"wires":[["9464b038a9977204","6033718fce891596","e2cb31304ab675d7"]]},{"id":"ed8f8ebc0bba82d0","type":"ui_text_input","z":"de6b68dedc3c24af","name":"","label":"手動查詢資料的uidname","tooltip":"","group":"9eb91d4467c6eec5","order":13,"width":3,"height":1,"passthru":true,"mode":"text","delay":300,"topic":"topic","sendOnBlur":true,"className":"","topicType":"msg","x":130,"y":680,"wires":[["3f37ee591b72395b"]]},{"id":"e4a25dc8daba5c17","type":"function","z":"de6b68dedc3c24af","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":520,"y":1300,"wires":[["87b4f6310accbe91","75a36ff74b38b0fb"]]},{"id":"81f3a137e4563f5d","type":"ui_switch","z":"de6b68dedc3c24af","name":"","label":"新增模式  /自動比對模式 ","tooltip":"","group":"9eb91d4467c6eec5","order":9,"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":310,"y":1300,"wires":[["e4a25dc8daba5c17","120cc349eea91ca0","6e64c47a74cd073b"]]},{"id":"87b4f6310accbe91","type":"debug","z":"de6b68dedc3c24af","name":"debug 150","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":1260,"wires":[]},{"id":"874b8f2669aff611","type":"switch","z":"de6b68dedc3c24af","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":600,"wires":[["de6d85fb8d8c21d1","01312e69f1a91d6f","0726d97b0d7ac659","cbff289eacce4f3d"],["d3740acc03146f26","01312e69f1a91d6f","06de54b55a8793f0"]]},{"id":"023ff18076f47a28","type":"function","z":"de6b68dedc3c24af","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":520,"wires":[["874b8f2669aff611"]]},{"id":"cbff289eacce4f3d","type":"debug","z":"de6b68dedc3c24af","name":"debug 151","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":310,"y":500,"wires":[]},{"id":"75a36ff74b38b0fb","type":"ui_audio","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":680,"y":1300,"wires":[]},{"id":"3196806f61d06fa0","type":"debug","z":"de6b68dedc3c24af","name":"debug ","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":450,"y":1180,"wires":[]},{"id":"9464b038a9977204","type":"debug","z":"de6b68dedc3c24af","name":"debug 152","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":660,"wires":[]},{"id":"d3740acc03146f26","type":"function","z":"de6b68dedc3c24af","name":"function","func":"\nvar a= flow.get(\"uid_temp\");\nmsg.payload=a;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":620,"wires":[["3f37ee591b72395b","a53cf8c89d5e51db"]]},{"id":"6033718fce891596","type":"ui_text","z":"de6b68dedc3c24af","group":"9eb91d4467c6eec5","order":25,"width":6,"height":1,"name":"","label":"自動感應RFID查詢的uid","format":"{{msg.payload}}","layout":"row-left","className":"","x":750,"y":580,"wires":[]},{"id":"e2cb31304ab675d7","type":"link out","z":"de6b68dedc3c24af","name":"link out 14","mode":"link","links":[],"x":675,"y":700,"wires":[]},{"id":"c2844f3fc3485e72","type":"link in","z":"de6b68dedc3c24af","name":"link in 16","links":["06de54b55a8793f0","f316a7347a7ff85a"],"x":215,"y":1180,"wires":[["8197d851517adcd6"]]},{"id":"793b7c06ee66a09d","type":"function","z":"de6b68dedc3c24af","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":440,"y":1400,"wires":[["55d5866aa06954f9","81fbf35c3593d646"]]},{"id":"55d5866aa06954f9","type":"sqlite","z":"de6b68dedc3c24af","mydb":"dd7f8134e015a1cb","sqlquery":"msg.topic","sql":"","name":"My_RFID_database","x":640,"y":1400,"wires":[["6abdbb53799f733b"]]},{"id":"6abdbb53799f733b","type":"function","z":"de6b68dedc3c24af","name":"function ","func":"var num=msg.payload[0].總共有幾筆資料;\nmsg.payload=num;\nreturn msg; \n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":680,"y":1440,"wires":[["f656f4ce5c5825e4","93c6038d83d6e45e","ed8e9bbe0b660849"]]},{"id":"51408af806740d10","type":"ui_text","z":"de6b68dedc3c24af","group":"9eb91d4467c6eec5","order":16,"width":4,"height":1,"name":"","label":"查詢結果","format":"<font face='arial'><font size=3><font color={{fcolor}}>{{msg.payload}}","layout":"row-left","className":"","x":1040,"y":1460,"wires":[]},{"id":"5c747d8c5a9e5afe","type":"ui_text","z":"de6b68dedc3c24af","group":"9eb91d4467c6eec5","order":12,"width":4,"height":1,"name":"","label":"查詢結果:筆數","format":"<font face='arial'><font size=6><font color={{fcolor}}>{{msg.payload}}","layout":"row-left","className":"","x":1060,"y":1420,"wires":[]},{"id":"1f9956bf0ebf4cd5","type":"mqtt in","z":"de6b68dedc3c24af","name":"","topic":"alex9ufo/esp32/Starting","qos":"2","datatype":"auto-detect","broker":"841df58d.ee5e98","nl":false,"rap":true,"rh":0,"inputs":0,"x":120,"y":1540,"wires":[["84cf23995cc33dbf","3364a4e3960b0d0f","02eb16ed2193d722"]]},{"id":"84cf23995cc33dbf","type":"ui_audio","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":300,"y":1580,"wires":[]},{"id":"3364a4e3960b0d0f","type":"debug","z":"de6b68dedc3c24af","name":"debug 153","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":310,"y":1540,"wires":[]},{"id":"02eb16ed2193d722","type":"change","z":"de6b68dedc3c24af","name":"","rules":[{"t":"set","p":"fcolor","pt":"msg","to":"red","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":1500,"wires":[["ce4819dc58cabcf1"]]},{"id":"ce4819dc58cabcf1","type":"ui_text","z":"de6b68dedc3c24af","group":"9eb91d4467c6eec5","order":7,"width":2,"height":2,"name":"","label":"","format":"<font face='arial'><font size=4><font color={{fcolor}}>{{msg.payload}}","layout":"row-left","className":"","x":470,"y":1500,"wires":[]},{"id":"81fbf35c3593d646","type":"debug","z":"de6b68dedc3c24af","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":590,"y":1360,"wires":[]},{"id":"f656f4ce5c5825e4","type":"change","z":"de6b68dedc3c24af","name":"","rules":[{"t":"set","p":"fcolor","pt":"msg","to":"red","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":880,"y":1420,"wires":[["5c747d8c5a9e5afe","da23f41333cf4692"]]},{"id":"f979f977c88ab3cf","type":"change","z":"de6b68dedc3c24af","name":"","rules":[{"t":"set","p":"fcolor","pt":"msg","to":"red","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":880,"y":1460,"wires":[["51408af806740d10"]]},{"id":"76cf80ba60d90816","type":"function","z":"de6b68dedc3c24af","name":"function","func":"\nvar a= flow.get(\"uidtemp\");\nmsg.payload=a;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":280,"y":1400,"wires":[["793b7c06ee66a09d"]]},{"id":"06de54b55a8793f0","type":"link out","z":"de6b68dedc3c24af","name":"link out 15","mode":"link","links":["3a56d7b39bf4404b","c2844f3fc3485e72"],"x":195,"y":640,"wires":[]},{"id":"3a56d7b39bf4404b","type":"link in","z":"de6b68dedc3c24af","name":"link in 17","links":["06de54b55a8793f0","f316a7347a7ff85a"],"x":155,"y":1360,"wires":[["76cf80ba60d90816"]]},{"id":"2974da7c7ae9fc5b","type":"ui_audio","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":1040,"y":1500,"wires":[]},{"id":"da23f41333cf4692","type":"debug","z":"de6b68dedc3c24af","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1030,"y":1380,"wires":[]},{"id":"ed495484b61d4da2","type":"delay","z":"de6b68dedc3c24af","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":860,"y":1500,"wires":[["2974da7c7ae9fc5b"]]},{"id":"93c6038d83d6e45e","type":"function","z":"de6b68dedc3c24af","name":"function ","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":680,"y":1500,"wires":[["ed495484b61d4da2","f979f977c88ab3cf","e59c8531bc5da2f0"]]},{"id":"4b6e281f7c69ae1a","type":"ui_button","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","order":1,"width":2,"height":1,"passthru":false,"label":"LED 開","tooltip":"","color":"white","bgcolor":"","className":"","icon":"fa-circle","payload":"on","payloadType":"str","topic":"","topicType":"str","x":80,"y":60,"wires":[["6ec9d19e5d866476","76186891a0198849","b544f160142cab97"]]},{"id":"b24e49f23a3b1747","type":"ui_button","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","order":2,"width":3,"height":1,"passthru":false,"label":"LED 關","tooltip":"","color":"black","bgcolor":"","className":"","icon":"fa-circle-o","payload":"off","payloadType":"str","topic":"","topicType":"str","x":80,"y":100,"wires":[["6ec9d19e5d866476","76186891a0198849","b544f160142cab97"]]},{"id":"6ec9d19e5d866476","type":"mqtt out","z":"de6b68dedc3c24af","name":"Control LED","topic":"alex9ufo/esp32/led","qos":"1","retain":"true","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"70940176.2b2d3","x":330,"y":60,"wires":[]},{"id":"3f35df02d5bd58e8","type":"comment","z":"de6b68dedc3c24af","name":"Node-Red publish 到 HiveMQ Broker  , ESP32訂閱","info":"將 alex9ufo/inTopic 到(publish)HiveMQ Broker \n給 Arduino 訂閱(Subscribe)","x":210,"y":20,"wires":[]},{"id":"76186891a0198849","type":"ui_audio","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","voice":"zh-TW","always":true,"x":320,"y":200,"wires":[]},{"id":"b544f160142cab97","type":"ui_text","z":"de6b68dedc3c24af","group":"9eb91d4467c6eec5","order":6,"width":12,"height":1,"name":"","label":"Node-RED發行到MQTT的資料","format":"{{msg.payload}}","layout":"row-left","className":"","x":390,"y":140,"wires":[]},{"id":"6b67bc741bb39662","type":"ui_audio","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","voice":"en-US","always":"","x":560,"y":300,"wires":[]},{"id":"aad59551acfe441b","type":"mqtt in","z":"de6b68dedc3c24af","name":"LED status ","topic":"alex9ufo/esp32/led_status","qos":"2","datatype":"utf8","broker":"841df58d.ee5e98","nl":false,"rap":true,"rh":0,"inputs":0,"x":80,"y":300,"wires":[["a32ae721feaf3569"]]},{"id":"0070b6f2844db90c","type":"comment","z":"de6b68dedc3c24af","name":"Node-Red  subscribe HiveMQ Broker  , ESP32發行","info":"將 alex9ufo/inTopic 到(publish)HiveMQ Broker \n給 Arduino 訂閱(Subscribe)","x":200,"y":260,"wires":[]},{"id":"68f673eba4748e05","type":"ui_text","z":"de6b68dedc3c24af","group":"9eb91d4467c6eec5","order":8,"width":12,"height":1,"name":"","label":"Node-RED 訂閱MQTT的資料","format":"{{msg.payload}}","layout":"row-left","className":"","x":480,"y":340,"wires":[]},{"id":"1b815df13dd55ce2","type":"ui_button","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","order":5,"width":3,"height":1,"passthru":false,"label":"LED 閃爍","tooltip":"","color":"black","bgcolor":"","className":"","icon":"fa-circle-o","payload":"flash","payloadType":"str","topic":"","topicType":"str","x":80,"y":140,"wires":[["6ec9d19e5d866476","b544f160142cab97","76186891a0198849"]]},{"id":"d353783e63346a77","type":"ui_button","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","order":4,"width":3,"height":1,"passthru":false,"label":"LED反向","tooltip":"","color":"black","bgcolor":"","className":"","icon":"fa-circle-o","payload":"toggle","payloadType":"str","topic":"","topicType":"str","x":80,"y":180,"wires":[["6ec9d19e5d866476","b544f160142cab97","76186891a0198849"]]},{"id":"a265c934fc2e261b","type":"ui_button","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","order":3,"width":3,"height":1,"passthru":false,"label":"LED 定時5秒","tooltip":"","color":"black","bgcolor":"","className":"","icon":"fa-circle-o","payload":"timer","payloadType":"str","topic":"","topicType":"str","x":90,"y":220,"wires":[["6ec9d19e5d866476","b544f160142cab97","76186891a0198849"]]},{"id":"a32ae721feaf3569","type":"function","z":"de6b68dedc3c24af","name":"function 60","func":"msg.payload=\" ---ESP32回來資料---\" +msg.payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":300,"wires":[["68f673eba4748e05","0ed9fe1998238b19","72840622b8d7725c"]]},{"id":"0ed9fe1998238b19","type":"delay","z":"de6b68dedc3c24af","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":420,"y":300,"wires":[["6b67bc741bb39662"]]},{"id":"7c450c7cd77f061e","type":"function","z":"de6b68dedc3c24af","name":"function 61","func":"var myRFID = flow.get('uid_temp');\nmsg.payload=myRFID;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":580,"wires":[["6033718fce891596"]]},{"id":"001ff89bacb2f192","type":"ui_audio","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":300,"y":1100,"wires":[]},{"id":"7d62d25d9e1c45bd","type":"ui_audio","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":300,"y":940,"wires":[]},{"id":"ed8e9bbe0b660849","type":"debug","z":"de6b68dedc3c24af","name":"debug ","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":850,"y":1380,"wires":[]},{"id":"72840622b8d7725c","type":"function","z":"de6b68dedc3c24af","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":530,"y":240,"wires":[["c0b30cb8c441c75d"]]},{"id":"c0b30cb8c441c75d","type":"http request","z":"de6b68dedc3c24af","name":"","method":"POST","ret":"txt","paytoqs":false,"url":"https://notify-api.line.me/api/notify","tls":"","persist":false,"proxy":"","authType":"","x":700,"y":240,"wires":[["d8148d59672f7c60"]]},{"id":"d8148d59672f7c60","type":"debug","z":"de6b68dedc3c24af","name":"debug 155","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":870,"y":240,"wires":[]},{"id":"414abec3b8057999","type":"comment","z":"de6b68dedc3c24af","name":"Line Notify Message ","info":"","x":710,"y":200,"wires":[]},{"id":"31d81b4cef1ac635","type":"function","z":"de6b68dedc3c24af","name":"function 62","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":530,"y":380,"wires":[["db7f825c6899a04a"]]},{"id":"db7f825c6899a04a","type":"function","z":"de6b68dedc3c24af","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":750,"y":380,"wires":[["275a27cc65cc3533"]]},{"id":"275a27cc65cc3533","type":"http request","z":"de6b68dedc3c24af","name":"","method":"POST","ret":"txt","paytoqs":false,"url":"https://notify-api.line.me/api/notify","tls":"","persist":false,"proxy":"","authType":"","x":900,"y":380,"wires":[["c437c87bbac66774"]]},{"id":"c437c87bbac66774","type":"debug","z":"de6b68dedc3c24af","name":"debug 156","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1050,"y":380,"wires":[]},{"id":"c69c2e0173b43d9a","type":"comment","z":"de6b68dedc3c24af","name":"Line Notify Message ","info":"","x":810,"y":340,"wires":[]},{"id":"d2e0c88689bc12bc","type":"function","z":"de6b68dedc3c24af","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":850,"y":1600,"wires":[["a6c1bb66ba045735"]]},{"id":"bb8df8633a1e1d3b","type":"comment","z":"de6b68dedc3c24af","name":"Line Notify Message ","info":"","x":810,"y":1560,"wires":[]},{"id":"a6c1bb66ba045735","type":"http request","z":"de6b68dedc3c24af","name":"","method":"POST","ret":"txt","paytoqs":false,"url":"https://notify-api.line.me/api/notify","tls":"","persist":false,"proxy":"","authType":"","x":1000,"y":1600,"wires":[["956fe7d2463ead0a"]]},{"id":"956fe7d2463ead0a","type":"debug","z":"de6b68dedc3c24af","name":"debug 157","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1010,"y":1660,"wires":[]},{"id":"e59c8531bc5da2f0","type":"function","z":"de6b68dedc3c24af","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":1600,"wires":[["d2e0c88689bc12bc"]]},{"id":"6a690fd190600030","type":"ui_table","z":"de6b68dedc3c24af","group":"9eb91d4467c6eec5","name":"","order":27,"width":14,"height":12,"columns":[],"outputs":0,"cts":false,"x":910,"y":1040,"wires":[]},{"id":"49bbb67158e55009","type":"inject","z":"de6b68dedc3c24af","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"1","topic":"","payload":"0","payloadType":"str","x":110,"y":1300,"wires":[["81f3a137e4563f5d","63c8538fc0982af9"]]},{"id":"120cc349eea91ca0","type":"change","z":"de6b68dedc3c24af","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":340,"y":1240,"wires":[["1ba07c0966a51c2c"]]},{"id":"1ba07c0966a51c2c","type":"ui_text_input","z":"de6b68dedc3c24af","name":"","label":"State:","tooltip":"","group":"9eb91d4467c6eec5","order":10,"width":3,"height":1,"passthru":true,"mode":"text","delay":300,"topic":"","sendOnBlur":true,"className":"","topicType":"str","x":510,"y":1240,"wires":[[]]},{"id":"63c8538fc0982af9","type":"debug","z":"de6b68dedc3c24af","name":"debug ","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":270,"y":1340,"wires":[]},{"id":"6e64c47a74cd073b","type":"debug","z":"de6b68dedc3c24af","name":"debug ","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":450,"y":1340,"wires":[]},{"id":"9eb91d4467c6eec5","type":"ui_group","name":"Default","tab":"23af251c8a87881d","order":1,"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":"85b1efd8d6d7c0dd","type":"ui_group","name":"ESP32","tab":"eeb8e179.a47a4","order":2,"disp":true,"width":"13","collapse":false,"className":""},{"id":"70940176.2b2d3","type":"mqtt-broker","name":"","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"23af251c8a87881d","type":"ui_tab","name":"Home_RFID","icon":"dashboard","disabled":false,"hidden":false},{"id":"eeb8e179.a47a4","type":"ui_tab","name":"2023 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 *topic3 = "alex9ufo/esp32/led_status";



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 = "";
bool Flash = false;  //true
bool Timer = false;  //true
bool Send = false;  //true
int Count= 0;

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_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}    
//===========================================================
void setup() {
    // Set software serial baud to 115200;
    Serial.begin(115200);
    delay(1000); // Delay for stability
    //======================================================
    // Connecting to a WiFi network
    setup_wifi();
    // 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;  //ledState = ture HIGH
        Flash = false;
        Timer = false;
        json ="ON";
        Send = true ;
    }
    if (message == "off" && ledState) {
        digitalWrite(LED, LOW); // Turn off the LED
        ledState = false; //ledState = false LOW
        Flash = false;
        Timer = false;
        json ="OFF";
        Send = true ;

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

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

    if (message == "toggle" ) {
        digitalWrite(LED, !digitalRead(LED));   // Turn the LED toggle
        if (digitalRead(LED))
            ledState = true;
        else
            ledState = false;
       
        Flash = false;
        Timer = false;
        json ="TOGGLE";
        Send = true ;        
    }

    Serial.println();
    Serial.println("-----------------------");
}
//===========================================================
void loop() {
    client.loop();
    delay(100); // Delay for a short period in each loop iteration
    if (Flash){
        digitalWrite(LED, !digitalRead(LED));
        delay(500);
        if (digitalRead(LED))
            ledState = true;
        else
            ledState = false;
    } //(Flash)
       
    if (Timer) {
        digitalWrite(LED, HIGH);
        delay(500);
        if (digitalRead(LED))
            ledState = true;
        else
            ledState = false;

        Count=Count-1;
        if (Count == 0 ){
            Timer=false;
            digitalWrite(LED, LOW);
            ledState = false;
        }
    } //(Timer)
   
    if (client.connected()) {        
        if (Send) {
          // Convert JSON string to character array
          json.toCharArray(jsonChar1, json.length()+1);
          Serial.print("Publish message: ");
          Serial.println(json);
          // Publish JSON character array to MQTT topic
          client.publish(topic3,jsonChar1);
        }
        Send = false;    
    }

    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
         
         
        }
      }
       
  }

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

沒有留言:

張貼留言

2024產專班 作業2 (純模擬)

2024產專班 作業2  (純模擬) 1) LED ON,OFF,TIMER,FLASH 模擬 (switch 控制) 2)RFID卡號模擬 (buttom  模擬RFID UID(不從ESP32) Node-Red 程式 [{"id":"d8886...