Welcome to Nick's Random Musings

Of course I don't know what I'm doing, what fun would that be?


Debian 11.6 bullseye on Raspberry Pi 3 with python3


meta
Nick Gully, Denver

I have a garage door I operate via a small raspberry pi with a solenoid wired to the normal garage door button. It mimics the action of pressing it. This is really nice for having access to operate the door even from my phone or smartwatch. Recently I realized it was running a very old version of Raspbian, and I wanted to update to a more up to date debian.

This makes for a interesting interaction between the nginx webserver, and getting a python script to access the GPIO ports of the pi. There are levels of security to traverse.

Python packages: gipod 2.2.20

Python script:

import gpiod
import cgi

form = cgi.FieldStorage()
action = form.getvalue('group')

print("Content-type: text/html\n\n")
print("Status: 200 OK\n\n")


with gpiod.Chip("/dev/gpiochip0") as chip:
     info = chip.get_info()
     print(f"{info.name} [{info.label}] ({info.num_lines} lines)")
import time

from gpiod.line import Direction, Value

LINE = 16

with gpiod.request_lines(
     "/dev/gpiochip0",
     consumer="blink-example",
     config={
         LINE: gpiod.LineSettings(
             direction=Direction.OUTPUT, output_value=Value.ACTIVE
         )
     },
) as request:
     request.set_value(LINE, Value.ACTIVE)
     time.sleep(1)
     request.set_value(LINE, Value.INACTIVE)
     time.sleep(1)

And you may have to make sure that other users (like www-data), can touch the /dev/gpiochip0 device.

sudo chmod a+rw /dev/gpiochip0