Once you've finished setting up your Feather ESP32-S3 with CircuitPython, you can access the code and necessary libraries by downloading the Project Bundle.
To do this, click on the Download Project Bundle button in the window below. It will download to your computer as a zipped folder.
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries # SPDX-License-Identifier: MIT import time import board import busio from adafruit_bluefruit_connect.packet import Packet from adafruit_bluefruit_connect.button_packet import ButtonPacket from adafruit_ble import BLERadio from adafruit_ble.advertising.standard import ProvideServicesAdvertisement from adafruit_ble.services.nordic import UARTService import neopixel # baud rate for your device baud = 38400 # commands for your device commands = ["AVI=1", "AVI=2", "AVI=3", "AVI=4"] # Initialize UART for the RS232 uart = busio.UART(board.TX, board.RX, baudrate=baud) # onboard neopixel pixels = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.5, auto_write=True) RED = (255, 0, 0) BLUE = (0, 0, 255) # BLE setup ble = BLERadio() ble_uart = UARTService() advertisement = ProvideServicesAdvertisement(ble_uart) advertising = False print("advertising..") while True: if not ble.connected and not advertising: # not connected in the app yet pixels.fill(RED) ble.start_advertising(advertisement) advertising = True while ble.connected: pixels.fill(BLUE) # after connected via app advertising = False if ble_uart.in_waiting: # waiting for input from app packet = Packet.from_stream(ble_uart) if isinstance(packet, ButtonPacket): # if buttons in the app are pressed if packet.pressed: if packet.button == ButtonPacket.BUTTON_1: uart.write((commands[0] + "\r\n").encode('ascii')) if packet.button == ButtonPacket.BUTTON_2: uart.write((commands[1] + "\r\n").encode('ascii')) if packet.button == ButtonPacket.BUTTON_3: uart.write((commands[2] + "\r\n").encode('ascii')) if packet.button == ButtonPacket.BUTTON_4: uart.write((commands[3] + "\r\n").encode('ascii')) # empty buffer to collect the incoming data response_buffer = bytearray() # check for data time.sleep(1) while uart.in_waiting: data = uart.read(uart.in_waiting) if data: response_buffer.extend(data) # decode and print if response_buffer: print(response_buffer.decode('ascii'), end='') print()
Upload the Code and Libraries to the Feather ESP32-S3
After downloading the Project Bundle, plug your Feather ESP32-S3 into the computer's USB port with a known good USB data+power cable. You should see a new flash drive appear in the computer's File Explorer or Finder (depending on your operating system) called CIRCUITPY. Unzip the folder and copy the following items to the Feather ESP32-S3's CIRCUITPY drive.
- lib folder
- code.py
Your Feather ESP32-S3 CIRCUITPY drive should look like this after copying the lib folder and the code.py file.
How the CircuitPython Code Works
At the top of the code, you can edit the baud rate and the commands that you want to send to your RS232 device. Then, UART is instantiated with the TX and RX pins.
# baud rate for your device baud = 38400 # commands for your device commands = ["AVI=1", "AVI=2", "AVI=3", "AVI=4"] # Initialize UART for the RS232 uart = busio.UART(board.TX, board.RX, baudrate=baud)
# onboard neopixel pixels = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.5, auto_write=True) RED = (255, 0, 0) BLUE = (0, 0, 255)
# BLE setup ble = BLERadio() ble_uart = UARTService() advertisement = ProvideServicesAdvertisement(ble_uart) advertising = False print("advertising..")
The Loop
In the loop, if the BLE connection is disconnected, then the NeoPixel is red and BLE starts advertising for a connection.
if not ble.connected and not advertising: # not connected in the app yet pixels.fill(RED) ble.start_advertising(advertisement) advertising = True
Once a connection is established, the board starts listening for incoming BLE UART commands. These are sent from the buttons in the Bluefruit connect app. Buttons 1 through 4 switch between the four commands in the commands
array. These commands are sent over UART to the RS232 breakout.
while ble.connected: pixels.fill(BLUE) # after connected via app advertising = False if ble_uart.in_waiting: # waiting for input from app packet = Packet.from_stream(ble_uart) if isinstance(packet, ButtonPacket): # if buttons in the app are pressed if packet.pressed: if packet.button == ButtonPacket.BUTTON_1: uart.write((commands[0] + "\r\n").encode('ascii')) if packet.button == ButtonPacket.BUTTON_2: uart.write((commands[1] + "\r\n").encode('ascii')) if packet.button == ButtonPacket.BUTTON_3: uart.write((commands[2] + "\r\n").encode('ascii')) if packet.button == ButtonPacket.BUTTON_4: uart.write((commands[3] + "\r\n").encode('ascii'))
A buffer is created to hold any incoming data from the RS232 device. If data comes in over UART from the RS232 device, it is printed to the serial console.
# empty buffer to collect the incoming data response_buffer = bytearray() # check for data time.sleep(1) while uart.in_waiting: data = uart.read(uart.in_waiting) if data: response_buffer.extend(data) # decode and print if response_buffer: print(response_buffer.decode('ascii'), end='') print()
Text editor powered by tinymce.