2024年11月22日 星期五

WOKWI DHT22 & LED , Node-Red + SQLite database

 WOKWI DHT22 & LED , Node-Red + SQLite database















Node-Red程式

[{"id":"6f0240353e534bbd","type":"comment","z":"d90cd4f43857dd3e","name":"TABLE DHT22STATUS","info":"//CREATE TABLE DHT22STATUS (\n//id INTEGER,\n//Temp TEXT,\n//Humi TEXT,\n//Date DATE,\n//Time TIME,\n//PRIMARY KEY (id)\n//);\n","x":160,"y":300,"wires":[]},{"id":"826ba79734e77bca","type":"sqlite","z":"d90cd4f43857dd3e","mydb":"4e71ffcc.32ba8","sqlquery":"msg.topic","sql":"","name":"DHT22_STATUS","x":530,"y":340,"wires":[["04fd1dd0adc2e130"]]},{"id":"4a334564f94f7fcc","type":"function","z":"d90cd4f43857dd3e","name":"CREATE DATABASE","func":"//CREATE TABLE DHT22STATUS (\n//id INTEGER,\n//Temp TEXT,\n//Humi TEXT,\n//Date DATE,\n//Time TIME,\n//PRIMARY KEY (id)\n//);\n//CREATE TABLE DHT22STATUS (id INTEGER,Temp TEXT,Humi TEXT,Date DATE,Time TIME,PRIMARY KEY (id));\nmsg.topic = \"CREATE TABLE DHT22STATUS (id INTEGER,Temp TEXT,Humi TEXT,Date DATE,Time TIME,PRIMARY KEY (id))\";\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":340,"wires":[["826ba79734e77bca"]]},{"id":"314685a445c2381d","type":"ui_button","z":"d90cd4f43857dd3e","name":"","group":"92ed45c1ead25706","order":7,"width":0,"height":0,"passthru":false,"label":"建立資料庫","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"建立資料庫","payloadType":"str","topic":"topic","topicType":"msg","x":130,"y":340,"wires":[["4a334564f94f7fcc","779d325bdf9e2cc0"]]},{"id":"779d325bdf9e2cc0","type":"ui_audio","z":"d90cd4f43857dd3e","name":"","group":"a4d2ace4206ad2ca","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":245,"y":400,"wires":[],"l":false},{"id":"7a108bc5db731170","type":"ui_button","z":"d90cd4f43857dd3e","name":"","group":"a4d2ace4206ad2ca","order":1,"width":6,"height":1,"passthru":false,"label":"ON","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"on","payloadType":"str","topic":"topic","topicType":"msg","x":130,"y":60,"wires":[["f396c3b64911fa04","a14b4674c6a34660","8697ad0d5894c796"]]},{"id":"449746f56ea6c82e","type":"ui_button","z":"d90cd4f43857dd3e","name":"","group":"a4d2ace4206ad2ca","order":2,"width":6,"height":1,"passthru":false,"label":"OFF","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"off","payloadType":"str","topic":"topic","topicType":"msg","x":130,"y":100,"wires":[["f396c3b64911fa04","a14b4674c6a34660","8697ad0d5894c796"]]},{"id":"128ad71539ada8a6","type":"ui_button","z":"d90cd4f43857dd3e","name":"","group":"a4d2ace4206ad2ca","order":3,"width":6,"height":1,"passthru":false,"label":"TIMER","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"timer","payloadType":"str","topic":"topic","topicType":"msg","x":140,"y":140,"wires":[["f396c3b64911fa04","a14b4674c6a34660","8697ad0d5894c796"]]},{"id":"4a4c2d1ec53a6f62","type":"ui_button","z":"d90cd4f43857dd3e","name":"","group":"a4d2ace4206ad2ca","order":4,"width":6,"height":1,"passthru":false,"label":"FLASH","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"flash","payloadType":"str","topic":"topic","topicType":"msg","x":140,"y":180,"wires":[["f396c3b64911fa04","a14b4674c6a34660","8697ad0d5894c796"]]},{"id":"f396c3b64911fa04","type":"ui_audio","z":"d90cd4f43857dd3e","name":"","group":"a4d2ace4206ad2ca","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":295,"y":120,"wires":[],"l":false},{"id":"04fd1dd0adc2e130","type":"debug","z":"d90cd4f43857dd3e","name":"debug ","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":710,"y":340,"wires":[]},{"id":"270e32067dabb9ef","type":"ui_button","z":"d90cd4f43857dd3e","name":"","group":"92ed45c1ead25706","order":4,"width":0,"height":0,"passthru":false,"label":"檢視資料庫資料","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"檢視資料","payloadType":"str","topic":"topic","topicType":"msg","x":140,"y":500,"wires":[["93dc37739f4c49ca","21e4e036d8987fea"]]},{"id":"93dc37739f4c49ca","type":"function","z":"d90cd4f43857dd3e","name":"檢視資料","func":"//CREATE TABLE DHT22STATUS (\n//id INTEGER,\n//Temp TEXT,\n//Humi TEXT,\n//Date DATE,\n//Time TIME,\n//PRIMARY KEY (id)\n//);\n\n//SELECT * FROM DHT22STATUS ORDER BY  id DESC LIMIT 50;\n\nmsg.topic = \"SELECT * FROM DHT22STATUS ORDER BY id DESC LIMIT 50\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":500,"wires":[["83e2a325774e26e2"]]},{"id":"f30298c1e6890b2d","type":"ui_table","z":"d90cd4f43857dd3e","group":"3a18026dc181f483","name":"","order":1,"width":10,"height":12,"columns":[],"outputs":0,"cts":false,"x":810,"y":500,"wires":[]},{"id":"83e2a325774e26e2","type":"sqlite","z":"d90cd4f43857dd3e","mydb":"4e71ffcc.32ba8","sqlquery":"msg.topic","sql":"","name":"DHT22_STATUS","x":550,"y":500,"wires":[["f30298c1e6890b2d"]]},{"id":"fc5c75ae20fdb73b","type":"ui_button","z":"d90cd4f43857dd3e","name":"","group":"92ed45c1ead25706","order":6,"width":3,"height":1,"passthru":false,"label":"刪除資料庫 ","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"刪除資料庫 ","payloadType":"str","topic":"topic","topicType":"msg","x":130,"y":740,"wires":[["7265060f2af01c7f","4e2701b4f43c7f2f"]]},{"id":"03f1dfd7ad10fccd","type":"function","z":"d90cd4f43857dd3e","name":"DROP DATABASE","func":"//CREATE TABLE DHT22STATUS (\n//id INTEGER,\n//Temp TEXT,\n//Humi TEXT,\n//Date DATE,\n//Time TIME,\n//PRIMARY KEY (id)\n//);\n\nmsg.topic = \"DROP TABLE DHT22STATUS\";\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":680,"wires":[["a92134f903f04a0a"]]},{"id":"a92134f903f04a0a","type":"sqlite","z":"d90cd4f43857dd3e","mydb":"4e71ffcc.32ba8","sqlquery":"msg.topic","sql":"","name":"DHT22_STATUS","x":770,"y":600,"wires":[["5664a000d04d8985"]]},{"id":"41311e4178c4f2b2","type":"ui_button","z":"d90cd4f43857dd3e","name":"","group":"92ed45c1ead25706","order":5,"width":3,"height":1,"passthru":false,"label":"刪除所有資料 ","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"刪除所有資料 ","payloadType":"str","topic":"topic","topicType":"msg","x":140,"y":640,"wires":[["4e2701b4f43c7f2f","b5cc53139a165a6d"]]},{"id":"a9df1f0ce94a2af1","type":"function","z":"d90cd4f43857dd3e","name":"DELETE ALL DATA","func":"//CREATE TABLE DHT22STATUS (\n//id INTEGER,\n//Temp TEXT,\n//Humi TEXT,\n//Date DATE,\n//Time TIME,\n//PRIMARY KEY (id)\n//);\n\nmsg.topic = \"DELETE from DHT22STATUS\";\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":580,"wires":[["a92134f903f04a0a"]]},{"id":"7265060f2af01c7f","type":"ui_toast","z":"d90cd4f43857dd3e","position":"prompt","displayTime":"3","highlight":"","sendall":true,"outputs":1,"ok":"OK","cancel":"Cancel","raw":true,"className":"","topic":"","name":"","x":330,"y":740,"wires":[["22df757e0314aef0"]]},{"id":"22df757e0314aef0","type":"function","z":"d90cd4f43857dd3e","name":"function Y/N ?","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":520,"y":740,"wires":[["03f1dfd7ad10fccd"],[]]},{"id":"4e2701b4f43c7f2f","type":"ui_audio","z":"d90cd4f43857dd3e","name":"","group":"92ed45c1ead25706","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":225,"y":680,"wires":[],"l":false},{"id":"b5cc53139a165a6d","type":"ui_toast","z":"d90cd4f43857dd3e","position":"prompt","displayTime":"3","highlight":"","sendall":true,"outputs":1,"ok":"OK","cancel":"Cancel","raw":true,"className":"","topic":"","name":"","x":350,"y":640,"wires":[["41e98cfedebb5434"]]},{"id":"41e98cfedebb5434","type":"function","z":"d90cd4f43857dd3e","name":"function  Y/N ?","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":520,"y":640,"wires":[["a9df1f0ce94a2af1"],[]]},{"id":"21e4e036d8987fea","type":"ui_audio","z":"d90cd4f43857dd3e","name":"","group":"a4d2ace4206ad2ca","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":215,"y":540,"wires":[],"l":false},{"id":"f20067d3f5f79578","type":"ui_button","z":"d90cd4f43857dd3e","name":"","group":"92ed45c1ead25706","order":3,"width":2,"height":1,"passthru":false,"label":"查詢一筆資料","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"查詢一筆資料","payloadType":"str","topic":"topic","topicType":"msg","x":140,"y":800,"wires":[["4e2701b4f43c7f2f","c180197f4e3bda67"]]},{"id":"21b30402f72f5e23","type":"function","z":"d90cd4f43857dd3e","name":"查詢一筆資料","func":"//\nvar id = msg.payload.id;\nvar s=global.get(\"SEL1\")\nmsg.topic=\"\";\nvar temp=\"\";\n\nif (s==1)\n{\n    temp =\"SELECT * FROM DHT22STATUS\";\n    temp=temp+\" WHERE id LIKE '\"+ id +\"'\";\n}\nmsg.topic=temp;\nglobal.set(\"SEL1\",0);\n\nreturn msg;\n\n//CREATE TABLE DHT22STATUS (\n//id INTEGER,\n//Temp TEXT,\n//Humi TEXT,\n//Date DATE,\n//Time TIME,\n//PRIMARY KEY (id)\n//);","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":680,"y":800,"wires":[["2cb949ba4cadd7aa"]]},{"id":"2cb949ba4cadd7aa","type":"sqlite","z":"d90cd4f43857dd3e","mydb":"4e71ffcc.32ba8","sqlquery":"msg.topic","sql":"","name":"DHT22_STATUS","x":790,"y":720,"wires":[["f30298c1e6890b2d"]]},{"id":"9f2f73aa6c35403b","type":"ui_button","z":"d90cd4f43857dd3e","name":"","group":"92ed45c1ead25706","order":1,"width":2,"height":1,"passthru":false,"label":"刪除一筆資料","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"刪除一筆資料","payloadType":"str","topic":"topic","topicType":"msg","x":140,"y":860,"wires":[["3ad5d199dc7b72bf","6e916cb7bb92703e"]]},{"id":"3ad5d199dc7b72bf","type":"ui_audio","z":"d90cd4f43857dd3e","name":"","group":"11d8514.a44dcaf","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":275,"y":900,"wires":[],"l":false},{"id":"9f833d46767c6d92","type":"ui_form","z":"d90cd4f43857dd3e","name":"","label":"輸入id","group":"92ed45c1ead25706","order":8,"width":0,"height":0,"options":[{"label":"ID","value":"id","type":"number","required":true,"rows":null}],"formValue":{"id":""},"payload":"","submit":"Submit","cancel":"Cancle","topic":"Form","topicType":"str","splitLayout":false,"className":"","x":530,"y":860,"wires":[["21b30402f72f5e23","95385e58736c8781","d54c996990ff5aed"]]},{"id":"95385e58736c8781","type":"function","z":"d90cd4f43857dd3e","name":"刪除一筆資料","func":"//\nvar id = msg.payload.id;\nvar s=global.get(\"SEL2\")\nmsg.topic=\"\";\nvar temp=\"\";\n\nif (s==2)\n{\n    temp =\"DELETE FROM DHT22STATUS\";\n    temp=temp+\" WHERE id LIKE '\"+ id +\"'\";\n}\n\nmsg.topic=temp;\nglobal.set(\"SEL2\",0)\nreturn msg;\n\n//CREATE TABLE DHT22STATUS (\n//id INTEGER,\n//Temp TEXT,\n//Humi TEXT,\n//Date DATE,\n//Time TIME,\n//PRIMARY KEY (id)\n//);","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":700,"y":860,"wires":[["29520d8aeaf20941","3e3ce5e5c22f23d4"]]},{"id":"5664a000d04d8985","type":"link out","z":"d90cd4f43857dd3e","name":"link out 62","mode":"link","links":["821d3319de5c02a3"],"x":895,"y":600,"wires":[]},{"id":"821d3319de5c02a3","type":"link in","z":"d90cd4f43857dd3e","name":"link in 60","links":["5664a000d04d8985"],"x":275,"y":540,"wires":[["93dc37739f4c49ca"]]},{"id":"29520d8aeaf20941","type":"sqlite","z":"d90cd4f43857dd3e","mydb":"4e71ffcc.32ba8","sqlquery":"msg.topic","sql":"","name":"DHT22_STATUS","x":950,"y":860,"wires":[["5664a000d04d8985"]]},{"id":"7921d7bb4a433c0e","type":"ui_button","z":"d90cd4f43857dd3e","name":"","group":"92ed45c1ead25706","order":2,"width":2,"height":1,"passthru":false,"label":"更正一筆資料","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"更正一筆資料","payloadType":"str","topic":"topic","topicType":"msg","x":140,"y":940,"wires":[["3ad5d199dc7b72bf","cc1524b3ce85e730"]]},{"id":"0922993ffe425812","type":"comment","z":"d90cd4f43857dd3e","name":"UPDATE查詢的WHERE","info":"UPDATE查詢的WHERE子句的基本語法如下:\n\nUPDATE table_name\nSET column1 = value1, column2 = value2...., columnN = valueN\nWHERE [condition];","x":160,"y":980,"wires":[]},{"id":"b97e40cdd6b22098","type":"function","z":"d90cd4f43857dd3e","name":"更正一筆資料","func":"//\nvar id = global.get(\"ID\");\n\nvar Temper = msg.payload.Temper;\nvar Humid = msg.payload.Humi;\nvar date = msg.payload.date;\nvar time = msg.payload.time;\n\nvar s=global.get(\"SEL3\")\nmsg.topic=\"\";\nvar temp=\"\";\n\nif (s==3)\n{\n    temp =\"update DHT22STATUS set \";\n    temp=temp+\"   Temp= '\" + Temper + \"'\";\n    temp=temp+\" , Humi= '\" + Humid  + \"'\";    \n    temp=temp+\" , Date= '\" + date  + \"'\";\n    temp=temp+\" , Time= '\" + time  + \"'\";\n    temp=temp+\"  WHERE id=\" + id;\n    \n    //msg.topic = \"update DHT22STATUS set ( id , Temp , Humi  , Date , Time ) VALUES ($id,  $status ,  $date ,  $time ) \" ;\n    //msg.payload = [id,status,date,time]\n}\nmsg.topic=temp;\n\nreturn msg;\n\n//CREATE TABLE DHT22STATUS (\n//id INTEGER,\n//Temp TEXT,\n//Humi TEXT,\n//Date DATE,\n//Time TIME,\n//PRIMARY KEY (id)\n//);\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":800,"y":980,"wires":[["29520d8aeaf20941","c905b674511f7799"]]},{"id":"c180197f4e3bda67","type":"function","z":"d90cd4f43857dd3e","name":"function flow set1","func":"var s1=1;\nglobal.set(\"SEL1\",s1);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":800,"wires":[["9f833d46767c6d92"]]},{"id":"6e916cb7bb92703e","type":"function","z":"d90cd4f43857dd3e","name":"function flow set2","func":"var s1=2;\nglobal.set(\"SEL2\",s1);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":860,"wires":[["9f833d46767c6d92"]]},{"id":"cc1524b3ce85e730","type":"function","z":"d90cd4f43857dd3e","name":"function flow set3","func":"var s1=3;\nglobal.set(\"SEL3\",s1);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":940,"wires":[["9f833d46767c6d92"]]},{"id":"66799965948d7085","type":"ui_form","z":"d90cd4f43857dd3e","name":"","label":"更正欄位","group":"92ed45c1ead25706","order":9,"width":0,"height":0,"options":[{"label":"Temperature","value":"Temper","type":"text","required":true,"rows":null},{"label":"Humidity","value":"Humi","type":"text","required":true,"rows":null},{"label":"DATE","value":"date","type":"text","required":true,"rows":null},{"label":"TIME","value":"time","type":"text","required":true,"rows":null}],"formValue":{"Temper":"","Humi":"","date":"","time":""},"payload":"","submit":"Submit","cancel":"Cancle","topic":"Form","topicType":"str","splitLayout":false,"className":"","x":620,"y":980,"wires":[["b97e40cdd6b22098"]]},{"id":"3e3ce5e5c22f23d4","type":"debug","z":"d90cd4f43857dd3e","name":"debug 324","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"topic","targetType":"msg","statusVal":"","statusType":"auto","x":910,"y":820,"wires":[]},{"id":"d54c996990ff5aed","type":"function","z":"d90cd4f43857dd3e","name":"Store ID資料","func":"//\nvar id = msg.payload.id;\nglobal.set(\"ID\",id)\nreturn msg;\n\n//CREATE TABLE DHT22STATUS (\n//id INTEGER,\n//Temp TEXT,\n//Humi TEXT,\n//Date DATE,\n//Time TIME,\n//PRIMARY KEY (id)\n//);","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":630,"y":920,"wires":[["66799965948d7085","90ec5c2a06dc131c"]]},{"id":"90ec5c2a06dc131c","type":"function","z":"d90cd4f43857dd3e","name":"查詢一筆資料","func":"//\nvar id = global.get(\"ID\");\nmsg.topic=\"\";\nvar temp=\"\";\ntemp =\"SELECT * FROM DHT22STATUS\";\ntemp=temp+\" WHERE id LIKE '\"+ id +\"'\";\n\nmsg.topic=temp;\n\nreturn msg;\n\n//CREATE TABLE DHT22STATUS (\n//id INTEGER,\n//Temp TEXT,\n//Humi TEXT,\n//Date DATE,\n//Time TIME,\n//PRIMARY KEY (id)\n//);","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":820,"y":920,"wires":[["2cb949ba4cadd7aa"]]},{"id":"c905b674511f7799","type":"debug","z":"d90cd4f43857dd3e","name":"debug 325","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"topic","targetType":"msg","statusVal":"","statusType":"auto","x":990,"y":980,"wires":[]},{"id":"431ca02417754844","type":"mqtt in","z":"d90cd4f43857dd3e","name":"LED status ","topic":"alex9ufo/wokwi/dht22","qos":"1","datatype":"utf8","broker":"584db2f88f8050c2","nl":false,"rap":true,"rh":0,"inputs":0,"x":100,"y":1280,"wires":[["f24f827457cd66df"]]},{"id":"f13742343cdf0af6","type":"function","z":"d90cd4f43857dd3e","name":"function ","func":"var temp=msg.payload[0];\nvar humi=msg.payload[1];\n\nmsg.payload=\" ---ESP32回來資料---\" + '溫度' + temp + '濕度' + humi;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":1220,"wires":[["d506fccb2fead3c9","d4a628bbb74220ad","af50936ec9348b60"]]},{"id":"d506fccb2fead3c9","type":"function","z":"d90cd4f43857dd3e","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":610,"y":1160,"wires":[["32a67e448f49163b"]]},{"id":"32a67e448f49163b","type":"http request","z":"d90cd4f43857dd3e","name":"","method":"POST","ret":"txt","paytoqs":false,"url":"https://notify-api.line.me/api/notify","tls":"","persist":false,"proxy":"","authType":"","x":760,"y":1160,"wires":[["170217955493ed65"]]},{"id":"170217955493ed65","type":"debug","z":"d90cd4f43857dd3e","name":"debug 326","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":910,"y":1160,"wires":[]},{"id":"05a2015e7195ca04","type":"comment","z":"d90cd4f43857dd3e","name":"Line Notify Message ","info":"","x":630,"y":1120,"wires":[]},{"id":"a14b4674c6a34660","type":"mqtt out","z":"d90cd4f43857dd3e","name":"Control LED","topic":"alex9ufo/wokwi/led","qos":"1","retain":"true","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"584db2f88f8050c2","x":350,"y":60,"wires":[]},{"id":"cefeec1ac1ca5e7f","type":"comment","z":"d90cd4f43857dd3e","name":"Topic : alex9ufo/wokwi/led","info":"### ## # **Topic : alex9ufo/wokwi/led******","x":470,"y":100,"wires":[]},{"id":"8697ad0d5894c796","type":"ui_text","z":"d90cd4f43857dd3e","group":"a4d2ace4206ad2ca","order":5,"width":6,"height":1,"name":"","label":"Node-RED發行到MQTT的資料 : ","format":"{{msg.payload}}","layout":"row-left","className":"","x":410,"y":180,"wires":[]},{"id":"df92bbeeb09f18c7","type":"ui_audio","z":"d90cd4f43857dd3e","name":"","group":"3a18026dc181f483","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":"","x":760,"y":1200,"wires":[]},{"id":"d4a628bbb74220ad","type":"delay","z":"d90cd4f43857dd3e","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":600,"y":1200,"wires":[["df92bbeeb09f18c7"]]},{"id":"f1abdc1260b065ed","type":"comment","z":"d90cd4f43857dd3e","name":"資料庫位置 C:\\Users\\User\\.node-red\\202409_DHT22.db","info":"","x":480,"y":300,"wires":[]},{"id":"1da4c0e301a2d608","type":"comment","z":"d90cd4f43857dd3e","name":"mqtt_server = \"broker.mqttgo.io\"","info":"const char* mqtt_server = \"broker.mqttgo.io\";\n//const char* mqtt_server = \"test.mosquitto.org\";","x":410,"y":20,"wires":[]},{"id":"af50936ec9348b60","type":"ui_text","z":"d90cd4f43857dd3e","group":"3a18026dc181f483","order":2,"width":10,"height":1,"name":"","label":"訂閱MQTT的資料 : ","format":"{{msg.payload}}","layout":"row-left","className":"","x":630,"y":1240,"wires":[]},{"id":"f24f827457cd66df","type":"function","z":"d90cd4f43857dd3e","name":"function 103","func":"msg.payload = msg.payload.split(\",\");\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":270,"y":1280,"wires":[["4aef162671169373","60705eadddf942b3","5c6e40bdc2ead748","f13742343cdf0af6","daf0202ce55f5c9b"]]},{"id":"f2096640664014f4","type":"debug","z":"d90cd4f43857dd3e","name":"debug 327","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":1280,"wires":[]},{"id":"4aef162671169373","type":"function","z":"d90cd4f43857dd3e","name":"function 104","func":"msg.payload=msg.payload[0];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":1280,"wires":[["f2096640664014f4"]]},{"id":"60705eadddf942b3","type":"function","z":"d90cd4f43857dd3e","name":"function 105","func":"msg.payload=msg.payload[1];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":1340,"wires":[["18a2cab9108622e9"]]},{"id":"18a2cab9108622e9","type":"debug","z":"d90cd4f43857dd3e","name":"debug 328","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":1340,"wires":[]},{"id":"5c6e40bdc2ead748","type":"debug","z":"d90cd4f43857dd3e","name":"debug 329","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":430,"y":1160,"wires":[]},{"id":"daf0202ce55f5c9b","type":"function","z":"d90cd4f43857dd3e","name":"INSERT","func":"var temp=msg.payload[0];\nvar humi=msg.payload[1];\n\nvar 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\n\nmsg.topic = \"INSERT INTO DHT22STATUS ( Temp, Humi , Date , Time ) VALUES ($temp, $humi, $var_date ,  $var_time ) \" ;\nmsg.payload = [temp, humi, var_date , var_time ]\nreturn msg;\n\n\n//CREATE TABLE DHT22STATUS (\n//id INTEGER,\n//Temp TEXT,\n//Humi TEXT,\n//Date DATE,\n//Time TIME,\n//PRIMARY KEY (id)\n//);\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":1060,"wires":[["cf0ec0632468bc8d"]]},{"id":"cf0ec0632468bc8d","type":"sqlite","z":"d90cd4f43857dd3e","mydb":"4e71ffcc.32ba8","sqlquery":"msg.topic","sql":"","name":"DHT22_STATUS","x":610,"y":1060,"wires":[["5ca84b1ded133e95"]]},{"id":"5ca84b1ded133e95","type":"link out","z":"d90cd4f43857dd3e","name":"link out 63","mode":"link","links":["6032fd42e42809e1"],"x":735,"y":1060,"wires":[]},{"id":"6032fd42e42809e1","type":"link in","z":"d90cd4f43857dd3e","name":"link in 61","links":["5ca84b1ded133e95"],"x":265,"y":460,"wires":[["93dc37739f4c49ca"]]},{"id":"4e71ffcc.32ba8","type":"sqlitedb","db":"C:\\Users\\User\\.node-red\\202409_DHT22.db","mode":"RWC"},{"id":"92ed45c1ead25706","type":"ui_group","name":"DHT22 資料庫命令區","tab":"62b7557c711185f0","order":3,"disp":true,"width":"6","collapse":false,"className":""},{"id":"a4d2ace4206ad2ca","type":"ui_group","name":"LED控制","tab":"62b7557c711185f0","order":1,"disp":true,"width":6,"collapse":false,"className":""},{"id":"3a18026dc181f483","type":"ui_group","name":"DHT22 資料庫顯示區","tab":"62b7557c711185f0","order":2,"disp":true,"width":10,"collapse":false,"className":""},{"id":"11d8514.a44dcaf","type":"ui_group","name":"控制","tab":"8f1ada5fa4df30e2","order":2,"disp":true,"width":"6","collapse":false,"className":""},{"id":"584db2f88f8050c2","type":"mqtt-broker","name":"broker.mqttgo.io","broker":"broker.mqttgo.io","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""},{"id":"62b7557c711185f0","type":"ui_tab","name":"WOKWI SQLite DHT22 LED","icon":"dashboard","order":136,"disabled":false,"hidden":false},{"id":"8f1ada5fa4df30e2","type":"ui_tab","name":"作業2-1","icon":"dashboard","disabled":false,"hidden":false}]


WOKWI程式


#include <WiFi.h>
#include <PubSubClient.h>
#include <LiquidCrystal_I2C.h>
#include "DHTesp.h"

const int DHT_PIN = 15;
const int LED = 2;

DHTesp dhtSensor;

#define I2C_ADDR    0x27
#define LCD_COLUMNS 20
#define LCD_LINES   4

LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);

// Update these with values suitable for your network.
const char* ssid =  "Wokwi-GUEST";   // your network SSID (name)
const char* password = "";   // your network password
//const char* mqtt_server = "broker.mqttdashboard.com";// choose your mqtt server
const char* mqtt_server = "broker.mqttgo.io";
//const char* mqtt_server = "test.mosquitto.org";

const char Pubtopic1[]  = "alex9ufo/wokwi/dht22";
//const char Pubtopic2[]  = "alex9ufo/hum";
const char Subtopic1[]  = "alex9ufo/wokwi/led";

WiFiClient espClient;
PubSubClient client(espClient);

long lastMsg = 0;
char msg[50];
int value = 0;

int Count= 0;
bool Flash = false;  //true
bool Timer = false;  //true

//=========================================================
void callback(char* topic, byte* payload, unsigned int length)
{
  Serial.print("Command from MQTT broker is : [");
  Serial.print(topic);
  Serial.print("]");
  Serial.println();
  Serial.print(" publish data is:");
  lcd.clear();
  String message="";
  {
    for(int i=0;i<length;i++)
    {
      Serial.print((char)payload[i]);  
      message +=(char)payload[i];

      lcd.setCursor(0, 0);
      lcd.print("alex9ufo LCD"); // Start Print text to Line 1
      lcd.setCursor(i, 1);
      lcd.write((char)payload[i]);
    }
  }
 
  Serial.println();
  //Serial.println(message);
  message.trim();

  if (String(topic)=="alex9ufo/wokwi/led") {
    if (message == "on") {
      digitalWrite(LED, LOW);  // Turn on the LED
      Serial.print("LED = on ");
    }

    if (message == "off" ) {
      digitalWrite(LED, HIGH); // Turn off the LED
      Serial.print("LED = off ");
    }

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

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

  }
  Serial.println();
} //end callback
//=========================================================
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected())
  {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP32Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    //if you MQTT broker has clientID,username and password
    //please change following line to    if (client.connect(clientId,userName,passWord))
    if (client.connect(clientId.c_str()))
    {
      Serial.println("connected");
      //once connected to MQTT broker, subscribe command if any
      //client.subscribe("alex9ufo/Command");
      client.subscribe(Subtopic1);

    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 6 seconds before retrying
      delay(6000);
    }
  }
} //end reconnect()


//===========================================================
//判斷 旗號Flash , Timer 是否為真
void LED_Message() {
  //判斷 旗號 Flash / timer  是否為真 ? 閃爍 定時
  if (Flash){
    digitalWrite(LED, !digitalRead(LED));
    delay(500);
  } //(Flash)

  if (Timer) {
    digitalWrite(LED, LOW);
    delay(500);
    Count=Count-1;
    if (Count == 0 ){
      Timer=false;
      digitalWrite(LED, HIGH);
     }
  } //(Timer)
}
//=========================================================
void setup_wifi() {
   delay(100);
  // We start by 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(".");
    }
  randomSeed(micros());
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}
//=========================================================


void setup() {
  Serial.begin(115200);
 
  pinMode(LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
 
  // Init
  lcd.init();
  lcd.backlight();

  // Print something
  lcd.setCursor(3, 0);
  lcd.print("Welcome to");
  lcd.setCursor(2, 1);
  lcd.print("lcd display");
  lcd.setCursor(5, 2);
  lcd.print("Simulator");
  lcd.setCursor(7, 3);
  lcd.print("Enjoy!");
}


void loop() {
    if (!client.connected()) {
    reconnect();
  }
  client.setCallback(callback);
  client.loop();

  LED_Message();

  unsigned long now = millis();
  if (now - lastMsg > 25000) {
    lastMsg = now;
    TempAndHumidity  data = dhtSensor.getTempAndHumidity();

    String temp = String(data.temperature, 2);
    Serial.print("Temperature: ");
    Serial.println(temp);
 
   
    String hum = String(data.humidity, 1);
    Serial.print("Humidity: ");
    Serial.println(hum);
    //client.publish("alex9ufo/temp", temp.c_str());
    //client.publish("alex9ufo/hum", hum.c_str());
    String TempHumi=temp + ','+ hum ;
    Serial.println(TempHumi);
    client.publish("alex9ufo/wokwi/dht22", TempHumi.c_str() );
  }
}



Diagram.json 線路json檔案

{
  "version": 1,
  "author": "4JN20EC077 - Supriya M",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-esp32-devkit-v1", "id": "esp", "top": -350.5, "left": 43, "attrs": {} },
    {
      "type": "wokwi-lcd1602",
      "id": "lcd1",
      "top": -339.2,
      "left": 293.6,
      "attrs": { "pins": "i2c" }
    },
    {
      "type": "wokwi-dht22",
      "id": "dht1",
      "top": -316.5,
      "left": -53.4,
      "attrs": { "humidity": "64", "temperature": "32.4" }
    },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": -493.2,
      "left": 195.8,
      "attrs": { "color": "red" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r1",
      "top": -417.6,
      "left": 191.45,
      "rotate": 90,
      "attrs": { "value": "1000" }
    }
  ],
  "connections": [
    [ "esp:TX0", "$serialMonitor:RX", "", [] ],
    [ "esp:RX0", "$serialMonitor:TX", "", [] ],
    [ "lcd1:SDA", "esp:D21", "cyan", [ "h0" ] ],
    [ "lcd1:VCC", "esp:3V3", "red", [ "h-15.76", "v116.31" ] ],
    [ "lcd1:GND", "esp:GND.1", "black", [ "h-25.36", "v106.61" ] ],
    [ "esp:D22", "lcd1:SCL", "magenta", [ "h28.5", "v-0.3" ] ],
    [ "esp:3V3", "dht1:VCC", "red", [ "h57.3", "v86.4", "h-278.4" ] ],
    [ "esp:GND.1", "dht1:GND", "black", [ "h28.5", "v67.1", "h-9.6" ] ],
    [ "dht1:SDA", "esp:D15", "blue", [ "v86.4", "h259.3", "v-96", "h-9.6" ] ],
    [ "led1:A", "r1:1", "green", [ "v0" ] ],
    [ "esp:3V3", "r1:2", "red", [ "v0", "h38.1", "v-211.2" ] ],
    [ "esp:D2", "led1:C", "violet", [ "h18.9", "v-231.1" ] ]
  ],
  "dependencies": {}
}


Libaaries.txt檔案

# Wokwi Library List # See https://docs.wokwi.com/guides/libraries # Automatically added based on includes: PubSubClient LiquidCrystal I2C DHT sensor library for ESPx

2024年11月21日 星期四

Messaging API作為替代方案

 LINE超好用功能要沒了!LINE Notify明年3月底終止服務,有什麼替代方案?

LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。





















//CHANNEL_ACCESS_TOKEN = 'Messaging API Token';


CHANNEL_ACCESS_TOKEN = 'E2ElxCQeoDszSHF0N1TJsyytnnae+HF1XZ733YVYj1RQcpjAT1oi2Fa2TScNbOz+q815fFVp4MuFW+UC4PsfKbvtc/o0OM6hchpxuQURZyvizGq+/cAz/TgCwJ9wQ4ebkQyEXEDzaYUZ/Dx3yaaBgwdB04t89/1O/w1cDnyilFU=';

USER_ID = 'Ua2f646d82a75d31dee884a6fdfa95f76';   //'使用者ID(不是Line ID)';



message = {

    type:'text',

    text:'目前日圓匯率:'+msg.payload.jpy

};

headers = {

    'Content-Type': 'application/json; charset=UTF-8',

    'Authorization': 'Bearer ' + CHANNEL_ACCESS_TOKEN,

};


payload = {

    'to':  USER_ID,

    'messages': [message]

};

msg.headers = headers;

msg.payload = payload;


return msg;



Node-Red程式


[{"id":"e3c78b4eb9b36766","type":"function","z":"e4f0d10c6b941ff5","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,"initialize":"","finalize":"","libs":[],"x":350,"y":80,"wires":[["d259989e5f0c687e","32a55009c3264c1a"]]},{"id":"d259989e5f0c687e","type":"http request","z":"e4f0d10c6b941ff5","name":"","method":"POST","ret":"txt","paytoqs":"ignore","url":"https://notify-api.line.me/api/notify","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":510,"y":80,"wires":[["9a3461f722903f80"]]},{"id":"9a3461f722903f80","type":"debug","z":"e4f0d10c6b941ff5","name":"debug 319","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":690,"y":80,"wires":[]},{"id":"e8c26f627214b891","type":"comment","z":"e4f0d10c6b941ff5","name":"Line Notify Message ","info":"","x":160,"y":20,"wires":[]},{"id":"95a71388533af3b8","type":"ui_audio","z":"e4f0d10c6b941ff5","name":"","group":"c9efaecb79309b70","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":"","x":510,"y":140,"wires":[]},{"id":"93f1a65f0c4b5f5e","type":"delay","z":"e4f0d10c6b941ff5","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":350,"y":140,"wires":[["95a71388533af3b8"]]},{"id":"f55a546528eea8fa","type":"inject","z":"e4f0d10c6b941ff5","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"哈囉","payloadType":"str","x":140,"y":80,"wires":[["e3c78b4eb9b36766","93f1a65f0c4b5f5e","9cde4d571ca039f8"]]},{"id":"9cde4d571ca039f8","type":"debug","z":"e4f0d10c6b941ff5","name":"debug 320","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":340,"y":40,"wires":[]},{"id":"32a55009c3264c1a","type":"debug","z":"e4f0d10c6b941ff5","name":"debug 321","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":540,"y":40,"wires":[]},{"id":"639381b4.decba","type":"inject","z":"e4f0d10c6b941ff5","name":"","repeat":"3600","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":520,"wires":[["63a4a95.925ad58"]]},{"id":"63a4a95.925ad58","type":"http request","z":"e4f0d10c6b941ff5","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"https://rate.bot.com.tw/xrt?Lang=zh-TW","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":280,"y":520,"wires":[["38a8cc.2269c734","62c92c02bdc3eb99"]]},{"id":"38a8cc.2269c734","type":"html","z":"e4f0d10c6b941ff5","name":"filter","property":"payload","outproperty":"payload","tag":".rate-content-cash.text-right.print_hide","ret":"html","as":"single","x":430,"y":520,"wires":[["29ecf860.b24508","91e26219.6161e"]]},{"id":"29ecf860.b24508","type":"function","z":"e4f0d10c6b941ff5","name":"Get JPY Currency 1","func":"var currency = 0.20;\n\nvar data = {\n    jpy: Number(msg.payload[15])\n}\n\nvar isLow =  flow.get('isLow') || false;\nmsg.payload = data\nif (data.jpy < currency && !isLow) {\n    isLow = true;\n    flow.set('isLow', isLow);\n    return msg;\n}\nif (data.jpy > currency && !isLow) {\n    isLow = false;\n    flow.set('isLow', isLow);\n    return msg;\n}","outputs":1,"noerr":0,"x":640,"y":520,"wires":[["8dc397w373.5403288","038ebd4c7f77884c"]]},{"id":"1d418e6a.0e52a2","type":"debug","z":"e4f0d10c6b941ff5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":830,"y":480,"wires":[]},{"id":"8dc397w373.5403288","type":"function","z":"e4f0d10c6b941ff5","name":"傳送信息","func":"//CHANNEL_ACCESS_TOKEN = 'Messaging API Token';\nCHANNEL_ACCESS_TOKEN = 'E2ElxCQeoDszSHF0N1TJsyytnnae+HF1XZ733YVYj1RQcpjAT1oi2Fa2TScNbOz+q815fFVp4MuFW+UC4PsfKbvtc/o0OM6hchpxuQURZyvizGq+/cAz/TgCwJ9wQ4ebkQyEXEDzaYUZ/Dx3yaaBgwdB04t89/1O/w1cDnyilFU=';\nUSER_ID = 'Ua2f646d82a75d31dee884a6fdfa95f76'; //'使用者ID(不是Line ID)';\nmessage = {\n    type:'text',\n    text:'目前日圓匯率:'+msg.payload.jpy\n};\nheaders = {\n    'Content-Type': 'application/json; charset=UTF-8',\n    'Authorization': 'Bearer ' + CHANNEL_ACCESS_TOKEN,\n};\npayload = {\n    'to':  USER_ID,\n    'messages': [message]\n};\nmsg.headers = headers;\nmsg.payload = payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":820,"y":580,"wires":[["ea9fccf4.509b4"]]},{"id":"ea9fccf4.509b4","type":"http request","z":"e4f0d10c6b941ff5","name":"Messaging API 傳送","method":"POST","ret":"txt","paytoqs":"ignore","url":"https://api.line.me/v2/bot/message/push","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":1000,"y":580,"wires":[["5c8619a1.7b35a8"]]},{"id":"a3af338a.4308","type":"http in","z":"e4f0d10c6b941ff5","name":"Messaging API 接收","url":"","method":"post","upload":false,"swaggerDoc":"","x":170,"y":700,"wires":[["3774c8fd.50b198"]]},{"id":"3774c8fd.50b198","type":"debug","z":"e4f0d10c6b941ff5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":370,"y":700,"wires":[]},{"id":"5c8619a1.7b35a8","type":"debug","z":"e4f0d10c6b941ff5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1190,"y":580,"wires":[]},{"id":"91e26219.6161e","type":"function","z":"e4f0d10c6b941ff5","name":"GET JP Currency 2","func":"//var jp = msg.payload[15];\nvar data = {\n    jpy: Number(msg.payload[15])\n}\nvar date = new Date();\nmsg.payload = data\nvar h = date.getHours();\nvar m = date.getMinutes();\nvar s = date.getSeconds();\nif(h<10){\n    h = '0'+h;\n}\nif(m<10){\n    m = '0' + m;\n}\nif(s<10){\n    s = '0' + s;\n}\nmsg.payload = '(' + h + ':' + m + ':' + s + ')\\n'+\n'日幣匯率:' + data.jpy ;\nreturn msg;","outputs":1,"noerr":0,"x":630,"y":480,"wires":[["1d418e6a.0e52a2"]]},{"id":"62c92c02bdc3eb99","type":"debug","z":"e4f0d10c6b941ff5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":450,"y":560,"wires":[]},{"id":"038ebd4c7f77884c","type":"debug","z":"e4f0d10c6b941ff5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":830,"y":520,"wires":[]},{"id":"a77dd5e79cda71ae","type":"function","z":"e4f0d10c6b941ff5","name":"LINE  Messaging API","func":"//CHANNEL_ACCESS_TOKEN = 'Messaging API Token';\nCHANNEL_ACCESS_TOKEN = 'E2ElxCQeoDszSHF0N1TJsyytnnae+HF1XZ733YVYj1RQcpjAT1oi2Fa2TScNbOz+q815fFVp4MuFW+UC4PsfKbvtc/o0OM6hchpxuQURZyvizGq+/cAz/TgCwJ9wQ4ebkQyEXEDzaYUZ/Dx3yaaBgwdB04t89/1O/w1cDnyilFU=';\nUSER_ID = 'Ua2f646d82a75d31dee884a6fdfa95f76'; //'使用者ID(不是Line ID)';\n\nvar msg1=msg.payload;\n\nmessage = {\n    type:'text',\n    text:'Line Developers傳送的訊息:'+msg1\n};\n\n\nheaders = {\n    'Content-Type': 'application/json; charset=UTF-8',\n    'Authorization': 'Bearer ' + CHANNEL_ACCESS_TOKEN,\n};\npayload = {\n    'to':  USER_ID,\n    'messages': [message]\n};\nmsg.headers = headers;\nmsg.payload = payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":280,"wires":[["b9697b72e188770b"]]},{"id":"b9697b72e188770b","type":"http request","z":"e4f0d10c6b941ff5","name":"LINE Developers Messaging API 傳送","method":"POST","ret":"txt","paytoqs":"ignore","url":"https://api.line.me/v2/bot/message/push","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":610,"y":280,"wires":[["868ceae98d227c48"]]},{"id":"868ceae98d227c48","type":"debug","z":"e4f0d10c6b941ff5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":890,"y":280,"wires":[]},{"id":"19fef339aba6cd45","type":"inject","z":"e4f0d10c6b941ff5","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"哈囉","payloadType":"str","x":130,"y":280,"wires":[["a77dd5e79cda71ae"]]},{"id":"3004785b1fd576ce","type":"comment","z":"e4f0d10c6b941ff5","name":"Line  Developers","info":"","x":140,"y":240,"wires":[]},{"id":"d2749fb6ac34f812","type":"comment","z":"e4f0d10c6b941ff5","name":"官方建議改用Messaging API作為替代方案","info":"LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。","x":220,"y":200,"wires":[]},{"id":"a2b7ba62fa9265d9","type":"node-line-bot-push-message","z":"e4f0d10c6b941ff5","name":"","useExternalMessage":false,"useExternalDestinationId":false,"hasDestinationId":true,"hasMessage":true,"message":"Hi, I am push message node 😊","messageType":0,"disabledNotification":false,"apiConfig":"b159d4d910a9bb05","credentials":{},"x":360,"y":340,"wires":[["6e6b1b08023263a3"]]},{"id":"6e6b1b08023263a3","type":"debug","z":"e4f0d10c6b941ff5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":570,"y":340,"wires":[]},{"id":"684deafd4ac910c9","type":"inject","z":"e4f0d10c6b941ff5","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":340,"wires":[["a2b7ba62fa9265d9"]]},{"id":"5d412d443b0f7ba3","type":"node-line-bot-push-message","z":"e4f0d10c6b941ff5","name":"","useExternalMessage":true,"useExternalDestinationId":false,"hasDestinationId":true,"hasMessage":true,"message":"Hi, I am push message node 😊","messageType":0,"disabledNotification":true,"apiConfig":"b159d4d910a9bb05","x":360,"y":400,"wires":[["67abc426251151d6"]]},{"id":"67abc426251151d6","type":"debug","z":"e4f0d10c6b941ff5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":570,"y":400,"wires":[]},{"id":"08175c5e5d2432e1","type":"debug","z":"e4f0d10c6b941ff5","name":"debug 322","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":330,"y":440,"wires":[]},{"id":"8ebe9e3e08e9d256","type":"inject","z":"e4f0d10c6b941ff5","name":"Send msg","props":[{"p":"payload"},{"p":"messageType","v":"0","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Normal Text Example 😊","payloadType":"str","x":140,"y":420,"wires":[["5d412d443b0f7ba3","08175c5e5d2432e1"]]},{"id":"c9efaecb79309b70","type":"ui_group","name":"LED控制區","tab":"fd234124f2ccfcf6","order":1,"disp":true,"width":4,"collapse":false,"className":""},{"id":"b159d4d910a9bb05","type":"line-messaging-api-config","channelName":"日幣匯率","hasToken":true,"hasSecret":true,"credentials":{}},{"id":"fd234124f2ccfcf6","type":"ui_tab","name":"2024-09-EX3","icon":"dashboard","order":135,"disabled":false,"hidden":false}]

2024年11月3日 星期日

2024_113_RFID 作業2 ESP32 Arduino程式

2024_113_RFID 作業2 ESP32 Arduino程式 



const char broker[] = "test.mosquitto.org";

//const char broker[] = "broker.mqtt-dashboard.com";

int        port     = 1883;

const char *SubTopic1 = "alex9ufo/2024/RFID/LED_control";

const char *PubTopic2 = "alex9ufo/2024/RFID/LED_status";

const char *PubTopic3 = "alex9ufo/2024/RFID/RFID_UID";

//const char *PubTopic4 = "alex9ufo/2024/RFID/RFID_PICC";





ESP32 與 MFRC522連接方式 (硬體連接)


GND     = GND   3.3V    = 3.3V

The following table shows the typical pin layout used:

 *                    MFRC522          ESP32     

 *                    Reader/PCD             

 * Signal          Pin                      Pin         

 * RST/Reset   RST                  GPIO27   

 * SPI SS          SDA(SS)          GPIO5     

 * SPI MOSI    MOSI              GPIO23    

 * SPI MISO    MISO             GPIO19    

 * SPI SCK     SCK                  GPIO18    

[1] (1, 2) Configurable, typically defined as RST_PIN in sketch/program.

[2] (1, 2) Configurable, typically defined as SS_PIN in sketch/program.

[3] The SDA pin might be labeled SS on some/older MFRC522 boards







(軟體)

Arduino 1.85版本 下載解壓縮或安裝

( 載點 https://www.npackd.org/p/ArduinoSA.arduino/1.8.15)

https://www.arduino.cc/en/software/OldSoftwareReleases  Arduino 1.8.16

參考網址

https://randomnerdtutorials.com/installing-the-esp32-board-in-arduino-ide-windows-instructions/


File> Preferences




https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json

Tools > Board > Boards Manager…







 Select the Port (if you don’t see the COM Port in your Arduino IDE, you need to install the CP210x USB to UART Bridge VCP Drivers):





 SKetch --- Include Library ---Manage Libraries


// Wifi 與 MQttClient 程式庫

#include <ArduinoMqttClient.h>

#include <WiFi.h>

//#include "arduino_secrets1.h"

//MFRC522 程式庫

#include <SPI.h>

#include <MFRC522.h>

安裝程式庫




貼上下方 "程式" 按上傳 

 Press the Upload button in the Arduino IDE


按住板子上的 BOOT 按鈕,再點 Arduino 上傳鈕
, 當編譯訊息出現 “Connecting...” 的時候,再放開 BOOT 按鈕。如此,應可成功上傳程式。




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_secrets1.h"


//MFRC522 程式庫

#include <SPI.h>

#include <MFRC522.h>


//GPIO 2 D1 Build in LED


//#define LED 13           //定義LED接腳

int LED = 13;

///////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  修改成自己的wifi ssid

const char pass[] = "alex9981";  // Enter Wi-Fi password  修改成自己的wifi pwd


//char ssid[] = "ASUS_D0";    // your network SSID (name)

//char pass[] = "night_9754";    // 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 *SubTopic1 = "alex9ufo/2024/RFID/LED_control";

const char *PubTopic2 = "alex9ufo/2024/RFID/LED_status";

const char *PubTopic3 = "alex9ufo/2024/RFID/RFID_UID";

//const char *PubTopic4 = "alex9ufo/2024/RFID/RFID_PICC";


const char willTopic[] = "alex9ufo/2024/RFID/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 Send = false;  //true

String LEDjson = "";

int Count= 0;

bool Flash = false;  //true

bool Timer = false;  //true


//===========================================================

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/2024/RFID/LED_control") {

  if (message == "on") {

    digitalWrite(LED, LOW);  // Turn on the LED

    //ledState = true;  //ledState = ture HIGH

    //設定 各個 旗號

    LEDjson ="ON";

    Send = true ;

    Flash = false;

    Timer = false;

    Serial.print("LED =");

    Serial.println(LEDjson);

  }


  if (message == "off" ) {

    digitalWrite(LED, HIGH); // Turn off the LED

    //ledState = false; //ledState = false LOW

    LEDjson ="OFF";

    Send = true ;

    Flash = false;

    Timer = false;

    Serial.print("LED =");

    Serial.println(LEDjson);

  }

  

  if (message == "flash" ) {

    digitalWrite(LED, HIGH); // 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);   

  }

  

    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() {

  //判斷 旗號 Flash / timer  是否為真 ? 閃爍 定時

  

  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(PubTopic2,  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(SubTopic1);

  // 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(SubTopic1, 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="";

    String json1=printHex(mfrc522.uid.uidByte, mfrc522.uid.size);

    json1.toUpperCase();

    json = json + json1;

    json.trim();


    bool retained = false;

    int qos = 1;

    bool dup = false;


    mqttClient.beginMessage(PubTopic3,  json.length(), retained, qos, dup);

    mqttClient.print(json);

    mqttClient.endMessage();

    

    /*

    json="";

    json = "PICC type: ";

    json =  json + Type;

    json.trim();

    

    retained = false;

    qos = 1;

    dup = false;


    mqttClient.beginMessage(PubTopic4,  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

  }

}

////===========================================================

2024_113_RFID 作業2 Node-Red程式

2024_113_RFID 作業2 Node-Red程式 


const char broker[] = "test.mosquitto.org";

//const char broker[] = "broker.mqtt-dashboard.com";

int        port     = 1883;

const char *SubTopic1 = "alex9ufo/2024/RFID/LED_control";

const char *PubTopic2 = "alex9ufo/2024/RFID/LED_status";

const char *PubTopic3 = "alex9ufo/2024/RFID/RFID_UID";









[{"id":"e0dec2aa1b0a5122","type":"ui_button","z":"945ffbf50a1b12c0","name":"","group":"ee086ab4c47d7171","order":1,"width":5,"height":1,"passthru":false,"label":"ON","tooltip":"","color":"","bgcolor":"green","className":"","icon":"","payload":"on","payloadType":"str","topic":"topic","topicType":"msg","x":90,"y":40,"wires":[["900ff9cafea98c7d","56231d75f39a8dbf","9acff35655511203"]]},{"id":"1954dc18d399e5dd","type":"ui_button","z":"945ffbf50a1b12c0","name":"","group":"ee086ab4c47d7171","order":2,"width":5,"height":1,"passthru":false,"label":"OFF","tooltip":"","color":"","bgcolor":"green","className":"","icon":"","payload":"off","payloadType":"str","topic":"topic","topicType":"msg","x":90,"y":80,"wires":[["900ff9cafea98c7d","56231d75f39a8dbf","9acff35655511203"]]},{"id":"e171636099d66df3","type":"ui_button","z":"945ffbf50a1b12c0","name":"","group":"ee086ab4c47d7171","order":3,"width":5,"height":1,"passthru":false,"label":"TIMER","tooltip":"","color":"","bgcolor":"green","className":"","icon":"","payload":"timer","payloadType":"str","topic":"topic","topicType":"msg","x":100,"y":120,"wires":[["900ff9cafea98c7d","56231d75f39a8dbf","9acff35655511203"]]},{"id":"3790a4bff86cdcc1","type":"ui_button","z":"945ffbf50a1b12c0","name":"","group":"ee086ab4c47d7171","order":4,"width":5,"height":1,"passthru":false,"label":"FLASH","tooltip":"","color":"","bgcolor":"green","className":"","icon":"","payload":"flash","payloadType":"str","topic":"topic","topicType":"msg","x":100,"y":160,"wires":[["900ff9cafea98c7d","56231d75f39a8dbf","9acff35655511203"]]},{"id":"900ff9cafea98c7d","type":"ui_audio","z":"945ffbf50a1b12c0","name":"","group":"ee086ab4c47d7171","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":275,"y":100,"wires":[],"l":false},{"id":"56231d75f39a8dbf","type":"mqtt out","z":"945ffbf50a1b12c0","name":"Control LED","topic":"alex9ufo/2024/RFID/LED_control","qos":"1","retain":"true","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"70940176.2b2d3","x":350,"y":40,"wires":[]},{"id":"9f313fca3147ae42","type":"comment","z":"945ffbf50a1b12c0","name":"alex9ufo/2024/RFID/LED_control","info":"const char broker[] = \"test.mosquitto.org\";\n//const char broker[] = \"broker.mqtt-dashboard.com\";\nint        port     = 1883;\n\nconst char *SubTopic1 = \"alex9ufo/2024/RFID/LED_control\";\nconst char *PubTopic2 = \"alex9ufo/2024/RFID/LED_status\";\nconst char *PubTopic3 = \"alex9ufo/2024/RFID/RFID_UID\";\n//const char *PubTopic4 = \"alex9ufo/2024/RFID/RFID_PICC\";\n\nconst char willTopic[] = \"alex9ufo/2024/RFID/Starting\";","x":570,"y":40,"wires":[]},{"id":"9acff35655511203","type":"ui_text","z":"945ffbf50a1b12c0","group":"ee086ab4c47d7171","order":5,"width":5,"height":1,"name":"","label":"(L)發行到MQTT的資料 : ","format":"{{msg.payload}}","layout":"row-left","className":"","x":390,"y":160,"wires":[]},{"id":"97f5f427bb8d85ba","type":"mqtt in","z":"945ffbf50a1b12c0","name":"LED status ","topic":"alex9ufo/2024/RFID/LED_status","qos":"2","datatype":"utf8","broker":"70940176.2b2d3","nl":false,"rap":true,"rh":0,"inputs":0,"x":100,"y":300,"wires":[["a3b518eab0cff724"]]},{"id":"a3b518eab0cff724","type":"function","z":"945ffbf50a1b12c0","name":"function ","func":"msg.payload=\" ---ESP32回來資料---\" +msg.payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":260,"y":300,"wires":[["45f9744a6361bd69","21965fd09e226d61"]]},{"id":"45f9744a6361bd69","type":"ui_text","z":"945ffbf50a1b12c0","group":"ee086ab4c47d7171","order":6,"width":5,"height":1,"name":"","label":"(L)訂閱MQTT的資料 : ","format":"{{msg.payload}}","layout":"row-left","className":"","x":480,"y":300,"wires":[]},{"id":"21965fd09e226d61","type":"ui_audio","z":"945ffbf50a1b12c0","name":"","group":"ee086ab4c47d7171","voice":"Microsoft Hanhan - Chinese (Traditional, Taiwan)","always":true,"x":395,"y":340,"wires":[],"l":false},{"id":"cc69246b826e5005","type":"mqtt in","z":"945ffbf50a1b12c0","name":"新增 RFID","topic":"alex9ufo/2024/RFID/RFID_UID","qos":"2","datatype":"auto-detect","broker":"70940176.2b2d3","nl":false,"rap":true,"rh":0,"inputs":0,"x":100,"y":360,"wires":[["ab3b7a92be9a2dab","5962dedde48ef666","03810c759ac92c93"]]},{"id":"ab3b7a92be9a2dab","type":"ui_toast","z":"945ffbf50a1b12c0","position":"top right","displayTime":"3","highlight":"","sendall":true,"outputs":0,"ok":"OK","cancel":"Cancel","raw":false,"className":"","topic":"","name":"","x":290,"y":360,"wires":[]},{"id":"a91daa79603e0aaf","type":"ui_text","z":"945ffbf50a1b12c0","group":"511a4bc4145bfda6","order":1,"width":10,"height":2,"name":"","label":"RFID的uid","format":"{{msg.payload}}","layout":"row-left","className":"","x":530,"y":400,"wires":[]},{"id":"5962dedde48ef666","type":"function","z":"945ffbf50a1b12c0","name":"function ","func":"msg.payload=\" ------ESP32回來UID------  \" +msg.payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":280,"y":400,"wires":[["a91daa79603e0aaf","21965fd09e226d61","58cc595d2ca7cf4d"]]},{"id":"d7857fd993703346","type":"comment","z":"945ffbf50a1b12c0","name":"(template) - displaying a one dimensional table","info":"(template) - displaying a one dimensional table","x":750,"y":660,"wires":[]},{"id":"58cc595d2ca7cf4d","type":"delay","z":"945ffbf50a1b12c0","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":400,"y":460,"wires":[["51282a4dd2573ecf"]]},{"id":"51282a4dd2573ecf","type":"function","z":"945ffbf50a1b12c0","name":"function ","func":"msg.payload=\"                \"  ;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":460,"wires":[["a91daa79603e0aaf"]]},{"id":"03810c759ac92c93","type":"function","z":"945ffbf50a1b12c0","name":"function ","func":"var no=global.get(\"countTemp\");\nvar data=msg.payload;\nmsg.payload={no,data}; \n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":160,"y":500,"wires":[["5c64e544d6b3502e","d3e7683a2bd816ec"]]},{"id":"9ac4df9bafb21449","type":"inject","z":"945ffbf50a1b12c0","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":200,"wires":[["125844c3789ebbdb"]]},{"id":"125844c3789ebbdb","type":"function","z":"945ffbf50a1b12c0","name":"function ","func":"var temp=0;\nglobal.set(\"countTemp\",temp);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":260,"y":200,"wires":[[]]},{"id":"5c64e544d6b3502e","type":"debug","z":"945ffbf50a1b12c0","name":"debug 308","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":330,"y":500,"wires":[]},{"id":"d3e7683a2bd816ec","type":"function","z":"945ffbf50a1b12c0","name":"function ","func":"var no=global.get(\"countTemp\") || 0;\nvar temp=global.get(\"rfid_store\") || [] ;\nvar data=msg.payload.data;\nmsg.payload={no,data}; \ntemp.push(msg.payload);\nno=no+1;\nglobal.set(\"countTemp\",no);\nglobal.set(\"rfid_store\",temp);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":540,"wires":[["5a77341e25ce21d9","9ac21bcf2bd6bf52"]]},{"id":"5a77341e25ce21d9","type":"debug","z":"945ffbf50a1b12c0","name":"debug 310","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":490,"y":540,"wires":[]},{"id":"e4b2acd4367c2e3a","type":"debug","z":"945ffbf50a1b12c0","name":"debug 309","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":450,"y":660,"wires":[]},{"id":"a7f458c8fe02ce92","type":"ui_button","z":"945ffbf50a1b12c0","name":"","group":"511a4bc4145bfda6","order":2,"width":0,"height":0,"passthru":false,"label":"清除stack堆疊資料","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"","payloadType":"str","topic":"topic","topicType":"msg","x":130,"y":660,"wires":[["baebe63027eeb131"]]},{"id":"baebe63027eeb131","type":"function","z":"945ffbf50a1b12c0","name":"function ","func":"global.set('rfid_store',[]);\nglobal.set('countTemp',0);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":660,"wires":[["e4b2acd4367c2e3a"]]},{"id":"9ac21bcf2bd6bf52","type":"function","z":"945ffbf50a1b12c0","name":"function ","func":"var temp=global.get(\"rfid_store\") || [] ;\nmsg.payload=temp; \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":480,"y":580,"wires":[["ce1e190051b16012","b7f115329e065945"]]},{"id":"ce1e190051b16012","type":"debug","z":"945ffbf50a1b12c0","name":"debug 311","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":580,"wires":[]},{"id":"8acc187fca3752e6","type":"ui_template","z":"945ffbf50a1b12c0","group":"511a4bc4145bfda6","name":"Display","order":3,"width":10,"height":7,"format":"<table id=\"table\" border=\"1\">\n     <tr>\n        <th>number </th> \n        <th>UID number</th>\n </tr>\n <tbody>\n <tr ng-repeat=\"row in msg.payload\">\n   <td ng-repeat=\"item in row\" >{{item}}</td>\n </tr>\n </tbody>\n</table>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"local","className":"","x":780,"y":620,"wires":[[]]},{"id":"b7f115329e065945","type":"function","z":"945ffbf50a1b12c0","name":"function ","func":"var temp=global.get(\"rfid_store\") || [] ;\nmsg.payload=temp; \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":640,"y":620,"wires":[["8acc187fca3752e6"]]},{"id":"262bc2f392ccdb78","type":"comment","z":"945ffbf50a1b12c0","name":"set global variable","info":"set global variable","x":130,"y":240,"wires":[]},{"id":"ed6c15c4665718c2","type":"comment","z":"945ffbf50a1b12c0","name":"MQTT參數","info":"const char broker[] = \"test.mosquitto.org\";\n//const char broker[] = \"broker.mqtt-dashboard.com\";\nint        port     = 1883;\n\nconst char *SubTopic1 = \"alex9ufo/2024/RFID/LED_control\";\nconst char *PubTopic2 = \"alex9ufo/2024/RFID/LED_status\";\nconst char *PubTopic3 = \"alex9ufo/2024/RFID/RFID_UID\";","x":500,"y":80,"wires":[]},{"id":"575629afc7416e13","type":"comment","z":"945ffbf50a1b12c0","name":"MQTT參數","info":"const char broker[] = \"test.mosquitto.org\";\n//const char broker[] = \"broker.mqtt-dashboard.com\";\nint        port     = 1883;\n\nconst char *SubTopic1 = \"alex9ufo/2024/RFID/LED_control\";\nconst char *PubTopic2 = \"alex9ufo/2024/RFID/LED_status\";\nconst char *PubTopic3 = \"alex9ufo/2024/RFID/RFID_UID\";","x":100,"y":420,"wires":[]},{"id":"2349a8efd984f6d2","type":"comment","z":"945ffbf50a1b12c0","name":"alex9ufo/2024/RFID/RFID_UID","info":"const char broker[] = \"test.mosquitto.org\";\n//const char broker[] = \"broker.mqtt-dashboard.com\";\nint        port     = 1883;\n\nconst char *SubTopic1 = \"alex9ufo/2024/RFID/LED_control\";\nconst char *PubTopic2 = \"alex9ufo/2024/RFID/LED_status\";\nconst char *PubTopic3 = \"alex9ufo/2024/RFID/RFID_UID\";\n//const char *PubTopic4 = \"alex9ufo/2024/RFID/RFID_PICC\";\n\nconst char willTopic[] = \"alex9ufo/2024/RFID/Starting\";","x":170,"y":460,"wires":[]},{"id":"75b40ac2892cc730","type":"comment","z":"945ffbf50a1b12c0","name":"clear global variable","info":"global.set('rfid_store',[]);\nglobal.set('countTemp',0);","x":130,"y":700,"wires":[]},{"id":"47e86dd019ee9ed7","type":"comment","z":"945ffbf50a1b12c0","name":"get  push data \"rfid_store\"","info":"","x":450,"y":620,"wires":[]},{"id":"6d8ceff921e14d5c","type":"comment","z":"945ffbf50a1b12c0","name":"push data into  \"rfid_store\" variable","info":"","x":240,"y":580,"wires":[]},{"id":"af78feeb3798187b","type":"comment","z":"945ffbf50a1b12c0","name":"set data  ","info":"","x":150,"y":540,"wires":[]},{"id":"51e794ffb163d823","type":"comment","z":"945ffbf50a1b12c0","name":"publish on , off , timer , flash to BROKER","info":"publish \"on , off , timer , flash\"  to BROKER\nby Topic : alex9ufo/2024/RFID/LED_control","x":460,"y":120,"wires":[]},{"id":"3571c490416d01bf","type":"comment","z":"945ffbf50a1b12c0","name":"清除 UID 號碼","info":"","x":550,"y":500,"wires":[]},{"id":"ee086ab4c47d7171","type":"ui_group","name":"LED控制顯示區","tab":"a83d954002d4689e","order":2,"disp":true,"width":5,"collapse":false,"className":""},{"id":"70940176.2b2d3","type":"mqtt-broker","name":"","broker":"test.mosquitto.org","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""},{"id":"511a4bc4145bfda6","type":"ui_group","name":"RFID區","tab":"a83d954002d4689e","order":1,"disp":true,"width":10,"collapse":false,"className":""},{"id":"a83d954002d4689e","type":"ui_tab","name":"2024_09_EX2_RFID","icon":"dashboard","order":134,"disabled":false,"hidden":false}]

  清除項目編譯快取 執行 flutter clean 重新取得套件依賴 執行 flutter pub get 完整重新編譯並執行 flutter run 1. 新增專案套件 (pubspec.yaml) 在 Flutter 專案根目錄下的 pubspec.yaml 中,新增 ...