Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1225

Python • Showing connected GPIO pins via I2C

$
0
0
Hi there,

I've got a script which - via flask - will show which GPIO pins are currently connected to a common ground in a web browser.

I've got an I2C display connected, and functioning. However, I'd like to adapt the script to show the pins that are currently connected on that display. And for the life of me, I can't work it out. At the moment I've got placeholder text showing, but can't work out how to pass the GPIO pin data.

Code:

from flask import Flask, render_templatefrom RPLCD.i2c import CharLCDimport RPi.GPIO as GPIOimport smbusimport timeimport jsonapp = Flask(__name__)lcd = CharLCD(i2c_expander='PCF8574', address=0x27, port=1, cols=16, rows=2, dotsize=8)lcd.clear()lcd.write_string('testing')# Set up GPIOGPIO.setmode(GPIO.BCM)total_pins = 26  connected_pins = []# Set up I2Ci2c = smbus.SMBus(1)  DISPLAY_ADDRESS = 0x27  DISPLAY_WIDTH = 16# Function to read GPIO pin statusdef read_gpio(pin):    GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)    return GPIO.input(pin)@app.route('/')def index():    global connected_pins    connected_pins = []        for pin in range(total_pins):        if not read_gpio(pin):            connected_pins.append(pin)    display_pins(connected_pins)    return render_template('index.html', connected_pins=connected_pins)# Route for updating pin status via AJAX@app.route('/update_pins')def update_pins():    global connected_pins    connected_pins = []        for pin in range(total_pins):        if not read_gpio(pin):            connected_pins.append(pin)    display_pins(connected_pins)    return json.dumps({'connected_pins': connected_pins})if __name__ == '__main__':    app.run(debug=True, host='0.0.0.0')
Any ideas would be amazing!
Cheers,

Statistics: Posted by rory1234 — Sat Jan 06, 2024 2:44 am



Viewing all articles
Browse latest Browse all 1225

Trending Articles