[{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n        \u003cp\u003eSo you want your friends/family/strangers to enjoy the luxury of modern technology, scanning a QR-code to join a wifi network, but wait...you want to save that in \u003ccode\u003esettings.toml\u003c/code\u003e too? Sheeesh, a tall order, lets get on with it then!\u003c/p\u003e\n\u003cp\u003eWe'll need to check a button at boot to decide if the Memento camera has write access to the flash drive, or the PC. Then we can update the \u003ccode\u003esettings.toml\u003c/code\u003e file if it's writable, and either way we can offer to join the network listed in the QRcode.\u003c/p\u003e\n      \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"user_image","content":"https://cdn-learn.adafruit.com/user_assets/assets/000/000/684/original/qr-code_%282%29.png?1707611188","metadata":{"caption":""}},{"element_type":"alert","content":"Easiest way to begin is to download then copy both the linked files (boot.py + code.py) to your memento camera running CircuitPython. Then install circup with: \npip install circup\n then install the requirements by running circup from the usb drive (you can also try running from any path/folder as it auto-detects devices) with: \ncircup install --auto\n or if you want to be able to read and modify the modules installed in the 'lib' folder then instead use: \ncircup install --auto --py","metadata":{"class":"element row-fluid alert-element build-alert alert-info","alert_type":"info"}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n        \u003cp\u003eThe code example can be found here:\u003cbr\u003e\u003ca href=\"https://github.com/tyeth/Adafruit_CircuitPython_PyCamera/tree/wifi-qrsetup/examples/qrio-update-wifi\"\u003ehttps://github.com/tyeth/Adafruit_CircuitPython_PyCamera/tree/wifi-qrsetup/examples/qrio-update-wifi\u003c/a\u003e\u003c/p\u003e\n\u003cp\u003eThere are two files, the\u0026nbsp;\u003ccode\u003eboot.py\u003c/code\u003e which handles the first load when the device is turned on or reset button is pressed. It prints a message to the screen and waits 2.5seconds before checking for the shutter button being held down, and then does the switching / flicking between read-only for the USB / PC, or read-only for the Memento camera (circuitpython). See here: \u003ca href=\"https://github.com/tyeth/Adafruit_CircuitPython_PyCamera/blob/wifi-qrsetup/examples/qrio-update-wifi/boot.py\" target=\"_blank\"\u003ehttps://github.com/tyeth/Adafruit_CircuitPython_PyCamera/blob/wifi-qrsetup/examples/qrio-update-wifi/boot.py\u003c/a\u003e\u003c/p\u003e\n      \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"embed","content":"https://github.com/tyeth/Adafruit_CircuitPython_PyCamera/blob/wifi-qrsetup/examples/qrio-update-wifi/boot.py"},{"element_type":"text","content":"\n  \n  \n  \n  \n        \u003cp\u003eSecond is the main \u003ccode\u003ecode.py\u003c/code\u003e file, which is a modified version of the \u003ccode\u003eqrio\u003c/code\u003e example, \u003ca href=\"https://github.com/tyeth/Adafruit_CircuitPython_PyCamera/blob/wifi-qrsetup/examples/qrio-update-wifi/code.py\"\u003ehttps://github.com/tyeth/Adafruit_CircuitPython_PyCamera/blob/wifi-qrsetup/examples/qrio-update-wifi/code.py\u003c/a\u003e.\u003cbr\u003e\u003cbr\u003eThe changes to \u003ccode\u003ecode.py\u003c/code\u003e begin after verifying if the QR-code is unseen compared to the last seen code, on \u003ca href=\"https://github.com/tyeth/Adafruit_CircuitPython_PyCamera/blob/wifi-qrsetup/examples/qrio-update-wifi/code.py#L47\"\u003eline 47\u003c/a\u003e. This means that once a QRcode for a wifi network has been seen, another qrcode must be seen before you can show that wifi one again. After this check we then verify if the beginning of the QR-Code's data payload contains the characters \u003ccode\u003eWIFI:\u003c/code\u003e, as that prefix designates a Wi-Fi network related QR-code.\u003cbr\u003e\u003cbr\u003eHere are two examples, one of my open home network: \u003ccode\u003eWIFI:S:free4all;t:WEP;P:password;;\u003c/code\u003e\u0026nbsp;and my phone hotspot \u003ccode\u003eWIFI:S:THERMAL;t:SAE;P:password;;\u003c/code\u003e (WPA2/3) or hotspot with no security\u0026nbsp;\u003ccode\u003eWIFI:S:THERMAL;t:nopass;P:;;\u003c/code\u003e [I think I've previously seen \u003ccode\u003et:open;\u003c/code\u003e on other phones/networks with security disabled, and the type designator \u003ccode\u003et:\u003c/code\u003e should probably be capitalised, but I hand typed these from memory]\u003c/p\u003e\n\u003cp\u003eIf a valid Wi-Fi QR code is found then we run all the magic, first decoding the WiFi network name, a.k.a. SSID (between \u003ccode\u003eS:\u003c/code\u003e and the next delimiter \u003ccode\u003e;\u003c/code\u003e ), then the password (whose field begins \u003ccode\u003eP:\u003c/code\u003e ), and finally we use \u003ccode\u003eos.getenv\u003c/code\u003e to read the current values saved in \u003ccode\u003esettings.toml\u003c/code\u003e and then if different we attempt to save the updated values.\u003c/p\u003e\n      \n\n\n\n","metadata":{}},{"element_type":"embed","content":"https://github.com/tyeth/Adafruit_CircuitPython_PyCamera/blob/wifi-qrsetup/examples/qrio-update-wifi/code.py"},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n        \u003ch2\u003eReading TOML, errors, updating wifi entries, and spitting it out again safely:\u003c/h2\u003e\n      \n\n\n\n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n        \u003cp\u003eTo safely overwrite the settings.toml file with the new values we first need to use the \u003ccode\u003etoml\u003c/code\u003e library to read all of the settings.toml entries (lines - each one is a toml entry), then we update the SSID and password (already forced to strings to handle empty values), and write the new settings dictionary back to the file after first converting it back to toml format.\u003c/p\u003e\n      \n\n\n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n  \n        \u003cp\u003eLastly, and especially useful if the saving fails due to the flash drive being read-only to the Memento, the user is asked if they'd like to connect to the wifi network immediately? A count-down happens and the shutter button is watched, and if pressed then the new wifi network is joined. The only problems here were the count-down being uneven between numbers, the click event defaulting to 200ms hold-time, and crashes if network not found. All of those would be cleaned up / handled in a real project.\u003c/p\u003e\n      \n\n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n        \u003cp\u003eJob done!\u003c/p\u003e\n\u003cp\u003eObviously this is not a real complete project, instead the code would be included as a portion of another project, that benefitted from being on a wireless network. Like the IP-Cam examples in the pycamera repository,\u003cbr\u003e\u003ca href=\"https://github.com/adafruit/Adafruit_CircuitPython_PyCamera\"\u003ehttps://github.com/adafruit/Adafruit_CircuitPython_PyCamera\u003c/a\u003e, or maybe you'll use the web-workflow to connect remotely to the device and take photos / modify code that way...\u003c/p\u003e\n      \n\n\n\n\n\n\n","metadata":{}}]