|
| 1 | +import time |
| 2 | +import board |
| 3 | +import busio |
| 4 | +from digitalio import DigitalInOut |
| 5 | +import neopixel |
| 6 | +from adafruit_esp32spi import adafruit_esp32spi |
| 7 | +from adafruit_esp32spi.adafruit_esp32spi_wifimanager import ESPSPI_WiFiManager |
| 8 | + |
| 9 | +print("ESP32 SPI WPA2 Enterprise webclient test") |
| 10 | + |
| 11 | +# Get wifi details and more from a secrets.py file |
| 12 | +try: |
| 13 | + from secrets import secrets |
| 14 | +except ImportError: |
| 15 | + print("WiFi secrets are kept in secrets.py, please add them there!") |
| 16 | + raise |
| 17 | + |
| 18 | +# If you are using a board with pre-defined ESP32 Pins: |
| 19 | +esp32_cs = DigitalInOut(board.ESP_CS) |
| 20 | +esp32_ready = DigitalInOut(board.ESP_BUSY) |
| 21 | +esp32_reset = DigitalInOut(board.ESP_RESET) |
| 22 | + |
| 23 | +# If you have an externally connected ESP32: |
| 24 | +# esp32_cs = DigitalInOut(board.D9) |
| 25 | +# esp32_ready = DigitalInOut(board.D10) |
| 26 | +# esp32_reset = DigitalInOut(board.D5) |
| 27 | + |
| 28 | +spi = busio.SPI(board.SCK, board.MOSI, board.MISO) |
| 29 | +esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) |
| 30 | +"""Use below for Most Boards""" |
| 31 | +status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards |
| 32 | +"""Uncomment below for ItsyBitsy M4""" |
| 33 | +#status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) |
| 34 | +wifi = ESPSPI_WiFiManager(esp, secrets, status_light, connection_type=ESPSPI_WiFiManager.ENTERPRISE) |
| 35 | + |
| 36 | +counter = 0 |
| 37 | + |
| 38 | +while True: |
| 39 | + try: |
| 40 | + print("Posting data...", end='') |
| 41 | + data = counter |
| 42 | + feed = 'test' |
| 43 | + payload = {'value':data} |
| 44 | + response = wifi.post( |
| 45 | + "https://io.adafruit.com/api/v2/"+secrets['aio_username']+"/feeds/"+feed+"/data", |
| 46 | + json=payload, |
| 47 | + headers={"X-AIO-KEY":secrets['aio_key']}) |
| 48 | + print(response.json()) |
| 49 | + response.close() |
| 50 | + counter = counter + 1 |
| 51 | + print("OK") |
| 52 | + except (ValueError, RuntimeError) as e: |
| 53 | + print("Failed to get data, retrying\n", e) |
| 54 | + wifi.reset() |
| 55 | + continue |
| 56 | + response = None |
| 57 | + time.sleep(15) |
0 commit comments