In this example, you'll test your ESP32-C6 Feather WiFi connection by connecting to your SSID, printing your MAC address and IP address to the REPL and then pinging Google.
settings.toml File
If you've worked on WiFi projects with CircuitPython before, you're probably familiar with the secrets.py file. This file is a Python file that is stored on your CIRCUITPY drive that contains all of your secret WiFi information, such as your SSID, SSID password and any API keys for IoT services.
As of CircuitPython 8, there is support for a settings.toml file. Similar to secrets.py, the settings.toml file separates your sensitive information from your main code.py file.
Your settings.toml file should be stored in the main directory of your board. It should not be in a folder.
# 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, octal (
0o567
) and binary (0b11011
) are not supported.
- Floats, octal (
- 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)
- Example:
- Unicode emoji, and non-ASCII characters, stand for themselves as long as you're careful to save in "UTF-8 without BOM" format
When your settings.toml file is ready, you can save it in your text editor with the .toml extension.
In the example below, click the Download Project Bundle button below to download the code.py file in a zip file. Extract the contents of the zip file and then click on the directory that matches the version of CircuitPython you're using.
# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries # # SPDX-License-Identifier: MIT import os import ipaddress import wifi import socketpool print() print("Connecting to WiFi") # connect to your SSID wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD')) print("Connected to WiFi") pool = socketpool.SocketPool(wifi.radio) # 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) # pings Google ipv4 = ipaddress.ip_address("8.8.4.4") print("Ping google.com: %f ms" % (wifi.radio.ping(ipv4)*1000))
In the editor window in your browser, click the Open button to view the file dialog. Then, click the Upload button and select Upload Files.
You'll be asked if you want to overwrite the previous code.py with the new code.py file from the Project Bundle. Click OK.
Update Your settings.toml File
Remember to add your settings.toml file as described earlier in this page. You'll need to include your CIRCUITPY_WIFI_SSID
and CIRCUITPY_WIFI_PASSWORD
in the file.
You can edit the file manually in the USB code editor by clicking Open, selecting settings.toml and clicking Open at the bottom of the dialog box.
With settings.toml open in the editor, you can add your WiFi credentials:
CIRCUITPY_WIFI_SSID = "your-ssid-here"
CIRCUITPY_WIFI_PASSWORD = "your-ssid-password-here"
Once your credentials are entered, click Save above the editor to save your changes to settings.toml.
Once everything is saved to the board, Restart the Serial Console to see the data printed out!
How the CircuitPython WiFi Example Works
In the basic WiFi test, the board connects to your SSID by importing your SSID and SSID password from the settings.toml file.
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
Then, your MAC address and IP address are printed to the REPL.
# 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.
# 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 board is connecting to WiFi with CircuitPython successfully and you can move on to more advanced projects.
Text editor powered by tinymce.