2023年8月25日 星期五

ESP32 +PN532 NFC Reader + Thread分時多工 + Node-Red + Line +MQTT+ SQLite

  (實驗2-2) ESP32 +PN532 NFC Reader + Thread分時多工 + Node-Red + Line +MQTT + SQLite (實驗2-2)















[{"id":"4e54c967b1626af6","type":"ui_button","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","order":22,"width":3,"height":1,"passthru":false,"label":"建立資料庫","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"建立資料庫","payloadType":"str","topic":"topic","topicType":"msg","x":90,"y":500,"wires":[["94067eb7c7c4f8bd","ba2e883e5331bb11"]]},{"id":"1ecd88f00201384e","type":"ui_button","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","order":24,"width":8,"height":2,"passthru":false,"label":"檢視資料庫資料","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"檢視資料","payloadType":"str","topic":"topic","topicType":"msg","x":100,"y":920,"wires":[["b28951d3afee5ba0","9ab108d4b11f6116","52ab84e02af9ebe7"]]},{"id":"2deebbd16b375147","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":100,"y":1220,"wires":[["7d62d25d9e1c45bd","f723bf8d47cfafad"]]},{"id":"02588c019abc3e8d","type":"sqlite","z":"de6b68dedc3c24af","mydb":"8d99f15e50a51fcd","sqlquery":"msg.topic","sql":"","name":"My_RFID_database","x":640,"y":580,"wires":[["77e55fedaddea6af","68d9feaf2c5be655"]]},{"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":280,"y":500,"wires":[["dba2810aa1fb6858"]]},{"id":"77e55fedaddea6af","type":"debug","z":"de6b68dedc3c24af","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":890,"y":580,"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":460,"y":580,"wires":[["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":560,"y":1220,"wires":[["e98dd3a35d819282"]]},{"id":"e98dd3a35d819282","type":"sqlite","z":"de6b68dedc3c24af","mydb":"8d99f15e50a51fcd","sqlquery":"msg.topic","sql":"","name":"My_RFID_database","x":760,"y":1220,"wires":[["edacf88b26879f53","57ca47c2065dbf1e"]]},{"id":"f09af2e9a89a0d21","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":90,"y":1300,"wires":[["001ff89bacb2f192","399aff4307deb748"]]},{"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":550,"y":1300,"wires":[["6f8a0a1997c5c232"]]},{"id":"edacf88b26879f53","type":"debug","z":"de6b68dedc3c24af","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":870,"y":1260,"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":700,"y":1000,"wires":[["1937eb60bf90a4ea"]]},{"id":"1937eb60bf90a4ea","type":"sqlite","z":"de6b68dedc3c24af","mydb":"8d99f15e50a51fcd","sqlquery":"msg.topic","sql":"","name":"My_RFID_database","x":880,"y":1000,"wires":[["6a690fd190600030"]]},{"id":"3a28746c5fadde64","type":"function","z":"de6b68dedc3c24af","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":1140,"wires":[["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":520,"y":1080,"wires":[["1b293eb9d944ccd4"]]},{"id":"639703d60b687034","type":"ui_numeric","z":"de6b68dedc3c24af","name":"","label":"刪除的database_id","tooltip":"","group":"9eb91d4467c6eec5","order":19,"width":5,"height":1,"wrap":true,"passthru":true,"topic":"topic","topicType":"msg","format":"{{value}}","min":"1","max":"1000","step":1,"className":"","x":110,"y":1140,"wires":[["3a28746c5fadde64","5434549be9f18323"]]},{"id":"1b293eb9d944ccd4","type":"sqlite","z":"de6b68dedc3c24af","mydb":"8d99f15e50a51fcd","sqlquery":"msg.topic","sql":"DELETE FROM dhtreadings WHERE id =  VALUES ($theid)","name":"My_RFID_database","x":700,"y":1080,"wires":[["c87b80839d7841ac"]]},{"id":"a9194f9c58dca945","type":"ui_button","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","order":18,"width":3,"height":1,"passthru":false,"label":"刪除一筆資料","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"刪除一筆資料","payloadType":"str","topic":"topic","topicType":"msg","x":100,"y":1020,"wires":[["b28951d3afee5ba0","5fb442caa3abbe77"]]},{"id":"750f9598fb9877e9","type":"debug","z":"de6b68dedc3c24af","name":"debug 148","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":490,"y":1140,"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":580,"wires":[["023ff18076f47a28","509ceaf0d23249fb"]]},{"id":"01312e69f1a91d6f","type":"ui_text_input","z":"de6b68dedc3c24af","name":"","label":"新增一筆資料","tooltip":"","group":"9eb91d4467c6eec5","order":23,"width":3,"height":1,"passthru":true,"mode":"text","delay":300,"topic":"topic","sendOnBlur":true,"className":"","topicType":"msg","x":340,"y":700,"wires":[[]]},{"id":"a53cf8c89d5e51db","type":"ui_audio","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":540,"y":740,"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":360,"y":660,"wires":[["a53cf8c89d5e51db","7c450c7cd77f061e","1cf2cb78b6f74fdb"]]},{"id":"b28951d3afee5ba0","type":"ui_audio","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":320,"y":1000,"wires":[]},{"id":"ba2e883e5331bb11","type":"ui_audio","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":240,"y":460,"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":1380,"wires":[["55adfc13fd175119","3196806f61d06fa0"]]},{"id":"e34d781fec5002e3","type":"ui_button","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","order":15,"width":3,"height":1,"passthru":false,"label":"比對資料庫","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"比對資料庫","payloadType":"str","topic":"topic","topicType":"msg","x":90,"y":1380,"wires":[["8197d851517adcd6","001ff89bacb2f192","76cf80ba60d90816"]]},{"id":"55adfc13fd175119","type":"sqlite","z":"de6b68dedc3c24af","mydb":"8d99f15e50a51fcd","sqlquery":"msg.topic","sql":"","name":"My_RFID_database","x":860,"y":1380,"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":550,"y":780,"wires":[["9464b038a9977204","6033718fce891596"]]},{"id":"ed8f8ebc0bba82d0","type":"ui_text_input","z":"de6b68dedc3c24af","name":"","label":"手動查詢資料的uidname","tooltip":"","group":"9eb91d4467c6eec5","order":14,"width":3,"height":1,"passthru":true,"mode":"text","delay":300,"topic":"topic","sendOnBlur":true,"className":"","topicType":"msg","x":330,"y":840,"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":1580,"wires":[["87b4f6310accbe91","75a36ff74b38b0fb"]]},{"id":"81f3a137e4563f5d","type":"ui_switch","z":"de6b68dedc3c24af","name":"","label":"新增模式  /自動比對模式 ","tooltip":"","group":"9eb91d4467c6eec5","order":10,"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":1580,"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":1540,"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":130,"y":700,"wires":[["de6d85fb8d8c21d1","01312e69f1a91d6f","0726d97b0d7ac659"],["01312e69f1a91d6f","d3740acc03146f26"]]},{"id":"023ff18076f47a28","type":"function","z":"de6b68dedc3c24af","name":"Query 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":240,"y":580,"wires":[["874b8f2669aff611"]]},{"id":"75a36ff74b38b0fb","type":"ui_audio","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","voice":"Google US English","always":true,"x":680,"y":1580,"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":470,"y":1400,"wires":[]},{"id":"9464b038a9977204","type":"debug","z":"de6b68dedc3c24af","name":"debug 152","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":750,"y":800,"wires":[]},{"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":790,"y":700,"wires":[]},{"id":"c2844f3fc3485e72","type":"link in","z":"de6b68dedc3c24af","name":"link in 16","links":["06de54b55a8793f0","f316a7347a7ff85a"],"x":215,"y":1460,"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":1680,"wires":[["55d5866aa06954f9","81fbf35c3593d646"]]},{"id":"55d5866aa06954f9","type":"sqlite","z":"de6b68dedc3c24af","mydb":"8d99f15e50a51fcd","sqlquery":"msg.topic","sql":"","name":"My_RFID_database","x":640,"y":1680,"wires":[["6abdbb53799f733b","0862ac38abe4dfdb"]]},{"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":580,"y":1740,"wires":[["f656f4ce5c5825e4","93c6038d83d6e45e"]]},{"id":"51408af806740d10","type":"ui_text","z":"de6b68dedc3c24af","group":"9eb91d4467c6eec5","order":17,"width":4,"height":1,"name":"","label":"查詢結果","format":"<font face='arial'><font size=3><font color={{fcolor}}>{{msg.payload}}","layout":"row-left","className":"","x":920,"y":1780,"wires":[]},{"id":"5c747d8c5a9e5afe","type":"ui_text","z":"de6b68dedc3c24af","group":"9eb91d4467c6eec5","order":13,"width":4,"height":1,"name":"","label":"查詢結果:筆數","format":"<font face='arial'><font size=6><font color={{fcolor}}>{{msg.payload}}","layout":"row-left","className":"","x":940,"y":1740,"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":1840,"wires":[["84cf23995cc33dbf","3364a4e3960b0d0f","8a8ebb530711e784"]]},{"id":"84cf23995cc33dbf","type":"ui_audio","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","voice":"Google US English","always":false,"x":300,"y":1880,"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":1800,"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":1640,"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":740,"y":1740,"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":760,"y":1780,"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":1680,"wires":[["793b7c06ee66a09d"]]},{"id":"2974da7c7ae9fc5b","type":"ui_audio","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":900,"y":1820,"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":910,"y":1700,"wires":[]},{"id":"ed495484b61d4da2","type":"delay","z":"de6b68dedc3c24af","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":760,"y":1820,"wires":[["2974da7c7ae9fc5b"]]},{"id":"93c6038d83d6e45e","type":"function","z":"de6b68dedc3c24af","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":580,"y":1800,"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","1eb1cde2be18eec3","bb74384bab8236ab"]]},{"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","1eb1cde2be18eec3","bb74384bab8236ab"]]},{"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":"Google US English","always":true,"x":580,"y":220,"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":510,"y":140,"wires":[]},{"id":"6b67bc741bb39662","type":"ui_audio","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":"","x":640,"y":380,"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":380,"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":300,"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":540,"y":420,"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","1eb1cde2be18eec3","bb74384bab8236ab"]]},{"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","1eb1cde2be18eec3","bb74384bab8236ab"]]},{"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","1eb1cde2be18eec3","bb74384bab8236ab"]]},{"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":270,"y":380,"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":480,"y":380,"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":550,"y":700,"wires":[["6033718fce891596"]]},{"id":"001ff89bacb2f192","type":"ui_audio","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":240,"y":1340,"wires":[]},{"id":"7d62d25d9e1c45bd","type":"ui_audio","z":"de6b68dedc3c24af","name":"","group":"9eb91d4467c6eec5","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":260,"y":1220,"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":490,"y":340,"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":640,"y":340,"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":790,"y":340,"wires":[]},{"id":"414abec3b8057999","type":"comment","z":"de6b68dedc3c24af","name":"Line Notify Message ","info":"","x":510,"y":300,"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":610,"y":620,"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":790,"y":620,"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":940,"y":620,"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":1090,"y":620,"wires":[]},{"id":"c69c2e0173b43d9a","type":"comment","z":"de6b68dedc3c24af","name":"Line Notify Message ","info":"","x":810,"y":660,"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":750,"y":1900,"wires":[["a6c1bb66ba045735"]]},{"id":"bb8df8633a1e1d3b","type":"comment","z":"de6b68dedc3c24af","name":"Line Notify Message ","info":"","x":570,"y":1920,"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":900,"y":1900,"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":870,"y":1960,"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":580,"y":1880,"wires":[["d2e0c88689bc12bc","472251da3f24ae8a","59dad5014aaaae13"]]},{"id":"6a690fd190600030","type":"ui_table","z":"de6b68dedc3c24af","group":"9eb91d4467c6eec5","name":"","order":26,"width":14,"height":12,"columns":[],"outputs":0,"cts":false,"x":1070,"y":1000,"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":1580,"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":1520,"wires":[["1ba07c0966a51c2c","773a18647f1dfab7"]]},{"id":"1ba07c0966a51c2c","type":"ui_text_input","z":"de6b68dedc3c24af","name":"","label":"State:","tooltip":"","group":"9eb91d4467c6eec5","order":11,"width":3,"height":1,"passthru":true,"mode":"text","delay":300,"topic":"","sendOnBlur":true,"className":"","topicType":"str","x":510,"y":1520,"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":1620,"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":1620,"wires":[]},{"id":"cd35f9e5e42aa4c5","type":"inject","z":"de6b68dedc3c24af","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"1","topic":"","payload":"","payloadType":"date","x":700,"y":60,"wires":[["50ac3fae0abc3d51"]]},{"id":"50ac3fae0abc3d51","type":"function","z":"de6b68dedc3c24af","name":"SET DEFAULT VALUE","func":"var del_idtemp=1;\nflow.set(\"idtemp\", del_idtemp);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":900,"y":60,"wires":[[]]},{"id":"442d1b9a3f27dc16","type":"comment","z":"de6b68dedc3c24af","name":"Set Default Value","info":"","x":700,"y":20,"wires":[]},{"id":"be8e0b2d2db12df5","type":"comment","z":"de6b68dedc3c24af","name":"alex9ufo/esp32/led","info":"","x":430,"y":100,"wires":[]},{"id":"8a8ebb530711e784","type":"ui_toast","z":"de6b68dedc3c24af","position":"top right","displayTime":"3","highlight":"","sendall":true,"outputs":0,"ok":"OK","cancel":"Cancel","raw":false,"className":"","topic":"","name":"","x":320,"y":1840,"wires":[]},{"id":"773a18647f1dfab7","type":"ui_toast","z":"de6b68dedc3c24af","position":"top right","displayTime":"3","highlight":"","sendall":true,"outputs":0,"ok":"OK","cancel":"Cancel","raw":false,"className":"","topic":"","name":"","x":540,"y":1480,"wires":[]},{"id":"f723bf8d47cfafad","type":"ui_toast","z":"de6b68dedc3c24af","position":"prompt","displayTime":"3","highlight":"","sendall":true,"outputs":1,"ok":"OK","cancel":"Cancel","raw":true,"className":"","topic":"","name":"","x":250,"y":1260,"wires":[["da052d481cedb18a"]]},{"id":"da052d481cedb18a","type":"function","z":"de6b68dedc3c24af","name":"function 68","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":410,"y":1260,"wires":[["d4b03f7f13a61517"],[]]},{"id":"399aff4307deb748","type":"ui_toast","z":"de6b68dedc3c24af","position":"prompt","displayTime":"3","highlight":"","sendall":true,"outputs":1,"ok":"OK","cancel":"Cancel","raw":true,"className":"","topic":"","name":"","x":290,"y":1300,"wires":[["8dec540d6eec04ca"]]},{"id":"8dec540d6eec04ca","type":"function","z":"de6b68dedc3c24af","name":"function 69","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":430,"y":1340,"wires":[["296f1c2d6e1196b3"],[]]},{"id":"cf4eaa9df81b4aee","type":"comment","z":"de6b68dedc3c24af","name":"alex9ufo/esp32/led_status","info":"","x":130,"y":340,"wires":[]},{"id":"5fb442caa3abbe77","type":"ui_toast","z":"de6b68dedc3c24af","position":"prompt","displayTime":"3","highlight":"","sendall":true,"outputs":1,"ok":"OK","cancel":"Cancel","raw":true,"className":"","topic":"","name":"","x":210,"y":1080,"wires":[["bdf0da8bc7b11836"]]},{"id":"bdf0da8bc7b11836","type":"function","z":"de6b68dedc3c24af","name":"function 70","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":370,"y":1080,"wires":[["08325cef072ffe08"],[]]},{"id":"57ca47c2065dbf1e","type":"link out","z":"de6b68dedc3c24af","name":"link out 16","mode":"link","links":["36417b0d4f4665a2"],"x":945,"y":1180,"wires":[]},{"id":"36417b0d4f4665a2","type":"link in","z":"de6b68dedc3c24af","name":"link in 18","links":["57ca47c2065dbf1e","68d9feaf2c5be655","9ab108d4b11f6116"],"x":615,"y":960,"wires":[["c87b80839d7841ac"]]},{"id":"1eb1cde2be18eec3","type":"link out","z":"de6b68dedc3c24af","name":"link out 17","mode":"link","links":["b10c787e4352fbb1"],"x":265,"y":140,"wires":[]},{"id":"b10c787e4352fbb1","type":"link in","z":"de6b68dedc3c24af","name":"link in 19","links":["1eb1cde2be18eec3"],"x":325,"y":140,"wires":[["b544f160142cab97"]]},{"id":"3a52ac71a8cfce0d","type":"ui_toast","z":"de6b68dedc3c24af","position":"top right","displayTime":"3","highlight":"","sendall":true,"outputs":0,"ok":"OK","cancel":"Cancel","raw":false,"className":"","topic":"","name":"","x":610,"y":180,"wires":[]},{"id":"bb74384bab8236ab","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":280,"y":220,"wires":[["ac096ff7da6702e1"]]},{"id":"68d9feaf2c5be655","type":"link out","z":"de6b68dedc3c24af","name":"link out 18","mode":"link","links":["36417b0d4f4665a2"],"x":775,"y":560,"wires":[]},{"id":"9ab108d4b11f6116","type":"link out","z":"de6b68dedc3c24af","name":"link out 19","mode":"link","links":["36417b0d4f4665a2"],"x":275,"y":960,"wires":[]},{"id":"c87d4c6d35ba8cf1","type":"ui_toast","z":"de6b68dedc3c24af","position":"top right","displayTime":"3","highlight":"","sendall":true,"outputs":0,"ok":"OK","cancel":"Cancel","raw":false,"className":"","topic":"","name":"","x":930,"y":1860,"wires":[]},{"id":"52ab84e02af9ebe7","type":"ui_toast","z":"de6b68dedc3c24af","position":"top right","displayTime":"3","highlight":"","sendall":true,"outputs":0,"ok":"OK","cancel":"Cancel","raw":false,"className":"","topic":"","name":"","x":350,"y":920,"wires":[]},{"id":"1cf2cb78b6f74fdb","type":"ui_toast","z":"de6b68dedc3c24af","position":"top right","displayTime":"3","highlight":"","sendall":true,"outputs":0,"ok":"OK","cancel":"Cancel","raw":false,"className":"","topic":"","name":"","x":570,"y":660,"wires":[]},{"id":"ac096ff7da6702e1","type":"function","z":"de6b68dedc3c24af","name":"function 71","func":"msg.payload=\"LED , \"+msg.payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":220,"wires":[["76186891a0198849","3a52ac71a8cfce0d"]]},{"id":"dba2810aa1fb6858","type":"sqlite","z":"de6b68dedc3c24af","mydb":"8d99f15e50a51fcd","sqlquery":"msg.topic","sql":"","name":"My_RFID_database","x":540,"y":500,"wires":[["f2cc4b9f6bf239ec"]]},{"id":"f2cc4b9f6bf239ec","type":"debug","z":"de6b68dedc3c24af","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":500,"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":320,"y":780,"wires":[["3f37ee591b72395b","2e778c488c1d1eed"]]},{"id":"6f8a0a1997c5c232","type":"sqlite","z":"de6b68dedc3c24af","mydb":"8d99f15e50a51fcd","sqlquery":"msg.topic","sql":"","name":"My_RFID_database","x":760,"y":1300,"wires":[["edacf88b26879f53"]]},{"id":"5434549be9f18323","type":"debug","z":"de6b68dedc3c24af","name":"debug 162","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":310,"y":1180,"wires":[]},{"id":"472251da3f24ae8a","type":"function","z":"de6b68dedc3c24af","name":"function","func":"var b=msg.payload;\nmsg.payload=\"Line Notify --->\"+b;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":740,"y":1860,"wires":[["c87d4c6d35ba8cf1"]]},{"id":"0862ac38abe4dfdb","type":"debug","z":"de6b68dedc3c24af","name":"debug 163","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":910,"y":1660,"wires":[]},{"id":"59dad5014aaaae13","type":"debug","z":"de6b68dedc3c24af","name":"debug 164","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":750,"y":2000,"wires":[]},{"id":"509ceaf0d23249fb","type":"ui_toast","z":"de6b68dedc3c24af","position":"top right","displayTime":"3","highlight":"","sendall":true,"outputs":0,"ok":"OK","cancel":"Cancel","raw":false,"className":"","topic":"","name":"","x":250,"y":540,"wires":[]},{"id":"2e778c488c1d1eed","type":"link out","z":"de6b68dedc3c24af","name":"link out 32","mode":"link","links":["50159ebadb2e3c14"],"x":415,"y":760,"wires":[]},{"id":"50159ebadb2e3c14","type":"link in","z":"de6b68dedc3c24af","name":"link in 30","links":["2e778c488c1d1eed"],"x":125,"y":1700,"wires":[["76cf80ba60d90816"]]},{"id":"9eb91d4467c6eec5","type":"ui_group","name":"Default","tab":"23af251c8a87881d","order":1,"disp":true,"width":"14","collapse":false,"className":""},{"id":"8d99f15e50a51fcd","type":"sqlitedb","db":"C:\\Users\\User\\.node-red\\2023RFID.db","mode":"RWC"},{"id":"841df58d.ee5e98","type":"mqtt-broker","name":"","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":"","autoConnect":true,"usetls":false,"compatmode":false,"protocolVersion":4,"keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"70940176.2b2d3","type":"mqtt-broker","name":"","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":4,"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}]


//====================================================
// esp32-wroom-32 devkit v1 default pins
//       I2C MODE SEL1:HI (ON) SEL2:LO (OFF)
//       SCL D22
//       SDA D21
//====================================================
// Define the interface type
#include <WiFi.h>           //WIFI
#include <PubSubClient.h>   //MQTT

// PN532 I2c Mode
#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>
#include <NfcAdapter.h>

// GPIO 2 D1
#define LED 2
// WiFi
WiFiClient espClient;
PubSubClient client(espClient);


// 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;

PN532_I2C pn532_i2c(Wire);
NfcAdapter nfc = NfcAdapter(pn532_i2c);

String tagId = "None";
byte nuidPICC[4];
String UID="";

//宣告任務Task1
TaskHandle_t Task1;

//MQTT 
char msg[50];
String json = "";
char jsonChar1[50];  //client.publish("alex9ufo/Esp32/RFID"
bool Flash = false;  //true
bool Timer = false;  //true
bool Send = false;  //true
bool ledState = false;
int Count= 0;

// Wifi reconnect
unsigned long previousMillis = 0;
unsigned long interval = 30000;
//===========================================================
//任務1副程式Task1_senddata
void Task1_senddata(void * pvParameters ) {
  //無窮迴圈
  for (;;) {
    //偵測上傳旗標是否為true
    Serial.println("Reading NFC tag");
    if (nfc.tagPresent())
    {
      NfcTag tag = nfc.read();
      Serial.println(tag.getTagType());
      Serial.print("UID: ");
      UID=tag.getUidString();
      //Serial.println(tag.getUidString()); // Retrieves the Unique Identification from your tag
      Serial.println(UID); // Retrieves the Unique Identification from your tag
      //=====================================  
      String json = UID;  
      // 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);
      }  
      else
      {
        Serial.print("MQTT not connected ");
      }
    }  
    else  
    {
      // PN532 probably timed out waiting for a card
      Serial.println("Timed out waiting for a card");
    }      
    //Task1休息,delay(X)不可省略
    delay(2000);
  }
}
//===========================================================
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect

    if (client.connect("esp32-client-")) {
      Serial.println("connected");
      // Subscribe
      client.subscribe("alex9ufo/esp32/led");
      client.subscribe("alex9ufo/esp32/RFID");      
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
      if (WiFi.status() != WL_CONNECTED)  {
        Serial.println("Reconnecting to WiFi...");
        WiFi.disconnect();
        WiFi.reconnect();
      }
    }
  }
}


//===========================================================
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 LED_Message() {
    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;    
    }

}
//===========================================================
void setup(void)
{
  Serial.begin(115200);
  Serial.println("System initialized");
  //======================================================
  // Connecting to a WiFi network
  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());
 
  // Setting LED pin as output
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);  // Turn off the LED initially
  //======================================================

  Serial.println(F("Ready!"));
  Serial.println(F("========================================"));
  Serial.println("*** Testing Module PN532 NFC TAG 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);  //topic1 = "alex9ufo/esp32/led";
  client.publish(topic,"ESP32 at work");

  nfc.begin();
  //在核心0啟動任務1
  xTaskCreatePinnedToCore(
    Task1_senddata, /*任務實際對應的Function*/
    "Task1",        /*任務名稱*/
    10000,          /*堆疊空間*/
    NULL,           /*無輸入值*/
    0,              /*優先序0*/
    &Task1,         /*對應的任務變數位址*/
    0);             /*指定在核心0執行 */
}


//==============================================
void loop()
{
 if (!client.connected()) {
      reconnect();
      Serial.print(" client not connected  reconnect ");
      delay(200);
    }
  client.loop();
  // Process LED message
  LED_Message();

  unsigned long currentMillis = millis();
  // if WiFi is down, try reconnecting
  if ((WiFi.status() != WL_CONNECTED) && (currentMillis - previousMillis >=interval)) {
    Serial.print(millis());
    Serial.println("Reconnecting to WiFi...");
    WiFi.disconnect();
    WiFi.reconnect();
    previousMillis = currentMillis;
  }

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


沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...