UniFi WiFi Password QR Code Generator
[!NOTE] Note Unifi provides a guest portal, which might be overkill for the home, so I over-engineered a solution using Node-red
We often have guests, repair people, or our friendly neighborhood kids visiting our home, and they almost always want the guest's Wi-Fi password. The annoyance (not really) of having to repeat the password became the mother of invention for this flow.
The flow requires the following palettes:
- node-red-contrib-bigtimer
- node-red-contrib-image-tools
- node-red-contrib-unifi
You'll have to use the Unifi node to identify the exact ID of your guest network or visit the console URL and pull it from the URI.
The steps are:
- Decide your rotation cadence
- Delete the existing QR Code Image, as I ran into some caching issues with the home tablet
- Use a function to generate a random password
- Use a function to create a payload with the following:
- setWLANPassword;
- wland_id
- x_passphrase that was generated in the previous step
- Use a function to create a payload with the following:
- SSID name;
- Encryption type;
- Password that was generated in the previous step
- Create a string for the QR code
- Generate a random color for the QR code that's displayed on the home tablet
- Generate the QR code
- Write to disk
- And as a last step, display the QR code in the node-red flow
The generated QR code is displayed on the home table via the Home Assistant companion app. I use the refreshable-picture-card
from the HACS store to refresh the image on the dashboard to ensure it's up-to-date.
[{"id":"79d153f10dbb34e1","type":"tab","label":"Wi-Fi QR Code Generator","disabled":false,"info":"","env":[]},{"id":"6bb59c683a390683","type":"Barcode Generator","z":"79d153f10dbb34e1","name":"QRCODE","data":"payload","dataType":"msg","barcode":"qrcode","barcodeType":"barcode","options":"options","optionsType":"msg","sendProperty":"payload","props":[],"x":1080,"y":280,"wires":[["9fad9b5a6f0d04be"]]},{"id":"9fad9b5a6f0d04be","type":"jimp-image","z":"79d153f10dbb34e1","name":"","data":"payload","dataType":"msg","ret":"img","parameter1":"/homeassistant/www/qr-code.png","parameter1Type":"str","parameter2":"","parameter2Type":"msg","parameter3":"","parameter3Type":"msg","parameter4":"","parameter4Type":"msg","parameter5":"","parameter5Type":"msg","parameter6":"","parameter6Type":"msg","parameter7":"","parameter7Type":"msg","parameter8":"","parameter8Type":"msg","sendProperty":"payload","sendPropertyType":"msg","parameterCount":1,"jimpFunction":"write","selectedJimpFunction":{"name":"write","fn":"write","description":"Write to file. NOTE: You can specify an alternative file extension type to change the type. Currently support types are jpg, png, bmp.","parameters":[{"name":"filename","type":"str","required":true,"hint":"Name of the file","defaultType":"str"}]},"x":1230,"y":280,"wires":[["ac07d3562c826db5"]]},{"id":"ac07d3562c826db5","type":"image viewer","z":"79d153f10dbb34e1","name":"","width":"160","data":"payload","dataType":"msg","active":true,"x":1370,"y":280,"wires":[[]]},{"id":"d3840bd2d88efded","type":"Unifi","z":"79d153f10dbb34e1","name":"Push pwd to UniFi","server":"d4ee8e33449c822a","command":"110","debug":false,"x":910,"y":200,"wires":[[]]},{"id":"2592510c71689841","type":"function","z":"79d153f10dbb34e1","name":"Set WLAN Password","func":"// Assuming msg.payload contains the new password from the previous step\nlet newPassword = msg.payload;\n\nmsg.payload = {\n command: \"setWLanPassword\",\n wlan_id: \"WIFI ID\",\n x_passphrase: newPassword\n};\n\nreturn msg;\n","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":680,"y":200,"wires":[["d3840bd2d88efded"]]},{"id":"249148a77f9cc58c","type":"inject","z":"79d153f10dbb34e1","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":320,"wires":[["c2ddc55355aa240b"]]},{"id":"c7c550f896e3f384","type":"function","z":"79d153f10dbb34e1","name":"Generate Password","func":"// Example JavaScript code to generate a simple password\nlet words = [\"Spring\", \"Summer\", \"Autumn\", \"Winter\"];\nlet numbers = Math.floor(Math.random() * 99) + 10; // Random number between 10-99\nlet symbols = \"!@$\";\nlet randomSymbol = symbols[Math.floor(Math.random() * symbols.length)];\nlet password = words[Math.floor(Math.random() * words.length)] + numbers + randomSymbol;\n\nmsg.payload = password;\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":240,"wires":[["2592510c71689841","84ce246e95962b59"]]},{"id":"84ce246e95962b59","type":"function","z":"79d153f10dbb34e1","name":"Create QR Code Data","func":"// Modify with your actual WiFi network details\nlet ssid = 'Guest Network';\nlet securityType = 'WPA2'; // or 'WEP' or '' for no security\nlet password = msg.payload; // Using the generated password\n\n// Format the string for QR code\nlet wifiString = `WIFI:S:${ssid};T:${securityType};P:${password};;`;\nmsg.payload = wifiString;\n\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":680,"y":280,"wires":[["c48065192d7c3418"]]},{"id":"5fb9ac9c41407d10","type":"bigtimer","z":"79d153f10dbb34e1","outtopic":"","outpayload1":"on","outpayload2":"off","name":"Monthly Rotation","comment":"","lat":"0","lon":"0","starttime":"0","endtime":"1425","startoff":"0","endoff":"0","startoff2":"","endoff2":"","offs":0,"outtext1":"on","outtext2":"off","timeout":1440,"sun":false,"mon":false,"tue":false,"wed":false,"thu":false,"fri":false,"sat":false,"jan":true,"feb":true,"mar":true,"apr":true,"may":true,"jun":true,"jul":true,"aug":true,"sep":true,"oct":true,"nov":true,"dec":true,"day1":"0","month1":"0","day2":0,"month2":0,"day3":0,"month3":0,"day4":0,"month4":0,"day5":0,"month5":0,"day6":"0","month6":"0","day7":"0","month7":"0","day8":"0","month8":"0","day9":"0","month9":"0","day10":"0","month10":"0","day11":"0","month11":"0","day12":"0","month12":"0","d1":0,"w1":"1","d2":0,"w2":0,"d3":0,"w3":0,"d4":0,"w4":0,"d5":0,"w5":0,"d6":"0","w6":"0","xday1":"0","xmonth1":"0","xday2":"0","xmonth2":"0","xday3":"0","xmonth3":"0","xday4":"0","xmonth4":"0","xday5":"0","xmonth5":"0","xday6":"0","xmonth6":"0","xday7":"0","xmonth7":"0","xday8":"0","xmonth8":"0","xday9":"0","xmonth9":"0","xday10":"0","xmonth10":"0","xday11":"0","xmonth11":"0","xday12":"0","xmonth12":"0","xd1":"0","xw1":"0","xd2":"0","xw2":"0","xd3":"0","xw3":"0","xd4":"0","xw4":"0","xd5":"0","xw5":"0","xd6":"0","xw6":"0","suspend":false,"random":false,"randon1":false,"randoff1":false,"randon2":false,"randoff2":false,"repeat":false,"atstart":false,"odd":false,"even":false,"x":230,"y":160,"wires":[["c2ddc55355aa240b"],[],[]]},{"id":"c48065192d7c3418","type":"function","z":"79d153f10dbb34e1","name":"Barcode Color","func":"// Generate a random hexadecimal color\nfunction getRandomColor() {\n let letters = '0123456789ABCDEF';\n let color = '';\n for (let i = 0; i < 6; i++) {\n color += letters[Math.floor(Math.random() * 16)];\n }\n return color;\n}\n\n// Set random colors for the barcode and background\nmsg.options = {\n barcolor: getRandomColor(),\n backgroundcolor: \"FFFFFF\"\n};\n\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":900,"y":280,"wires":[["6bb59c683a390683"]]},{"id":"c2ddc55355aa240b","type":"file","z":"79d153f10dbb34e1","name":"Delete Existing","filename":"/homeassistant/www/qr-code.png","filenameType":"str","appendNewline":true,"createDir":false,"overwriteFile":"delete","encoding":"none","x":240,"y":240,"wires":[["c7c550f896e3f384"]]},{"id":"5a597a758468f802","type":"comment","z":"79d153f10dbb34e1","name":"Generate Random WiFi Password","info":"","x":280,"y":100,"wires":[]},{"id":"d4ee8e33449c822a","type":"unificonfig","name":"","ip":"10.0.0.1","port":"443","site":"default","unifios":true,"ssl":true}]