Simple test¶
Ensure your device works with this simple test.
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import time
5import board
6import busio
7from digitalio import DigitalInOut
8from digitalio import Direction
9from adafruit_espatcontrol import adafruit_espatcontrol
10
11
12# Get wifi details and more from a secrets.py file
13try:
14 from secrets import secrets
15except ImportError:
16 print("WiFi secrets are kept in secrets.py, please add them there!")
17 raise
18# Debug Level
19# Change the Debug Flag if you have issues with AT commands
20debugflag = False
21
22if board.board_id == "challenger_rp2040_wifi":
23 RX = board.ESP_RX
24 TX = board.ESP_TX
25 resetpin = DigitalInOut(board.WIFI_RESET)
26 rtspin = False
27 uart = busio.UART(TX, RX, baudrate=11520, receiver_buffer_size=2048)
28 esp_boot = DigitalInOut(board.WIFI_MODE)
29 esp_boot.direction = Direction.OUTPUT
30 esp_boot.value = True
31else:
32 RX = board.ESP_TX
33 TX = board.ESP_RX
34 resetpin = DigitalInOut(board.ESP_WIFI_EN)
35 rtspin = DigitalInOut(board.ESP_CTS)
36 uart = busio.UART(TX, RX, timeout=0.1)
37 esp_boot = DigitalInOut(board.ESP_BOOT_MODE)
38 esp_boot.direction = Direction.OUTPUT
39 esp_boot.value = True
40
41
42print("ESP AT commands")
43# For Boards that do not have an rtspin like challenger_rp2040_wifi set rtspin to False.
44esp = adafruit_espatcontrol.ESP_ATcontrol(
45 uart, 115200, reset_pin=resetpin, rts_pin=rtspin, debug=debugflag
46)
47print("Resetting ESP module")
48esp.hard_reset()
49
50first_pass = True
51while True:
52 try:
53 if first_pass:
54 # Some ESP do not return OK on AP Scan.
55 # See https://github.com/adafruit/Adafruit_CircuitPython_ESP_ATcontrol/issues/48
56 # Comment out the next 3 lines if you get a No OK response to AT+CWLAP
57 print("Scanning for AP's")
58 for ap in esp.scan_APs():
59 print(ap)
60 print("Checking connection...")
61 # secrets dictionary must contain 'ssid' and 'password' at a minimum
62 print("Connecting...")
63 esp.connect(secrets)
64 print("Connected to AT software version ", esp.version)
65 print("IP address ", esp.local_ip)
66 first_pass = False
67 print("Pinging 8.8.8.8...", end="")
68 print(esp.ping("8.8.8.8"))
69 time.sleep(10)
70 except (ValueError, RuntimeError, adafruit_espatcontrol.OKError) as e:
71 print("Failed to get data, retrying\n", e)
72 print("Resetting ESP module")
73 esp.hard_reset()
74 continue