Flow Nodered per aprire il garage

Questo articolo ti aiuta a configurare il Raspberry per utilizzarlo come sistema di controllo via Web dell’apertura di un garage o cancello automatico.

In particolare viene spiegato come aggiungere un flow Nodered per l’apertura del garage.

Prerequisiti

Per eseguire questa procedura devi aver già:

  • installato un Raspberry nella tua rete domestica
  • installato Il sistema operativo Raspbian
  • abilitato Nodered
  • installato Arduino

Per ottenere i prerequisiti leggi prima questo articolo:

Installare il software del Raspberry

Controllare Nodered

Con un computer, collegarsi sulla stessa rete dove è acceso il raspberry con indirizzo:

192.168.1.102

Aprire il browser e digitare nella barra degli indirizzi:

http://192.168.1.102:1880

Dovrebbe aprirsi una pagina

Nodered
pagina Nodered

 

Installazione del flow di controllo

Selezionare tutto il testo della riga qui sotto ed eseguire copia o CTRL+C:

[{"id":"2dbfbbf4.ae94a4","type":"inject","z":"94fae675.1d692","name":"apre box","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":324.3333282470703,"y":105.33331680297852,"wires":[["34703531.eed92a"]]},{"id":"aa47cb32.612a48","type":"serial in","z":"94fae675.1d692","name":"legge risposte","serial":"9564af5.de8a25","x":200,"y":300,"wires":[["8fee9d74.4a1cd","e4aec66d.35b6f","ba9c5a36.93fd18","5c20250e.7577a4","490301d5.6de9a"]]},{"id":"874c3e54.e72b6","type":"serial out","z":"94fae675.1d692","name":"invia comando","serial":"9564af5.de8a25","x":1240,"y":140,"wires":[]},{"id":"34703531.eed92a","type":"function","z":"94fae675.1d692","name":"comando apri","func":"msg.payload=\"set output:0=0 4\\nstatus\\n\"\nreturn msg;","outputs":1,"noerr":0,"x":672.3333129882812,"y":103.9999885559082,"wires":[["874c3e54.e72b6"]]},{"id":"63746e68.d27258","type":"function","z":"94fae675.1d692","name":"comando chiudi","func":"msg.payload=\"set output:2=0 4\\nstatus\\n\"\nreturn msg;","outputs":1,"noerr":0,"x":687.1666259765625,"y":197.33333206176758,"wires":[["874c3e54.e72b6"]]},{"id":"aeee81b7.da0cf","type":"inject","z":"94fae675.1d692","name":"chiude box","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":335.8333282470703,"y":201.33334350585938,"wires":[["63746e68.d27258"]]},{"id":"8744b55f.d16ba8","type":"inject","z":"94fae675.1d692","name":"legge stato","topic":"","payload":"","payloadType":"date","repeat":"60","crontab":"","once":true,"x":310,"y":60,"wires":[["bcb5fff.9f0cc"]]},{"id":"bcb5fff.9f0cc","type":"function","z":"94fae675.1d692","name":"comando status","func":"msg.payload=\"status\\n\"\nreturn msg;","outputs":1,"noerr":0,"x":660,"y":60,"wires":[["874c3e54.e72b6"]]},{"id":"8fee9d74.4a1cd","type":"debug","z":"94fae675.1d692","name":"","active":false,"console":"false","complete":"false","x":270,"y":500,"wires":[]},{"id":"d89d1082.08f57","type":"websocket in","z":"94fae675.1d692","name":"comando da websocket","server":"aaf6a7c3.cd43a8","client":"","x":285,"y":875,"wires":[["f945d14a.6144f","5ca2bae.cffdbc4","7d8c690d.bb57b","5172868d.527da","6cef6712.54dcb","8c172a11.6d7928"]]},{"id":"f945d14a.6144f","type":"switch","z":"94fae675.1d692","name":"richiesta lista sensori","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"get sensors","vt":"str"}],"checkall":"true","outputs":1,"x":590.7498474121094,"y":768.4166030883789,"wires":[["e018d9b5.ed7cc8"]]},{"id":"e018d9b5.ed7cc8","type":"function","z":"94fae675.1d692","name":"genera lista sensori","func":"msg.payload={};\nmsg.payload.sensors={};\nmsg.payload.sensors[\"Apre box\"]={};\nmsg.payload.sensors[\"Chiude box\"]={};\nmsg.payload.sensors[\"Luce\"]={};\nmsg.payload.sensors[\"Box\"]={};\nreturn msg;","outputs":1,"noerr":0,"x":807.7498474121094,"y":768.4166030883789,"wires":[["9dbd507.e9de8b","423f646f.f1893c"]]},{"id":"9dbd507.e9de8b","type":"debug","z":"94fae675.1d692","name":"Risposta","active":false,"console":"false","complete":"payload","x":1016.4165140787759,"y":777.7499364217122,"wires":[]},{"id":"5ca2bae.cffdbc4","type":"switch","z":"94fae675.1d692","name":"richiesto status","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"status","vt":"str"}],"checkall":"true","outputs":1,"x":580,"y":1080,"wires":[["874c3e54.e72b6"]]},{"id":"7d8c690d.bb57b","type":"function","z":"94fae675.1d692","name":"Apre box","func":"msg.INsensor=\"Apre box\";\nmsg.OUTsensor=\"0\";\nmsg.OUTaccendi=\"set output:\"+msg.OUTsensor+\"=0 4\\nstatus\";\nmsg.OUTspegni=\"status\"\nreturn msg;\n","outputs":1,"noerr":0,"x":866.4165954589844,"y":826.0832748413086,"wires":[["14de4734.c0f9a1"]]},{"id":"5172868d.527da","type":"function","z":"94fae675.1d692","name":"Chiude box","func":"msg.INsensor=\"Chiude box\";\nmsg.OUTsensor=\"2\";\nmsg.OUTaccendi=\"set output:\"+msg.OUTsensor+\"=0 4\\nstatus\";\nmsg.OUTspegni=\"status\"\nreturn msg;\n","outputs":1,"noerr":0,"x":887.2497253417969,"y":871.0832824707031,"wires":[["14de4734.c0f9a1"]]},{"id":"6cef6712.54dcb","type":"function","z":"94fae675.1d692","name":"Luce","func":"msg.INsensor=\"Luce\";\nmsg.OUTsensor=\"1\";\nmsg.OUTaccendi=\"set output:\"+msg.OUTsensor+\"=1\\nstatus\";\nmsg.OUTspegni=\"set output:\"+msg.OUTsensor+\"=0\\nstatus\"\nreturn msg;","outputs":1,"noerr":0,"x":853.9165344238281,"y":912.7499389648438,"wires":[["14de4734.c0f9a1"]]},{"id":"14de4734.c0f9a1","type":"function","z":"94fae675.1d692","name":"traduci","func":"var INsensor=msg.INsensor;\nvar OUTsensor=msg.OUTsensor;\nvar INaccendi=\"accendi \"+INsensor;\nvar INspegni=\"spegni \"+INsensor;\nvar OUTaccendi=msg.OUTaccendi;\nvar OUTspegni=msg.OUTspegni;\nif(msg.payload==INaccendi){\n msg.payload=OUTaccendi;\n return msg;\n}\nif(msg.payload==INspegni){\n msg.payload=OUTspegni;\n return msg;\n}\nreturn null;","outputs":1,"noerr":0,"x":1231.416534423828,"y":836.0832824707031,"wires":[["874c3e54.e72b6"]]},{"id":"e4aec66d.35b6f","type":"switch","z":"94fae675.1d692","name":"Apre box","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"status: out0 low","vt":"str"},{"t":"cont","v":"status: out0 high","vt":"str"}],"checkall":"true","outputs":2,"x":780,"y":240,"wires":[["4e597552.708904","750bc868.f7fa68"],["f11f6c5c.0184b8","750bc868.f7fa68"]]},{"id":"ba9c5a36.93fd18","type":"switch","z":"94fae675.1d692","name":"Chiude box","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"status: out2 low","vt":"str"},{"t":"cont","v":"status: out2 high","vt":"str"}],"checkall":"true","outputs":2,"x":750,"y":300,"wires":[["5bc60d27.8baa6c","750bc868.f7fa68"],["c84f79cb.ba2c58","750bc868.f7fa68"]]},{"id":"4e597552.708904","type":"function","z":"94fae675.1d692","name":"Risposta Apre box premuto","func":"msg.payload={};\nmsg.payload[\"status: Apre box\"]={sensor:\"PT\",status:\"premuto\"};\nmsg.payload[\"sensor buttons: Apre box\"]={sensor:\"Apre box\"};\nmsg.payload[\"sensor buttons: Apre box\"].button1={description:\"non premere\",command:\"spegni Apre box\"};\n\nreturn msg;","outputs":1,"noerr":0,"x":1236.523681640625,"y":220.71426391601562,"wires":[["423f646f.f1893c"]]},{"id":"f11f6c5c.0184b8","type":"function","z":"94fae675.1d692","name":"Risposta Apre box non premuto","func":"msg.payload={};\nmsg.payload[\"status: Apre box\"]={sensor:\"Apre box\",status:\"non premuto\"};\nmsg.payload[\"sensor buttons: Apre box\"]={sensor:\"Apre box\"};\nmsg.payload[\"sensor buttons: Apre box\"].button1={description:\"apri\",command:\"accendi Apre box\"};\n\n\nreturn msg;","outputs":1,"noerr":0,"x":1252.5240478515625,"y":256.3810729980469,"wires":[["423f646f.f1893c"]]},{"id":"5bc60d27.8baa6c","type":"function","z":"94fae675.1d692","name":"Risposta Chiude box premuto","func":"msg.payload={};\nmsg.payload[\"status: Chiude box\"]={sensor:\"Chiude box\",status:\"premuto\"};\nmsg.payload[\"sensor buttons: Chiude box\"]={sensor:\"Chiude box\"};\nmsg.payload[\"sensor buttons: Chiude box\"].button1={description:\"non premere\",command:\"spegni Chiude box\"};\n\nreturn msg;","outputs":1,"noerr":0,"x":1250,"y":292,"wires":[["423f646f.f1893c"]]},{"id":"c84f79cb.ba2c58","type":"function","z":"94fae675.1d692","name":"Risposta Chiude box non premuto","func":"msg.payload={};\nmsg.payload[\"status: Chiude box\"]={sensor:\"Chiude box\",status:\"non premuto\"};\nmsg.payload[\"sensor buttons: Chiude box\"]={sensor:\"Chiude box\"};\nmsg.payload[\"sensor buttons: Chiude box\"].button1={description:\"chiudi\",command:\"accendi Chiude box\"};\n\n\nreturn msg;","outputs":1,"noerr":0,"x":1270.2384033203125,"y":329.04766845703125,"wires":[["423f646f.f1893c"]]},{"id":"5c20250e.7577a4","type":"switch","z":"94fae675.1d692","name":"Box","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"status: Box aperto1","vt":"str"},{"t":"cont","v":"status: Box chiuso1","vt":"str"}],"checkall":"false","outputs":2,"x":390,"y":420,"wires":[["b7a3857a.2993f","750bc868.f7fa68","8fd4f87.359e908"],["e2c39cb2.6378f","750bc868.f7fa68","8546d4e5.9839a"]],"outputLabels":["aperto","chiuso"]},{"id":"b7a3857a.2993f","type":"function","z":"94fae675.1d692","name":"Risposta Box aperto","func":"msg.payload={};\nmsg.payload[\"status: Box\"]={sensor:\"Box\",status:\"aperto\"};\nmsg.payload[\"sensor buttons: Box\"]={sensor:\"Box\"};\nmsg.payload[\"sensor buttons: Box\"].button0={description:\"chiudi\",command:\"spegni Box\"};\n\nreturn msg;","outputs":1,"noerr":0,"x":1232.90478515625,"y":392.2379150390625,"wires":[["423f646f.f1893c"]]},{"id":"e2c39cb2.6378f","type":"function","z":"94fae675.1d692","name":"Risposta Box chiuso","func":"msg.payload={};\nmsg.payload[\"status: Box\"]={sensor:\"Box\",status:\"chiuso\"};\nmsg.payload[\"sensor buttons: Box\"]={sensor:\"Box\"};\nmsg.payload[\"sensor buttons: Box\"].button1={description:\"apri\",command:\"accendi Box\"};\n\nreturn msg;","outputs":1,"noerr":0,"x":1225.333251953125,"y":448.8570556640625,"wires":[["423f646f.f1893c"]]},{"id":"82717a8f.4bdf6","type":"function","z":"94fae675.1d692","name":"CurrentDoorState:true","func":"msg.payload={CurrentDoorState:true\n}\n//msg.payload={CurrentDoorState:true,\n// TargetDoorState:true\n//}\n//msg.mittente=\"sensori\";\nreturn msg;\n\n//Try one of these: Name, CurrentDoorState, TargetDoorState, ObstructionDetected, LockCurrentState, LockTargetState, Name","outputs":1,"noerr":0,"x":360,"y":1417,"wires":[["3e653eac.32147a","8971e941.8eced8"]]},{"id":"565accaf.c1c69c","type":"inject","z":"94fae675.1d692","name":"simula chiuso","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":120,"y":1439,"wires":[["82717a8f.4bdf6"]]},{"id":"3cc10acb.7304c6","type":"function","z":"94fae675.1d692","name":"CurrentDoorState:false","func":"msg.payload={CurrentDoorState:false}\n//msg.payload={CurrentDoorState:false,\n//TargetDoorState:false}\n//msg.mittente=\"sensori\";\nreturn msg;\n\n//Try one of these: Name, CurrentDoorState, TargetDoorState, ObstructionDetected, LockCurrentState, LockTargetState, Name","outputs":1,"noerr":0,"x":370,"y":1319,"wires":[["3e653eac.32147a","8971e941.8eced8"]]},{"id":"703b654b.ab7f6c","type":"inject","z":"94fae675.1d692","name":"simula aperto","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":170,"y":1319,"wires":[["3cc10acb.7304c6"]]},{"id":"750bc868.f7fa68","type":"debug","z":"94fae675.1d692","name":"interpretato","active":false,"console":"false","complete":"payload","x":930,"y":360,"wires":[]},{"id":"92b4080.17156f8","type":"http in","z":"94fae675.1d692","name":"","url":"/box","method":"get","upload":false,"swaggerDoc":"","x":320,"y":1660,"wires":[["a17914bc.cb6e9"]]},{"id":"423f646f.f1893c","type":"websocket out","z":"94fae675.1d692","name":"","server":"aaf6a7c3.cd43a8","client":"","x":1546.4285714285713,"y":352.85714285714283,"wires":[]},{"id":"490301d5.6de9a","type":"switch","z":"94fae675.1d692","name":"Luce","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"status: out1 low","vt":"str"},{"t":"cont","v":"status: out1 high","vt":"str"}],"checkall":"true","outputs":2,"x":710,"y":500,"wires":[["c544f4e1.41c4"],["a76da343.a5931"]]},{"id":"c544f4e1.41c4","type":"function","z":"94fae675.1d692","name":"Risposta Luce accesa","func":"msg.payload={};\nmsg.payload[\"status: Luce\"]={sensor:\"Luce\",status:\"accesa\"};\nmsg.payload[\"sensor buttons: Luce\"]={sensor:\"Luce\"};\nmsg.payload[\"sensor buttons: Luce\"].button0={description:\"spegni\",command:\"spegni Luce\"};\nreturn msg;","outputs":1,"noerr":0,"x":1400,"y":500,"wires":[["423f646f.f1893c"]]},{"id":"a76da343.a5931","type":"function","z":"94fae675.1d692","name":"Risposta Luce spenta","func":"msg.payload={};\nmsg.payload[\"status: Luce\"]={sensor:\"Luce\",status:\"spenta\"};\nmsg.payload[\"sensor buttons: Luce\"]={sensor:\"Luce\"};\nmsg.payload[\"sensor buttons: Luce\"].button1={description:\"accendi\",command:\"accendi Luce\"};\n\nreturn msg;","outputs":1,"noerr":0,"x":1380,"y":540,"wires":[["423f646f.f1893c"]]},{"id":"a17914bc.cb6e9","type":"function","z":"94fae675.1d692","name":"Pagename=box","func":"msg.egoPageName=\"box\";\nmsg.egoWebSocket=\"box\";\nmsg.egoFiltro=\"\";\nmsg.egoPageTitle=\"Box\";\nreturn msg;","outputs":1,"noerr":0,"x":560,"y":1700,"wires":[["fcdd7771.03e5c8"]]},{"id":"8c172a11.6d7928","type":"function","z":"94fae675.1d692","name":"Box","func":"msg.INsensor=\"Box\";\nmsg.OUTsensor=\"0 2\";\nmsg.OUTaccendi=\"set output:\"+0+\"=0 4\\nstatus\";\nmsg.OUTspegni=\"set output:\"+2+\"=0 4\\nstatus\";\nreturn msg;","outputs":1,"noerr":0,"x":850,"y":960,"wires":[["14de4734.c0f9a1"]]},{"id":"5ae4d5ba.7c49f4","type":"debug","z":"94fae675.1d692","name":"web","active":true,"console":"false","complete":"true","x":830,"y":1720,"wires":[]},{"id":"df7c2da4.29ec78","type":"function","z":"94fae675.1d692","name":"discover","func":"msg.payload={\n DiscoverBridgedAccessories: 0\n}\n//se premo su applicazione apri garage\n//risponde\n//payload.TargetDoorState:0\n\n//Characteristic TargetDoorState cannot be written.\n//Try one of these: ConfigureBridgedAccessoryStatus, DiscoverBridgedAccessories, DiscoveredBridgedAccessories, Name\nreturn msg;","outputs":1,"noerr":0,"x":1170.5,"y":1528,"wires":[[]]},{"id":"4aaad7b9.1685","type":"debug","z":"94fae675.1d692","name":"brige conf","active":true,"console":"false","complete":"true","x":1120,"y":1680,"wires":[]},{"id":"f5e8ac79.42ef8","type":"inject","z":"94fae675.1d692","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":992.5,"y":1504,"wires":[["df7c2da4.29ec78"]]},{"id":"d4bb3088.bd872","type":"function","z":"94fae675.1d692","name":"aggiuge garage","func":"msg.payload={\n DiscoveredBridgedAccessories:\"111-11-112\"\n}\n\n//Characteristic TargetDoorState cannot be written.\n//Try one of these: ConfigureBridgedAccessoryStatus, DiscoverBridgedAccessories, DiscoveredBridgedAccessories, Name\nreturn msg;","outputs":1,"noerr":0,"x":1172,"y":1560,"wires":[[]]},{"id":"8662923.c4bf67","type":"inject","z":"94fae675.1d692","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":965.5,"y":1558,"wires":[["d4bb3088.bd872"]]},{"id":"6de40470.dffa44","type":"debug","z":"94fae675.1d692","name":"Garage","active":true,"console":"false","complete":"true","x":1143.5,"y":1440,"wires":[]},{"id":"3406fdc.082df82","type":"function","z":"94fae675.1d692","name":"Pagename=garage","func":"msg.egoPageName=\"garage\";\nmsg.egoWebSocket=\"box\";\nmsg.egoFiltro=\"Box\";\nmsg.egoPageTitle=\"Garage\";\nreturn msg;","outputs":1,"noerr":0,"x":590,"y":1800,"wires":[["fcdd7771.03e5c8","760cc26e.8c730c"]]},{"id":"ddaecef.66ea9b","type":"http in","z":"94fae675.1d692","name":"","url":"/garage","method":"get","upload":false,"swaggerDoc":"","x":310,"y":1800,"wires":[["3406fdc.082df82","760cc26e.8c730c"]]},{"id":"fcdd7771.03e5c8","type":"template","z":"94fae675.1d692","name":"Status Web Page","field":"payload","fieldType":"msg","format":"html","syntax":"mustache","template":"<!DOCTYPE HTML>\n<html>\n <head>\n <title>{{egoPageTitle}}</title>\n\t <style>\n h1{\n font-family: arial, sans-serif;\n }\n\t\ttable {\n\t\t\tfont-family: arial, sans-serif;\n\t\t\tborder-collapse: collapse;\n\t\t\twidth: 100%;\n\t\t\tfont-size: 36px;\n\t\t}\n\n\t\ttd, th {\n\t\t\tborder: 1px solid #dddddd;\n\t\t\ttext-align: left;\n\t\t\tpadding: 8px;\n\t\t}\n\n\t\ttr:nth-child(even) {\n\t\t\tbackground-color: #dddddd;\n\t\t}\n\t\tbutton {\n\t\t\tfont-family: arial, sans-serif;\n\t\t\twidth: 90%;\n\t\t\tfont-size: 36px;\n\t\t}\n\t</style>\n <script type=\"text/javascript\">\n var ws;\n var wsUri = \"ws:\";\n var loc = window.location;\n console.log(loc);\n if (loc.protocol === \"https:\") { wsUri = \"wss:\"; }\n // This needs to point to the web socket in the Node-RED flow\n // ... in this case it's ws/simple\n wsUri += \"//\" + loc.host + loc.pathname.replace(\"{{egoPageName}}\",\"ws/{{egoWebSocket}}\");\n\n function wsConnect() {\n console.log(\"connect\",wsUri);\n ws = new WebSocket(wsUri);\n //var line = \"\"; // either uncomment this for a building list of messages\n ws.onmessage = function(msg) {\n var line = \"\"; // or uncomment this to overwrite the existing message\n // parse the incoming message as a JSON object\n var data = msg.data;\n\t\t\t\tconsole.log(\"data '\"+data+\"'\");\n\t\t\t\tvar obj=JSON.parse(data);\n\t\t\t\tfor(var cmd in obj){//interprete\n\t\t\t\t\tconsole.log(\"cmd \"+cmd);\n\t\t\t\t\tif(cmd==\"sensors\"){ //risposta sensors\n\t\t\t\t\t\tgenerateTable(obj[cmd]);\n\t\t\t\t\t\tws.send(\"status\");\n\t\t\t\t\t}\n\t\t\t\t\tif(cmd.startsWith(\"status:\")){ //risposta status\n\t\t\t\t\t\tgenerateStatus(obj[cmd]);\n\t\t\t\t\t}\n\t\t\t\t\tif(cmd.startsWith(\"sensor buttons:\")){ //risposta sensor buttons\n\t\t\t\t\t\tgenerateButtons(obj[cmd]);\n\t\t\t\t\t}\n\t\t\t\t}\n //console.log(data);\n // build the output from the topic and payload parts of the object\n //line += \"<p>\"+data+\"</p>\";\n // replace the messages div with the new \"line\"\n //document.getElementById('messages').innerHTML = line;\n //ws.send(JSON.stringify({data:data}));\n }\n ws.onopen = function() {\n // update the status div with the connection status\n document.getElementById('status').innerHTML = \"connected\";\n\t\t\t\t//comando get sensors\n ws.send(\"get sensors\");\n console.log(\"connected\");\n\t\t\t\tvar autoCommand=\"{{egoAutoCommand}}\";\n\t\t\t\tif ( autoCommand != null ) {\n\t\t\t\t\tif(autoCommand!=\"\"){\n\t\t\t\t\t\tws.send(autoCommand);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t\t\n }\n ws.onclose = function() {\n // update the status div with the connection status\n document.getElementById('status').innerHTML = \"not connected\";\n // in case of lost connection tries to reconnect every 3 secs\n setTimeout(wsConnect,3000);\n }\n\t\t\t ws.disconnect = function() {\n // update the status div with the connection status\n document.getElementById('status').innerHTML = \"not connected\";\n // in case of lost connection tries to reconnect every 3 secs\n setTimeout(wsConnect,3000);\n }\n }\n \n function doit(m) {\n if (ws) { ws.send(m); }\n }\n\t\tfunction generateRow(sensor){\n\t\t\tconsole.log(\"generateRow \"+sensor);\n\t\t\trow=\"<tr>\\n\";\n\t\t\trow+=\"<td id='\"+sensor+\"'>\"+ sensor +\"</td>\\n\";\n\t\t\trow+=\"<td id='\"+sensor+\"-status'></td>\\n\";\n\t\t\trow+=\"<td id='\"+sensor+\"-button0'></td>\\n\"; \n\t\t\trow+=\"<td id='\"+sensor+\"-button1'></td>\\n\"; \n\t\t\trow+=\"</tr>\\n\";\n\t\t\treturn row;\n\t\t}\n\t\tfunction generateButton(sensor,description,command,buttonN){\n\t\t\tline=\"<button type='button' onclick='doit(\\\"\"+command+\"\\\");'>\"+description+\"</button>\";\n\t\t\tdocument.getElementById(sensor+\"-button\"+buttonN).innerHTML = line;\n\t\t\t\n\t\t\treturn;\n\t\t}\n\t\tfunction generateStatus(sensorStatus){\n\t\t\tvar sensor=sensorStatus[\"sensor\"];\n\t\t\tvar status=sensorStatus[\"status\"];\n\t\t\t\n\t\t\tvar el=document.getElementById(sensor+\"-status\");\n\t\t\tif(el===null)\n\t\t\t\treturn;\n\t\t\tel.innerHTML = status;\n\t\t\t\n\t\t}\n\t\tfunction eraseButton(sensor,buttonN){\n\t\t\t\n\t\t\tdocument.getElementById(sensor+\"-button\"+buttonN).innerHTML = \"\";\n\t\t\t\n\t\t\treturn;\n\t\t}\n\t\tfunction generateButtons(sensorButtons){\n\t\t\tvar sensor=sensorButtons[\"sensor\"];\n\t\t\tvar i=0;\n\t\t\tfor(var i = 0;i < 2;i++){\n\t\t\t\tvar sb=sensorButtons[\"button\"+i];\n\t\t\t\tconsole.log(\"valore sb \"+sb);\n\t\t\t\tif(typeof sb===\"undefined\"){\n\t\t\t\t\n\t\t\t\t\teraseButton(sensor,i);\n\t\t\t\t}else{\n\t\t\t\t\tvar description=sb[\"description\"];\n\t\t\t\t\tvar command=sb[\"command\"];\n\t\t\t\t\tvar buttonN=i;\n\t\t\t\t\tgenerateButton(sensor,description,command,buttonN);\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\t\n\t\t\treturn;\n\t\t}\n\t\tfunction generateTable(sensors){\n\t\t\tvar rows=\"<tr><th>Sensore</th>\\n\";\n\t\t\trows+=\"<th>Stato</th>\\n\";\n\t\t\trows+=\"<th>spegni</th>\\n\"; \n\t\t\trows+=\"<th>accendi</th>\\n\";\n\t\t\trows+=\"</tr>\\n\";\n\t\t\tvar filtro=\"{{egoFiltro}}\";\n\t\t\tfor(var sensor in sensors){\t\t\t\t\n\t\t\t\tif ( (filtro != null) && !(filtro==\"\")) {\n\t\t\t\t\tconsole.log(\"filtro \"+filtro);\n\t\t\t\t\tif(sensor==filtro){\n\t\t\t\t\t\tconsole.log(\"sensor \"+sensor);\n\t\t\t\t\t\trows+=generateRow(sensor);\n\t\t\t\t\t}\n\t\t\t\t}else{\n\t\t\t\t\tconsole.log(\"sensor \"+sensor);\n\t\t\t\t\trows+=generateRow(sensor);\n\t\t\t\t}\n\t\t\t}\n\t\t console.log(rows);\n\t\t\tdocument.getElementById(\"sensors\").innerHTML = rows;\n\t\t//\tfor(var sensor in sensors){\n\t\t//\t\tdoit(\"get \"+sensor);\n\t\t//\t}\n\t\t\t\n\t\t}\n </script>\n </head>\n <body onload=\"wsConnect();\" onunload=\"ws.disconnect();\">\n <font face=\"Arial\">\n <h1>Sensori</h1>\n <div id=\"messages\"></div>\n <button type=\"button\" onclick='doit(\"get sensors\");'>Refresh</button>\n <hr/>\n <div id=\"status\">unknown</div>\n </font>\n\t\t\n\t\t\n\t\t\n\t\t\n\t\t\n\t\t\n\t\t<h1>Stato dei sensori</h1>\n\t\t<table style=\"border:1px solid black\" id=\"sensors\">\n\t\t<tr>\n\t\t\t<th>Sensore</th>\n\t\t\t<th>Stato</th>\n\t\t\t<th>spegni</th> \n\t\t\t<th>accendi</th>\n\t\t</tr>\n \n\t\n </table>\n\n<p> </p>\n\t\t\n\t\t\n\t\t\n\t\t\n\t\t\n\t\t\n\t\t\n </body>\n</html>\n","x":950,"y":1800,"wires":[["d10d920e.385218","760cc26e.8c730c"]]},{"id":"760cc26e.8c730c","type":"debug","z":"94fae675.1d692","name":"web","active":true,"console":"false","complete":"true","x":1210,"y":1940,"wires":[]},{"id":"d10d920e.385218","type":"http response","z":"94fae675.1d692","name":"","statusCode":"","headers":{},"x":1210,"y":1800,"wires":[]},{"id":"a315d422.3e68a","type":"http in","z":"94fae675.1d692","name":"","url":"/aprigarage","method":"get","upload":false,"swaggerDoc":"","x":300,"y":1860,"wires":[["5bfe903d.2d798"]]},{"id":"5bfe903d.2d798","type":"function","z":"94fae675.1d692","name":"Pagename=aprigarage","func":"msg.egoPageName=\"aprigarage\";\nmsg.egoWebSocket=\"box\";\nmsg.egoFiltro=\"Box\";\nmsg.egoPageTitle=\"Apri Garage\";\nmsg.egoAutoCommand=\"accendi Box\";\nreturn msg;","outputs":1,"noerr":0,"x":590,"y":1860,"wires":[["fcdd7771.03e5c8"]]},{"id":"bb7626dd.fa8798","type":"http in","z":"94fae675.1d692","name":"","url":"/chiudigarage","method":"get","upload":false,"swaggerDoc":"","x":290,"y":1940,"wires":[["fd138888.f74748"]]},{"id":"fd138888.f74748","type":"function","z":"94fae675.1d692","name":"Pagename=chiudigarage","func":"msg.egoPageName=\"chiudigarage\";\nmsg.egoWebSocket=\"box\";\nmsg.egoFiltro=\"Box\";\nmsg.egoPageTitle=\"Chiudi Garage\";\nmsg.egoAutoCommand=\"spegni Box\";\nreturn msg;","outputs":1,"noerr":0,"x":590,"y":1940,"wires":[["fcdd7771.03e5c8"]]},{"id":"40e76ff1.348e98","type":"function","z":"94fae675.1d692","name":"","func":"if(typeof msg.hap.context == \"undefined\")\n return null;\nif(msg.hap.context.keepalive){\nif(!msg.payload.TargetDoorState){\n msg.payload=\"accendi Box\";\n \n}else{\n if(msg.payload.TargetDoorState){\n msg.payload=\"spegni Box\";\n \n }\n \n}\n}\nreturn msg;","outputs":1,"noerr":0,"x":1322,"y":1319,"wires":[["8c172a11.6d7928"]]},{"id":"eab5b11.47355d","type":"link in","z":"94fae675.1d692","name":"Invia comandi","links":["aa050083.5381d"],"x":1141.5,"y":1318,"wires":[["6de40470.dffa44","3509093b.93f056","d58f8b30.2101b8","803697ee.bb469","40e76ff1.348e98","7e188d25.0811f4"]]},{"id":"3e653eac.32147a","type":"link out","z":"94fae675.1d692","name":"Ricevi stato","links":["adecaf51.0c0c88"],"x":987,"y":1319,"wires":[]},{"id":"7adfdcfc.60976c","type":"function","z":"94fae675.1d692","name":"","func":"msg.payload={a:true}\nreturn msg;\n\n//Try one of these: Name, CurrentDoorState, TargetDoorState, ObstructionDetected, LockCurrentState, LockTargetState, Name","outputs":1,"noerr":0,"x":530,"y":1499,"wires":[["3e653eac.32147a"]]},{"id":"335d521c.135ede","type":"inject","z":"94fae675.1d692","name":"Lista properties","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":320,"y":1499,"wires":[["7adfdcfc.60976c"]]},{"id":"3509093b.93f056","type":"debug","z":"94fae675.1d692","name":"Garage payload","active":true,"console":"false","complete":"payload","x":1242,"y":1379,"wires":[]},{"id":"d58f8b30.2101b8","type":"debug","z":"94fae675.1d692","name":"Garage payload","active":true,"console":"false","complete":"hap","x":1312,"y":1459,"wires":[]},{"id":"eae5a78a.c3f3d8","type":"inject","z":"94fae675.1d692","name":"simula target aperto","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":150,"y":620,"wires":[["199ba6d8.c068a1"]]},{"id":"199ba6d8.c068a1","type":"function","z":"94fae675.1d692","name":"TargetDoorState:false","func":"msg.payload={TargetDoorState:false}\n//msg.payload={CurrentDoorState:false,\n//TargetDoorState:false}\nmsg.mittente=\"sensori\";\nreturn msg;\n\n//Try one of these: Name, CurrentDoorState, TargetDoorState, ObstructionDetected, LockCurrentState, LockTargetState, Name","outputs":1,"noerr":0,"x":420,"y":620,"wires":[["3e653eac.32147a","8971e941.8eced8"]]},{"id":"789df96c.9c74d8","type":"inject","z":"94fae675.1d692","name":"simula target chiuso","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":130,"y":760,"wires":[["3afcc4d5.36b5ac"]]},{"id":"3afcc4d5.36b5ac","type":"function","z":"94fae675.1d692","name":"TargetDoorState:true","func":"msg.payload={TargetDoorState:true}\n//msg.payload={CurrentDoorState:false,\n//TargetDoorState:false}\nmsg.mittente=\"sensori\";\nreturn msg;\n\n//Try one of these: Name, CurrentDoorState, TargetDoorState, ObstructionDetected, LockCurrentState, LockTargetState, Name","outputs":1,"noerr":0,"x":380,"y":700,"wires":[["3e653eac.32147a","8971e941.8eced8"]]},{"id":"803697ee.bb469","type":"debug","z":"94fae675.1d692","name":"mittente","active":true,"console":"false","complete":"mittente","x":1379.7777777777778,"y":1546.7777777777778,"wires":[]},{"id":"8971e941.8eced8","type":"debug","z":"94fae675.1d692","name":"invio a app","active":true,"console":"false","complete":"true","x":1022,"y":1399,"wires":[]},{"id":"7e188d25.0811f4","type":"function","z":"94fae675.1d692","name":"Aggiorna stato target","func":"\nif(typeof msg.payload.TargetDoorState== \"undefined\"){\n\n return null;\n}\n\nglobal.set('target', msg.payload.TargetDoorState);\n\nreturn msg;","outputs":1,"noerr":0,"x":1392,"y":1219,"wires":[["4e3ac065.f64e3"]]},{"id":"a7da892a.6f6b08","type":"function","z":"94fae675.1d692","name":"filtra incoerenti","func":"//memorizza current nel contesto\n//se current = target allora invia current all'app\n//se diverso allora passa al prossimo nodo\n//il quale attende 15 sec\n//e controlla se current di 15 sec prima = contest\n//se = allora invia target=current e current all'app\n//se diverso butta\n\n\nif(typeof msg.payload.CurrentDoorState== \"undefined\"){\n\n return null;\n}\n\n\nglobal.set('current',msg.payload.CurrentDoorState);\n\nvar target=global.get('target');\nif(typeof target== \"undefined\"){\n\n return [null,msg];\n}\n//node.log(target);\n//node.log(target==msg.payload.CurrentDoorState);\nif(target==msg.payload.CurrentDoorState)\n return [msg,null];\nreturn [null,msg];\n\n","outputs":"2","noerr":0,"x":500,"y":1239,"wires":[["3e653eac.32147a","fe37a57b.1dd4a"],["9cea4123.10dfa","2a78877c.f0d4e8"]]},{"id":"63bf719c.120f88","type":"function","z":"94fae675.1d692","name":"Invia current se stabile","func":"\nif(typeof msg.payload.CurrentDoorState== \"undefined\"){\n\n return null;\n}\n\nvar actual=global.get('current')||msg.payload.CurrentDoorState;\nif(actual==msg.payload.CurrentDoorState){\n msg.payload={TargetDoorState:actual,\n CurrentDoorState:actual}; \n return msg;\n}\nreturn null; \n ","outputs":"1","noerr":0,"x":820,"y":1200,"wires":[["3e653eac.32147a","7e188d25.0811f4","f7836047.c8765"]]},{"id":"9cea4123.10dfa","type":"delay","z":"94fae675.1d692","name":"","pauseType":"delay","timeout":"15","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":660,"y":1160,"wires":[["63bf719c.120f88","3bf0876.28f8cf8"]]},{"id":"8fd4f87.359e908","type":"function","z":"94fae675.1d692","name":"CurrentDoorState:false","func":"msg.payload={CurrentDoorState:false}\n//msg.payload={CurrentDoorState:false,\n//TargetDoorState:false}\n//msg.mittente=\"sensori\";\nreturn msg;\n\n//Try one of these: Name, CurrentDoorState, TargetDoorState, ObstructionDetected, LockCurrentState, LockTargetState, Name","outputs":1,"noerr":0,"x":290,"y":1199,"wires":[["a7da892a.6f6b08","3ef0dfd0.f7d108"]]},{"id":"8546d4e5.9839a","type":"function","z":"94fae675.1d692","name":"CurrentDoorState:true","func":"msg.payload={CurrentDoorState:true\n}\n//msg.payload={CurrentDoorState:true,\n// TargetDoorState:true\n//}\n//msg.mittente=\"sensori\";\nreturn msg;\n\n//Try one of these: Name, CurrentDoorState, TargetDoorState, ObstructionDetected, LockCurrentState, LockTargetState, Name","outputs":1,"noerr":0,"x":260,"y":1260,"wires":[["a7da892a.6f6b08","3ef0dfd0.f7d108"]]},{"id":"4e3ac065.f64e3","type":"debug","z":"94fae675.1d692","name":"Stato target","active":true,"console":"false","complete":"payload","x":1377.142857142857,"y":1097.142857142857,"wires":[]},{"id":"fe37a57b.1dd4a","type":"debug","z":"94fae675.1d692","name":"stato porta coerente","active":true,"console":"false","complete":"payload","x":1127.9999999999998,"y":1043.9999999999998,"wires":[]},{"id":"2a78877c.f0d4e8","type":"debug","z":"94fae675.1d692","name":"stato porta incoerente","active":true,"console":"false","complete":"payload","x":1140,"y":1100,"wires":[]},{"id":"f7836047.c8765","type":"debug","z":"94fae675.1d692","name":"invio dopo attesa","active":true,"console":"false","complete":"payload","x":1137.9999999999998,"y":1161.9999999999998,"wires":[]},{"id":"3ef0dfd0.f7d108","type":"debug","z":"94fae675.1d692","name":"dai sensori","active":true,"console":"false","complete":"payload","x":307.99999999999994,"y":1097.9999999999998,"wires":[]},{"id":"3bf0876.28f8cf8","type":"delay","z":"94fae675.1d692","name":"","pauseType":"delay","timeout":"10","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":340,"y":1020,"wires":[["bcb5fff.9f0cc"]]},{"id":"9564af5.de8a25","type":"serial-port","z":"","serialport":"/dev/ttyACM0","serialbaud":"9600","databits":"8","parity":"none","stopbits":"1","newline":"\\n","bin":"false","out":"char","addchar":true},{"id":"aaf6a7c3.cd43a8","type":"websocket-listener","z":"","path":"/ws/box","wholemsg":"false"}]

Cercare l’icona a righe sulla barra nera di Nodered, accanto a Deploy. Cliccarla. Appare un menù e scegliere import/clipboard

Incollare il contenuto e premere import.

Sotto il mouse appare il flow. Cliccare per fissarlo.

A questo punto premere Deploy per attivare il flow.

Eseguire doppio click sul tab Flow1 e cambiare il nome in Garage.

Per vedere se funziona, controllare la pagina:

http://192.168.1.102:1880/garage

Deve apparire:

Pagina web garage

Provare ad aprire il garage.

Se non funziona, controllare l’installazione di Arduino e controllare nel flow le porte su cui comunica.

Controllare il blocco della seriale chiamato Legge risposta.

Se sotto c’è scritto connected allora comunica con arduino, altrimenti il nome della seriale è sbagliato.

Per cambiarlo fare doppio click sul nodo chiamato Legge risposta.

Cliccare sulla pennina e cambiare seriale.

 

Eseguire il deploy e controllare che ci sia scritto connected sotto il nodo Legge risposta

Se vuoi aggiungere il flow per Apple Homekit

Installare il nodo homekit

Aprire una schermata di raspberry, con la tastiera e TV collegati, oppure tramite una sessione vnc.

Aprire una sessione terminale

Digitare

sudo apt-get install npm 
sudo npm install -g homebridge
sudo apt-get install libavahi-compat-libdnssd-dev
cd /home/pi/.node-red
npm install node-red-contrib-homekit
sudo systemctl restart nodered.service

 

Installare il flow

Cliccare sul +.

Aggiungere il flow

Doppio click

Cambia il nome in Apple Homekit

Seleziona la riga

[{"id":"adecaf51.0c0c88","type":"link in","z":"d1f9b302.63419","name":"Stato garage","links":["3e653eac.32147a"],"x":235,"y":160,"wires":[["8c921674.c64dd8"]]},{"id":"aa050083.5381d","type":"link out","z":"d1f9b302.63419","name":"Comandi Garage","links":["eab5b11.47355d"],"x":754.5,"y":164,"wires":[]},{"id":"608912d9.b1fa6c","type":"comment","z":"d1f9b302.63419","name":"Se non riesci a trovare l'accessorio","info":"Apri il blocco Garage.\nPrendi nota delle impostazioni Garage\nCancella il blocco chiamato Garage\nRicrea un blocco chiamato Garage2\nImpostalo come l'altro","x":480,"y":360,"wires":[]},{"id":"8c921674.c64dd8","type":"homekit-service","z":"d1f9b302.63419","accessory":"ad614da5.b9dd38","name":"Garage di casa","serviceName":"GarageDoorOpener","x":480.6249809265137,"y":153.2499885559082,"wires":[["aa050083.5381d"]]},{"id":"ad614da5.b9dd38","type":"homekit-accessory","z":"","accessoryName":"Garage","pinCode":"111-11-111","port":"","manufacturer":"EgoMaker","model":"EgoGarage","serialNo":"num1","accessoryType":"4"}]

eseguire Copia o premere CTRL+C.

Andare sul flow Apple Homekit.

Cercare l’icona a righe sulla barra nera di Nodered, accanto a Deploy. Cliccarla. Appare un menù e scegliere import/clipboard

Incollare il contenuto e premere import.

Sotto il mouse appare il flow. Cliccare per fissarlo.

A questo punto premere Deploy per attivare il flow.

Per provarlo, leggere l’articolo:

Aprire il garage con iPhone

Leave a Reply

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *