# Part 2: Using WiFi
###### tags: `TA Stuff RP2` `Raspberry Pi Pico`
To access the internet from your development board you can use the built-in WiFi on your **Raspberry Pi Pico W**. In this tutorial, you will:
+ Connect your development board to your home wireless router.
+ Print the assigned IP address from the router to your board.
+ Try to use a TCP connection to a website and print the content of the page.
As always, we'll continuously update this walkthrough. **Is there anything that needs to be clarified, or have you experienced some issues? Please add a comment.** You do this by highlighting the text and then you can write a comment on the highlighted part. You need to log in/create an account on HackMD first.
:::info
**You should know that your Raspberry Pi Pico W board works on 2.4 GHz and most of the routers support both 2.4 and 5 GHz devices. If all settings and the credentials are correct and you still can not connect to the WiFi check if your router activates/supports 2.4 GHz connections. You can read more about it [*here*](https://www.actcorp.in/blog/difference-between-2.4ghz-and-5ghz-wifi).**
:::
Create a file called `keys.py`, it should be populated with the following two lines.
```python
WIFI_SSID = 'Your_SSID'
WIFI_PASS = 'Your_Password'
```
You should change **Your_SSID** and **Your_Password** on these two lines to your home WiFi name and password.
:::info
**:bulb: This passage is important as it ensures that your credentials are never part of the main program itself. In the future, you might want to share your program with others and it's important that they can't see your various tokens or passwords. Keeping the program's code and the credentials separated helps make it easier to keep your project secure.**
:::
---
</br>
The following self-commented code could be copied to `boot.py` and it first try to connect to your home WiFi then it tries to send an $HTTP$ request to $http://detectportal.firefox.com/$ and print the response on REPL.
:::warning
The following code works only on a Wi-Fi secured with WPA or WPA2.
:::
```pyython=
import keys
import network
from time import sleep
def connect():
wlan = network.WLAN(network.STA_IF) # Put modem on Station mode
if not wlan.isconnected(): # Check if already connected
print('connecting to network...')
wlan.active(True) # Activate network interface
# set power mode to get WiFi power-saving off (if needed)
wlan.config(pm = 0xa11140)
wlan.connect(keys.WIFI_SSID, keys.WIFI_PASS) # Your WiFi Credential
print('Waiting for connection...', end='')
# Check if it is connected otherwise wait
while not wlan.isconnected() and wlan.status() >= 0:
print('.', end='')
sleep(1)
# Print the IP assigned by router
ip = wlan.ifconfig()[0]
print('\nConnected on {}'.format(ip))
return ip
def http_get(url = 'http://detectportal.firefox.com/'):
import socket # Used by HTML get request
import time # Used for delay
_, _, host, path = url.split('/', 3) # Separate URL request
addr = socket.getaddrinfo(host, 80)[0][-1] # Get IP address of host
s = socket.socket() # Initialise the socket
s.connect(addr) # Try connecting to host address
# Send HTTP request to the host with specific path
s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8'))
time.sleep(1) # Sleep for a second
rec_bytes = s.recv(10000) # Receve response
print(rec_bytes) # Print the response
s.close() # Close connection
# WiFi Connection
try:
ip = connect()
except KeyboardInterrupt:
print("Keyboard interrupt")
# HTTP request
try:
http_get()
except (Exception, KeyboardInterrupt) as err:
print("No Internet", err)
```
</br></br>
:::info
:bulb: The complete code for using WiFi is in our [**GitHub Repo**](https://github.com/iot-lnu/pico-w/tree/main/network-examples/N1_WiFi_Connection) and you can copy and change the SSID and Password.
:::
:::success
If you can see the assigned IP address from router and the printed result of HTTP request, it means you successfully connected to the WiFi. You can continue with the third part of the tutorial to send your collected data to the cloud.
:::
*[REPL]: Read-Evaluate-Print Loop