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)

What the heck is settings.toml?

re. Using and getting Environment Variables

settings.toml a config file that lives next to your code.py and is used to store WiFi credentials and other global settings. It is also used (invisibly) by many Adafruit libraries that do WiFi. You can use it (as in the examples above) without those libraries. The settings names used by CircuitPython are documented in CircuitPython Web Workflow.

Using settings.toml replaces using secrets.py in modern CircuitPython code.

Note: You can use any variable names for your WiFI credentials (a common pair is WIFI_SSID and WIFI_PASSWORD), but if you use the CIRCUITPY_WIFI_* names that will also start up the Web Workflow

You use it like this for basic WiFi connectivity:

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

# 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)

See the page below as an example of using settings.toml:

Adafruit ESP32-S2 Feather
By Kattni Rembor
View Guide

The examples below still use the old secrets.py rather than the modern settings.toml used above.

import time
import wifi
import ipaddress
from secrets import secrets
ip_to_ping = "1.1.1.1"

wifi.radio.connect(ssid=secrets['ssid'],password=secrets['password'])

print("my IP addr:", wifi.radio.ipv4_address)
print("pinging ",ip_to_ping)
ip1 = ipaddress.ip_address(ip_to_ping)
while True:
    print("ping:", wifi.radio.ping(ip1))
    time.sleep(1)
import time
import wifi
import socketpool
import ssl
import adafruit_requests
from secrets import secrets
wifi.radio.connect(ssid=secrets['ssid'],password=secrets['password'])
print("my IP addr:", wifi.radio.ipv4_address)
pool = socketpool.SocketPool(wifi.radio)
session = adafruit_requests.Session(pool, ssl.create_default_context())
while True:
    response = session.get("https://todbot.com/tst/randcolor.php")
    data = response.json()
    print("data:",data)
    time.sleep(5)

What the heck is secrets.py?

It's a config file that lives next to your code.py and is used (invisibly) by many Adafruit WiFi libraries. You can use it too (as in the examples above) without those libraries

It looks like this for basic WiFi connectivity:

# secrets.py
secrets = {
  "ssid": "Pretty Fly for a WiFi",
  "password": "donthackme123"
}
# code.py
from secrets import secrets
print("your WiFi password is:", secrets['password'])

Other field/value pairs may be in secrets.py for using passwords to Adafruit IO or other applications, to denote a time zone, etc.

This guide was first published on Apr 02, 2022. It was last updated on May 15, 2024.

This page (Networking) was last updated on May 15, 2024.

Text editor powered by tinymce.