User Tools

Site Tools


connect_pico_to_wifi

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
connect_pico_to_wifi [2026/08/17 11:45] – [Introduction] walkeradminconnect_pico_to_wifi [2026/08/17 12:26] (current) – [Wifi Connect with LCD] walkeradmin
Line 9: Line 9:
 {{ :wifi-logo-s.png?150 |}} {{ :wifi-logo-s.png?150 |}}
  
-One +A very common activity for a Raspberry Pi Pico is to connect it to the Internet via WiFi (it has to be a Pico 1/2 'W' variant for this to work' Because a Pico does not have an OS of sorts, there is no built in UI to use to connect to WIFI, so we have to do this using a script. 
 +\\  
 +\\  
 +Now, you can do this at boot, but if you lose WIFI you might need to reboot. You could define a function that you call every say 6 hours to connect to the WIFI so you know it is always connected. Or you could use a button to force connection when you press it.  
 +\\  
 +\\  
 +After this, your main issue is working out what IP Address the Pico now has. That is something we could solve with say a small LCD Screen. 
 +\\  
 +\\  
 +---- 
 +==== WIFI Script ==== 
 +\\  
 +Below is the script for connecting to a WIFI network. You can save this as main.py, so it runs on boot, and the rest of your code will have to be in main.py also. This is what we will do here, but later on, in another module, we will look at how to define this as a subroutine using a def statement. 
 +\\  
 +\\  
 +Below is the WIFI connect code, it is quite short, it seems long because of the comments, but when you understand the code you can remove them. 
 + 
 +<code:python | download> 
 +### Connect Pico to Wifi ### 
 + 
 +#Import the network module 
 +#The Pico W uses the built‑in CYW43439 Wi‑Fi chip, controlled through MicroPython’s network module. 
 +import network 
 +import time 
 + 
 + 
 +#Create a WLAN station interface 
 +#This puts the Pico W into Wi‑Fi client mode so it can join your router. 
 +wlan = network.WLAN(network.STA_IF) 
 +wlan.active(True) 
 + 
 +#Enter your Wi‑Fi SSID and password 
 +#You must provide your router’s network name and password. 
 +ssid = "YOUR-SSID" 
 +password = "YOUR-WIFI-PASSWORD" 
 +wlan.connect(ssid, password) 
 + 
 + 
 +#Wait for connection 
 +#The Pico W needs a few seconds to negotiate with your router. 
 + 
 +#Add a simple wait loop: 
 +while not wlan.isconnected(): 
 +    print("Connecting..."
 +    time.sleep(0.5) 
 + 
 +#Print your IP address 
 +#This confirms the Pico W is online and shows the IP assigned by your router. 
 +print("Connected!"
 +print(wlan.ifconfig()) 
 +</code> 
 + 
 +For comparison, here is the same code with no comments: 
 + 
 +<code:python | download> 
 +import network 
 +import time 
 +wlan = network.WLAN(network.STA_IF) 
 +wlan.active(True) 
 +ssid = "YOUR-SSID" 
 +password = "YOUR-WIFI-PASSWORD" 
 +wlan.connect(ssid, password) 
 + 
 +while not wlan.isconnected(): 
 +    print("Connecting..."
 +    time.sleep(0.5) 
 +print("Connected!"
 +print(wlan.ifconfig()) 
 +</code> 
 +\\  
 +\\  
 +---- 
 +==== Running the Code ==== 
 +\\  
 +I have called my script wifi-connect.py because if I run it at boot, I may not see the IP Address, I need to test this idea. 
 +\\  
 +\\  
 +When I run my wifi-connect.py from Thonny, this is what I see: 
 +\\  
 +\\  
 +{{ :wifi-connect-code-001.png?800 |}} 
 +\\  
 +At the bottom of the Thonny IDE we see the following: 
 +\\  
 +  * Connected! 
 +  * ('10.194.1.205', '255.255.255.0', '10.194.1.1', '10.194.1.1'
 + 
 +This tells us that we were successfully connected and that we have the following IP details: 
 + 
 +  * IP Address - 10.194.1.205 
 +  * Subnet - 255.255.255.0 
 +  * Gateway - 10.194.1.1 
 +  * DNS - 10.194.1.1 
 + 
 +This works very well, but unfortunately you need to be connected to an IDE to see the output of the IP Address details. 
 +\\  
 +\\  
 +---- 
 +==== Wifi Connect with LCD ==== 
 +\\  
 +If you have completed the wiki page [[using_pico_with_ssd1306_based_lcd|Using Pico with SSD1306 based LCD]] and you still have your LCD, then you can use this following code to: 
 + 
 +  * Connect to the Wifi 
 +  * Print IP to LCD 
 + 
 +Download the code below and save it to your Pico as lcd-test-wifi.py 
 + 
 +<code:python | download> 
 +from machine import Pin, I2C 
 +import ssd1306 
 +import network 
 +import time 
 + 
 +#------------- wufi connect -------------------- 
 +wlan = network.WLAN(network.STA_IF) 
 +wlan.active(True) 
 +ssid = "YOUR-SSID" 
 +password = "YOUR-WIFI-PASSWORD" 
 +wlan.connect(ssid, password) 
 + 
 + 
 +while not wlan.isconnected(): 
 +    print("Connecting..."
 +    time.sleep(0.5) 
 + 
 +#Print your IP address 
 +print("Connected!"
 +print(wlan.ifconfig()) 
 + 
 +#------------------------------------------------ 
 + 
 +#-------------- LCD Show ------------------------ 
 + 
 +# Your wiring: SDA=GP4, SCL=GP5 → I2C0 
 +i2c = I2C(0, scl=Pin(5), sda=Pin(4), freq=200000) 
 + 
 +lcd = ssd1306.SSD1306_I2C(128, 64, i2c) 
 + 
 +lcd.fill(0) 
 +lcd.text("Yo Tuesday Club!", 0, 0) 
 +lcd.text("SSD1306 LCD", 0, 16) 
 +lcd.text("Address 0x3C", 0, 32) 
 + 
 +#converts IP information (its a tuple, kinda like an array, into a string) 
 +ip = wlan.ifconfig()[0]   # first item = IP address 
 +lcd.text("IP: " + ip, 0, 48) 
 + 
 +lcd.show() 
 +#------------------------------------------------ 
 + 
 +time.sleep(300) 
 +lcd.fill(0) 
 +lcd.show() 
 +</code> 
 + 
 +If this has worked then you should see something similar to below. 
 +\\  
 +\\  
 +{{ :wifi-connect-lcd.png?800 |}} 
 +\\  
 +Here we can see the last line on the LCD is my Pico IP Address
connect_pico_to_wifi.1786967143.txt.gz · Last modified: by walkeradmin