{"id":182776,"date":"2026-02-05T12:40:35","date_gmt":"2026-02-05T12:40:35","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=182776"},"modified":"2026-02-05T12:40:38","modified_gmt":"2026-02-05T12:40:38","slug":"raspberry-pi-pico-w-save-network-credentials","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-save-network-credentials\/","title":{"rendered":"Raspberry Pi Pico W: Save Network Credentials on a Separate File (MicroPython)"},"content":{"rendered":"\n<p>In this quick guide for the Raspberry Pi Pico W, you&#8217;ll learn how to save your network credentials in a separate file saved on the Pico&#8217;s filesystem (in addition to your <em>main.py<\/em> script).<\/p>\n\n\n\n<p>When creating networking projects, it might be useful to save configuration settings, such as the SSID and password, in a separate file stored on the Pico&#8217;s filesystem. Then, you can easily reference that file in your main code to use those credentials. This is not only practical but also prevents you from accidentally sharing your network credentials or sensitive information when sharing your code with a friend or online.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/Raspberry-Pi-Pico-external-File-Save-Network-Credentials.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico W Save Network Credentials on a Separate File MicroPython\" class=\"wp-image-189784\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/Raspberry-Pi-Pico-external-File-Save-Network-Credentials.jpg?w=1920&amp;quality=100&amp;strip=all&amp;ssl=1 1920w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/Raspberry-Pi-Pico-external-File-Save-Network-Credentials.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/Raspberry-Pi-Pico-external-File-Save-Network-Credentials.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/Raspberry-Pi-Pico-external-File-Save-Network-Credentials.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/Raspberry-Pi-Pico-external-File-Save-Network-Credentials.jpg?resize=1536%2C864&amp;quality=100&amp;strip=all&amp;ssl=1 1536w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntclgreen\"><strong>New to the Raspberry Pi Pico?<\/strong> Start here: <a href=\"https:\/\/randomnerdtutorials.com\/getting-started-raspberry-pi-pico-2-w\/\">Getting Started with Raspberry Pi Pico 2 and Pico 2 W<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>To follow this tutorial, you need:<\/p>\n\n\n\n<p><strong>1)<\/strong> A <strong><a href=\"https:\/\/makeradvisor.com\/tools\/raspberry-pi-pico-w\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Raspberry Pi Pico W<\/a><\/strong> or <a href=\"https:\/\/makeradvisor.com\/tools\/raspberry-pi-pico-2-w\/\" target=\"_blank\" rel=\"noopener\" title=\"\"><strong>Raspberry Pi Pico 2W<\/strong><\/a> board (these models support Wi-Fi).<\/p>\n\n\n\n<p><strong>2)<\/strong> MicroPython firmware installed on your Raspberry Pi Pico board, and an IDE to write and upload code. Follow this tutorial: <a href=\"https:\/\/randomnerdtutorials.com\/getting-started-raspberry-pi-pico-w\/#install-thonny-ide\">Programming Raspberry Pi Pico using MicroPython<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a Configuration File<\/h2>\n\n\n\n<p><strong>1.<\/strong> Open Thonny IDE and establish a connection with your Raspberry Pi Pico W.<\/p>\n\n\n\n<p><strong>2.<\/strong> Create a new file.<\/p>\n\n\n\n<p><strong>3.<\/strong> Write the following inside that file, but replace it with your network credentials:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>wifi_ssid = 'REPLACE_WITH_YOUR_SSID'\nwifi_password = 'REPLACE_WITH_YOUR_PASSWORD'<\/code><\/pre>\n\n\n\n<p><strong>4.<\/strong> Save the file on the Raspberry Pi Pico filesystem. In Thonny IDE, go to <strong>File<\/strong> &gt; <strong>Save as..<\/strong>. &gt; <strong>Raspberry Pi Pico<\/strong> and save the file with the name <span class=\"rnthl rntliteral\"><em>config.py<\/em><\/span>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"214\" height=\"202\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/Save-File-RPi-Pico-Thonny-IDE.png?resize=214%2C202&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Save File to Raspberry Pi Pico - Thonny IDE\" class=\"wp-image-182783\"\/><\/figure><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"545\" height=\"327\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/save-config-file-rpi-pico-thonny-ide.png?resize=545%2C327&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Save config.py file to Pico's filesystem on Thonny IDE\" class=\"wp-image-182784\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/save-config-file-rpi-pico-thonny-ide.png?w=545&amp;quality=100&amp;strip=all&amp;ssl=1 545w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/save-config-file-rpi-pico-thonny-ide.png?resize=300%2C180&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 545px) 100vw, 545px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Importing the Network Credentials<\/h2>\n\n\n\n<p>After creating the file with your network credentials, you can easily import them to your main code as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>from config import wifi_ssid, wifi_password<\/code><\/pre>\n\n\n\n<p>Then, you can use the <span class=\"rnthl rntliteral\">wifi_ssid<\/span>, and <span class=\"rnthl rntliteral\">wifi_password<\/span> variables to initialize Wi-Fi, when calling the <span class=\"rnthl rntliteral\">wlan.connect()<\/span> function.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>wlan.connect(wifi_ssid, wifi_password)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Web Server with <em>config.py<\/em> File<\/h2>\n\n\n\n<p>To show you how to apply this in a practical example, here&#8217;s a simple &#8220;Hello, World&#8221; web server script that uses the SSID and password credentials from the external file to connect to the internet.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n# Complete project details: https:\/\/RandomNerdTutorials.com\/raspberry-pi-pico-w-save-network-credentials\/\n\nimport network\nimport socket\nimport time\nimport random\n\n# Import SSID and password from the config.py file\nfrom config import wifi_ssid, wifi_password\n\n# HTML template for the webpage\nwebpage = &quot;&quot;&quot;\n        &lt;!DOCTYPE html&gt;\n        &lt;html&gt;\n        &lt;head&gt;\n            &lt;title&gt;Pico Web Server&lt;\/title&gt;\n            &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;\n        &lt;\/head&gt;\n        &lt;body&gt;\n            &lt;h1&gt;Raspberry Pi Pico Web Server&lt;\/h1&gt;\n            &lt;p&gt;Hello, World!&lt;\/p&gt;\n        &lt;\/body&gt;\n        &lt;\/html&gt;\n        &quot;&quot;&quot;\n\n# Init Wi-Fi Interface\ndef init_wifi(ssid, password):\n    wlan = network.WLAN(network.STA_IF)\n    wlan.active(True)\n    # Connect to your network\n    wlan.connect(ssid, password)\n    # Wait for Wi-Fi connection\n    connection_timeout = 10\n    while connection_timeout &gt; 0:\n        if wlan.status() &gt;= 3:\n            break\n        connection_timeout -= 1\n        print('Waiting for Wi-Fi connection...')\n        time.sleep(1)\n    # Check if connection is successful\n    if wlan.status() != 3:\n        return False\n    else:\n        print('Connection successful!')\n        network_info = wlan.ifconfig()\n        print('IP address:', network_info[0])\n        return True\n\nif init_wifi(wifi_ssid, wifi_password):\n    # Set up socket and start listening\n    addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]\n    s = socket.socket()\n    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n    s.bind(addr)\n    s.listen()\n    print('Listening on', addr)\n\n# Main loop to listen for connections\nwhile True:\n    try:\n        conn, addr = s.accept()\n        print('Got a connection from', addr)\n\n        # Receive and parse the request\n        request = conn.recv(1024)\n        request_str = request.decode('utf-8')\n        print('Request content:')\n        \n        # Split headers and body\n        headers, body = request_str.split('\\r\\n\\r\\n', 1)\n        print('Headers:\\n', headers)\n        print('Body:\\n', body)\n\n        # Generate HTML response\n        response = webpage\n\n        # Send the HTTP response and close the connection\n        conn.send('HTTP\/1.0 200 OK\\r\\nContent-type: text\/html\\r\\n\\r\\n')\n        conn.send(response)\n        conn.close()\n\n    except OSError as e:\n        conn.close()\n        print('Connection closed')\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Raspberry-Pi-Pico\/MicroPython\/Web_Server_External_config_py_file.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>Run this previous code on your Raspberry Pi Pico.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"470\" height=\"114\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/thonny-ide-run-button.png?resize=470%2C114&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Thonny IDE Green Run Button\" class=\"wp-image-144594\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/thonny-ide-run-button.png?w=470&amp;quality=100&amp;strip=all&amp;ssl=1 470w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/thonny-ide-run-button.png?resize=300%2C73&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 470px) 100vw, 470px\" \/><\/figure><\/div>\n\n\n<p>After a while, it will connect to your network and display its IP address.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"734\" height=\"342\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/RPi-Pico-Successfully-Connected-To-Network.png?resize=734%2C342&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"RPi Pico Connecting Successfully to Wi-Fi Network and printing its IP address\" class=\"wp-image-182785\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/RPi-Pico-Successfully-Connected-To-Network.png?w=734&amp;quality=100&amp;strip=all&amp;ssl=1 734w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/RPi-Pico-Successfully-Connected-To-Network.png?resize=300%2C140&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 734px) 100vw, 734px\" \/><\/figure><\/div>\n\n\n<p>You can access the web server on your web browser by typing the IP address.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"604\" height=\"299\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/RPi-Pico-Web-Server-Hello-World.png?resize=604%2C299&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico Hello World Web Server\" class=\"wp-image-182786\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/RPi-Pico-Web-Server-Hello-World.png?w=604&amp;quality=100&amp;strip=all&amp;ssl=1 604w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/RPi-Pico-Web-Server-Hello-World.png?resize=300%2C149&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 604px) 100vw, 604px\" \/><\/figure><\/div>\n\n\n<p>As you can see, there&#8217;s no need to insert the credentials in your main code if you have stored them in an external file that is saved on the Pico filesystem and referenced in the main code.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Saving Multiple Network Credentials<\/h2>\n\n\n\n<p>Instead of saving a single SSID and password combination, it might be beneficial to use multiple combinations. To do that, you can create a JSON file with multiple SSID and password combinations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating a JSON File<\/h3>\n\n\n\n<p>Create a new file on Thonny IDE with the following content.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>&#091;\n&nbsp; &nbsp; {\"ssid\": \"your_ssid_1\", \"password\": \"your_password_1\"},\n&nbsp; &nbsp; {\"ssid\": \"your_ssid_2\", \"password\": \"your_password_2\"},\n&nbsp; &nbsp; {\"ssid\": \"your_ssid_3\", \"password\": \"your_password_3\"}\n]<\/code><\/pre>\n\n\n\n<p>Replace <span class=\"rnthl rntliteral\">your_ssid1<\/span> and <span class=\"rnthl rntliteral\">your_password_1<\/span> with the first SSID and password combination, and so on. You can add as many combinations as you need, or only have one or two.<\/p>\n\n\n\n<p>Save it on your Raspberry Pi Pico with the name: <span class=\"rnthl rntliteral\"><em>wifi_credentials.json<\/em><\/span>. In Thonny IDE, go to <strong>File <\/strong>&gt; <strong>Save as&#8230;<\/strong> &gt; <strong>Raspberry Pi Pico<\/strong>. Call it <span class=\"rnthl rntliteral\"><em>wifi_credentials.json<\/em><\/span>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"545\" height=\"327\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/save-wifi-credentials-json-file-rpi-pico.png?resize=545%2C327&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"RPi-Pico-Saving-JSON-file-with-Wi-Fi-credentials\" class=\"wp-image-182787\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/save-wifi-credentials-json-file-rpi-pico.png?w=545&amp;quality=100&amp;strip=all&amp;ssl=1 545w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/save-wifi-credentials-json-file-rpi-pico.png?resize=300%2C180&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 545px) 100vw, 545px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">Connecting to Your Network Using the JSON File<\/h3>\n\n\n\n<p>With just a few modifications to the previous example, we can use that JSON file to try the different SSID and password combinations. Here\u2019s the modified code:<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n# Complete project details: https:\/\/RandomNerdTutorials.com\/raspberry-pi-pico-w-save-network-credentials\/\n\nimport network\nimport socket\nimport time\nimport json\n\n# HTML template for the webpage\nwebpage = &quot;&quot;&quot;\n        &lt;!DOCTYPE html&gt;\n        &lt;html&gt;\n        &lt;head&gt;\n            &lt;title&gt;Pico Web Server&lt;\/title&gt;\n            &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;\n        &lt;\/head&gt;\n        &lt;body&gt;\n            &lt;h1&gt;Raspberry Pi Pico Web Server&lt;\/h1&gt;\n            &lt;p&gt;Hello, World!&lt;\/p&gt;\n        &lt;\/body&gt;\n        &lt;\/html&gt;\n        &quot;&quot;&quot;\n\ndef init_wifi_from_file(file_path='wifi_credentials.json'):\n    try:\n        with open(file_path, 'r') as file:\n            credentials = json.load(file)\n    except OSError:\n        print(f&quot;Error: Unable to read {file_path}. Make sure the file exists and is properly formatted.&quot;)\n        return False\n\n    for cred in credentials:\n        ssid = cred.get('ssid')\n        password = cred.get('password')\n        if ssid and password:\n            if init_wifi(ssid, password):\n                return True\n\n    print(&quot;Unable to connect to any Wi-Fi network.&quot;)\n    return False\n\n# Init Wi-Fi Interface\ndef init_wifi(ssid, password):\n    wlan = network.WLAN(network.STA_IF)\n    wlan.active(True)\n    wlan.disconnect()\n\n    print(f&quot;Trying to connect to Wi-Fi network with SSID: {ssid}&quot;)\n\n    wlan.connect(ssid, password)\n\n    # Wait for Wi-Fi connection\n    connection_timeout = 10\n    while connection_timeout &gt; 0:\n        if wlan.isconnected():\n            print('Connection successful!')\n            network_info = wlan.ifconfig()\n            print('IP address:', network_info[0])\n            return True\n\n        connection_timeout -= 1\n        print('Waiting for Wi-Fi connection...')\n        time.sleep(1)\n\n    print(f&quot;Failed to connect to Wi-Fi network with SSID: {ssid}&quot;)\n    return False\n\n# Set up Wi-Fi\nif not init_wifi_from_file():\n    print(&quot;Exiting program.&quot;)\nelse:\n    try:\n        # Set up socket and start listening\n        addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]\n        s = socket.socket()\n        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n        s.bind(addr)\n        s.listen()\n        print('Listening on', addr)\n\n        # Main loop to listen for connections\n        while True:\n            try:\n                conn, addr = s.accept()\n                print('Got a connection from', addr)\n\n                # Receive and parse the request\n                request = conn.recv(1024)\n                request_str = request.decode('utf-8')\n                print('Request content:')\n                \n                # Split headers and body\n                headers, body = request_str.split('\\r\\n\\r\\n', 1)\n                print('Headers:\\n', headers)\n                print('Body:\\n', body)\n\n                # Generate HTML response\n                response = webpage\n\n                # Send the HTTP response and close the connection\n                conn.send('HTTP\/1.0 200 OK\\r\\nContent-type: text\/html\\r\\n\\r\\n')\n                conn.send(response)\n                conn.close()\n\n            except OSError as e:\n                conn.close()\n                print('Connection closed')\n\n    except KeyboardInterrupt:\n        print('Server stopped by user.')\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Raspberry-Pi-Pico\/MicroPython\/Web_Server_wifi_credentials_json.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>Let\u2019s just take a quick look at the relevant parts for this example.<\/p>\n\n\n\n<p>We create a new function that will initialize Wi-Fi with the credentials saved in the JSON file.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>def init_wifi_from_file(file_path='wifi_credentials.json'):\n&nbsp; &nbsp; try:\n&nbsp; &nbsp; &nbsp; &nbsp; with open(file_path, 'r') as file:\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; credentials = json.load(file)\n&nbsp; &nbsp; except OSError:\n&nbsp; &nbsp; &nbsp; &nbsp; print(f\"Error: Unable to read {file_path}. Make sure the file exists\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; and is properly formatted.\")\n&nbsp; &nbsp; &nbsp; &nbsp; return False\n&nbsp; &nbsp; for cred in credentials:\n&nbsp; &nbsp; &nbsp; &nbsp; ssid = cred.get('ssid')\n&nbsp; &nbsp; &nbsp; &nbsp; password = cred.get('password')\n&nbsp; &nbsp; &nbsp; &nbsp; if ssid and password:\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if init_wifi(ssid, password):\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return True\n&nbsp; &nbsp; print(\"Unable to connect to any Wi-Fi network.\")\n&nbsp; &nbsp; return False<\/code><\/pre>\n\n\n\n<p>It opens the file and saves the JSON content to the <span class=\"rnthl rntliteral\">credentials<\/span> variable.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>with open(file_path, 'r') as file:\n&nbsp; &nbsp; credentials = json.load(file)<\/code><\/pre>\n\n\n\n<p>Then, we go through all JSON elements, and for each SSID\/password combination, we attempt to connect to Wi-Fi:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>for cred in credentials:\n&nbsp; &nbsp; ssid = cred.get('ssid')\n&nbsp; &nbsp; password = cred.get('password')\n&nbsp; &nbsp; if ssid and password:\n&nbsp; &nbsp; &nbsp; &nbsp; if init_wifi(ssid, password):\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return True<\/code><\/pre>\n\n\n\n<p>Then, before proceeding with the web server, we check if we are able to connect to any of the Wi-Fi networks. If we don\u2019t, it will exit the program.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Set up Wi-Fi\nif not init_wifi_from_file():\n&nbsp; &nbsp; print(\"Exiting program.\")<\/code><\/pre>\n\n\n\n<p>Otherwise, it will proceed with the rest of the code to create the web server.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Testing the Example<\/h3>\n\n\n\n<p>Run this previous example on your board. It will attempt to connect to your network using each SSID and password combination until it gets a connection.<\/p>\n\n\n\n<p>In my case, to test this example, I purposedly only entered the right credentials in the second option. You can see that it attempts several times to connect using the firt SSID\/password pair. It fails, so it will try the second option.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"713\" height=\"527\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/RPi-Pico-Connecting-to-Network-Credentials-JSON-File.png?resize=713%2C527&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico Trying to Connect to Different Networks\" class=\"wp-image-182789\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/RPi-Pico-Connecting-to-Network-Credentials-JSON-File.png?w=713&amp;quality=100&amp;strip=all&amp;ssl=1 713w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/RPi-Pico-Connecting-to-Network-Credentials-JSON-File.png?resize=300%2C222&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 713px) 100vw, 713px\" \/><\/figure><\/div>\n\n\n<p>With this second option, it will successfully connect to the network. This approach can be useful if you&#8217;re working on your project on different places with different networks.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this quick example, we\u2019ve shown you how to load the Wi-Fi credentials from a separate file. This can be useful because you don\u2019t need to type your credentials in each code you want to test, and it prevents sharing your network credentials accidentally.<\/p>\n\n\n\n<p>If you want your Raspberry Pi Pico to try more than one combination of SSID\/password, you can create a JSON file with multiple combinations.<\/p>\n\n\n\n<p>When creating web server projects or any networking projects, choose the method that is more suitable for your application.<\/p>\n\n\n\n<p>We hope you&#8217;ve found this quick guide useful. We have more tutorials related to Wi-Fi with the Raspberry Pi Pico that you may like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-wi-fi-micropython\/\" title=\"\">Raspberry Pi Pico W: Getting Started with Wi-Fi (MicroPython)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-http-requests-micropython\/\">Raspberry Pi Pico W: Getting Started with HTTP GET Requests (MicroPython)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-web-server-micropython\/\">Raspberry Pi Pico: Web Server (MicroPython)<\/a><\/li>\n<\/ul>\n\n\n\n<p>Learn more about the Raspberry Pi Pico with our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-raspberry-pi-pico\/\" title=\"\">All our Raspberry Pi Pico Projects and Guides<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-micropython-ebook\/\" title=\"\">Learn Raspberry Pi Pico\/Pico W with MicroPython eBook<\/a><\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this quick guide for the Raspberry Pi Pico W, you&#8217;ll learn how to save your network credentials in a separate file saved on the Pico&#8217;s filesystem (in addition to &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Raspberry Pi Pico W: Save Network Credentials on a Separate File (MicroPython)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-save-network-credentials\/#more-182776\" aria-label=\"Read more about Raspberry Pi Pico W: Save Network Credentials on a Separate File (MicroPython)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":189784,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[324,326],"tags":[],"class_list":["post-182776","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-raspberry-pi-pico","category-raspberry-pi-pico-micropython"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/11\/Raspberry-Pi-Pico-external-File-Save-Network-Credentials.jpg?fit=1920%2C1080&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/182776","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=182776"}],"version-history":[{"count":6,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/182776\/revisions"}],"predecessor-version":[{"id":189790,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/182776\/revisions\/189790"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/189784"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=182776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=182776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=182776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}