In this example, you'll test your QT Py ESP32-S3 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.
# 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.
Once you've finished setting up your QT Py ESP32-S3 with CircuitPython, you can access the code and necessary libraries by downloading the Project Bundle.
To do this, click on the Download Project Bundle button in the window below. It will download as a zipped folder.
# 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))
Upload the Code and Libraries to the QT Py ESP32-S3
After downloading the Project Bundle, plug your QT Py ESP32-S3 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 CIRCUITPY. Unzip the folder and copy the following items to the QT Py ESP32-S3's CIRCUITPY drive.
- code.py
Your QT Py ESP32-S3 CIRCUITPY drive should look like this after copying the code.py file.
Add 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.
CIRCUITPY_WIFI_SSID = "your-ssid-here" CIRCUITPY_WIFI_PASSWORD = "your-ssid-password-here"
Once everything is saved to the CIRCUITPY drive, connect to 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.
For more information on the basics of doing networking in CircuitPython, see this guide:
Text editor powered by tinymce.