{"id":162278,"date":"2024-10-10T13:34:34","date_gmt":"2024-10-10T13:34:34","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=162278"},"modified":"2024-10-10T13:45:14","modified_gmt":"2024-10-10T13:45:14","slug":"raspberry-pi-pico-w-wi-fi-micropython","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-wi-fi-micropython\/","title":{"rendered":"Raspberry Pi Pico W: Getting Started with Wi-Fi (MicroPython)"},"content":{"rendered":"\n<p>The Raspberry Pi Pico W comes with the Infineon CYW43439 chip that contains a 2.4 GHz radio providing 802.11n Wi-Fi. This tutorial is an introduction to Wi-Fi on the Raspberry Pi Pico W using MicroPython. We&#8217;ll cover setting the Pico as a Wi-Fi station and as an Access Point (AP), scanning for nearby Wi-Fi networks, connecting your Pico to your local network, and getting information about its IP address.<\/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\/2024\/09\/Raspberry-Pi-Pico-Wi-Fi.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico W Getting Started with Wi-Fi MicroPython\" class=\"wp-image-162289\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/Raspberry-Pi-Pico-Wi-Fi.jpg?w=1920&amp;quality=100&amp;strip=all&amp;ssl=1 1920w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/Raspberry-Pi-Pico-Wi-Fi.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/Raspberry-Pi-Pico-Wi-Fi.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/Raspberry-Pi-Pico-Wi-Fi.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/Raspberry-Pi-Pico-Wi-Fi.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\">New to the Raspberry Pi Pico? Get started here: <a href=\"https:\/\/randomnerdtutorials.com\/getting-started-raspberry-pi-pico-w\/\">Get started with the Raspberry Pi Pico here<\/a>.<\/p>\n\n\n\n<p><strong>Table of Contents: <\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"#station-access-point\" title=\"\">Wi-Fi Station and Wi-Fi Access Point<\/a><\/li>\n\n\n\n<li><a href=\"#station-access-point\" title=\"\">Station and Access Point Interfaces<\/a><\/li>\n\n\n\n<li><a href=\"#scanning-networks\" title=\"\">Scanning Nearby Wi-Fi Networks<\/a><\/li>\n\n\n\n<li><a href=\"#connecting-to-a-network\" title=\"\">Connecting the Raspberry Pi Pico to a Wi-Fi network<\/a><\/li>\n\n\n\n<li><a href=\"#static-ip\" title=\"\">Setting a Static IP Address to Your Raspberry Pi Pico<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites &#8211; MicroPython Firmware<\/h2>\n\n\n\n<p>To follow this tutorial you need MicroPython firmware installed in your Raspberry Pi Pico board. You also need an IDE to write and upload the code to your board.<\/p>\n\n\n\n<p>The recommended MicroPython IDE for the Raspberry Pi Pico is Thonny IDE. Follow the next tutorial to learn how to install Thonny IDE, flash MicroPython firmware, and upload code to the board.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-raspberry-pi-pico-w\/#install-thonny-ide\">Programming Raspberry Pi Pico using MicroPython<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"station-access-point\">Wi-Fi Station and Wi-Fi Access Point<\/h2>\n\n\n\n<p>The Raspberry Pi Pico can act as a Wi-Fi station, or as a Wi-Fi access point.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Wi-Fi Station<\/h3>\n\n\n\n<p>When the Pico is set as a Wi-Fi station, it can connect to other networks (like your router\u2014local network). In this scenario, the router assigns a unique IP address to your board. You can communicate with the Raspberry Pi Pico using other devices (stations) that are also connected to the same network by referring to the Raspberry Pi Pico&#8217;s local IP address.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"442\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/Wi-Fi-Station-Rpi-Pico.png?resize=750%2C442&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico Wi-Fi Station\" class=\"wp-image-162279\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/Wi-Fi-Station-Rpi-Pico.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/Wi-Fi-Station-Rpi-Pico.png?resize=300%2C177&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>Since the router is also connected to the internet, we can <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-http-requests-micropython\/\" title=\"\">request information from the internet<\/a> using our board, like data from APIs (weather data, for example), publish data to online platforms, use icons and images from the internet in our web server pages or include JavaScript libraries.<\/p>\n\n\n\n<p>However, in some cases, we may not have a router nearby to connect the Raspberry Pi Pico. In this scenario, you can set your board as an access point.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Access Point (AP)<\/h3>\n\n\n\n<p>When your Pico is set up as an Access Point (AP), other devices (such as your smartphone, tablet, or computer) can connect to it without the need for a router; the Pico creates its own Wi-Fi network. Unlike a router, an Access Point doesn\u2019t connect further to a wired network or the Internet, so you can\u2019t access external libraries, publish sensor readings to the cloud, or use services like mail.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"529\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/Wi-Fi-Access-Point-Pico.png?resize=750%2C529&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico as an access point\" class=\"wp-image-162280\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/Wi-Fi-Access-Point-Pico.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/Wi-Fi-Access-Point-Pico.png?resize=300%2C212&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"station-access-point-interfaces\">Station and Access Point Interfaces<\/h2>\n\n\n\n<p>There is a MicroPython module called <span class=\"rnthl rntliteral\">network<\/span> that is used to configure and handle everything Wi-Fi-related. There are two Wi-Fi interfaces, one for the station and another for the access point. The following example creates a station interface and an access point interface and checks if they are active.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Rui Santos &amp; Sara Santos - Random Nerd Tutorials\r\n# Complete project details at https:\/\/RandomNerdTutorials.com\/raspberry-pi-pico-w-wi-fi-micropython\/\r\n\r\nimport network\r\n\r\nsta_if = network.WLAN(network.STA_IF)\r\nprint(sta_if.active())\r\n\r\nap_if = network.WLAN(network.AP_IF)\r\nprint(ap_if.active())<\/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\/Station_Access_Point_Interfaces.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>After running this example, you should get something as follows.<\/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=\"602\" height=\"265\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/checking-station-access-point-interfaces.png?resize=602%2C265&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Rpi Pico Checking Wi-Fi Station and access point interfaces\" class=\"wp-image-162282\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/checking-station-access-point-interfaces.png?w=602&amp;quality=100&amp;strip=all&amp;ssl=1 602w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/checking-station-access-point-interfaces.png?resize=300%2C132&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/figure><\/div>\n\n\n<p>At the moment, you don\u2019t have any active interface.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"scanning-networks\">Scanning Nearby Wi-Fi Networks<\/h2>\n\n\n\n<p>To scan for nearby Wi-Fi networks, we must start a Wi-Fi interface station network first. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Init Wi-Fi interface\nwlan = network.WLAN(network.STA_IF)\nwlan.active(True)<\/code><\/pre>\n\n\n\n<p>Then we can use <span class=\"rnthl rntliteral\">wlan.scan()<\/span> to scan for nearby networks. This command returns a list of named tuples that include SSDI, BSSID, RSSI, Wi-Fi channel, and other details.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Scan for available Wi-Fi networks\nnetworks = wlan.scan()<\/code><\/pre>\n\n\n\n<p>Then, we can go through all the returned networks and print their info. The complete code can be found below.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Rui Santos &amp; Sara Santos - Random Nerd Tutorials\r\n# Complete project details at https:\/\/RandomNerdTutorials.com\/raspberry-pi-pico-w-wi-fi-micropython\/\r\n\r\nimport network\r\n\r\n# Init Wi-Fi interface\r\nwlan = network.WLAN(network.STA_IF)\r\nwlan.active(True)\r\n\r\n# Scan for Wi-Fi networks\r\nnetworks = wlan.scan()\r\n\r\n# Print Wi-Fi networks\r\nprint(&quot;Available WiFi Networks:&quot;)\r\nfor network_info in networks:\r\n    print(network_info)<\/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\/Scan_Networks.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>Run the previous code on your Raspberry Pi Pico. It should return a list of nearby Wi\u2011Fi networks and corresponding info. The network information is separated by commas and is in the following order: SSDI, BSSID, RSSI, channel.<\/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=\"705\" height=\"335\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/scan-wi-fi-networks.png?resize=705%2C335&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"RPi Pico Scanning Wi-Fi Networks\" class=\"wp-image-162283\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/scan-wi-fi-networks.png?w=705&amp;quality=100&amp;strip=all&amp;ssl=1 705w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/scan-wi-fi-networks.png?resize=300%2C143&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 705px) 100vw, 705px\" \/><\/figure><\/div>\n\n\n<p>The RSSI is the received signal strength indicator and it shows how strong is the Wi\u2011Fi signal. The lower the value, the worse the signal.<\/p>\n\n\n\n<p>As for the last two numbers, at the moment, the documentation is not clear what those numbers exactly mean. Supposedly they represent the auth mode and if the network is hidden or not. Unfortunately, the values we get don\u2019t match the range in the documentation. So, they either represent something different, or there is some sort of bug.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"connecting-to-a-network\">Connecting the Raspberry Pi Pico to a Wi-Fi Network<\/h2>\n\n\n\n<p>Connecting your Pico board to your Wi-Fi network is very straightforward thanks to the <span class=\"rnthl rntliteral\">network<\/span> MicroPython module. To connect to your network, you need to know the SSID (the name of your Wi-Fi network) and password.<\/p>\n\n\n\n<p>When a device connects to a network, the router assigns an IP address to that device. All devices connected to a network, have a unique IP address. So, when you connect the Raspberry Pi Pico to your router, it will assign it a unique IP address on your network.<\/p>\n\n\n\n<p>The following example connects the Raspberry Pi Pico to your network and prints the IP address assigned to the Pico on your network.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Rui Santos &amp; Sara Santos - Random Nerd Tutorials\r\n# Complete project details at https:\/\/RandomNerdTutorials.com\/raspberry-pi-pico-w-wi-fi-micropython\/\r\n\r\nimport network\r\nfrom time import sleep\r\n# Wi-Fi credentials\r\nssid = 'REPLACE_WITH_YOUR_SSID'\r\npassword = 'REPLACE_WITH_YOUR_PASSWORD'\r\n\r\n# Init Wi-Fi Interface\r\nwlan = network.WLAN(network.STA_IF)\r\nwlan.active(True)\r\n\r\n# Connect to your network\r\nwlan.connect(ssid, password)\r\n\r\n# Wait for Wi-Fi connection\r\nconnection_timeout = 10\r\nwhile connection_timeout &gt; 0:\r\n    if wlan.status() &gt;= 3:\r\n        break\r\n    connection_timeout -= 1\r\n    print('Waiting for Wi-Fi connection...')\r\n    sleep(1)\r\n\r\n# Check if connection is successful\r\nif wlan.status() != 3:\r\n    raise RuntimeError('Failed to establish a network connection')\r\nelse:\r\n    print('Connection successful!')\r\n    network_info = wlan.ifconfig()\r\n    print('IP address:', network_info[0])<\/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\/Connect_to_Wi-Fi.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 take a quick look at how this previous code works.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Importing the network module<\/h4>\n\n\n\n<p>First, you need to import the <span class=\"rnthl rntliteral\">network<\/span> module.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>import network<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">network<\/span> module provides several methods to create and handle the Wi-Fi interfaces and connections.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Inserting your Wi-Fi Credentials<\/h4>\n\n\n\n<p>In the following two variables, you must insert your network SSID and password.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Wi-Fi credentials\nssid = 'REPLACE_WITH_YOUR_SSID'\npassword = 'REPLACE_WITH_YOUR_PASSWORD'<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Connecting to the Internet<\/h4>\n\n\n\n<p>Create a station network interface and then activate it.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Init Wi-Fi Interface\nwlan = network.WLAN(network.STA_IF)\nwlan.active(True)<\/code><\/pre>\n\n\n\n<p>Then, use that interface to connect to your home network using the SSID and password you defined earlier.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Connect to your network\nwlan.connect(ssid, password)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Checking the Connection Status and Printing the IP Address<\/h4>\n\n\n\n<p>Then, we\u2019ll check the connection status and print the IP address when we successfully connect.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Wait for Wi-Fi connection\nconnection_timeout = 10\nwhile connection_timeout &gt; 0:\n    if wlan.status() &gt;= 3:\n        break\n    connection_timeout -= 1\n    print('Waiting for Wi-Fi connection...')\n    sleep(1)\n\n# Check if connection is successful\nif wlan.status() != 3:\n    raise RuntimeError('Failed to establish a network connection')\nelse:\n    print('Connection successful!')\n    network_info = wlan.ifconfig()\n    print('IP address:', network_info&#091;0])<\/code><\/pre>\n\n\n\n<p>The code first checks the connection status, and if it\u2019s <span class=\"rnthl rntliteral\">wlan.status() &gt;= 3<\/span> (this means it has even connected to the network or failed to connect), the loop exits. It waits for a maximum of 10 seconds (10 attempts defined in the <span class=\"rnthl rntliteral\">connection_timeout<\/span> variable).<\/p>\n\n\n\n<p>The method <span class=\"rnthl rntliteral\">wlan.status()<\/span>, as the name suggests, checks the status of the Wi-Fi connection of the Raspberry Pi. This method returns an integer with the following meaning:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>0:<\/strong> WLAN is not enabled<\/li>\n\n\n\n<li><strong>1:<\/strong> WLAN is currently scanning for networks<\/li>\n\n\n\n<li><strong>2:<\/strong> WLAN is connecting to a network<\/li>\n\n\n\n<li><strong>3:<\/strong> WLAN is connected to a network<\/li>\n\n\n\n<li><strong>4:<\/strong> WLAN failed to connect to a network<\/li>\n<\/ul>\n\n\n\n<p>Then, we check if it\u2019s connected or not (we know that status = 3 means it\u2019s connected). If <span class=\"rnthl rntliteral\">wlan.status()<\/span> is different than 3, we know it failed to establish a connection.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>if wlan.status() != 3:\n&nbsp; &nbsp; raise RuntimeError('Failed to establish a network connection')<\/code><\/pre>\n\n\n\n<p>Otherwise, it means we succeed. In case we succeed, we can get the network information using the <span class=\"rnthl rntliteral\">ifconfig()<\/span> method. This method returns a tuple with the following information:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">network_info[0]<\/span>: The IP address assigned to the Pico on the network.<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">network_info[1]<\/span>: The subnet mask.<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">network_info[2]<\/span>: The gateway IP address.<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">network_info[3]<\/span>: The DNS (Domain Name System) server IP address.<\/li>\n<\/ul>\n\n\n\n<p>We just need the IP address, so we just get the first element of the array (<span class=\"rnthl rntliteral\">network_info[0]<\/span>).<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>else:\n    print('Connection successful!')\n    network_info = wlan.ifconfig()\n    print('IP address:', network_info&#091;0])<\/code><\/pre>\n\n\n\n<div class=\"wp-block-group rntbox rntclgray is-vertical is-layout-flex wp-container-core-group-is-layout-8cf370e7 wp-block-group-is-layout-flex\">\n<p><strong>The <\/strong>isconnected()<strong> method<\/strong><\/p>\n\n\n\n<p>Instead of checking the connection status, the <span class=\"rnthl rntliteral\">wlan<\/span> object supports a method called <span class=\"rnthl rntliteral\">isconnected()<\/span> that returns <span class=\"rnthl rntliteral\">True<\/span> if the board is connected to the network or <span class=\"rnthl rntliteral\">False<\/span> otherwise.You can use that approach instead of checking <span class=\"rnthl rntliteral\">wlan.status()<\/span>.<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Testing the Code<\/h3>\n\n\n\n<p>Run the previous code. Don\u2019t forget to replace the ssid and password variables with your network details.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Wi-Fi credentials\nssid = 'REPLACE_WITH_YOUR_SSID'\npassword = 'REPLACE_WITH_YOUR_PASSWORD'<\/code><\/pre>\n\n\n\n<p>After a few attempts, it should connect to your network and display 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=\"705\" height=\"289\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/pico-connect-network-get-ip-address.png?resize=705%2C289&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico: connect to network and get ip address\" class=\"wp-image-162284\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/pico-connect-network-get-ip-address.png?w=705&amp;quality=100&amp;strip=all&amp;ssl=1 705w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/pico-connect-network-get-ip-address.png?resize=300%2C123&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 705px) 100vw, 705px\" \/><\/figure><\/div>\n\n\n<h4 class=\"wp-block-heading\">Troubleshooting<\/h4>\n\n\n\n<p>If you\u2019re Raspberry Pi Pico is not connecting to your network:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>check that you\u2019re inserting the right SSID and password; spaces and uppercase\/lowercase matter;<\/li>\n\n\n\n<li>check that your network is showing up when we scanned for Wi-Fi networks previously;<\/li>\n\n\n\n<li>make sure the Raspberry Pi Pico is relatively close to your router so that it can catch the Wi-Fi signal.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"static-ip\">Setting a Static IP Address to the Raspberry Pi Pico<\/h1>\n\n\n\n<p>In the case of my network, even if I restart the Raspberry Pi Pico several times, my router will always assign it the same IP address. However, that\u2019s not the case for some other networks. In that scenario, it\u2019s nice to have a way to set a static IP address to your board, so that it doesn\u2019t change every time it resets. Additionally, it might also be useful for projects that are not connected to your computer or don\u2019t have a way to display the IP address.<\/p>\n\n\n\n<p>You can use the <span class=\"rnthl rntliteral\">ifconfig<\/span> method of the <span class=\"rnthl rntliteral\">network.WLAN<\/span> object to set a static IP address.<\/p>\n\n\n\n<p>We can easily modify the previous example with just a few lines to set a static IP address.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Rui Santos &amp; Sara Santos - Random Nerd Tutorials\r\n# Complete project details at https:\/\/RandomNerdTutorials.com\/raspberry-pi-pico-w-wi-fi-micropython\/\r\n\r\nimport network\r\nimport time\r\n\r\n# Wi-Fi credentials\r\nssid = 'REPLACE_WITH_YOUR_SSID'\r\npassword = 'REPLACE_WITH_YOUR_PASSWORD'\r\n\r\n# Static IP configuration\r\nstatic_ip = '192.168.1.100'  # Replace with your desired static IP\r\nsubnet_mask = '255.255.255.0'\r\ngateway_ip = '192.168.1.254'\r\ndns_server = '8.8.8.8'\r\n\r\n# Init Wi-Fi Interface\r\nwlan = network.WLAN(network.STA_IF)\r\nwlan.active(True)\r\n\r\n# Connect to your network\r\nwlan.connect(ssid, password)\r\n\r\n# Wait for Wi-Fi connection\r\nconnection_timeout = 10\r\nwhile connection_timeout &gt; 0:\r\n    if wlan.status() &gt;= 3:\r\n        break\r\n    connection_timeout -= 1\r\n    print('Waiting for Wi-Fi connection...')\r\n    time.sleep(1)\r\n\r\n# Set static IP address\r\nwlan.ifconfig((static_ip, subnet_mask, gateway_ip, dns_server))\r\n\r\n# Check if connection is successful\r\nif wlan.status() != 3:\r\n    raise RuntimeError('Failed to establish a network connection')\r\nelse:\r\n    print('Connection successful!')\r\n    network_info = wlan.ifconfig()\r\n    print('IP address:', network_info[0])<\/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\/Static_IP_Address.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Static IP Configuration<\/h3>\n\n\n\n<p>Replace the values in the <span class=\"rnthl rntliteral\">static_ip<\/span>, <span class=\"rnthl rntliteral\">subnet_mask<\/span>, <span class=\"rnthl rntliteral\">gateway_ip<\/span>, and <span class=\"rnthl rntliteral\">dns_server<\/span> variables with the appropriate values for your network.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Static IP configuration\nstatic_ip = '192.168.1.100'  # Replace with your desired static IP\nsubnet_mask = '255.255.255.0'\ngateway_ip = '192.168.1.254'\ndns_server = '8.8.8.8'<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">How to find those values?<\/h4>\n\n\n\n<p>There are different ways to get information about the subnet mask and the gateway IP. The easiest way is to open a Terminal window on your computer and run the following command:<\/p>\n\n\n\n<p>On Windows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ipconfig<\/code><\/pre>\n\n\n\n<p>On MacOS or Linux: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ifconfig<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"781\" height=\"115\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/running-ip-config.png?resize=781%2C115&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ipconfig commang\" class=\"wp-image-162285\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/running-ip-config.png?w=781&amp;quality=100&amp;strip=all&amp;ssl=1 781w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/running-ip-config.png?resize=300%2C44&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/running-ip-config.png?resize=768%2C113&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 781px) 100vw, 781px\" \/><\/figure><\/div>\n\n\n<p>It will return the information you\u2019re looking for: the gateway IP (the IP address of your router) and the subnet mask.<\/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=\"781\" height=\"324\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/WLAN-Details.png?resize=781%2C324&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ipconfig wlan details\" class=\"wp-image-162286\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/WLAN-Details.png?w=781&amp;quality=100&amp;strip=all&amp;ssl=1 781w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/WLAN-Details.png?resize=300%2C124&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/WLAN-Details.png?resize=768%2C319&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 781px) 100vw, 781px\" \/><\/figure><\/div>\n\n\n<p>In my case, the subnet mask is <strong>255.255.255.0<\/strong> and the gateway IP address is <strong>192.168.1.254<\/strong>.<\/p>\n\n\n\n<p>For the DNS server, you can always use<strong> 8.8.8.8<\/strong>, which is Google\u2019s public DNS server.<\/p>\n\n\n\n<p>When choosing the static IP address for your Raspberry Pi Pico, make sure that the address is not already being used by another device. To do that, you can use a software like <em>Angry IP Scanner<\/em> (compatible with Windows and Mac OS). Or you can login into your router dashboard and check which IP addresses are already assigned.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Setting a Static IP Address<\/h3>\n\n\n\n<p>After assigning the proper values for the configuration variables, we can simply use the <span class=\"rnthl rntliteral\">ifconfig()<\/span> method and pass as arguments the static IP, the subnet mask, the gateway IP, and the DNS server, in this order.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>wlan.ifconfig((static_ip, subnet_mask, gateway_ip, dns_server))<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Testing the Code<\/h3>\n\n\n\n<p>After inserting your network credentials and desired IP address settings, you can run the code on your Raspberry Pi Pico.<\/p>\n\n\n\n<p>Notice that it will have the IP address you defined on the code assigned to it. In our case, <strong>192.168.1.100<\/strong>.<\/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=\"705\" height=\"269\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/rpi-pico-set-static-ip-address.png?resize=705%2C269&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico Set Static IP address\" class=\"wp-image-162287\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/rpi-pico-set-static-ip-address.png?w=705&amp;quality=100&amp;strip=all&amp;ssl=1 705w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/rpi-pico-set-static-ip-address.png?resize=300%2C114&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 705px) 100vw, 705px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>This tutorial was a quick getting started guide for Wi-Fi on the Raspberry Pi Pico programmed with MicroPython. We hope you find it useful.<\/p>\n\n\n\n<p>You may also like other Wi-Fi-related tutorials with the Raspberry Pi Pico:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\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\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-asynchronous-web-server-micropython\/\">Raspberry Pi Pico W: Asynchronous 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><strong><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-micropython-ebook\/\" title=\"\">Learn Raspberry Pi Pico\/Pico W with MicroPython (eBook)<\/a> <\/strong><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-raspberry-pi-pico\/\">Free Raspberry Pi Pico Projects and Tutorials<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Raspberry Pi Pico W comes with the Infineon CYW43439 chip that contains a 2.4 GHz radio providing 802.11n Wi-Fi. This tutorial is an introduction to Wi-Fi on the Raspberry &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Raspberry Pi Pico W: Getting Started with Wi-Fi (MicroPython)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-wi-fi-micropython\/#more-162278\" aria-label=\"Read more about Raspberry Pi Pico W: Getting Started with Wi-Fi (MicroPython)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":162289,"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-162278","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\/2024\/09\/Raspberry-Pi-Pico-Wi-Fi.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\/162278","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=162278"}],"version-history":[{"count":10,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/162278\/revisions"}],"predecessor-version":[{"id":163061,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/162278\/revisions\/163061"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/162289"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=162278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=162278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=162278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}