Part 3 - Connect to a WiFi Network
To connect your microcontroller to a network, you must provide a network name (SSID) and password. However, directly using sensitive data like WiFi passwords in your code is risky. Therefore, creating a separate file to hold your personal keys and passwords outside your code.py file is a good idea. This way, you can share your code without revealing sensitive data to others.
-
Begin by clicking the
+icon labeled New in the top toolbar of Mu Editor. -
Copy and paste the following code to the new file, replacing
network-namewith the name of your WiFi network andnetwork-passwdwith your network’s password (or the iPSK generated during part two when connecting to TUD-facility).
settings = {
"ssid": "network-name",
"password": "network-passwd"
}
-
Click the Save button in the toolbar and save the file on your
CIRCUITPYdrive using the namesettings.py. -
Enter your
code.pyfile and replace the code you wrote in part one with the code below, then hit Save.
-
print("hello world") -
import board import busio import time import digitalio from adafruit_esp32spi import adafruit_esp32spi # Get WiFi details from your settings.py file from settings import settings # Define the pins used by the BitsyExpander's ESP32 WiFi module esp32_cs = digitalio.DigitalInOut(board.D9) esp32_ready = digitalio.DigitalInOut(board.D11) esp32_reset = digitalio.DigitalInOut(board.D12) spi = busio.SPI(board.SCK, board.MOSI, board.MISO) # Initialize the ESP32 WiFi module esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) if esp.status == adafruit_esp32spi.WL_IDLE_STATUS: print("\nESP32 WiFi Module found.") print("Firmware version:", str(esp.firmware_version, "utf-8")) print("*" * 40) print("\nScanning for available networks...\n") network_list = [str(ap["ssid"], "utf-8") for ap in esp.scan_networks()] if settings["ssid"] not in network_list: print(settings["ssid"], "not found.\nAvailable networks:", network_list) raise SystemExit(0) print(settings["ssid"], "found. Connecting...") while not esp.is_connected: try: esp.connect_AP(settings["ssid"], settings["password"]) except (RuntimeError, ConnectionError) as e: print("\nUnable to establish connection. Are you using a valid password?") print("Error message:", e, "\nRetrying...") continue print("Connected! IP address:", esp.pretty_ip(esp.ip_address)) while True: print("\nPinging google.com...") response = esp.ping("google.com") print("Ping successful. Response time:", response, "ms") time.sleep(5)
-
Open Mu’s
Serial Monitorto verify that the microcontroller successfully connects to the chosen network and can get a response fromgoogle.com. -
Note the differences from the code you encountered in part one of this tutorial:
- An
importstatement for your newly createdsettings.pyfile has been added. This gives the code access to the network name (SSID) and password. - The network scan has been moved out of the
while True:loop and is performed once before entering the loop. - The code verifies the network you configured is available and waits for the successful establishment of a connection.
- The content of the
while True:loop has been replaced: It now pings google.com and reports the response time. This confirms internet connectivity for your microcontroller.
- An
Your microcontroller is now ready for connecting to the internet or even other microcontroller’s! Good luck with your connected projects ;-)