# Quick-Start the Pico W WiFi with CircuitPython

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/115/888/medium800/adafruit_products_hero.jpg?1665764805)

The Raspberry Pi Foundation changed single-board computing&nbsp;[when they released the Raspberry Pi computer](https://www.raspberrypi.org/archives/723), now they're ready to do the same for microcontrollers with the release of the brand new&nbsp; **Raspberry Pi Pico W**. This low-cost microcontroller board features their&nbsp;powerful new chip, the&nbsp; **RP2040** , and all the fixin's to get started with IoT embedded electronics projects at a stress-free price.

Raspberry Pi Pico W brings WiFi to the Pico platform while retaining complete pin compatibility with its older sibling, [and now as of CircuitPython 8.0.0-beta.2](https://blog.adafruit.com/2022/10/13/circuitpython-8-0-0-beta-2-released/), there is CircuitPython WiFi support for the Pico W! This guide includes examples for testing your WiFi connection, using requests to pull JSON feeds, ping API's and log sensor data for IoT projects; all using CircuitPython!

## Status Bar

As of [CircuitPython 8.0.0](https://blog.adafruit.com/2022/10/01/circuitpython-8-0-0-beta-1-released/), if you have a smart terminal program like [Thonny](https://thonny.org/), [tio](https://github.com/tio/tio) or [Screen](https://www.gnu.org/software/screen/manual/screen.html#Virtual-Terminal), you will see the status of your CircuitPython board in the header bar of the terminal.

If you have an error while running your code, the status bar will tell you what line of code was running when the error occurred, as well as the type of error.

![adafruit_products_statusBarError.jpg](https://cdn-learn.adafruit.com/assets/assets/000/115/913/medium640/adafruit_products_statusBarError.jpg?1665785705)

Additionally, if you end the program from the shell with a `KeyboardInterrupt`, that information will be displayed in the status bar.

![adafruit_products_keyboardInterruptThonny.jpg](https://cdn-learn.adafruit.com/assets/assets/000/115/914/medium640/adafruit_products_keyboardInterruptThonny.jpg?1665785778)

## About the Code Examples

WiFi and networking are complicated and have many failure states. Rather than having extensive code to detect and recover from each specific kind of failure, the examples here use `microcontroller.reset()` which fully re-initialize both the microcontroller and the WiFi co-processor and start the code again with a clean slate.

The general pattern is:

```python
try:
	your_application_here()
except Exception as e:
    print("Error:\n", str(e))
    print("Resetting microcontroller in 10 seconds")
    time.sleep(10)
    microcontroller.reset()
```

## Parts
### Raspberry Pi Pico W

[Raspberry Pi Pico W](https://www.adafruit.com/product/5526)
The Raspberry Pi foundation changed single-board computing [when they released the Raspberry Pi computer](https://www.raspberrypi.org/archives/723), now they're ready to do the same for microcontrollers with the release of the brand new **Raspberry Pi Pico W**. This...

In Stock
[Buy Now](https://www.adafruit.com/product/5526)
[Related Guides to the Product](https://learn.adafruit.com/products/5526/guides)
![Angled shot of a green microcontroller with castellated pads.](https://cdn-shop.adafruit.com/640x480/5526-01.jpg)

### Fully Reversible Pink/Purple USB A to micro B Cable - 1m long

[Fully Reversible Pink/Purple USB A to micro B Cable - 1m long](https://www.adafruit.com/product/4111)
This cable is not only super-fashionable, with a woven pink and purple Blinka-like pattern, it's also fully reversible! That's right, you will save _seconds_ a day by not having to flip the cable around.

First let's talk about the cover and over-molding. We got these...

In Stock
[Buy Now](https://www.adafruit.com/product/4111)
[Related Guides to the Product](https://learn.adafruit.com/products/4111/guides)
![Fully Reversible Pink/Purple USB A to micro B Cable](https://cdn-shop.adafruit.com/640x480/4111-02.jpg)

### Adafruit AHT20 - Temperature & Humidity Sensor Breakout Board

[Adafruit AHT20 - Temperature & Humidity Sensor Breakout Board](https://www.adafruit.com/product/4566)
The AHT20 is a nice but inexpensive temperature and humidity sensor [from the same folks that brought us the DHT22](https://www.adafruit.com/product/385). You can take sensor readings as often as you like, and it uses standard I2C so its super easy to use with any Arduino or...

Out of Stock
[Buy Now](https://www.adafruit.com/product/4566)
[Related Guides to the Product](https://learn.adafruit.com/products/4566/guides)
![Angled Shot of the Adafruit AHT20 Temp+Hum Sensor - Assembled PCB](https://cdn-shop.adafruit.com/640x480/4566-04.jpg)

### Solderless Breadboard for Raspberry Pi Pico by Monk Makes

[Solderless Breadboard for Raspberry Pi Pico by Monk Makes](https://www.adafruit.com/product/5422)
It can be tricky to work out which pin is which when the [Raspberry Pi Pico](https://www.adafruit.com/?q=raspberry+pi+pico&sort=BestMatch) is attached to solderless breadboard. Luckily, the **[MonkMakes](https://monkmakes.com/)**  **Breadboard for...**

In Stock
[Buy Now](https://www.adafruit.com/product/5422)
[Related Guides to the Product](https://learn.adafruit.com/products/5422/guides)
![Half-size breadboard with a soldered skinny green microcontroller.](https://cdn-shop.adafruit.com/640x480/5422-03.jpg)

### STEMMA QT / Qwiic JST SH 4-pin to Premium Male Headers Cable

[STEMMA QT / Qwiic JST SH 4-pin to Premium Male Headers Cable](https://www.adafruit.com/product/4209)
This 4-wire cable is a little over 150mm / 6" long and fitted with JST-SH female 4-pin connectors on one end and premium Dupont male headers on the other. Compared with the chunkier JST-PH these are 1mm pitch instead of 2mm, but still have a nice latching feel, while being easy to insert...

In Stock
[Buy Now](https://www.adafruit.com/product/4209)
[Related Guides to the Product](https://learn.adafruit.com/products/4209/guides)
![Angled Shot of the STEMMA QT / Qwiic JST SH 4-pin to Premium Male Headers Cable.](https://cdn-shop.adafruit.com/640x480/4209-05.jpg)

# Quick-Start the Pico W WiFi with CircuitPython

## Installing CircuitPython

[CircuitPython](https://github.com/adafruit/circuitpython) is a derivative of [MicroPython](https://micropython.org) designed to simplify experimentation and education on low-cost microcontrollers. It makes it easier than ever to get prototyping by requiring no upfront desktop software downloads. Simply copy and edit files on the **CIRCUITPY** drive to iterate.

## CircuitPython Quickstart

Follow this step-by-step to quickly get CircuitPython working on your board.

[Download the latest version of CircuitPython for the Raspberry Pi Pico W from circuitpython.org](https://circuitpython.org/board/raspberry_pi_pico_w/)
 **Click the link above and download the latest UF2 file.**

Download and save it to your desktop (or wherever is handy).

![circuitpython_Cat_UF2_download.png](https://cdn-learn.adafruit.com/assets/assets/000/098/753/medium640/circuitpython_Cat_UF2_download.png?1611157944)

Start with your Pico W unplugged from USB. Hold down the **BOOTSEL** button, and while continuing to hold it (don't let go!), plug the Pico W into USB. **Continue to hold the BOOTSEL button until the RPI-RP2 drive appears!**

If the drive does not appear, unplug your Pico W and go through the above process again.

A lot of people end up using charge-only USB cables and it is very frustrating! **So make sure you have a USB cable you know is good for data sync.**

![adafruit_products_bootSel.jpg](https://cdn-learn.adafruit.com/assets/assets/000/115/887/medium640/adafruit_products_bootSel.jpg?1665763645)

You will see a new disk drive appear called **RPI-RP2**.

&nbsp;

Drag the **adafruit\_circuitpython\_etc.uf2** file to **RPI-RP2.**

![circuitpython_Cat_RPI-RP2_drive.png](https://cdn-learn.adafruit.com/assets/assets/000/098/756/medium640/circuitpython_Cat_RPI-RP2_drive.png?1611158210)

![circuitpython_Cat_drag_UF2.png](https://cdn-learn.adafruit.com/assets/assets/000/098/758/medium640/circuitpython_Cat_drag_UF2.png?1611158274)

The **RPI-RP2** drive will disappear and a new disk drive called **CIRCUITPY** will appear.

That's it, you're done! :)

![circuitpython_Cat_CIRCUITPY.png](https://cdn-learn.adafruit.com/assets/assets/000/098/759/medium640/circuitpython_Cat_CIRCUITPY.png?1611158312)

## Flash Resetting UF2

If your Pico W ever gets into a really _weird_ state and doesn't even show up as a disk drive when installing CircuitPython, try installing this 'nuke' UF2 which will do a 'deep clean' on your Flash Memory. You will lose all the files on the board, but at least you'll be able to revive it! After nuking, re-install CircuitPython

[flash_nuke.uf2](https://cdn-learn.adafruit.com/assets/assets/000/099/419/original/flash_nuke.uf2?1613329170)
# Quick-Start the Pico W WiFi with CircuitPython

## Create Your settings.toml File

CircuitPython works with WiFi-capable boards to enable you to make projects that have network connectivity. This means working with various passwords and API keys. As of [CircuitPython 8](https://circuitpython.org/downloads), there is support for a **settings.toml** file. This is a file that is stored on your **CIRCUITPY** drive, that contains all of your secret network information, such as your SSID, SSID password and any API keys for IoT services. It is designed to separate your sensitive information from your **code.py** file so you are able to share your code without sharing your credentials.

CircuitPython previously used a **secrets.py** file for this purpose. The **settings.toml** file is quite similar.

Warning: Your **settings.toml** file should be stored in the main directory of your **CIRCUITPY** drive. It should not be in a folder.

## CircuitPython **settings.toml** File

This section will provide a couple of examples of what your **settings.toml** file should look like, specifically for CircuitPython WiFi projects in general.

The most minimal **settings.toml** file must contain your WiFi SSID and password, as that is the minimum required to connect to WiFi. Copy this example, paste it into your **settings.toml** , and update:

- `your_wifi_ssid`
- `your_wifi_password`

```auto
CIRCUITPY_WIFI_SSID = "your_wifi_ssid"
CIRCUITPY_WIFI_PASSWORD = "your_wifi_password"
```

Many CircuitPython network-connected projects on the Adafruit Learn System involve using Adafruit IO. For these projects, you must _also_ include your Adafruit IO username and key. Copy the following example, paste it into your settings.toml file, and update:

- `your_wifi_ssid`
- `your_wifi_password`
- `your_aio_username`
- `your_aio_key`

```auto
CIRCUITPY_WIFI_SSID = "your_wifi_ssid"
CIRCUITPY_WIFI_PASSWORD = "your_wifi_password"
ADAFRUIT_AIO_USERNAME = "your_aio_username"
ADAFRUIT_AIO_KEY = "your_aio_key"
```

Some projects use different variable names for the entries in the **settings.toml** file. For example, a project might use `ADAFRUIT_AIO_ID` in the place of `ADAFRUIT_AIO_USERNAME`. **If you run into connectivity issues, one of the first things to check is that the names in the settings.toml file match the names in the code.**

Warning: Not every project uses the same variable name for each entry in the **settings.toml** file! Always verify it matches the code.

## **settings.toml** File Tips
Here is an example **settings.toml** file.

```auto
# Comments are supported
CIRCUITPY_WIFI_SSID = "guest wifi"
CIRCUITPY_WIFI_PASSWORD = "guessable"
CIRCUITPY_WEB_API_PORT = 80
CIRCUITPY_WEB_API_PASSWORD = "passw0rd"
test_variable = "this is a test"
thumbs_up = "\U0001f44d"
```

In a **settings.toml** file, it's important to keep these factors in mind:

- Strings are wrapped in double quotes; ex: `"your-string-here"`
- Integers are _ **not** _ quoted and may be written in decimal with optional sign (`+1`, `-1`, `1000`) or hexadecimal (`0xabcd`).
  - Floats (decimal numbers), octal (`0o567`) and binary (`0b11011`) are not supported.

- Use `\u` escapes for weird characters, `\x` and `\ooo` escapes are not available in **.toml** files
  - Example: `\U0001f44d` for 👍 (thumbs up emoji) and `\u20ac` for € (EUR sign)

- Unicode emoji, and non-ASCII characters, stand for themselves as long as you're careful to save in "UTF-8 without BOM" format

&nbsp;

&nbsp;

When your&nbsp; **settings.toml&nbsp;** file is ready, you can save it in your text editor with the **.toml** &nbsp;extension.

![adafruit_products_dotToml.jpg](https://cdn-learn.adafruit.com/assets/assets/000/117/071/medium640/adafruit_products_dotToml.jpg?1671034293)

## Accessing Your **settings.toml** Information in **code.py**
In your **code.py** file, you'll need to `import` the `os` library to access the **settings.toml** file. Your settings are accessed with the `os.getenv()` function. You'll pass your settings entry to the function to import it into the **code.py** file.

```python
import os

print(os.getenv("test_variable"))
```

![](https://cdn-learn.adafruit.com/assets/assets/000/117/072/medium800/adafruit_products_tomlOutput.jpg?1671034496)

In the upcoming CircuitPython WiFi examples, you'll see how the **settings.toml&nbsp;** file is used for connecting to your SSID and accessing your API keys.

# Quick-Start the Pico W WiFi with CircuitPython

## Environment Variables Docs

# Quick-Start the Pico W WiFi with CircuitPython

## Pico W Basic WiFi Test

In this example, you'll test your Pico W WiFi connection by connecting to your SSID, printing your MAC address and IP address to the REPL and then pinging Google.

## Add Your **settings.toml** File

Remember to add your **settings.toml** file as described in the [Create Your settings.toml File page](https://learn.adafruit.com/pico-w-wifi-with-circuitpython/create-your-settings-toml-file) earlier in the guide. You'll need to include your `CIRCUITPY_WIFI_SSID` and `CIRCUITPY_WIFI_PASSWORD` in the file.&nbsp;

```auto
CIRCUITPY_WIFI_SSID = "your-ssid-here"
CIRCUITPY_WIFI_PASSWORD = "your-ssid-password-here"
```

## Code the Basic WiFi Test

Once you've finished setting up your Pico W with CircuitPython, you can access the code by downloading the Project Bundle.

To do this, click on the **&nbsp;Download Project Bundle** &nbsp;button in the window below. It will download as a zipped folder.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Pico_W_CircuitPython_WiFi_Examples/Pico_W_Basic_WiFi_Test/code.py

## Upload the Code and Libraries to the Pico W

After downloading the Project Bundle, plug your Pico W into the computer's USB port with a known good USB data+power cable. You should see a new flash drive appear in the computer's File Explorer or Finder (depending on your operating system) called&nbsp; **CIRCUITPY**. Unzip the folder and copy the following items to the Pico W's&nbsp; **CIRCUITPY** &nbsp;drive.&nbsp;

- **code.py**

Your Pico W&nbsp; **CIRCUITPY&nbsp;** drive should look like this after copying the&nbsp; **code.py&nbsp;** file.

Info: No libraries from the library bundle are used in this example, so the **lib** folder is empty. All of the libraries are a part of the core.

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/Pico_W_CircuitPython_WiFi_Examples_Pico_W_Basic_WiFi_Test.png )

Once everything is saved to the&nbsp; **CIRCUITPY** &nbsp;drive,&nbsp;[connect to the serial console](https://learn.adafruit.com/welcome-to-circuitpython/kattni-connecting-to-the-serial-console)&nbsp;to see the data printed out!

![](https://cdn-learn.adafruit.com/assets/assets/000/115/907/medium800/adafruit_products_thonny_basic.jpg?1665785054)

## Having Problems?

If you get the message "TypeError: object with buffer protocol required", likely CircuitPython cannot find the file **settings.toml** on your **CIRCUITPY** drive. Check to be sure the file name is correct and that it contains the `CIRCUITPY_WIFI_SSID`&nbsp;and&nbsp;`CIRCUITPY_WIFI_PASSWORD` values as noted in the "Create Your settings.toml File" section.

If you get "Connecting to WiFi" but don't get to "Connected to WiFi", check to make sure the `CIRCUITPY_WIFI_SSID`&nbsp;and&nbsp;`CIRCUITPY_WIFI_PASSWORD` values in **settings.toml** are valid for your 2.4GHz network. There is no retry in this code, you might have to run the program a second time to get a response.

## How the Pico W Basic WiFi Test Works
In the basic WiFi test, the Pico W connects to your SSID by importing your SSID and SSID password from the **settings.toml**** &nbsp;**file.

```python
wifi.radio.connect(ssid, password)
```

Then, your MAC address and IP address are printed to the REPL.

```python
#  prints MAC address to REPL
print("My MAC addr:", [hex(i) for i in wifi.radio.mac_address])

#  prints IP address to REPL
print("My IP address is", wifi.radio.ipv4_address)
```

Finally, google.com is pinged. The amount of time it takes to ping is printed to the REPL and the code stops running.

```python
#  pings Google
ipv4 = ipaddress.ip_address("8.8.4.4")
print("Ping google.com: %f ms" % (wifi.radio.ping(ipv4)*1000))
```

By successfully running this WiFi test code, you can confirm that your Pico W is connecting to WiFi with CircuitPython successfully and you can move on to more advanced projects.

# Quick-Start the Pico W WiFi with CircuitPython

## Pico W Requests Test (Adafruit Quotes)

In this example, you'll use the [adafruit\_requests](https://docs.circuitpython.org/projects/requests/en/latest/) library to fetch a text response from the [Adafruit Quotes PHP script](https://www.adafruit.com/api/quotes.php). Each time the URL is requested, it returns a new quote from a large database of quotes.

## Code the Requests Test

Once you've finished setting up your Pico W with CircuitPython, you can access the code and necessary libraries by downloading the Project Bundle.

To do this, click on the **&nbsp;Download Project Bundle** &nbsp;button in the window below. It will download as a zipped folder.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Pico_W_CircuitPython_WiFi_Examples/Pico_W_Requests/code.py

## Upload the Code and Libraries to the Pico W

After downloading the Project Bundle, plug your Pico W into the computer's USB port with a known good USB data+power cable. You should see a new flash drive appear in the computer's File Explorer or Finder (depending on your operating system) called&nbsp; **CIRCUITPY**. Unzip the folder and copy the following items to the Pico W's&nbsp; **CIRCUITPY** &nbsp;drive.&nbsp;

- **lib** &nbsp;folder
- **code.py**

Your Pico W&nbsp; **CIRCUITPY&nbsp;** drive should look like this after copying the&nbsp; **lib** &nbsp;folder and the&nbsp; **code.py&nbsp;** file.

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/Pico_W_CircuitPython_WiFi_Examples_Pico_W_Requests.png )

## Add Your&nbsp; **settings.toml** &nbsp;File

Remember to add your&nbsp; **settings.toml** &nbsp;file as described in the&nbsp;[Create Your settings.toml File page](https://learn.adafruit.com/pico-w-wifi-with-circuitpython/create-your-settings-toml-file)&nbsp;earlier in the guide. You'll need to include your&nbsp;`WIFI_SSID`&nbsp;and&nbsp;`WIFI_PASSWORD`&nbsp;in the file.

```auto
CIRCUITPY_WIFI_SSID = "your-ssid-here"
CIRCUITPY_WIFI_PASSWORD = "your-ssid-password-here"
```

## Run **code.py**
Once everything is saved to the&nbsp; **CIRCUITPY** &nbsp;drive,&nbsp;[connect to the serial console](https://learn.adafruit.com/welcome-to-circuitpython/kattni-connecting-to-the-serial-console)&nbsp;to see the data printed out!

![](https://cdn-learn.adafruit.com/assets/assets/000/115/908/medium800/adafruit_products_thonny_quotes.jpg?1665785071)

Every 60 seconds, a `request` will be made to the Adafruit quotes URL. Then, the response will be printed to the REPL. The response includes the quote text and the author of the quote.

# Quick-Start the Pico W WiFi with CircuitPython

## Pico W JSON Feed (OpenWeatherMap)

In this example, you'll use the [OpenWeatherMap API](https://openweathermap.org/api) to receive&nbsp;and parse a JSON feed of your location's weather.

You'll need to&nbsp;register for an account with OpenWeatherMap and get your API key. Go to this&nbsp;[link](https://home.openweathermap.org/users/sign_up)&nbsp;and register for a free account. Once registered, you'll get an email containing your API key. This key will be added to your **settings.toml** file as the 'openweather\_token'.

## Code the JSON Feed Test

Once you've finished setting up your Pico W with CircuitPython, you can access the code and necessary libraries by downloading the Project Bundle.

To do this, click on the **&nbsp;Download Project Bundle** &nbsp;button in the window below. It will download as a zipped folder.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Pico_W_CircuitPython_WiFi_Examples/Pico_W_JSON_Feed/code.py

## Upload the Code and Libraries to the Pico W

After downloading the Project Bundle, plug your Pico W into the computer's USB port with a known good USB data+power cable. You should see a new flash drive appear in the computer's File Explorer or Finder (depending on your operating system) called&nbsp; **CIRCUITPY**. Unzip the folder and copy the following items to the Pico W's&nbsp; **CIRCUITPY** &nbsp;drive.&nbsp;

- **lib** &nbsp;folder
- **code.py**

Your Pico W&nbsp; **CIRCUITPY&nbsp;** drive should look like this after copying the&nbsp; **lib** &nbsp;folder and the&nbsp; **code.py&nbsp;** file.

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/Pico_W_CircuitPython_WiFi_Examples_Pico_W_JSON_Feed.png )

## Add Your **settings.toml** &nbsp;File

Remember to add your settings.toml&nbsp;file as described in the&nbsp;[Create Your settings.toml File page](https://learn.adafruit.com/pico-w-wifi-with-circuitpython/create-your-settings-toml-file)&nbsp;earlier in the guide. You'll need to include your `WIFI_SSID`, `WIFI_PASSWORD` and `openweather_token` in the file.

```auto
CIRCUITPY_WIFI_SSID = "your-ssid-here"
CIRCUITPY_WIFI_PASSWORD = "your-ssid-password-here"

openweather_token = "your-openweather-token-here"
```

## Run **code.py**

Once everything is saved to the&nbsp; **CIRCUITPY** &nbsp;drive,&nbsp;[connect to the serial console](https://learn.adafruit.com/welcome-to-circuitpython/kattni-connecting-to-the-serial-console)&nbsp;to see the data printed out!

![](https://cdn-learn.adafruit.com/assets/assets/000/115/909/medium800/adafruit_products_thonny_json.jpg?1665785098)

The code makes a `request` to the OpenWeatherMap API and receives a JSON feed of your location's weather. That JSON output is printed to the REPL. Then, the location, weather, humidity, pressure and temperature converted to Fahrenheit is printed to the REPL. This call is repeated every five minutes.

# Quick-Start the Pico W WiFi with CircuitPython

## Pico W with Adafruit IO

![](https://cdn-learn.adafruit.com/assets/assets/000/115/898/medium800thumb/adafruit_products_picowIO.jpg?1665777331)

In this example, you'll use the[AHT20 temperature and humidity sensor](https://learn.adafruit.com/adafruit-aht20/overview) to log temperature and humidity data to Adafruit IO.

## Prerequisite Guides
[Adafruit AHT20 Temperature &amp; Humidity Sensor](https://learn.adafruit.com/adafruit-aht20/overview)
[Welcome to Adafruit IO](https://learn.adafruit.com/welcome-to-adafruit-io)
## AHT20 Wiring
- **Board 3V** &nbsp;to&nbsp; **sensor VIN**
- **Board GND** &nbsp;to&nbsp; **sensor GND**
- **Board GP1** &nbsp;to&nbsp; **sensor SCL**
- **Board GP0** &nbsp;to&nbsp; **sensor SDA**

![adafruit_products_picowAHT20_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/115/899/medium640/adafruit_products_picowAHT20_bb.jpg?1665778254)

## Code the Adafruit IO Test

Once you've finished setting up your Pico W with CircuitPython, you can access the code and necessary libraries by downloading the Project Bundle.

To do this, click on the **&nbsp;Download Project Bundle** &nbsp;button in the window below. It will download as a zipped folder.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Pico_W_CircuitPython_WiFi_Examples/Pico_W_Adafruit_IO/code.py

## Upload the Code and Libraries to the Pico W

After downloading the Project Bundle, plug your Pico W into the computer's USB port with a known good USB data+power cable. You should see a new flash drive appear in the computer's File Explorer or Finder (depending on your operating system) called&nbsp; **CIRCUITPY**. Unzip the folder and copy the following items to the Pico W's&nbsp; **CIRCUITPY** &nbsp;drive.&nbsp;

- **lib** &nbsp;folder
- **code.py**

Your Pico W&nbsp; **CIRCUITPY&nbsp;** drive should look like this after copying the&nbsp; **lib** &nbsp;folder and the&nbsp; **code.py&nbsp;** file.

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/Pico_W_CircuitPython_WiFi_Examples_Pico_W_Adafruit_IO.png )

## Add Your **settings.toml** &nbsp;File

Remember to add your **settings.toml** &nbsp;file as described in the&nbsp;[Create Your settings.toml File page](https://learn.adafruit.com/pico-w-wifi-with-circuitpython/create-your-settings-toml-file)&nbsp;earlier in the guide. You'll need to include your `CIRCUITPY_WIFI_SSID`, `CIRCUITPY_WIFI_PASSWORD`, `aio_username` and `aio_key` in the file.

```auto
CIRCUITPY_WIFI_SSID = "your-ssid-here"
CIRCUITPY_WIFI_PASSWORD = "your-ssid-password-here"

aio_username = "your-aio-username-here"
aio_key = "your-aio-key-here"
```

## Run **code.py**

Once everything is saved to the&nbsp; **CIRCUITPY** &nbsp;drive,&nbsp;[connect to the serial console](https://learn.adafruit.com/welcome-to-circuitpython/kattni-connecting-to-the-serial-console)&nbsp;to see the data printed out!

![](https://cdn-learn.adafruit.com/assets/assets/000/115/911/medium800/adafruit_products_thonny_io.jpg?1665785141)

In the code, two feeds for Adafruit IO are created for each of the streams of data from the AHT20.

```python
try:
# get feed
    picowTemp_feed = io.get_feed("pitemp")
    picowHumid_feed = io.get_feed("pihumid")
except AdafruitIO_RequestError:
# if no feed exists, create one
    picowTemp_feed = io.create_new_feed("pitemp")
    picowHumid_feed = io.create_new_feed("pihumid")
```

In the loop, every 5 minutes the AHT20's temperature and humidity measures are sent to Adafruit IO. When the data is sent, it is printed to the REPL.

```python
#  when the clock runs out..
        if clock &gt; 300:
            #  read sensor
            data = [aht20.temperature, aht20.relative_humidity]
            #  send sensor data to respective feeds
            for z in range(2):
                io.send_data(feed_names[z]["key"], data[z])
                print("sent %0.1f" % data[z])
                time.sleep(1)
            #  print sensor data to the REPL
            print("\nTemperature: %0.1f C" % aht20.temperature)
            print("Humidity: %0.1f %%" % aht20.relative_humidity)
            print()
            time.sleep(1)
            #  reset clock
            clock = 0
```

On Adafruit IO, you can add the feeds to your dashboard to view your data over time.

![](https://cdn-learn.adafruit.com/assets/assets/000/115/900/medium800/adafruit_products_picowIO_still.jpg?1665778535)

# Quick-Start the Pico W WiFi with CircuitPython

## Pico W with Azure IoT Central

In this example, you'll use the[&nbsp;AHT20 temperature and humidity sensor](https://learn.adafruit.com/adafruit-aht20/overview)&nbsp;to log temperature and humidity data to Microsoft Azure IoT Central.

## Prerequisite Guides
[Adafruit AHT20 Temperature &amp; Humidity Sensor](https://learn.adafruit.com/adafruit-aht20/overview)
[Getting Started with Microsoft Azure and CircuitPython](https://learn.adafruit.com/getting-started-with-microsoft-azure-and-circuitpython)
## AHT20 Wiring
- **Board 3V** &nbsp;to&nbsp; **sensor VIN**
- **Board GND** &nbsp;to&nbsp; **sensor GND**
- **Board GP1** &nbsp;to&nbsp; **sensor SCL**
- **Board GP0** &nbsp;to&nbsp; **sensor SDA**

![adafruit_products_picowAHT20_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/115/901/medium640/adafruit_products_picowAHT20_bb.jpg?1665778745)

## Code the Azure IoT Central Test

Once you've finished setting up your Pico W with CircuitPython, you can access the code and necessary libraries by downloading the Project Bundle.

To do this, click on the **&nbsp;Download Project Bundle** &nbsp;button in the window below. It will download as a zipped folder.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Pico_W_CircuitPython_WiFi_Examples/Pico_W_Azure_IoT_Central/code.py

## Upload the Code and Libraries to the Pico W

After downloading the Project Bundle, plug your Pico W into the computer's USB port with a known good USB data+power cable. You should see a new flash drive appear in the computer's File Explorer or Finder (depending on your operating system) called&nbsp; **CIRCUITPY**. Unzip the folder and copy the following items to the Pico W's&nbsp; **CIRCUITPY** &nbsp;drive.&nbsp;

- **lib** &nbsp;folder
- **code.py**

Your Pico W&nbsp; **CIRCUITPY&nbsp;** drive should look like this after copying the&nbsp; **lib** &nbsp;folder and the&nbsp; **code.py&nbsp;** file.

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/Pico_W_CircuitPython_WiFi_Examples_Pico_W_Azure_IoT_Central.png )

## Add Your&nbsp; **settings.toml** &nbsp;File

Remember to add your&nbsp; **settings.toml** &nbsp;file as described in the&nbsp;[Create Your settings.toml File page](https://learn.adafruit.com/pico-w-wifi-with-circuitpython/create-your-settings-toml-file)&nbsp;earlier in the guide. You'll need to include your `WIFI_SSID` and `WIFI_PASSWORD` in the file. Additionally, you'll need your Azure IoT Central `id_scope`, `device_id` and `device_primary_key`.&nbsp;

```auto
CIRCUITPY_WIFI_SSID = "your-ssid-here"
CIRCUITPY_WIFI_PASSWORD = "your-ssid-password-here"

id_scope = "your-id-scope-here"
device_id = "your-device-id-here"
device_primary_key = "your-device-primary-key-here"
```

You'll need to setup a Microsoft Azure account and create an Azure IoT Central application to properly use this example. Be sure to reference the [getting started with Microsoft Azure and CircuitPython guide](https://learn.adafruit.com/getting-started-with-microsoft-azure-and-circuitpython) to follow all of the steps for this process successfully.

[Getting Started with Microsoft Azure and CircuitPython](https://learn.adafruit.com/getting-started-with-microsoft-azure-and-circuitpython)
## Run&nbsp; **code.py**

Once everything is saved to the **CIRCUITPY** drive, [connect to the serial console](https://learn.adafruit.com/welcome-to-circuitpython/kattni-connecting-to-the-serial-console) to see the data printed out!

![](https://cdn-learn.adafruit.com/assets/assets/000/115/912/medium800/adafruit_products_thonny_azure.jpg?1665785161)

Every five minutes, the AHT20's temperature and humidity data will be packed into a JSON message and transmitted to your Azure IoT Central app. In the REPL, you'll see `DEBUG` messages from `adafruit_requests` and messages from the loop letting you know when the JSON message has been sent and the sensor readings from the AHT20.

```python
#  when the azure clock runs out
        if azure_clock &gt; 500:
			#  pack message
            message = {"Temperature": aht20.temperature,
                       "Humidity": aht20.relative_humidity}
            print("sending json")
            device.send_telemetry(json.dumps(message))
            print("data sent")
			#  reset azure clock
            azure_clock = 0
```

In your Azure IoT Central app, you'll see your transmitted telemetry under your device.

![adafruit_products_azureTelemetry.jpg](https://cdn-learn.adafruit.com/assets/assets/000/115/904/medium640/adafruit_products_azureTelemetry.jpg?1665779309)


## Featured Products

### Raspberry Pi Pico W

[Raspberry Pi Pico W](https://www.adafruit.com/product/5526)
The Raspberry Pi foundation changed single-board computing [when they released the Raspberry Pi computer](https://www.raspberrypi.org/archives/723), now they're ready to do the same for microcontrollers with the release of the brand new **Raspberry Pi Pico W**. This...

In Stock
[Buy Now](https://www.adafruit.com/product/5526)
[Related Guides to the Product](https://learn.adafruit.com/products/5526/guides)
### Fully Reversible Pink/Purple USB A to micro B Cable - 1m long

[Fully Reversible Pink/Purple USB A to micro B Cable - 1m long](https://www.adafruit.com/product/4111)
This cable is not only super-fashionable, with a woven pink and purple Blinka-like pattern, it's also fully reversible! That's right, you will save _seconds_ a day by not having to flip the cable around.

First let's talk about the cover and over-molding. We got these...

In Stock
[Buy Now](https://www.adafruit.com/product/4111)
[Related Guides to the Product](https://learn.adafruit.com/products/4111/guides)
### Adafruit AHT20 - Temperature & Humidity Sensor Breakout Board

[Adafruit AHT20 - Temperature & Humidity Sensor Breakout Board](https://www.adafruit.com/product/4566)
The AHT20 is a nice but inexpensive temperature and humidity sensor [from the same folks that brought us the DHT22](https://www.adafruit.com/product/385). You can take sensor readings as often as you like, and it uses standard I2C so its super easy to use with any Arduino or...

Out of Stock
[Buy Now](https://www.adafruit.com/product/4566)
[Related Guides to the Product](https://learn.adafruit.com/products/4566/guides)
### Solderless Breadboard for Raspberry Pi Pico by Monk Makes

[Solderless Breadboard for Raspberry Pi Pico by Monk Makes](https://www.adafruit.com/product/5422)
It can be tricky to work out which pin is which when the [Raspberry Pi Pico](https://www.adafruit.com/?q=raspberry+pi+pico&sort=BestMatch) is attached to solderless breadboard. Luckily, the **[MonkMakes](https://monkmakes.com/)**  **Breadboard for...**

In Stock
[Buy Now](https://www.adafruit.com/product/5422)
[Related Guides to the Product](https://learn.adafruit.com/products/5422/guides)
### STEMMA QT / Qwiic JST SH 4-pin to Premium Male Headers Cable

[STEMMA QT / Qwiic JST SH 4-pin to Premium Male Headers Cable](https://www.adafruit.com/product/4209)
This 4-wire cable is a little over 150mm / 6" long and fitted with JST-SH female 4-pin connectors on one end and premium Dupont male headers on the other. Compared with the chunkier JST-PH these are 1mm pitch instead of 2mm, but still have a nice latching feel, while being easy to insert...

In Stock
[Buy Now](https://www.adafruit.com/product/4209)
[Related Guides to the Product](https://learn.adafruit.com/products/4209/guides)

## Related Guides

- [Adafruit AHT20 Temperature & Humidity Sensor](https://learn.adafruit.com/adafruit-aht20.md)
- [Pico W PiCowBell Case](https://learn.adafruit.com/pico-w-picowbell-case.md)
- [No-Code IoT Humidity and Temperature Sensor with Adafruit IO WipperSnapper](https://learn.adafruit.com/no-code-humidity-and-temp-tracker.md)
- [Modern Replacements for DHT11 and DHT22 Sensors](https://learn.adafruit.com/modern-replacements-for-dht11-dht22-sensors.md)
- [Quick Start: Pico W / 2W with WipperSnapper](https://learn.adafruit.com/quick-start-the-pico-w-with-wippersnapper.md)
- [Adafruit BME680](https://learn.adafruit.com/adafruit-bme680-humidity-temperature-barometic-pressure-voc-gas.md)
- [DeviceScript, TypeScript for Microcontrollers](https://learn.adafruit.com/devicescript.md)
- [Fisher-Price USB Foot Pedal](https://learn.adafruit.com/fisher-price-usb-foot-pedal.md)
- [Adafruit Proto Under Plate PiCowBell](https://learn.adafruit.com/adafruit-proto-under-plate-picowbell.md)
- [Adafruit AirLift Shield - ESP32 WiFi Co-Processor](https://learn.adafruit.com/adafruit-airlift-shield-esp32-wifi-co-processor.md)
- [CircuitPython Powered AT Hand-Raiser](https://learn.adafruit.com/at-hand-raiser.md)
- [Adafruit VL53L0X Time of Flight Micro-LIDAR Distance Sensor Breakout](https://learn.adafruit.com/adafruit-vl53l0x-micro-lidar-distance-sensor-breakout.md)
- [Circuit Playground TFT Gizmo Snow Globe](https://learn.adafruit.com/circuit-playground-tft-gizmo-snow-globe.md)
- [Adafruit Infrared IR Remote Receiver](https://learn.adafruit.com/adafruit-infrared-ir-remote-receiver.md)
- [Adafruit FunHouse](https://learn.adafruit.com/adafruit-funhouse.md)
