connect_pico_to_wifi
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| connect_pico_to_wifi [2026/08/17 11:57] – [WIFI Script] walkeradmin | connect_pico_to_wifi [2026/08/17 12:26] (current) – [Wifi Connect with LCD] walkeradmin | ||
|---|---|---|---|
| Line 61: | Line 61: | ||
| </ | </ | ||
| + | For comparison, here is the same code with no comments: | ||
| + | < | ||
| + | import network | ||
| + | import time | ||
| + | wlan = network.WLAN(network.STA_IF) | ||
| + | wlan.active(True) | ||
| + | ssid = " | ||
| + | password = " | ||
| + | wlan.connect(ssid, | ||
| + | |||
| + | while not wlan.isconnected(): | ||
| + | print(" | ||
| + | time.sleep(0.5) | ||
| + | print(" | ||
| + | print(wlan.ifconfig()) | ||
| + | </ | ||
| + | \\ | ||
| + | \\ | ||
| + | ---- | ||
| + | ==== 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: | ||
| + | \\ | ||
| + | \\ | ||
| + | {{ : | ||
| + | \\ | ||
| + | At the bottom of the Thonny IDE we see the following: | ||
| + | \\ | ||
| + | * Connected! | ||
| + | * (' | ||
| + | |||
| + | 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 | ||
| + | |||
| + | < | ||
| + | from machine import Pin, I2C | ||
| + | import ssd1306 | ||
| + | import network | ||
| + | import time | ||
| + | |||
| + | # | ||
| + | wlan = network.WLAN(network.STA_IF) | ||
| + | wlan.active(True) | ||
| + | ssid = " | ||
| + | password = " | ||
| + | wlan.connect(ssid, | ||
| + | |||
| + | |||
| + | while not wlan.isconnected(): | ||
| + | print(" | ||
| + | time.sleep(0.5) | ||
| + | |||
| + | #Print your IP address | ||
| + | print(" | ||
| + | print(wlan.ifconfig()) | ||
| + | |||
| + | # | ||
| + | |||
| + | # | ||
| + | |||
| + | # Your wiring: SDA=GP4, SCL=GP5 → I2C0 | ||
| + | i2c = I2C(0, scl=Pin(5), sda=Pin(4), freq=200000) | ||
| + | |||
| + | lcd = ssd1306.SSD1306_I2C(128, | ||
| + | |||
| + | lcd.fill(0) | ||
| + | lcd.text(" | ||
| + | lcd.text(" | ||
| + | lcd.text(" | ||
| + | |||
| + | #converts IP information (its a tuple, kinda like an array, into a string) | ||
| + | ip = wlan.ifconfig()[0] | ||
| + | lcd.text(" | ||
| + | |||
| + | lcd.show() | ||
| + | # | ||
| + | |||
| + | time.sleep(300) | ||
| + | lcd.fill(0) | ||
| + | lcd.show() | ||
| + | </ | ||
| + | |||
| + | If this has worked then you should see something similar to below. | ||
| + | \\ | ||
| + | \\ | ||
| + | {{ : | ||
| + | \\ | ||
| + | Here we can see the last line on the LCD is my Pico IP Address | ||
connect_pico_to_wifi.1786967830.txt.gz · Last modified: by walkeradmin
