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.
Here is an example on how to format your settings.toml 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.
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.
import os print(os.getenv("test_variable"))
In the upcoming CircuitPython WiFi examples, you'll see how the settings.toml file is used for connecting to your SSID and accessing your API keys.