Simple test

Ensure your device works with this simple test.

examples/esp32spi_simpletest.py
 1# SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import board
 5import busio
 6from digitalio import DigitalInOut
 7import adafruit_requests as requests
 8import adafruit_esp32spi.adafruit_esp32spi_socket as socket
 9from adafruit_esp32spi import adafruit_esp32spi
10
11# Get wifi details and more from a secrets.py file
12try:
13    from secrets import secrets
14except ImportError:
15    print("WiFi secrets are kept in secrets.py, please add them there!")
16    raise
17
18print("ESP32 SPI webclient test")
19
20TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
21JSON_URL = "http://api.coindesk.com/v1/bpi/currentprice/USD.json"
22
23
24# If you are using a board with pre-defined ESP32 Pins:
25esp32_cs = DigitalInOut(board.ESP_CS)
26esp32_ready = DigitalInOut(board.ESP_BUSY)
27esp32_reset = DigitalInOut(board.ESP_RESET)
28
29# If you have an AirLift Shield:
30# esp32_cs = DigitalInOut(board.D10)
31# esp32_ready = DigitalInOut(board.D7)
32# esp32_reset = DigitalInOut(board.D5)
33
34# If you have an AirLift Featherwing or ItsyBitsy Airlift:
35# esp32_cs = DigitalInOut(board.D13)
36# esp32_ready = DigitalInOut(board.D11)
37# esp32_reset = DigitalInOut(board.D12)
38
39# If you have an externally connected ESP32:
40# NOTE: You may need to change the pins to reflect your wiring
41# esp32_cs = DigitalInOut(board.D9)
42# esp32_ready = DigitalInOut(board.D10)
43# esp32_reset = DigitalInOut(board.D5)
44
45spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
46esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
47
48requests.set_socket(socket, esp)
49
50if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
51    print("ESP32 found and in idle mode")
52print("Firmware vers.", esp.firmware_version)
53print("MAC addr:", [hex(i) for i in esp.MAC_address])
54
55for ap in esp.scan_networks():
56    print("\t%s\t\tRSSI: %d" % (str(ap["ssid"], "utf-8"), ap["rssi"]))
57
58print("Connecting to AP...")
59while not esp.is_connected:
60    try:
61        esp.connect_AP(secrets["ssid"], secrets["password"])
62    except RuntimeError as e:
63        print("could not connect to AP, retrying: ", e)
64        continue
65print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
66print("My IP address is", esp.pretty_ip(esp.ip_address))
67print(
68    "IP lookup adafruit.com: %s" % esp.pretty_ip(esp.get_host_by_name("adafruit.com"))
69)
70print("Ping google.com: %d ms" % esp.ping("google.com"))
71
72# esp._debug = True
73print("Fetching text from", TEXT_URL)
74r = requests.get(TEXT_URL)
75print("-" * 40)
76print(r.text)
77print("-" * 40)
78r.close()
79
80print()
81print("Fetching json from", JSON_URL)
82r = requests.get(JSON_URL)
83print("-" * 40)
84print(r.json())
85print("-" * 40)
86r.close()
87
88print("Done!")

Other Examples

examples/esp32spi_cheerlights.py
 1# SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import time
 5import board
 6import busio
 7from digitalio import DigitalInOut
 8
 9import neopixel
10import adafruit_fancyled.adafruit_fancyled as fancy
11
12from adafruit_esp32spi import adafruit_esp32spi
13from adafruit_esp32spi import adafruit_esp32spi_wifimanager
14
15# Get wifi details and more from a secrets.py file
16try:
17    from secrets import secrets
18except ImportError:
19    print("WiFi secrets are kept in secrets.py, please add them there!")
20    raise
21
22print("ESP32 SPI webclient test")
23
24DATA_SOURCE = "https://api.thingspeak.com/channels/1417/feeds.json?results=1"
25DATA_LOCATION = ["feeds", 0, "field2"]
26
27esp32_cs = DigitalInOut(board.D9)
28esp32_ready = DigitalInOut(board.D10)
29esp32_reset = DigitalInOut(board.D5)
30spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
31esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
32"""Use below for Most Boards"""
33status_light = neopixel.NeoPixel(
34    board.NEOPIXEL, 1, brightness=0.2
35)  # Uncomment for Most Boards
36"""Uncomment below for ItsyBitsy M4"""
37# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
38wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
39
40# neopixels
41pixels = neopixel.NeoPixel(board.A1, 16, brightness=0.3)
42pixels.fill(0)
43
44# we'll save the value in question
45last_value = value = None
46
47while True:
48    try:
49        print("Fetching json from", DATA_SOURCE)
50        response = wifi.get(DATA_SOURCE)
51        print(response.json())
52        value = response.json()
53        for key in DATA_LOCATION:
54            value = value[key]
55            print(value)
56        response.close()
57    except (ValueError, RuntimeError) as e:
58        print("Failed to get data, retrying\n", e)
59        wifi.reset()
60        continue
61
62    if not value:
63        continue
64    if last_value != value:
65        color = int(value[1:], 16)
66        red = color >> 16 & 0xFF
67        green = color >> 8 & 0xFF
68        blue = color & 0xFF
69        gamma_corrected = fancy.gamma_adjust(fancy.CRGB(red, green, blue)).pack()
70
71        pixels.fill(gamma_corrected)
72        last_value = value
73    response = None
74    time.sleep(60)
examples/esp32spi_aio_post.py
 1# SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import time
 5import board
 6import busio
 7from digitalio import DigitalInOut
 8import neopixel
 9from adafruit_esp32spi import adafruit_esp32spi
10from adafruit_esp32spi import adafruit_esp32spi_wifimanager
11
12print("ESP32 SPI webclient test")
13
14# Get wifi details and more from a secrets.py file
15try:
16    from secrets import secrets
17except ImportError:
18    print("WiFi secrets are kept in secrets.py, please add them there!")
19    raise
20
21# If you are using a board with pre-defined ESP32 Pins:
22esp32_cs = DigitalInOut(board.ESP_CS)
23esp32_ready = DigitalInOut(board.ESP_BUSY)
24esp32_reset = DigitalInOut(board.ESP_RESET)
25
26# If you have an externally connected ESP32:
27# esp32_cs = DigitalInOut(board.D9)
28# esp32_ready = DigitalInOut(board.D10)
29# esp32_reset = DigitalInOut(board.D5)
30
31spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
32esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
33"""Use below for Most Boards"""
34status_light = neopixel.NeoPixel(
35    board.NEOPIXEL, 1, brightness=0.2
36)  # Uncomment for Most Boards
37"""Uncomment below for ItsyBitsy M4"""
38# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
39# Uncomment below for an externally defined RGB LED
40# import adafruit_rgbled
41# from adafruit_esp32spi import PWMOut
42# RED_LED = PWMOut.PWMOut(esp, 26)
43# GREEN_LED = PWMOut.PWMOut(esp, 27)
44# BLUE_LED = PWMOut.PWMOut(esp, 25)
45# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
46wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
47
48counter = 0
49
50while True:
51    try:
52        print("Posting data...", end="")
53        data = counter
54        feed = "test"
55        payload = {"value": data}
56        response = wifi.post(
57            "https://io.adafruit.com/api/v2/"
58            + secrets["aio_username"]
59            + "/feeds/"
60            + feed
61            + "/data",
62            json=payload,
63            headers={"X-AIO-KEY": secrets["aio_key"]},
64        )
65        print(response.json())
66        response.close()
67        counter = counter + 1
68        print("OK")
69    except (ValueError, RuntimeError) as e:
70        print("Failed to get data, retrying\n", e)
71        wifi.reset()
72        continue
73    response = None
74    time.sleep(15)