esp32spi_aio_post.py
1 # SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries 2 # SPDX-License-Identifier: MIT 3 4 import time 5 import board 6 import busio 7 from digitalio import DigitalInOut 8 import neopixel 9 from adafruit_esp32spi import adafruit_esp32spi 10 from adafruit_esp32spi import adafruit_esp32spi_wifimanager 11 12 print("ESP32 SPI webclient test") 13 14 # Get wifi details and more from a secrets.py file 15 try: 16 from secrets import secrets 17 except 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: 22 esp32_cs = DigitalInOut(board.ESP_CS) 23 esp32_ready = DigitalInOut(board.ESP_BUSY) 24 esp32_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 31 spi = busio.SPI(board.SCK, board.MOSI, board.MISO) 32 esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) 33 """Use below for Most Boards""" 34 status_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) 46 wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) 47 48 counter = 0 49 50 while 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)