Skip to content

Instantly share code, notes, and snippets.

@MathijsNL
Created March 3, 2023 21:18
Show Gist options
  • Save MathijsNL/18fb9d262b550b93a55cbac05292daae to your computer and use it in GitHub Desktop.
Save MathijsNL/18fb9d262b550b93a55cbac05292daae to your computer and use it in GitHub Desktop.
Streamlit app to check GPIO
import digitalio
import board
import time
import streamlit as st
# Define the list of all pin names
all_pin_names = [f"D{i}" for i in range(1, 41)]
# Initialize the GPIO pins for the switches
defined_pin_numbers = [3, 5, 7, 8, 10, 11, 12, 13, 15, 16, 18, 19, 21, 22, 23, 24, 27, 28, 29, 31, 32, 33, 35, 37, 38, 40]
switch_pins = [digitalio.DigitalInOut(eval(f"board.D{i}")) for i in defined_pin_numbers]
pin_names = [f"D{i}" for i in defined_pin_numbers]
for switch_pin in switch_pins:
switch_pin.direction = digitalio.Direction.INPUT
# Define a Streamlit app function
def app():
# Set up the Streamlit app layout
st.title("GPIO Input Monitor")
st.write("Monitoring GPIO inputs...")
table = st.empty()
# Start a while loop to continuously read the switch states
while True:
# Read the switch values and store them in a dictionary
switch_values = {}
for i, switch_pin in enumerate(switch_pins):
switch_value = switch_pin.value
switch_values[pin_names[i]] = switch_value
# Update the Streamlit table with the current switch values
table_data = []
for i, pin_name in enumerate(all_pin_names):
if i % 2 == 0:
left_pin = pin_name
left_value = switch_values.get(left_pin, '-')
row = {"Value L": str(left_value), "Pin# L": left_pin}
else:
right_pin = pin_name
right_value = switch_values.get(right_pin, '-')
row["Pin# R"] = right_pin
row["Value R"] = str(right_value)
table_data.append(row)
table.table(table_data)
# Wait for a short period before reading the switches again
time.sleep(0.5)
# Run the Streamlit app
if __name__ == "__main__":
app()
@MathijsNL
Copy link
Author

You need the Adafruit Blinka library and streamlit to run this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment