# Networking in CircuitPython

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/131/471/medium800/wireless_Untitled.png?1721747037)

The other day, the CircuitPython Team was talking about a new network feature and looking to add it to a guide. "Do we have a networking guide?" No, such a guide, while thought of, never materialized. No more!

This guide is aimed at helping to demonstrate wired and wireless networking using CircuitPython.

The options for wired connections are currently overshadowed by the wireless options, but the use is the same for both.

This guide will cover the following topics:

- networking hardware choices
- connecting to a local network
- typical network operations
- more esoteric things (UDP, mDNS, ...)

# Networking in CircuitPython

## Hardware Choices

Networking with CircuitPython is constrained to hardware that supports CircuitPython. While the hardware platforms compatible with CircuitPython contiues to grow, generally there is hardware only from several manufacturers.

This page lists Wifi-capable based on current products with CircuitPython support.

## Espressif Microcontrollers

Espressif makes several WiFi-capable microcontrollers. The processors include the original ESP32, the ESP32-S2, ESP32-S3, ESP32-C2 (aka ESP8584), ESP32-C3, and ESP32-C6.

CircuitPython does not support the earlier Espressif ESP8266 chip because of its memory and hardware limitations (although it is supported by MicroPython).

The CircuitPython `wifi` module is the primary interface with Espressif microcontrollers.

## Products

The products listed throughout are representative. Much of the time there are many more. [See CircuitPython.org for all the compatible boards](https://circuitpython.org/downloads).

### ESP32
### Adafruit QT Py ESP32 Pico - WiFi Dev Board with STEMMA QT

[Adafruit QT Py ESP32 Pico - WiFi Dev Board with STEMMA QT](https://www.adafruit.com/product/5395)
This dev board is like when you're watching a super-hero movie and the protagonist shows up in a totally amazing costume in the third act and you're like 'OMG! That's the hero and they're here to kick some serious butt!" but in this case its a...

In Stock
[Buy Now](https://www.adafruit.com/product/5395)
[Related Guides to the Product](https://learn.adafruit.com/products/5395/guides)
![Angled shot of purple square-shaped microcontroller.](https://cdn-shop.adafruit.com/640x480/5395-00.jpg)

### Adafruit ESP32 Feather V2 -  8MB Flash + 2 MB PSRAM

[Adafruit ESP32 Feather V2 -  8MB Flash + 2 MB PSRAM](https://www.adafruit.com/product/5400)
One of our star Feathers is the [Adafruit HUZZAH32 ESP32 Feather](https://www.adafruit.com/product/3405) - with the fabulous ESP32 WROOM module on there, it makes quick work of WiFi and Bluetooth® projects that take advantage of Espressifs most popular chipset. Recently we had...

Out of Stock
[Buy Now](https://www.adafruit.com/product/5400)
[Related Guides to the Product](https://learn.adafruit.com/products/5400/guides)
![Top view of black rectangle-shaped microcontroller.](https://cdn-shop.adafruit.com/640x480/5400-13.jpg)

### ESP32-S2
### Adafruit QT Py ESP32-S2 WiFi Dev Board with STEMMA QT

[Adafruit QT Py ESP32-S2 WiFi Dev Board with STEMMA QT](https://www.adafruit.com/product/5325)
What has your favorite Espressif WiFi microcontroller, comes with&nbsp;[our favorite connector - the STEMMA QT](http://adafruit.com/stemma), a chainable I2C port, and has lots of Flash and RAM memory for your next IoT project? What will make your next IoT project flyyyyy? What a...

In Stock
[Buy Now](https://www.adafruit.com/product/5325)
[Related Guides to the Product](https://learn.adafruit.com/products/5325/guides)
![Angled shot of small square purple dev board.](https://cdn-shop.adafruit.com/640x480/5325-12.jpg)

### Adafruit ESP32-S2 Feather - 4 MB Flash + 2 MB PSRAM

[Adafruit ESP32-S2 Feather - 4 MB Flash + 2 MB PSRAM](https://www.adafruit.com/product/5000)
What's Feather-shaped and has an ESP32-S2 WiFi module? What has a STEMMA QT connector for I2C devices? What has your favorite Espressif WiFi microcontroller and lots of Flash and RAM memory for your next IoT project? What will make your next IoT project flyyyyy?

That's right -...

Out of Stock
[Buy Now](https://www.adafruit.com/product/5000)
[Related Guides to the Product](https://learn.adafruit.com/products/5000/guides)
![Angled shot of rectangular microcontroller.](https://cdn-shop.adafruit.com/640x480/5000-12.jpg)

### Adafruit Metro ESP32-S2

[Adafruit Metro ESP32-S2](https://www.adafruit.com/product/4775)
What's Metro shaped and has an ESP32-S2 WiFi module? What has a STEMMA QT connector for I2C devices, and a Lipoly charger circuit? What has your favorite Espressif WiFi microcontroller and lots of memory for your next IoT project?

That's right - its the new Adafruit Metro...

In Stock
[Buy Now](https://www.adafruit.com/product/4775)
[Related Guides to the Product](https://learn.adafruit.com/products/4775/guides)
![Angled shot of Adafruit Metro esp32-s2 ](https://cdn-shop.adafruit.com/640x480/4775-06.jpg)

### ESP32-S3
### Adafruit QT Py ESP32-S3 WiFi Dev Board with STEMMA QT

[Adafruit QT Py ESP32-S3 WiFi Dev Board with STEMMA QT](https://www.adafruit.com/product/5426)
The ESP32-S3 has arrived in QT Py format - and what a great way to get started with this powerful new chip from Espressif! With dual 240 MHz cores, WiFi and BLE support, and native USB, this QT Py is great for powering your IoT projects.

The ESP32-S3&nbsp;is a highly-integrated,...

Out of Stock
[Buy Now](https://www.adafruit.com/product/5426)
[Related Guides to the Product](https://learn.adafruit.com/products/5426/guides)
![Angled shot of small purple microcontroller.](https://cdn-shop.adafruit.com/640x480/5426-00.jpg)

### Adafruit ESP32-S3 Feather with STEMMA QT / Qwiic

[Adafruit ESP32-S3 Feather with STEMMA QT / Qwiic](https://www.adafruit.com/product/5323)
The ESP32-S3 has arrived in Feather format - and what a great way to get started with this powerful new chip from Espressif! With dual 240 MHz cores, WiFi and BLE support, and native USB, this Feather is great for powering your IoT projects.

That's right - it's the new...

In Stock
[Buy Now](https://www.adafruit.com/product/5323)
[Related Guides to the Product](https://learn.adafruit.com/products/5323/guides)
![Angled shot of rectangular microcontroller.](https://cdn-shop.adafruit.com/640x480/5323-06.jpg)

### Adafruit Metro ESP32-S3 with 16 MB Flash 8 MB PSRAM

[Adafruit Metro ESP32-S3 with 16 MB Flash 8 MB PSRAM](https://www.adafruit.com/product/5500)
What's Metro-shaped and has an ESP32-S3&nbsp;WiFi module? What has a STEMMA QT connector for I2C devices and a Lipoly charger circuit? What has your favorite Espressif WiFi microcontroller and lots of memory for your next IoT project?

That's right - it's the new...

In Stock
[Buy Now](https://www.adafruit.com/product/5500)
[Related Guides to the Product](https://learn.adafruit.com/products/5500/guides)
![Angled shot of black, credit-card-sized dev board.](https://cdn-shop.adafruit.com/640x480/5500-10.jpg)

### ESP32-C3
### Adafruit QT Py ESP32-C3 WiFi Dev Board with STEMMA QT

[Adafruit QT Py ESP32-C3 WiFi Dev Board with STEMMA QT](https://www.adafruit.com/product/5405)
What's life without a little RISC? This miniature dev board is perfect for small projects: it comes with&nbsp;[our favorite connector - the STEMMA QT](http://adafruit.com/stemma), a chainable I2C port, WiFi, Bluetooth® LE, and plenty of FLASH and RAM memory for many IoT...

Out of Stock
[Buy Now](https://www.adafruit.com/product/5405)
[Related Guides to the Product](https://learn.adafruit.com/products/5405/guides)
![Angled shot of an Adafruit QT Py ESP32-C3 WiFi Dev Board with STEMMA QT.](https://cdn-shop.adafruit.com/640x480/5405-00.jpg)

### ESP32-C3 DevKitM-01 - 4 MB SPI Flash

[ESP32-C3 DevKitM-01 - 4 MB SPI Flash](https://www.adafruit.com/product/5337)
The ESP32-C3-DevKitM-01 is an entry-level development board equipped with the ESP32-C3-MINI-01, a&nbsp;powerful, generic Wi-Fi + Bluetooth® LE MCU module that features a rich set of peripherals, yet an optimized size. It's&nbsp;an ideal choice for a wide variety of...

In Stock
[Buy Now](https://www.adafruit.com/product/5337)
[Related Guides to the Product](https://learn.adafruit.com/products/5337/guides)
![Angled shot of ESP32-C3 board.](https://cdn-shop.adafruit.com/640x480/5337-04.jpg)

### ESP32-C6
### Adafruit ESP32-C6 Feather - STEMMA QT

[Adafruit ESP32-C6 Feather - STEMMA QT](https://www.adafruit.com/product/5933)
The&nbsp;ESP32-C6&nbsp;is Espressif’s first Wi-Fi 6 SoC integrating 2.4 GHz Wi-Fi 6, Bluetooth 5 (LE) and the 802.15.4 protocol. It brings the goodness you know from the [low-cost C3 series](https://www.adafruit.com/product/5337) and improves it with Zigbee/802.15.4 at 2.4Ghz....

In Stock
[Buy Now](https://www.adafruit.com/product/5933)
[Related Guides to the Product](https://learn.adafruit.com/products/5933/guides)
![Angled shot of rectangular dev board.](https://cdn-shop.adafruit.com/640x480/5933-08.jpg)

### ESP32-C6-DevKitC-1-N8 - 8MB SPI Flash

[ESP32-C6-DevKitC-1-N8 - 8MB SPI Flash](https://www.adafruit.com/product/5672)
The **ESP32-C6-DevKitC-1-N8** &nbsp;is an entry-level development board equipped with ESP32-C6-WROOM-1, &nbsp;a general-purpose Wi-Fi + Bluetooth®&nbsp;LE RISC-V MCU module that integrates complete Wi-Fi and Bluetooth® LE functions.&nbsp;This board integrates complete...

In Stock
[Buy Now](https://www.adafruit.com/product/5672)
[Related Guides to the Product](https://learn.adafruit.com/products/5672/guides)
![Angled view of ESP32-C6-DevKitC-1-N8](https://cdn-shop.adafruit.com/640x480/5672-00.jpg)

## Raspberry Pi Pico W
Raspberry Pi Pico W brings WiFi to the Pi Pico platform, adding on-board a single-band 2.4GHz wireless interface (802.11n) using the Infineon CYW43439 radio module, while retaining complete pin compatibility with its older sibling, the original Pi Pico.

The CircuitPython&nbsp;`wifi` module is the primary interface for WiFi on the Pico W.

Note that while the Pico W has more memory than many microcontroller boards, the WiFi software takes up a great deal of flash space and uses a lot of RAM at runtime, limiting the size of programs that can be created (which use WiFi) on the Pico W.

### Guide
### Quick-Start the Pico W WiFi with CircuitPython - Overview

[Quick-Start the Pico W WiFi with CircuitPython](https://learn.adafruit.com/pico-w-wifi-with-circuitpython)
[Overview](https://learn.adafruit.com/pico-w-wifi-with-circuitpython/overview)
### 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)
![Angled shot of a green microcontroller with castellated pads.](https://cdn-shop.adafruit.com/640x480/5526-01.jpg)

### Raspberry Pi Pico WH - Pico Wireless with Headers Soldered

[Raspberry Pi Pico WH - Pico Wireless with Headers Soldered](https://www.adafruit.com/product/5544)
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 WH**....

In Stock
[Buy Now](https://www.adafruit.com/product/5544)
[Related Guides to the Product](https://learn.adafruit.com/products/5544/guides)
![Angled shot of green rectangular microcontroller with a Wi-Fi module and pre-soldered headers.](https://cdn-shop.adafruit.com/640x480/5544-02.jpg)

## Airlift&nbsp;

Airlift is the term Adafruit uses for using an ESP32 microcontroller module as a WiFi coprocessor, connected to another microcontroller running CircuitPython. The ESP32 runs a modified version of the&nbsp; **NINA-FW** firmware developed by Arduino.

Your program controls the AirLift coprocessor using the&nbsp;`adafruit_esp32spi` library (aka **ESP32SPI** ), which is written in Python.

Note that ESP32SPI requires the CircuitPython microcontroller to have at least 128kb of memory, which rules out smaller CircuitPython-compatible microcontrollers like SAMD21.

The AirLift coprocessor is integrated onto boards such as the Adafruit PyPortal and the Adafruit Metro M4 AirLift Lite, and is also available as a separate breakout board.

### Guide
### Adafruit AirLift - ESP32 WiFi Co-Processor Breakout - CircuitPython WiFi

[Adafruit AirLift - ESP32 WiFi Co-Processor Breakout](https://learn.adafruit.com/adafruit-airlift-breakout)
[CircuitPython WiFi](https://learn.adafruit.com/adafruit-airlift-breakout/circuitpython-wifi)
### Products
### Adafruit AirLift FeatherWing – ESP32 WiFi Co-Processor

[Adafruit AirLift FeatherWing – ESP32 WiFi Co-Processor](https://www.adafruit.com/product/4264)
Give your Feather project a _lift_ with the Adafruit AirLift FeatherWing - a FeatherWing that lets you use the powerful ESP32 as a WiFi co-processor. You probably have your favorite Feather ([like the Feather M4](https://www.adafruit.com/product/3857)) that comes with its own...

In Stock
[Buy Now](https://www.adafruit.com/product/4264)
[Related Guides to the Product](https://learn.adafruit.com/products/4264/guides)
![Angled shot of Adafruit AirLift FeatherWing.](https://cdn-shop.adafruit.com/640x480/4264-07.jpg)

### Adafruit AirLift – ESP32 WiFi Co-Processor Breakout Board

[Adafruit AirLift – ESP32 WiFi Co-Processor Breakout Board](https://www.adafruit.com/product/4201)
Give your plain ol' microcontroller project a _lift_ with the Adafruit AirLift - a breakout board that lets you use the powerful ESP32 as a WiFi co-processor. You probably have your favorite microcontroller (like the ATmega328 or ATSAMD51), awesome peripherals and lots of libraries....

Out of Stock
[Buy Now](https://www.adafruit.com/product/4201)
[Related Guides to the Product](https://learn.adafruit.com/products/4201/guides)
![Top view of Adafruit AirLift Breakout Board.](https://cdn-shop.adafruit.com/640x480/4201-08.jpg)

### Adafruit AirLift Shield - ESP32 WiFi Co-Processor

[Adafruit AirLift Shield - ESP32 WiFi Co-Processor](https://www.adafruit.com/product/4285)
Give your Arduino project a _lift_ with the Adafruit AirLift Shield - a shield that lets you use the powerful ESP32 as a WiFi co-processor. You probably have your favorite Arduino-compatible ([like the Metro M4](https://www.adafruit.com/product/3382) or the classic <a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/4285)
[Related Guides to the Product](https://learn.adafruit.com/products/4285/guides)
![Angled shot of Wi-Fi co-processor shield PCB.](https://cdn-shop.adafruit.com/640x480/4285-05.jpg)

### Adafruit Metro M4 Express AirLift (WiFi) - Lite

[Adafruit Metro M4 Express AirLift (WiFi) - Lite](https://www.adafruit.com/product/4000)
Give your next project a lift with _AirLift_ - our witty name for the ESP32 co-processor that graces this Metro M4. You already know about the&nbsp; **Adafruit Metro M4** &nbsp;featuring the&nbsp; **Microchip ATSAMD51** , with it's 120MHz Cortex M4 with...

In Stock
[Buy Now](https://www.adafruit.com/product/4000)
[Related Guides to the Product](https://learn.adafruit.com/products/4000/guides)
![Adafruit Metro M4 Airlift Lite dev board with SAMD51 an ESP32 Wifi Co-processor.](https://cdn-shop.adafruit.com/640x480/4000-08.jpg)

## WizNet 5k Library

WizNet makes a number of chips for doing hardwired Ethernet through an SPI bus to a microcontroller. Their W5000 series chips are supported in CircuitPython through the Adafruit Wiznet5k Library module `adafruit_wiznet5k`.

### Guide
### Ethernet for CircuitPython with Wiznet5K - Overview

[Ethernet for CircuitPython with Wiznet5K](https://learn.adafruit.com/ethernet-for-circuitpython)
[Overview](https://learn.adafruit.com/ethernet-for-circuitpython/overview)
### Products
### Adafruit Ethernet FeatherWing

[Adafruit Ethernet FeatherWing](https://www.adafruit.com/product/3201)
Wireless is wonderful, but sometimes you want the strong reliability of a wire. If your Feather board is going to be part of a permanent installation, this **Ethernet FeatherWing** will let you add quick and easy wired Internet. Just plug in a standard

In Stock
[Buy Now](https://www.adafruit.com/product/3201)
[Related Guides to the Product](https://learn.adafruit.com/products/3201/guides)
![Angled shot of a Adafruit Ethernet FeatherWing with an Ethernet cable connected to it. ](https://cdn-shop.adafruit.com/640x480/3201-09.jpg)

# Networking in CircuitPython

## Network Settings

Following good code security practices, network name and security credentials should not be "hardcoded" into CircuitPython programs. Rather they are placed in Python environment variables.&nbsp;

As there is no operating system used on most CircuitPython devices, the values are placed in a separate file named **settings.toml**.

Using&nbsp; **settings.toml** &nbsp;replaces using&nbsp; **secrets.py** &nbsp;in modern CircuitPython code.

For those platforms presenting as a thumb drive (most microcontrollers), the file is placed in the root directory of the **CIRCUITPY** drive.

For microcontrollers not presenting as a thumb drive, the file should be uploaded along with the code and libraries using a compatible tool, such as the CircuitPython Web Workflow Code Editor (see below).

## Putting Your Networking Settings in **settings.toml**

There is a handy guide page for how to set up a **settings.toml** file for CircuitPython networking:

### Quick-Start the Pico W WiFi with CircuitPython - Create Your settings.toml File

[Quick-Start the Pico W WiFi with CircuitPython](https://learn.adafruit.com/pico-w-wifi-with-circuitpython)
[Create Your settings.toml File](https://learn.adafruit.com/pico-w-wifi-with-circuitpython/create-your-settings-toml-file)
Defining the values for `CIRCUITPY_WIFI_SSID` and&nbsp;`CIRCUITPY_WIFI_PASSWORD` provide an automatic way for the network name and password to be used in CircuitPython programs.&nbsp;

It is also possible to store the values in **settings.toml** and not have them used automatically by CircuitPython. You can still use **settings.toml** to store your credentials, say in&nbsp;`WIFI_SSID`&nbsp;and&nbsp;`WIFI_PASSWORD`, then use those values in your own code.

## Adafruit Web Workflow

Adafruit Web Workflow is an in-browser code editor and environment for CircuitPython using WiFi connections. See the following guide for setup and use of Web Workflow.

### CircuitPython Web Workflow Code Editor Quick Start - Overview

[CircuitPython Web Workflow Code Editor Quick Start](https://learn.adafruit.com/getting-started-with-web-workflow-using-the-code-editor)
[Overview](https://learn.adafruit.com/getting-started-with-web-workflow-using-the-code-editor/overview)
# Networking in CircuitPython

## Terminology

Here are some terms you'll see in this documentation referring to how networking is used in Python and CircuitPython.

### What is TCP vs. UDP?

TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) are both internet protocol suite methods for sending information across the internet.

The main difference between the two is that TCP is connection-based, while UDP is connectionless. This means that TCP requires the two ends of the communication link to remain connected throughout communication, while UDP does not.

### What is a Socket?

A _socket_ (network socket) is established within your CircuitPython program to serve as an endpoint for sending and receiving data from/to your local network or the Internet.

A socket's address is defined by its protocol, IP address and port number. The protocol is usually TCP, which is a reliable connection-based protocol with acknowledgments and hand-shaking. Other protocols might also be available such as UDP, which is a connectionless "send and forget" protocol.

For HTTP, a typical socket specifies TCP, the server IP address, and [port](https://en.wikipedia.org/wiki/Port_(computer_networking)) 80. For HTTPS, port number 443 is usually used. UDP sockets are generally 1024 or higher.

### What is a Socket Pool?

The `socketpool` module provides sockets through a pool of available sockets. When you are finished using a socket, it is returned to the pool. The pools themselves act like CPython’s&nbsp;[`socket`](https://docs.circuitpython.org/en/latest/shared-bindings/socketpool/index.html#socketpool.SocketPool.socket "socketpool.SocketPool.socket")&nbsp;module.

Only one socket pool can be created for each radio.

Due to the smaller memory size of most microcontrollers and single board computers, the amount of memory available for sockets is limited. Depending on the memory usage, the number of available sockets in the pool may be exhausted. You will need to use proper management of available sockets in a socketpool.

Detailed information on functions and parameters may be found in the CircuitPython [documentation](https://docs.circuitpython.org/en/latest/shared-bindings/socketpool/index.html).

### What is Secure Sockets Layer (SSL or SSL/TLS)?

_Secure Sockets Layer_ (_SSL_) is a way of encrypting data that is transmitted over a network, to make the connection secure. SSL evolved into&nbsp;_TLS_ (_Transport Layer Security_), and the mechanism is now often called _SSL/TLS_.

One of the most common uses for SSL/TLS is HTTPS, a secure way of making HTTP web requests. SSL/TLS in CircuitPython helps establish a secure HTTPS connection between a CircuitPython device and a secure internet server running HTTPS, now used by most of the web.

In the early days of microcontroller WiFi use, only insecure HTTP requests could be made. When the internet switched over to [HTTPS](https://en.wikipedia.org/wiki/HTTPS) due to threats around 2016, it left those older implementations without connections.

SSL/TLS provides authentication and encryption by using public-key cryptography. The public keys are known as [certificates](https://en.wikipedia.org/wiki/Public_key_certificate). The public keys have corresponding private keys that are kept secret. Trusted certificate providers issue root certificates. Other&nbsp; certificates are derived from the limited number of root certificates. A set of root certificates is usually stored in the WiFi firmware to allow connection to HTTPS servers whose certificates are based on those roots. You can also supply your own certificates.

Espressif discusses the certificates for ESP products [here](https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/protocols/esp_crt_bundle.html).

### What is JSON?

[JSON](https://www.json.org/json-en.html) (JavaScript Object Notation) array is a method of enncoding data in a standard format for files or data interchange ([Wikipedia](https://en.wikipedia.org/wiki/JSON)).

CircuitPython has the [`json`](https://docs.circuitpython.org/en/latest/docs/library/json.html) module to assist in converting between Python objects and the JSON data format.

You may see examples of the `response` Module using JSON to send and receive data.

# Networking in CircuitPython

## Networking with the wifi module

## The `wifi` Module

The `wifi` module provides a simple interface between CircuitPython and the internet using WiFi. It is a built-in module on Espressif and Pico W boards.

Here are a couple of short examples from the guide [Todbot's CircuitPython Tricks](https://learn.adafruit.com/todbot-circuitpython-tricks/networking):

### Scan Local WiFi Networks
```python
import wifi
networks = []
for network in wifi.radio.start_scanning_networks():
    networks.append(network)
wifi.radio.stop_scanning_networks()
networks = sorted(networks, key=lambda net: net.rssi, reverse=True)
for network in networks:
    print("ssid:",network.ssid, "rssi:",network.rssi)
```

### Displaying Your Local IP Address

This short program uses the `wifi` module&nbsp; to connect to the local network, using credentials you set up in a **settings.toml** file, and then gets the internet protocol (IP) address of your device and prints it out.

```auto
# settings.toml
CIRCUITPY_WIFI_SSID = "PrettyFlyForAWiFi"
CIRCUITPY_WIFI_PASSWORD = "mysecretpassword"
```

(Obviously change the SSID and password to the credentials for your own network)

```python
# code.py
import os, wifi
print("connecting...")
wifi.radio.connect(ssid=os.getenv('CIRCUITPY_WIFI_SSID'),
                   password=os.getenv('CIRCUITPY_WIFI_PASSWORD'))
print("my IP addr:", wifi.radio.ipv4_address)
```

## Using `adafruit_connection_manager`

The `adafruit_connection_manager` library provides a simple way to get a socket pool or an SSL context (used for HTTPS requests). It supports using the `wifi` module , the ESP32SPI library, and can also work on the desktop using CPython ("regular" Python).

Example:

```python
import wifi
import adafruit_connection_manager
import adafruit_requests

radio = wifi.radio

# Add code to make sure your radio is connected

pool = adafruit_connection_manager.get_radio_socketpool(radio)
ssl_context = adafruit_connection_manager.get_radio_ssl_context(radio)
requests = adafruit_requests.Session(pool, ssl_context)
requests.get("http://wifitest.adafruit.com/testwifi/index.html")

# Do something with response
```

## The `adafruit_requests` Library

The `adafruit_requests` library provides functions similar to the CPython `requests` module, used for HTTP(S) commands.

[Example](https://docs.circuitpython.org/projects/requests/en/latest/examples.html):

```python
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
# Updated for CircuitPython 9.0
"""WiFi Simpletest"""

import os

import adafruit_connection_manager
import wifi

import adafruit_requests

# Get WiFi details, ensure these are setup in settings.toml
ssid = os.getenv("CIRCUITPY_WIFI_SSID")
password = os.getenv("CIRCUITPY_WIFI_PASSWORD")

TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
JSON_GET_URL = "https://httpbin.org/get"
JSON_POST_URL = "https://httpbin.org/post"

# Initalize Wifi, Socket Pool, Request Session
pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
requests = adafruit_requests.Session(pool, ssl_context)
rssi = wifi.radio.ap_info.rssi

print(f"\nConnecting to {ssid}...")
print(f"Signal Strength: {rssi}")
try:
    # Connect to the Wi-Fi network
    wifi.radio.connect(ssid, password)
except OSError as e:
    print(f"❌ OSError: {e}")
print("✅ Wifi!")

print(f" | GET Text Test: {TEXT_URL}")
with requests.get(TEXT_URL) as response:
    print(f" | ✅ GET Response: {response.text}")
print("-" * 80)

print(f" | GET Full Response Test: {JSON_GET_URL}")
with requests.get(JSON_GET_URL) as response:
    print(f" | ✅ Unparsed Full JSON Response: {response.json()}")
print("-" * 80)

DATA = "This is an example of a JSON value"
print(f" | ✅ JSON 'value' POST Test: {JSON_POST_URL} {DATA}")
with requests.post(JSON_POST_URL, data=DATA) as response:
    json_resp = response.json()
    # Parse out the 'data' key from json_resp dict.
    print(f" | ✅ JSON 'value' Response: {json_resp['data']}")
print("-" * 80)

json_data = {"Date": "January 1, 1970"}
print(f" | ✅ JSON 'key':'value' POST Test: {JSON_POST_URL} {json_data}")
with requests.post(JSON_POST_URL, json=json_data) as response:
    json_resp = response.json()
    # Parse out the 'json' key from json_resp dict.
    print(f" | ✅ JSON 'key':'value' Response: {json_resp['json']}")
print("-" * 80)

print("Finished!")
```

## Using MQTT

MQTT is a messaging protocol for communicating between two nodes on the internet. It is often used for Internet of Things (IoT) devices to pass data.

```auto
# Simple demo of MQTT client in CircuitPython with native WiFi (ESP32-S Series)
# 9 Oct 2021 - @todbot / Tod Kurt 
# 31 July 2024 Anne Barela for Adafruit Industries
#
# This will connect to WiFi, then connect to an MQTT broker (shiftr.io was tested)
# and then listen to one MQTT feed while periodically publishing to another MQTT feed.
#
# Your settings.toml file contains something like:
#   CIRCUITPY_WIFI_SSID = "myWiFiName"
#   CIRCUITPY_WIFI_PASSWORD = "mywifipassword"
#   mqtt_broker ="test.mosquitto.org"
#   mqtt_port = 1883  # unencrytped, use 8883 for TLS encrypted
#   mqtt_username = ""
#   mqtt_password = ""
#
import os
import time
import ssl, socketpool, wifi
import adafruit_minimqtt.adafruit_minimqtt as MQTT

my_mqtt_topic_hello = "me/feeds/hello"  # the topic we send on
my_mqtt_topic_light = "me/feeds/light"  # the topic we receive on (could be the same)

# Connect to WiFi
print(f"Connecting to {os.getenv('CIRCUITPY_WIFI_SSID')}")
wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))

# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
    broker=os.getenv("mqtt_broker"),
    port=os.getenv("mqtt_port"),
    username=os.getenv("mqtt_username"),
    password=os.getenv("mqtt_password"),
    socket_pool=socketpool.SocketPool(wifi.radio),
    ssl_context=ssl.create_default_context(),
)

# Called when the client is connected successfully to the broker
def connected(client, userdata, flags, rc):
    print("Connected to MQTT broker!")
    
    client.subscribe( my_mqtt_topic_light) # say I want to listen to this topic
        
# Called when the client is disconnected
def disconnected(client, userdata, rc):
    print("Disconnected from MQTT broker!")

# Called when a topic the client is subscribed to has a new message
def message(client, topic, message):
    print("New message on topic {0}: {1}".format(topic, message))
    val = 0
    try: 
        val = int(message)  # attempt to parse it as a number
    except ValueError:
        pass
    print("setting LED to color:",val)
    # led.fill(val)  # if we had leds
    
# Set the callback methods defined above
mqtt_client.on_connect = connected
mqtt_client.on_disconnect = disconnected
mqtt_client.on_message = message

print("Connecting to MQTT broker...")
mqtt_client.connect()

last_msg_send_time = 0
       
while True:
    print("waiting")
    
    mqtt_client.loop(timeout=1)  # see if any messages to me
    
    if time.monotonic() - last_msg_send_time &gt; 3.0:  # send a message every 3 secs
        last_msg_send_time = time.monotonic()
        msg = "hi there! time is "+str(time.monotonic())
        print("sending MQTT msg..", msg)
        mqtt_client.publish( my_mqtt_topic_hello, msg )
```

## Companion Guides
### Quick-Start the Pico W WiFi with CircuitPython - Overview

[Quick-Start the Pico W WiFi with CircuitPython](https://learn.adafruit.com/pico-w-wifi-with-circuitpython)
[Overview](https://learn.adafruit.com/pico-w-wifi-with-circuitpython/overview)
## Further Reading

ReadTheDocs

- [Adafruit CircuitPython ConnectionManager Library](https://docs.circuitpython.org/projects/connectionmanager/en/latest/)
- [Adafruit Requests Library](https://docs.circuitpython.org/projects/requests/en/latest/index.html)

Third Party Guides

- [Connect to Multiple WiFi Networks with your Raspberry Pi Pico W](https://www.keeperofthecode.com/post.php?id=5)
- [CircuitPython WiFi Manager](https://github.com/dotpointer/circuitpython-wifimanager) - opens an access point to allow the user to configure the device to configure the device to connect to available WiFi networks. When the device is configured, it then connects to the first available matching network and hands over the control to your code.

## FAQ
### Networking with the ESP32-C6

CircuitPython and networking with the -C6 is new and if you have issues you should post on the Adafruit Discord [https://adafru.it/discord](https://adafru.it/discord) in the #circuitpython-dev channel.

### ESP-NOW Support

CircuitPython does not support ESP-NOW at present. You should use the ESP-IDF to use ESP-NOW to full advantage.

# Networking in CircuitPython

## Networking with ESP32SPI on Airlift

![](https://cdn-learn.adafruit.com/assets/assets/000/131/647/medium800/wireless_Untitled.png?1722453164)

Using an Adafruit Airlift module or Airlift capable development board with WiFi is not difficult, but it does require some code that other networking solutions do not require, mainly to set up the SPI bus communications between the main microcontroller and the ESP32 running NINA firmware.

Warning: Connections to Enterprise WiFi are not supported by Airlift.

## Airlift Board Wiring and Basic Code
Check out the page below for basic wiring with an Airlift breakout board connected to a Feather microcontroller board. The example scans for WiFi access points within range.

![](https://cdn-learn.adafruit.com/assets/assets/000/131/648/medium800/wireless_Untitled2.png?1722454951)

```python
import board
import busio
from digitalio import DigitalInOut

from adafruit_esp32spi import adafruit_esp32spi
import adafruit_requests as requests

print("ESP32 SPI hardware test")

esp32_cs = DigitalInOut(board.D10)
esp32_ready = DigitalInOut(board.D9)
esp32_reset = DigitalInOut(board.D7)

spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)

if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
    print("ESP32 found and in idle mode")
print("Firmware vers.", esp.firmware_version)
print("MAC addr:", [hex(i) for i in esp.MAC_address])

for ap in esp.scan_networks():
    print("\t%s\t\tRSSI: %d" % (str(ap['ssid'], 'utf-8'), ap['rssi']))

print("Done!")
```

### Adafruit AirLift - ESP32 WiFi Co-Processor Breakout - CircuitPython WiFi

[Adafruit AirLift - ESP32 WiFi Co-Processor Breakout](https://learn.adafruit.com/adafruit-airlift-breakout)
[CircuitPython WiFi](https://learn.adafruit.com/adafruit-airlift-breakout/circuitpython-wifi)
## Airlift on the Airlift Shield
![](https://cdn-learn.adafruit.com/assets/assets/000/131/649/medium800/wireless_Untitled2.png?1722455039)

The Adafruit Airlift Shield provides an Airlift ESP32 coprocessor on an Arduino shield form factor. The pins for the Airlift are as follows:

- `esp32_cs = DigitalInOut(board.D10)`
- `esp32_ready = DigitalInOut(board.D7) `
- `esp32_reset = DigitalInOut(board.D5)`

## Airlift on the Metro M4 Express Airlift
![](https://cdn-learn.adafruit.com/assets/assets/000/131/650/medium800/wireless_Untitled2.png?1722455107)

The ESP32 coprocessor is on the following Cortex M4 pins in CircuitPython:

- CS Pin - `board.ESP_CS`
- Ready/Busy - `board.ESP_BUSY`
- Reset - `board.ESP_RESET`

## Airlift on the Adafruit PyPortal
![](https://cdn-learn.adafruit.com/assets/assets/000/131/651/medium800/wireless_Untitled2.png?1722455244)

The ESP32 coprocessor is on the following Cortex M4 pins in CircuitPython:

- CS Pin - `board.ESP_CS`
- Ready/Busy - `board.ESP_BUSY`
- Reset - `board.ESP_RESET`

## Connection Manager Example
https://github.com/adafruit/Adafruit_CircuitPython_Requests/blob/main/examples/esp32spi/requests_esp32spi_advanced.py

## Requests and Connection Manager Example
https://github.com/adafruit/Adafruit_CircuitPython_Requests/blob/main/examples/esp32spi/requests_esp32spi_simpletest.py

## Companion Guides
### Adafruit AirLift - ESP32 WiFi Co-Processor Breakout - Overview

[Adafruit AirLift - ESP32 WiFi Co-Processor Breakout](https://learn.adafruit.com/adafruit-airlift-breakout)
[Overview](https://learn.adafruit.com/adafruit-airlift-breakout/overview)
### Adafruit Metro M4 Express AirLift (WiFi) - Overview

[Adafruit Metro M4 Express AirLift (WiFi)](https://learn.adafruit.com/adafruit-metro-m4-express-airlift-wifi)
[Overview](https://learn.adafruit.com/adafruit-metro-m4-express-airlift-wifi/overview)
### Adafruit PyPortal - IoT for CircuitPython - Overview

[Adafruit PyPortal - IoT for CircuitPython](https://learn.adafruit.com/adafruit-pyportal)
[Overview](https://learn.adafruit.com/adafruit-pyportal/overview)
### Adafruit AirLift Shield - ESP32 WiFi Co-Processor - Overview

[Adafruit AirLift Shield - ESP32 WiFi Co-Processor](https://learn.adafruit.com/adafruit-airlift-shield-esp32-wifi-co-processor)
[Overview](https://learn.adafruit.com/adafruit-airlift-shield-esp32-wifi-co-processor/overview)
## Resources

ReadTheDocs

- [`adafruit_esp32spi` Module](https://docs.circuitpython.org/projects/esp32spi/en/latest/api.html)
- [`adafruit_esp32spi` examples](https://docs.circuitpython.org/projects/esp32spi/en/latest/examples.html#)

# Networking in CircuitPython

## Networking with WizNet Ethernet

Wireless is wonderful, but sometimes you want the strong reliability of a wired connection. If your project is going to be part of a permanent installation, you may want to add Ethernet wired networking to your project.&nbsp;

Ethernet is incredibly easy to use&nbsp;- there's no network configuration or device pairing.&nbsp;Just plug a standard Ethernet cable into an Ethernet FeatherWing or Ethernet Shield and use the&nbsp;[CircuitPython Wiznet5k](https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k/) library for quick and reliable networking.

## Setup

First make sure you are running the&nbsp;[latest version of Adafruit CircuitPython](https://circuitpython.org/board/pyportal/)&nbsp;for your board.

Next you'll need to install the necessary libraries&nbsp;to use the hardware--carefully follow the steps to find and install these libraries from&nbsp;[Adafruit's CircuitPython library bundle](https://github.com/adafruit/Adafruit_CircuitPython_Bundle)&nbsp;matching your version of CircuitPython. The&nbsp;[Wiznet5k Library](https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k) requires at least CircuitPython version 4.0.0. The [latest version](https://circuitpython.org/downloads) is recommended.

Before continuing, make sure your board's&nbsp; **lib** &nbsp;folder has at least the following files and folders **&nbsp;** copied over:

- **adafruit\_wiznet5k**
- **adafruit\_bus\_device**
- **adafruit\_requests.mpy**
- **adafruit\_connection\_manager**

## Requests and Connection Manager Example
https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k/blob/main/examples/wiznet5k_simpletest.py

## Simple Server Example
https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k/blob/main/examples/wiznet5k_simpleserver.py

## `adafruit_httpserver` Example
https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k/blob/main/examples/wiznet5k_httpserver.py

## Network Time Protocol (NTP) Example

The following code looks for a WiFi capable chip. If it doesn't find one, it looks for the WizNet 5k library and sets up the microcontroller for an Ethernet SPI connection.&nbsp;

This code uses the `adafruit_ntp` and `adafruit_connection_manager` modules.

https://github.com/adafruit/Adafruit_CircuitPython_NTP/blob/main/examples/ntp_connection_manager.py

## Companion Guide
### Ethernet for CircuitPython with Wiznet5K - Overview

[Ethernet for CircuitPython with Wiznet5K](https://learn.adafruit.com/ethernet-for-circuitpython)
[Overview](https://learn.adafruit.com/ethernet-for-circuitpython/overview)
## Resources

- [Ethernet for CircuitPython with Wiznet5K Guide](https://learn.adafruit.com/ethernet-for-circuitpython/overview)
- [`Adafruit_CircuitPython_Wiznet5k`](https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k/tree/main/examples) examples on GitHub
- [Adafruit Connection Manager Adafruit Playground Note](https://adafruit-playground.com/u/justmobilize/pages/adafruit-connection-manager)

# Networking in CircuitPython

## Making HTTP and HTTPS Requests

The&nbsp;`requests`&nbsp;module allows you to send HTTP requests using regular Python on the desktop. The `adafruit_requests` library does the same in CircuitPython.

The HTTP request returns a&nbsp;`Response` object&nbsp;with all the response data (content, encoding, status, etc).

## A Simple Example Using&nbsp;`wifi`
https://github.com/adafruit/Adafruit_CircuitPython_Requests/blob/main/examples/wifi/requests_wifi_simpletest.py

## Advanced `wifi` Example
https://github.com/adafruit/Adafruit_CircuitPython_Requests/blob/main/examples/wifi/requests_wifi_advanced.py

## Simple Example for Airlift / ESP32SPI
https://github.com/adafruit/Adafruit_CircuitPython_Requests/blob/main/examples/esp32spi/requests_esp32spi_simpletest.py

## Using Wiznet5k Example
https://github.com/adafruit/Adafruit_CircuitPython_Requests/blob/main/examples/wiznet5k/requests_wiznet5k_simpletest.py

## Resources

ReadTheDocs

- [`adafruit_requests`](https://docs.circuitpython.org/projects/requests/en/latest/api.html)

Examples

- [`Adafruit_CircuitPython_Requests` GitHub repo Examples](https://github.com/adafruit/Adafruit_CircuitPython_Requests/tree/main/examples)

# Networking in CircuitPython

## HTTP Server Examples

## Using `wifi` with `adafruit_httpserver`
https://github.com/adafruit/Adafruit_CircuitPython_HTTPServer/blob/main/examples/httpserver_websocket.py

## Return CPU Information Example
https://github.com/adafruit/Adafruit_CircuitPython_HTTPServer/blob/main/examples/httpserver_cpu_information.py

## Simple Example with Requests
https://github.com/adafruit/Adafruit_CircuitPython_HTTPServer/blob/main/examples/httpserver_simpletest_auto_connection_manager.py

## Example for Wiznet5K
https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k/blob/main/examples/wiznet5k_httpserver.py

## Resources

ReadTheDocs

- [Adafruit CircuitPython HTTPServer Library Examples](https://docs.circuitpython.org/projects/httpserver/en/stable/starting_methods.html)&nbsp;

Additional Examples

- [`Adafruit_CircuitPython_HTTPServer` examples on GitHub](https://github.com/adafruit/Adafruit_CircuitPython_HTTPServer/tree/main/examples)

# Networking in CircuitPython

## NTP Time Example

Network Time Protocol allows for getting the time from specific time servers on a local network or internet.

## Example CircuitPython Code
The example code below assumes you have a **settings.toml** file in the **CIRCUITPY** root directory which contains the SSID and password for the local WiFi network, as discussed earlier in this guide.

https://github.com/adafruit/Adafruit_CircuitPython_NTP/blob/main/examples/ntp_simpletest.py

## Example for Wiznet5k
The following code looks for a WiFi capable chip. It it doesn't find one, it looks for the WizNet 5k library and sets up the microcontroller to Ethernet SPI connection.&nbsp;

This code uses the `adafruit_ntp` and `adafruit_connection_manager` modules.

https://github.com/adafruit/Adafruit_CircuitPython_NTP/blob/main/examples/ntp_connection_manager.py

## Resources

ReadTheDocs

- [adafruit\_ntp documentation](https://docs.circuitpython.org/projects/ntp/en/latest/api.html)

Examples

- [Adafruit\_CircuitPython\_NTP repository](https://github.com/adafruit/Adafruit_CircuitPython_NTP)
  - [Examples](https://github.com/adafruit/Adafruit_CircuitPython_NTP/tree/main/examples)

# Networking in CircuitPython

## Troubleshooting

Here are some issues and solutions regarding networking:

## General
### Can I put the network credentials in my code instead of in a **settings.toml** file?

Yes, of course. Code like `wifi.radio.connect(ssid="mynetwork",             password="12345")` is valid, but is highly discouraged.

If you save the code to GitHub or another online repository or publish a guide or Playground Note, you will be handing your WiFi credentials to the world.

Placing the values in&nbsp; **settings.toml** and using `os.getenv()` allows you to separate the values from the code.

### Can CircuitPython use IPv6 addressing?

There is active work on the ability to use IPv6 addresses in addition to IPv4. The work is incomplete as of mid-2024. Please keep an eye on CircuitPython version release notes for when the work will be complete. There is no estimated time of arrival (ETA) for this code to be ready.

## Wireless Networking
### My device fails to connect or `os.getenv()` returns an error

Current CircuitPython implementations use a file called **settings.toml** to store the WiFi SSID ("network name") and password. See the Network Settings page in this guide on how to create this file and format content in it. The values must be in double quotes ("). Typical entries are similar to the ones below:

```auto
# settings.toml
CIRCUITPY_WIFI_SSID = "MyLocalNet"
CIRCUITPY_WIFI_PASSWORD = "mysecretpassword"
```

Also note: the values in **settings.toml** must match those in the code. In&nbsp; **settings.toml** if the SSID is specified in the value `CIRCUITPY_WIFI_SSID` then use `ssid=os.getenv("CIRCUITPY_WIFI_SSID")` to get that value and not `ssid=os.getenv("SSID")` as there is a mismatch in the names which will not result in what you want.

### How do I get the IP address for a board on my local network?

You can print it using `print("my IP addr:", wifi.radio.ipv4_address)` or save it to a variable with `my_address = wifi.radio.ipv4_address`. Generally the libraries abstract the address such that you do not have to use its value explicitly. But there are times when it is handy to print it to verify there is a connection, re. if the value is `0.0.0.0` or another nonsense value when it should be similar to other devices on your network (example: `192.168.1.87`) then there may be an issue.

## Wired Networking
### Does CircuitPython support any hardwired networking other than WizNet 55xx?

Not at this time. This can be revisited as new technologies come on the scene and are adopted by the community.

If you are using a single board computer like Raspberry Pi, you'll be using CPython ("regular Python") which has extensive networking support which you can find documentation in standard Python texts.

# Networking in CircuitPython

## Advanced Topics: Ping and UDP

## Ping

Ping is a method of measuring the round trip time for messages sent from a host to a network destination and echoed back to the source on an IP network. Pinging involves sending an ICMP echo request to the target host and waiting for an ICMP echo reply.

In CircuitPython, the wifi Module's radio function provides ping functionality. `ping = wifi.radio.ping(ip=ping_ip)` where ping is the round trip time in seconds a request took place.

Warning: Limitations: On Espressif, calling `ping()` multiple times rapidly exhausts available resources after several calls. Rather than failing at that point, `ping()` will wait two seconds for enough resources to be freed up before proceeding.

The following shows how a ping can be sent and printed out.

```python
import os
import ipaddress
import ssl
import wifi

print(f"Connecting to {os.getenv('CIRCUITPY_WIFI_SSID')}")
wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
print(f"Connected to {os.getenv('CIRCUITPY_WIFI_SSID')}")
print(f"My IP address: {wifi.radio.ipv4_address}")

ping_ip = ipaddress.IPv4Address("8.8.8.8")  # Google.com
ping = wifi.radio.ping(ip=ping_ip)

# retry once if timed out
if ping is None:
    ping = wifi.radio.ping(ip=ping_ip)

if ping is None:
    print("Couldn't ping 'google.com' successfully")
else:
    # convert s to ms
    print(f"Pinging 'google.com' took: {ping * 1000} ms")
```

## UDP

TCP (Transmission Control Protocol), used for HTTP, HTTPS, and many other kinds of internet connections, is not the only method of transmitting data over a network connection. TCP is a "reliable delivery" protocol: it keeps trying until the data is delivered or gives up with an error. It sets up a persistent connection between two points and uses a sequence of protocol acknowledgments and "handshakes" to ensure reliable delivery.

By contrast, UDP (User Datagram Protocol) is a "connectionless" protocol. It simply tries to deliver packets of data from one point to another. The packets may be dropped along the way if there is congestion or other problems, and their ordering is not guaranteed. It is useful for streaming data such audio, video, or periodic data reporting, where loss of data is not fatal or corrupting.

In general, you request a socket supporting UDP with the following:&nbsp;

```python
wifi.radio.connect(ssid=os.getenv("CIRCUITPY_WIFI_SSID"), password=os.getenv("CIRCUITPY_WIFI_PASSWORD"))
pool = socketpool.SocketPool(wifi.radio)
sock = pool.socket(pool.AF_INET, pool.SOCK_DGRAM)
```

You'd then create your packet to send (a `bytearray`) and send it like this:

```auto
sock.sendto(packet, (URL, port))
```

Warning: The port used for UDP for your own use should be 1024 or greater, as lower ports (0-1023) are typically reserved for specific services and protected by the operating system.

Below is an example of using UDP for getting the time from an NTP server:

https://github.com/adafruit/circuitpython/blob/main/tests/circuitpython-manual/socketpool/datagram/ntp.py

Below is an example posted by Tod Kurt (@todbot):

```auto
# udp_recv_code.py -- receive UDP messages from any receiver, can be another CircuitPython device
# 24 Aug 2022 - @todbot / Tod Kurt
# cribbing from code at https://github.com/adafruit/circuitpython/blob/main/tests/circuitpython-manual/socketpool/datagram/ntp.py

import time, wifi, socketpool, os
print("Connecting to WiFi...")
wifi.radio.connect(ssid=os.getenv("CIRCUITPY_WIFI_SSID"), password=os.getenv("CIRCUITPY_WIFI_PASSWORD"))
print("my IP addr:", wifi.radio.ipv4_address)
pool = socketpool.SocketPool(wifi.radio)

# I, @PaulskPt, used for udp_host erroneously: os.getenv("MULTICAST_GROUP")
udp_host = str(wifi.radio.ipv4_address) # my LAN IP as a string  
udp_port = int(os.getenv("MULTICAST_PORT"))  # a number of your choosing, should be 1024-65000
udp_buffer = bytearray(64)  # stores our incoming packet

sock = pool.socket(pool.AF_INET, pool.SOCK_DGRAM) # UDP socket
sock.bind((udp_host, udp_port))  # say we want to listen on this host,port

print("waiting for packets on",udp_host, udp_port)
while True:
    size, addr = sock.recvfrom_into(udp_buffer)
    msg = udp_buffer.decode('utf-8')  # assume a string, so convert from bytearray
    print(f"Received message from {addr[0]}:", msg)
```

## Resources

ReadTheDocs

- [wifi Module](https://docs.circuitpython.org/en/latest/shared-bindings/wifi/index.html) (Ping)

Examples

- anecdata's [Socket Examples](https://github.com/anecdata/Socket) - GitHub
- DJDevon's [Web APIs & You](https://adafruit-playground.com/u/DJDevon3/pages/web-api-s-you) - Adafruit Playground

# Networking in CircuitPython

## MQTT in CircuitPython

# Networking in CircuitPython

## Adafruit IO


## Featured Products

### Adafruit Ethernet FeatherWing

[Adafruit Ethernet FeatherWing](https://www.adafruit.com/product/3201)
Wireless is wonderful, but sometimes you want the strong reliability of a wire. If your Feather board is going to be part of a permanent installation, this **Ethernet FeatherWing** will let you add quick and easy wired Internet. Just plug in a standard

In Stock
[Buy Now](https://www.adafruit.com/product/3201)
[Related Guides to the Product](https://learn.adafruit.com/products/3201/guides)
### Adafruit ESP32 Feather V2 -  8MB Flash + 2 MB PSRAM

[Adafruit ESP32 Feather V2 -  8MB Flash + 2 MB PSRAM](https://www.adafruit.com/product/5400)
One of our star Feathers is the [Adafruit HUZZAH32 ESP32 Feather](https://www.adafruit.com/product/3405) - with the fabulous ESP32 WROOM module on there, it makes quick work of WiFi and Bluetooth® projects that take advantage of Espressifs most popular chipset. Recently we had...

Out of Stock
[Buy Now](https://www.adafruit.com/product/5400)
[Related Guides to the Product](https://learn.adafruit.com/products/5400/guides)
### Adafruit ESP32-S3 Feather with 4MB Flash 2MB PSRAM

[Adafruit ESP32-S3 Feather with 4MB Flash 2MB PSRAM](https://www.adafruit.com/product/5477)
The ESP32-S3 has arrived in Feather format - and what a great way to get started with this powerful new chip from Espressif! With dual 240 MHz cores, WiFi and BLE support, and native USB, this Feather is great for powering your IoT projects.

That's right - it's the new...

Out of Stock
[Buy Now](https://www.adafruit.com/product/5477)
[Related Guides to the Product](https://learn.adafruit.com/products/5477/guides)
### Adafruit ESP32-S3 Feather with STEMMA QT / Qwiic

[Adafruit ESP32-S3 Feather with STEMMA QT / Qwiic](https://www.adafruit.com/product/5323)
The ESP32-S3 has arrived in Feather format - and what a great way to get started with this powerful new chip from Espressif! With dual 240 MHz cores, WiFi and BLE support, and native USB, this Feather is great for powering your IoT projects.

That's right - it's the new...

In Stock
[Buy Now](https://www.adafruit.com/product/5323)
[Related Guides to the Product](https://learn.adafruit.com/products/5323/guides)
### Adafruit ESP32-S2 Feather - 4 MB Flash + 2 MB PSRAM

[Adafruit ESP32-S2 Feather - 4 MB Flash + 2 MB PSRAM](https://www.adafruit.com/product/5000)
What's Feather-shaped and has an ESP32-S2 WiFi module? What has a STEMMA QT connector for I2C devices? What has your favorite Espressif WiFi microcontroller and lots of Flash and RAM memory for your next IoT project? What will make your next IoT project flyyyyy?

That's right -...

Out of Stock
[Buy Now](https://www.adafruit.com/product/5000)
[Related Guides to the Product](https://learn.adafruit.com/products/5000/guides)
### Adafruit AirLift FeatherWing – ESP32 WiFi Co-Processor

[Adafruit AirLift FeatherWing – ESP32 WiFi Co-Processor](https://www.adafruit.com/product/4264)
Give your Feather project a _lift_ with the Adafruit AirLift FeatherWing - a FeatherWing that lets you use the powerful ESP32 as a WiFi co-processor. You probably have your favorite Feather ([like the Feather M4](https://www.adafruit.com/product/3857)) that comes with its own...

In Stock
[Buy Now](https://www.adafruit.com/product/4264)
[Related Guides to the Product](https://learn.adafruit.com/products/4264/guides)
### Adafruit Metro ESP32-S3 with 16 MB Flash 8 MB PSRAM

[Adafruit Metro ESP32-S3 with 16 MB Flash 8 MB PSRAM](https://www.adafruit.com/product/5500)
What's Metro-shaped and has an ESP32-S3&nbsp;WiFi module? What has a STEMMA QT connector for I2C devices and a Lipoly charger circuit? What has your favorite Espressif WiFi microcontroller and lots of memory for your next IoT project?

That's right - it's the new...

In Stock
[Buy Now](https://www.adafruit.com/product/5500)
[Related Guides to the Product](https://learn.adafruit.com/products/5500/guides)
### Adafruit Metro ESP32-S2

[Adafruit Metro ESP32-S2](https://www.adafruit.com/product/4775)
What's Metro shaped and has an ESP32-S2 WiFi module? What has a STEMMA QT connector for I2C devices, and a Lipoly charger circuit? What has your favorite Espressif WiFi microcontroller and lots of memory for your next IoT project?

That's right - its the new Adafruit Metro...

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

## Related Guides

- [Adafruit Ethernet FeatherWing](https://learn.adafruit.com/adafruit-wiz5500-wiznet-ethernet-featherwing.md)
- [Adafruit AirLift - ESP32 WiFi Co-Processor Breakout](https://learn.adafruit.com/adafruit-airlift-breakout.md)
- [Adafruit AirLift FeatherWing - ESP32 WiFi Co-Processor](https://learn.adafruit.com/adafruit-airlift-featherwing-esp32-wifi-co-processor-featherwing.md)
- [Adafruit Metro ESP32-S2](https://learn.adafruit.com/adafruit-metro-esp32-s2.md)
- [Adafruit ESP32-S2 Feather](https://learn.adafruit.com/adafruit-esp32-s2-feather.md)
- [Adafruit ESP32 Feather V2](https://learn.adafruit.com/adafruit-esp32-feather-v2.md)
- [Adafruit ESP32-S3 Feather](https://learn.adafruit.com/adafruit-esp32-s3-feather.md)
- [Adafruit Metro ESP32-S3](https://learn.adafruit.com/adafruit-metro-esp32-s3.md)
- [Sound Reactive Paper Lanterns with LedFx](https://learn.adafruit.com/sound-reactive-paper-lanterns-with-led-fx.md)
- [LED Matrix Necklace Pendant](https://learn.adafruit.com/led-matrix-necklace-pendant.md)
- [Adafruit IO Basics: AirLift](https://learn.adafruit.com/adafruit-io-basics-airlift.md)
- [How To Install Drivers for WCH CH34X / CH340 / CH341 / CH342 / CH343 / CH344 / CH346 / CH347 / CH9101 / CH9102 / CH9103 / CH9104 / CH9111 / CH9114 / CH9143 USB to Serial Chips](https://learn.adafruit.com/how-to-install-drivers-for-wch-usb-to-serial-chips-ch9102f-ch9102.md)
- [Holiday Lights: Easy DIY Christmas Wreath & Garland with WLED](https://learn.adafruit.com/holiday-garland-decor-app-control-with-no-coding.md)
- [Plotting Offline Data - JSONL to CSV files, filters and graphs](https://learn.adafruit.com/plotting-offline-data-jsonl-to-csv-files-filters-and-graphs.md)
- [Grill Thermometer Dashboard](https://learn.adafruit.com/grill-thermometer-dashboard.md)
- [ESP32-S3 BLE RS-232 Controller](https://learn.adafruit.com/esp32-s3-ble-rs232-controller.md)
- [Quickstart - Raspberry Pi RP2040 with BLE and CircuitPython](https://learn.adafruit.com/quickstart-raspberry-pi-rp2040-with-ble-and-circuitpython.md)
- [TFT Spirit Board](https://learn.adafruit.com/tft-spirit-board.md)
- [No-Code IoT Humidity and Temperature Sensor with Adafruit IO WipperSnapper](https://learn.adafruit.com/no-code-humidity-and-temp-tracker.md)
- [Air Quality Sensor 3D Printed Enclosure](https://learn.adafruit.com/air-quality-sensor-silo-house.md)
- [LED Festival Coat with Mapping and WLED ](https://learn.adafruit.com/led-festival-coat-with-mapping-and-wled.md)
- [CircuitPython Stage game library](https://learn.adafruit.com/circuitpython-stage-game-library.md)
