|
| 1 | +""" |
| 2 | +Usage example of the ESP32 over UART |
| 3 | +using the CircuitPython ESP_ATControl library. |
| 4 | +
|
| 5 | +Dependencies: |
| 6 | + * https://github.com/adafruit/Adafruit_CircuitPython_ESP_ATcontrol |
| 7 | +""" |
| 8 | +from random import randint |
| 9 | +import board |
| 10 | +import busio |
| 11 | +from digitalio import DigitalInOut |
| 12 | + |
| 13 | +# Import Adafruit IO REST Client |
| 14 | +from adafruit_io.adafruit_io import RESTClient, AdafruitIO_RequestError |
| 15 | + |
| 16 | +# ESP32 AT |
| 17 | +from adafruit_espatcontrol import adafruit_espatcontrol, adafruit_espatcontrol_wifimanager |
| 18 | + |
| 19 | +#Use below for Most Boards |
| 20 | +import neopixel |
| 21 | +status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards |
| 22 | + |
| 23 | +#Uncomment below for ItsyBitsy M4# |
| 24 | +#import adafruit_dotstar as dotstar |
| 25 | +#status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) |
| 26 | + |
| 27 | +#Uncomment below for Particle Argon# |
| 28 | +#status_light = None |
| 29 | + |
| 30 | +# Get wifi details and more from a secrets.py file |
| 31 | +try: |
| 32 | + from secrets import secrets |
| 33 | +except ImportError: |
| 34 | + print("WiFi secrets are kept in secrets.py, please add them there!") |
| 35 | + raise |
| 36 | + |
| 37 | +# With a Metro or Feather M4 |
| 38 | +uart = busio.UART(board.TX, board.RX, timeout=0.1) |
| 39 | +resetpin = DigitalInOut(board.D5) |
| 40 | +rtspin = DigitalInOut(board.D6) |
| 41 | + |
| 42 | +# With a Particle Argon |
| 43 | +""" |
| 44 | +RX = board.ESP_TX |
| 45 | +TX = board.ESP_RX |
| 46 | +resetpin = DigitalInOut(board.ESP_WIFI_EN) |
| 47 | +rtspin = DigitalInOut(board.ESP_CTS) |
| 48 | +uart = busio.UART(TX, RX, timeout=0.1) |
| 49 | +esp_boot = DigitalInOut(board.ESP_BOOT_MODE) |
| 50 | +from digitalio import Direction |
| 51 | +esp_boot.direction = Direction.OUTPUT |
| 52 | +esp_boot.value = True |
| 53 | +""" |
| 54 | + |
| 55 | +esp = adafruit_espatcontrol.ESP_ATcontrol(uart, 115200, |
| 56 | + reset_pin=resetpin, rts_pin=rtspin, debug=False) |
| 57 | +wifi = adafruit_espatcontrol_wifimanager.ESPAT_WiFiManager(esp, secrets, status_light) |
| 58 | + |
| 59 | +# Set your Adafruit IO Username and Key in secrets.py |
| 60 | +# (visit io.adafruit.com if you need to create an account, |
| 61 | +# or if you need your Adafruit IO key.) |
| 62 | +ADAFRUIT_IO_USER = secrets['adafruit_io_user'] |
| 63 | +ADAFRUIT_IO_KEY = secrets['adafruit_io_key'] |
| 64 | + |
| 65 | +# Create an instance of the Adafruit IO REST client |
| 66 | +io = RESTClient(ADAFRUIT_IO_USER, ADAFRUIT_IO_KEY, wifi) |
| 67 | + |
| 68 | +try: |
| 69 | + # Get the 'temperature' feed from Adafruit IO |
| 70 | + temperature_feed = io.get_feed('temperature') |
| 71 | +except AdafruitIO_RequestError: |
| 72 | + # If no 'temperature' feed exists, create one |
| 73 | + temperature_feed = io.create_new_feed('temperature') |
| 74 | + |
| 75 | +# Send random integer values to the feed |
| 76 | +random_value = randint(0, 50) |
| 77 | +print('Sending {0} to temperature feed...'.format(random_value)) |
| 78 | +io.send_data(temperature_feed['key'], random_value) |
| 79 | +print('Data sent!') |
| 80 | + |
| 81 | +# Retrieve data value from the feed |
| 82 | +print('Retrieving data from temperature feed...') |
| 83 | +received_data = io.receive_data(temperature_feed['key']) |
| 84 | +print('Data from temperature feed: ', received_data['value']) |
0 commit comments