Simple test

Ensure your device works with this simple test.

examples/wsgi_simpletest.py
  1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
  2# SPDX-License-Identifier: MIT
  3
  4import board
  5import busio
  6from digitalio import DigitalInOut
  7import neopixel
  8
  9from adafruit_esp32spi import adafruit_esp32spi
 10import adafruit_esp32spi.adafruit_esp32spi_wifimanager as wifimanager
 11import adafruit_wsgi.esp32spi_wsgiserver as server
 12from adafruit_wsgi.wsgi_app import WSGIApp
 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# This example depends on a WSGI Server to run.
 22# We are using the wsgi server made for the ESP32
 23
 24print("ESP32 SPI simple web app test!")
 25
 26
 27# If you are using a board with pre-defined ESP32 Pins:
 28esp32_cs = DigitalInOut(board.ESP_CS)
 29esp32_ready = DigitalInOut(board.ESP_BUSY)
 30esp32_reset = DigitalInOut(board.ESP_RESET)
 31
 32# If you have an externally connected ESP32:
 33# esp32_cs = DigitalInOut(board.D9)
 34# esp32_ready = DigitalInOut(board.D10)
 35# esp32_reset = DigitalInOut(board.D5)
 36
 37spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
 38esp = adafruit_esp32spi.ESP_SPIcontrol(
 39    spi, esp32_cs, esp32_ready, esp32_reset
 40)  # pylint: disable=line-too-long
 41
 42"""Use below for Most Boards"""
 43status_light = neopixel.NeoPixel(
 44    board.NEOPIXEL, 1, brightness=0.2
 45)  # Uncomment for Most Boards
 46"""Uncomment below for ItsyBitsy M4"""
 47# import adafruit_dotstar as dotstar
 48# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=1)
 49
 50## If you want to connect to wifi with secrets:
 51wifi = wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light, debug=True)
 52wifi.connect()
 53
 54## If you want to create a WIFI hotspot to connect to with secrets:
 55# secrets = {"ssid": "My ESP32 AP!", "password": "supersecret"}
 56# wifi = wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
 57# wifi.create_ap()
 58
 59## To you want to create an un-protected WIFI hotspot to connect to with secrets:"
 60# secrets = {"ssid": "My ESP32 AP!"}
 61# wifi = wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
 62# wifi.create_ap()
 63
 64# Here we create our application, registering the
 65# following functions to be called on specific HTTP GET requests routes
 66
 67web_app = WSGIApp()
 68
 69
 70@web_app.route("/led_on/<r>/<g>/<b>")
 71def led_on(request, r, g, b):  # pylint: disable=unused-argument
 72    print("led on!")
 73    status_light.fill((int(r), int(g), int(b)))
 74    return ("200 OK", [], "led on!")
 75
 76
 77@web_app.route("/led_off")
 78def led_off(request):  # pylint: disable=unused-argument
 79    print("led off!")
 80    status_light.fill(0)
 81    return ("200 OK", [], "led off!")
 82
 83
 84# Here we setup our server, passing in our web_app as the application
 85server.set_interface(esp)
 86wsgiServer = server.WSGIServer(80, application=web_app)
 87
 88print("open this IP in your browser: ", esp.pretty_ip(esp.ip_address))
 89
 90# print(esp.get_time())
 91# Start the server
 92wsgiServer.start()
 93while True:
 94    # Our main loop where we have the server poll for incoming requests
 95    try:
 96        wsgiServer.update_poll()
 97        # Could do any other background tasks here, like reading sensors
 98    except (ValueError, RuntimeError) as e:
 99        print("Failed to update server, restarting ESP32\n", e)
100        wifi.reset()
101        continue