/ examples / esp_atcontrol_simpletest.py
esp_atcontrol_simpletest.py
 1  import time
 2  import board
 3  import busio
 4  from digitalio import DigitalInOut
 5  from digitalio import Direction
 6  from adafruit_espatcontrol import adafruit_espatcontrol
 7  
 8  
 9  # Get wifi details and more from a secrets.py file
10  try:
11      from secrets import secrets
12  except ImportError:
13      print("WiFi secrets are kept in secrets.py, please add them there!")
14      raise
15  
16  
17  # With a Particle Argon
18  RX = board.ESP_TX
19  TX = board.ESP_RX
20  resetpin = DigitalInOut(board.ESP_WIFI_EN)
21  rtspin = DigitalInOut(board.ESP_CTS)
22  uart = busio.UART(TX, RX, timeout=0.1)
23  esp_boot = DigitalInOut(board.ESP_BOOT_MODE)
24  esp_boot.direction = Direction.OUTPUT
25  esp_boot.value = True
26  
27  
28  print("ESP AT commands")
29  esp = adafruit_espatcontrol.ESP_ATcontrol(
30      uart, 115200, reset_pin=resetpin, rts_pin=rtspin, debug=False
31  )
32  print("Resetting ESP module")
33  esp.hard_reset()
34  
35  first_pass = True
36  while True:
37      try:
38          if first_pass:
39              print("Scanning for AP's")
40              for ap in esp.scan_APs():
41                  print(ap)
42              print("Checking connection...")
43              # secrets dictionary must contain 'ssid' and 'password' at a minimum
44              print("Connecting...")
45              esp.connect(secrets)
46              print("Connected to AT software version ", esp.version)
47              print("IP address ", esp.local_ip)
48              first_pass = False
49          print("Pinging 8.8.8.8...", end="")
50          print(esp.ping("8.8.8.8"))
51          time.sleep(10)
52      except (ValueError, RuntimeError, adafruit_espatcontrol.OKError) as e:
53          print("Failed to get data, retrying\n", e)
54          print("Resetting ESP module")
55          esp.hard_reset()
56          continue