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
  4from os import getenv
  5import board
  6import busio
  7from digitalio import DigitalInOut
  8import adafruit_connection_manager
  9import adafruit_requests
 10from adafruit_esp32spi import adafruit_esp32spi
 11
 12# Get wifi details and more from a settings.toml file
 13# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD
 14secrets = {
 15    "ssid": getenv("CIRCUITPY_WIFI_SSID"),
 16    "password": getenv("CIRCUITPY_WIFI_PASSWORD"),
 17}
 18if secrets == {"ssid": None, "password": None}:
 19    try:
 20        # Fallback on secrets.py until depreciation is over and option is removed
 21        from secrets import secrets
 22    except ImportError:
 23        print("WiFi secrets are kept in settings.toml, please add them there!")
 24        raise
 25
 26print("ESP32 SPI webclient test")
 27
 28TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
 29JSON_URL = "http://api.coindesk.com/v1/bpi/currentprice/USD.json"
 30
 31
 32# If you are using a board with pre-defined ESP32 Pins:
 33esp32_cs = DigitalInOut(board.ESP_CS)
 34esp32_ready = DigitalInOut(board.ESP_BUSY)
 35esp32_reset = DigitalInOut(board.ESP_RESET)
 36
 37# If you have an AirLift Shield:
 38# esp32_cs = DigitalInOut(board.D10)
 39# esp32_ready = DigitalInOut(board.D7)
 40# esp32_reset = DigitalInOut(board.D5)
 41
 42# If you have an AirLift Featherwing or ItsyBitsy Airlift:
 43# esp32_cs = DigitalInOut(board.D13)
 44# esp32_ready = DigitalInOut(board.D11)
 45# esp32_reset = DigitalInOut(board.D12)
 46
 47# If you have an externally connected ESP32:
 48# NOTE: You may need to change the pins to reflect your wiring
 49# esp32_cs = DigitalInOut(board.D9)
 50# esp32_ready = DigitalInOut(board.D10)
 51# esp32_reset = DigitalInOut(board.D5)
 52
 53# Secondary (SCK1) SPI used to connect to WiFi board on Arduino Nano Connect RP2040
 54if "SCK1" in dir(board):
 55    spi = busio.SPI(board.SCK1, board.MOSI1, board.MISO1)
 56else:
 57    spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
 58esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
 59
 60pool = adafruit_connection_manager.get_radio_socketpool(esp)
 61ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp)
 62requests = adafruit_requests.Session(pool, ssl_context)
 63
 64if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
 65    print("ESP32 found and in idle mode")
 66print("Firmware vers.", esp.firmware_version)
 67print("MAC addr:", ":".join("%02X" % byte for byte in esp.MAC_address))
 68
 69for ap in esp.scan_networks():
 70    print("\t%-23s RSSI: %d" % (ap.ssid, ap.rssi))
 71
 72print("Connecting to AP...")
 73while not esp.is_connected:
 74    try:
 75        esp.connect_AP(secrets["ssid"], secrets["password"])
 76    except OSError as e:
 77        print("could not connect to AP, retrying: ", e)
 78        continue
 79print("Connected to", esp.ap_info.ssid, "\tRSSI:", esp.ap_info.rssi)
 80print("My IP address is", esp.ipv4_address)
 81print(
 82    "IP lookup adafruit.com: %s" % esp.pretty_ip(esp.get_host_by_name("adafruit.com"))
 83)
 84print("Ping google.com: %d ms" % esp.ping("google.com"))
 85
 86# esp._debug = True
 87print("Fetching text from", TEXT_URL)
 88r = requests.get(TEXT_URL)
 89print("-" * 40)
 90print(r.text)
 91print("-" * 40)
 92r.close()
 93
 94print()
 95print("Fetching json from", JSON_URL)
 96r = requests.get(JSON_URL)
 97print("-" * 40)
 98print(r.json())
 99print("-" * 40)
100r.close()
101
102print("Done!")

Other Examples

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