Terrariachest
Posts: 2
Joined: Sat Aug 08, 2020 4:17 pm

Raspberry Pi buttons

Sat Aug 08, 2020 4:25 pm

Is there any way I can use a physical button to input a command into the terminal? Any help is deeply appreciated

User avatar
thagrol
Posts: 3178
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Raspberry Pi buttons

Sat Aug 08, 2020 5:35 pm

Not directly.

You can write a program that waits for a button press then runs your command but there are a bunch of things you need to take into consideration. Not least of which are:
  • Whether your command needs to output something
  • Whether it needs input from the user
  • How you're going to start this program and when
  • ...
Arguing with strangers on the internet since 1993.

Terrariachest
Posts: 2
Joined: Sat Aug 08, 2020 4:17 pm

Re: Raspberry Pi buttons

Sat Aug 08, 2020 5:52 pm

thagrol wrote:
Sat Aug 08, 2020 5:35 pm
Not directly.

You can write a program that waits for a button press then runs your command but there are a bunch of things you need to take into consideration. Not least of which are:
  • Whether your command needs to output something
  • Whether it needs input from the user
  • How you're going to start this program and when
  • ...
Thank you for giving me an answer, it just needs to activate bluetooth by running sudo bluetoothctl. It's a bluetooth speaker project. Bluetooth doesn't automatically power on during boot for some reason.

User avatar
thagrol
Posts: 3178
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Raspberry Pi buttons

Sat Aug 08, 2020 6:13 pm

Terrariachest wrote:
Sat Aug 08, 2020 5:52 pm
Thank you for giving me an answer, it just needs to activate bluetooth by running sudo bluetoothctl. It's a bluetooth speaker project. Bluetooth doesn't automatically power on during boot for some reason.
In which case, I'd look into using python and gpiozero. There are examples for butons in gpiozer here: https://gpiozero.readthedocs.io/en/stab ... tml#button

You can use the os or subprocess modules to make the call to bluetoothctl.
Arguing with strangers on the internet since 1993.

Return to “Other projects”