2020年10月9日 星期五

eWeLink + Node-RED + Line + SQLite

 Node : https://flows.nodered.org/node/node-red-contrib-ewelink

https://ottoszika.github.io/node-red-contrib-ewelink/#/




Configuration eWeLink

The only things you need to configure are the credentials for the API connection. Once you use your first eWeLink node, you can set up it's eWeLink Credentials:

  • E-mail address
  • Password
  • Region (eu, us, ...)

https://ottoszika.github.io/node-red-contrib-ewelink/#/nodes?id=power-state-out

Power state node (out)

Sets the power state for a channel of a device. The payload can be any of the following strings:

  • on
  • off
  • toggle

Result example:

{
    "status": "ok",
    "state": "on"
}


[{"id":"c514db32.60f9c8","type":"mqtt in","z":"757141ea.2d1fb","name":"","topic":"alex9ufo/led/led_event1","qos":"1","datatype":"auto","broker":"841df58d.ee5e98","x":140,"y":100,"wires":[["4d582f43.270bb","8019ba07.693108","e59293e5.465e2","99f1d5d2.4faed8","4660a499.bae16c"]]},{"id":"4d582f43.270bb","type":"ui_text","z":"757141ea.2d1fb","group":"6c9116b.b62d4e8","order":0,"width":0,"height":0,"name":"","label":"MQTT Suscribe Data","format":"{{msg.payload}}","layout":"row-left","x":420,"y":80,"wires":[]},{"id":"a45ad258.88696","type":"ui_button","z":"757141ea.2d1fb","name":"","group":"6c9116b.b62d4e8","order":0,"width":"0","height":"0","passthru":false,"label":"LED 1 開","tooltip":"","color":"white","bgcolor":"","icon":"fa-circle","payload":"ON1","payloadType":"str","topic":"","x":120,"y":300,"wires":[["2352b24f.4bca7e"]]},{"id":"a03fec92.da8a5","type":"ui_button","z":"757141ea.2d1fb","name":"","group":"6c9116b.b62d4e8","order":0,"width":0,"height":0,"passthru":false,"label":"LED 1 關","tooltip":"","color":"black","bgcolor":"","icon":"fa-circle-o","payload":"OFF1","payloadType":"str","topic":"","x":120,"y":340,"wires":[["2352b24f.4bca7e"]]},{"id":"2352b24f.4bca7e","type":"mqtt out","z":"757141ea.2d1fb","name":"","topic":"alex9ufo/led/led_event1","qos":"1","retain":"false","broker":"841df58d.ee5e98","x":390,"y":360,"wires":[]},{"id":"8019ba07.693108","type":"function","z":"757141ea.2d1fb","name":"Format timestamp","func":"var date = new Date();\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 = msg.payload + ' --> Time:(' + h + ':' + m + ':' + s + ')' ;\n\nreturn msg;","outputs":1,"noerr":0,"x":150,"y":240,"wires":[["bd2ff63d.0c2fe8"]]},{"id":"bd2ff63d.0c2fe8","type":"function","z":"757141ea.2d1fb","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":330,"y":260,"wires":[["9b2f454c.970038"]]},{"id":"9b2f454c.970038","type":"http request","z":"757141ea.2d1fb","name":"","method":"POST","ret":"txt","paytoqs":false,"url":"https://notify-api.line.me/api/notify","tls":"","persist":false,"proxy":"","authType":"","x":480,"y":280,"wires":[["8c282554.c4e1d8"]]},{"id":"8c282554.c4e1d8","type":"debug","z":"757141ea.2d1fb","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":630,"y":280,"wires":[]},{"id":"e59293e5.465e2","type":"debug","z":"757141ea.2d1fb","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":390,"y":40,"wires":[]},{"id":"794a7822.68aa68","type":"ui_audio","z":"757141ea.2d1fb","name":"","group":"6c9116b.b62d4e8","voice":"zh-TW","always":true,"x":340,"y":180,"wires":[]},{"id":"84f452a.2771eb","type":"ui_button","z":"757141ea.2d1fb","name":"","group":"6c9116b.b62d4e8","order":0,"width":0,"height":0,"passthru":false,"label":"LED 1 開關反向","tooltip":"","color":"blue","bgcolor":"","icon":"fa-circle-o","payload":"TOGGLE1","payloadType":"str","topic":"","x":140,"y":380,"wires":[["2352b24f.4bca7e"]]},{"id":"99f1d5d2.4faed8","type":"function","z":"757141ea.2d1fb","name":"","func":"var st1;\nif (msg.payload === \"ON1\") {\n   st1=\"LED 1 開\"; \n} \nelse if (msg.payload === \"OFF1\") {\n  st1=\"LED 1 關\";\n}\nelse if (msg.payload === \"TOGGLE1\") {\n  st1=\"LED 1 ON OFF 交換\";\n}\n\nmsg.payload=st1;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":170,"y":180,"wires":[["794a7822.68aa68","956549a0.f9adb8"]]},{"id":"956549a0.f9adb8","type":"function","z":"757141ea.2d1fb","name":"INSERT","func":"msg.topic = \"INSERT INTO eWeLink (Time, Status) VALUES (?,?)\";\n//CREATE TABLE \"eWeLink\" (\"id\"\tINTEGER,\"Time\"\tTEXT,\"Status\"\tTEXT,PRIMARY KEY(\"id\" AUTOINCREMENT))\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();     //秒\n\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}\n\nvar hms= yyyy + '/'+ MM + '/'+ dd + ' ' + h + ':' + m + ':' + s ;\n//var id= Date.now() ;\n//msg.payload = [id ,hms, msg.payload];\nmsg.payload = [hms, msg.payload];\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":340,"y":220,"wires":[["d757c32c.1a1ba"]]},{"id":"d757c32c.1a1ba","type":"sqlite","z":"757141ea.2d1fb","mydb":"19f59ce9.3edc23","sqlquery":"msg.topic","sql":"","name":"LED Status","x":490,"y":220,"wires":[["447a0a0d.4cc944"]]},{"id":"447a0a0d.4cc944","type":"debug","z":"757141ea.2d1fb","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":220,"wires":[]},{"id":"797471d5.3a6e7","type":"comment","z":"757141ea.2d1fb","name":"publish 到 HiveMQ Broker ","info":"將 alex9ufo/led/led_event 發行到(publish)HiveMQ Broker \n給 Arduino 訂閱(Subscribe)","x":370,"y":320,"wires":[]},{"id":"c6f16bf8.372bf8","type":"comment","z":"757141ea.2d1fb","name":"向 HiveMQ Broker 訂閱subscribe","info":"將  Arduino 發行到(publish)HiveMQ Broker alex9ufo/led/led_status \n給 Node-red 或 MQTTB-Box 訂閱(Subscribe)","x":150,"y":40,"wires":[]},{"id":"3e5a379.bde7ac8","type":"debug","z":"757141ea.2d1fb","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":140,"wires":[]},{"id":"4d36f710.f78328","type":"ewelink-power-state-write","z":"757141ea.2d1fb","name":"eWeLink-1","deviceId":"1000dad4d5","channel":"1","auth":"53e58455.b9d5dc","x":490,"y":140,"wires":[["3e5a379.bde7ac8"]]},{"id":"4660a499.bae16c","type":"function","z":"757141ea.2d1fb","name":"","func":"var st1;\nif (msg.payload === \"ON1\") {\n   st1=\"ON\"; \n} \nelse if (msg.payload === \"OFF1\") {\n  st1=\"OFF\";\n}\nelse if (msg.payload === \"TOGGLE1\") {\n  st1=\"TOGGLE\";\n}\n\nmsg.payload=st1;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":330,"y":140,"wires":[["4d36f710.f78328","e1f6000a.6c295"]]},{"id":"e1f6000a.6c295","type":"debug","z":"757141ea.2d1fb","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":640,"y":100,"wires":[]},{"id":"841df58d.ee5e98","type":"mqtt-broker","z":"","name":"","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"6c9116b.b62d4e8","type":"ui_group","z":"","name":"Storing IOT Data ","tab":"eeb8e179.a47a4","order":1,"disp":true,"width":"6","collapse":true},{"id":"19f59ce9.3edc23","type":"sqlitedb","z":"","db":"eWeLink-1.db","mode":"RWC"},{"id":"53e58455.b9d5dc","type":"ewelink-credentials","z":""},{"id":"eeb8e179.a47a4","type":"ui_tab","z":"","name":"eWeLink","icon":"dashboard","disabled":false,"hidden":false}]

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

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